Last active
April 10, 2016 23:28
-
-
Save noxan/5d36818ee99ec92edef3f9fc09040afb to your computer and use it in GitHub Desktop.
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
// 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: [ | |
], | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://jsonapi.org/format/#fetching