1.5.0 • Published 11 years ago

json-linker v1.5.0

Weekly downloads
1
License
MIT
Repository
github
Last release
11 years ago

json-linker

Provides linking compatible with jsonapi.

Usage Example

Linking for json serialization

// currently works only with singular models
var song = {
  title: 'Sunshine of Your Love'
  riffs: [
    { id: 'abc123', start: '1:04', stop: '1:20' },
    { id: 'qwe234', start: '2:34', stop: '2:59' }
  ],
  tags: [
    { id: 'w00t', name: 'creamy' },
    { id: '4r33l', name: 'awesome' }
  ]
}

var JsonLinker = require('json-linker')

var json = new JsonLinker(song, 'songs')
  .links('riffs', 'tags')
  .toJson()

// outputs =>
//
// {
//   songs: [{
//     title: 'Sunshine of Your Love',
//     links: {
//       riffs: ['abc123', 'qwe234'],
//       tags: ['w00t', '4r33l']
//     }
//   }],
//   linked: {
//     riffs: [
//       { id: 'abc123', start: '1:04', stop: '1:20' },
//       { id: 'qwe234', start: '2:34', stop: '2:59' }
//     ],
//     tags: [
//       { id: 'w00t', name: 'creamy' },
//       { id: '4r33l', name: 'awesome' }
//     ]
//   }
// }

Unlinking for embedded models

// currently works only with singular models
var json = {
  songs: [{
    title: 'Sunshine of Your Love',
    links: {
      riffs: ['abc123', 'qwe234'],
      tags: ['w00t', '4r33l']
    }
  }],
  linked: {
    riffs: [
      { id: 'abc123', start: '1:04', stop: '1:20' },
      { id: 'qwe234', start: '2:34', stop: '2:59' }
    ],
    tags: [
      { id: 'w00t', name: 'creamy' },
      { id: '4r33l', name: 'awesome' }
    ]
  }
}

var JsonLinker = require('json-linker')

var model = new JsonLinker(song)
  .toEmbeddedModel()
  
// outputs =>
// var song = {
//   title: 'Sunshine of Your Love'
//   riffs: [
//     { id: 'abc123', start: '1:04', stop: '1:20' },
//     { id: 'qwe234', start: '2:34', stop: '2:59' }
//   ],
//   tags: [
//     { id: 'w00t', name: 'creamy' },
//     { id: '4r33l', name: 'awesome' }
//   ]
// }
1.5.0

11 years ago

1.4.1

11 years ago

1.4.0

11 years ago

1.3.0

11 years ago

1.2.0

11 years ago

1.1.2

11 years ago

1.1.1

11 years ago

1.0.1

11 years ago

1.0.0

11 years ago