Created
June 7, 2016 13:58
-
-
Save mfenniak/c23bd1c1dd5fdd7f2eb3470c13cf359a to your computer and use it in GitHub Desktop.
Example implementation of disabling loading of a specific fortunejs relationship (fortunejs/fortune#219)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const fortune = require('fortune'); | |
const http = require('http'); | |
const JsonApiSerializer = require('fortune-json-api'); | |
const MemoryAdapter = require('fortune/lib/adapter/adapters/memory'); | |
const HttpSerializer = fortune.net.http.Serializer; | |
const store = fortune( | |
{ | |
user: { | |
name: { type: String }, | |
password: { type: Buffer }, | |
salt: { type: Buffer }, | |
group: { link: 'group', inverse: 'users', isArray: true } | |
}, | |
group: { | |
name: { type: String }, | |
users: { link: 'user', inverse: 'group', isArray: true } | |
} | |
}, | |
{ | |
} | |
); | |
class CustomSerializer extends JsonApiSerializer(HttpSerializer) | |
{ | |
processRequest() { | |
const retval = super.processRequest(...arguments) | |
return Promise.resolve(retval).then(context => { | |
if (context.type === 'user') { | |
if (!context.options.fields) { | |
context.options.fields = {}; | |
} | |
context.options.fields.group = false; | |
} | |
return context | |
}) | |
} | |
} | |
const listener = fortune.net.http(store, { | |
serializers: [ | |
[ CustomSerializer ], | |
] | |
}) | |
const server = http.createServer(listener) | |
server.listen(8080) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment