Skip to content

Instantly share code, notes, and snippets.

@noxan
Last active April 10, 2016 23:28
Show Gist options
  • Save noxan/5d36818ee99ec92edef3f9fc09040afb to your computer and use it in GitHub Desktop.
Save noxan/5d36818ee99ec92edef3f9fc09040afb to your computer and use it in GitHub Desktop.
// source
const user = {
Id: '4aa86780f44a0d5547895e12f1346e30508fba05653c69d1c53c17e500cb60ec',
FirstName: 'Hans',
LastName: 'Gold'
email: 'mail@example.com',
departmentId: 412,
};
// schema definition
const userSchema = new Schema({
id: 'id',
type: 'users',
attributes: {
id: 'Id',
name: (obj) => `${obj.FirstName} ${obj.LastName}`,
email: 'email',
},
relationships: [
department: {
schema: departmentSchema,
id: 'departmentId',
included: true,
}
projects: {
schema: projectSchema,
included: false,
},
],
included: [
// list of included objs or define included for each relationship?
]
});
const projectSchema = new Schema({
links: {
self: (serializedObj) => `/projects/${serializedObj.id}`,
}
});
// example usages
router.get('/user', () => {
return userSchema.serialize(obj, {
// this object will be merged into serialize result
links: {
self: (serializedObj) => `/users/${serializedObj.id}`,
}
});
});
router.get('/users', () => {
return userSchema.serialize(objs);
});
// expected result
const result = {
links: {
self: '',
},
data: [{
type: '',
id: '',
attributes: {},
relationships: {},
}],
included: [
],
};
@noxan
Copy link
Author

noxan commented Apr 10, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment