Skip to content

mockra/node-rest-serializer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node Rest Serializer

Install

  npm install rest-serializer --save

Usage

Here's a basic example of serializing an array of post objects with their comments included.

  serialize('posts', posts, { sideload: { name: 'comments' } })

  // output
  { posts: [ { title: 'test post', comments: [24] } ],
    comments: [ { id: 24, content: 'hello world' } ] }

You can also specify to delete specific attributes from your objects. Here's an example with users.

  var users = [{email: '[email protected]', password: 'foobar'}]
  serialize('users', { without: 'password' })
  serialize('users', { without: ['password', 'token'] })

  //output
  {users: [{email: '[email protected]']}

If you're side loading a singular association, such as a user that belongs to a post, then you need to specify the plural version. Here's an example:

  var user = {id: 2, email: '[email protected]'}
  var post = {title: 'test post', user: user}
  serialize('post', post, { sideload: {name: 'user', plural: 'users' } })

  //output
  { post: { title: 'test post', user: 2 },
    users: [{id: 2, email: '[email protected]'}] }
  

Advanced Usage

Here's an example with a more complex use case.

  serialize('users', { without: 'password',
    sideload: [{name: 'posts', key: 'pid', without: 'author',
    sideload: [{name: 'comments', key: 'cid', without: 'author'}] }] })

  //output
  { users: [{email: '[email protected]', posts: [1, 2]}],
    posts: [{pid: 1, comments: [1]}, {pid: 2, comments: [1, 2]}],
    comments: [{cid: 1}, {cid: 2}] }

In this example, we're serializing a users array, but including the related posts and post comments. Since the primary key for these records isn't id, we specify a key. This key is used to generate the id array that ties the side loaded posts and comments to their parent.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published