JsonAPI Adapter based on js-data-http for js-data
This adapter just add a light serialize and deserialize layer over DSHttpAdapter.
package | requirement |
---|---|
js-data | >= 3.0.0 |
js-data-http | >= 3.0.0 |
By design it is not compatible with JS-Data v2, for a full integration on v2, you should use: js-data-jsonapi.
First you need to install everything needed :
npm install --save js-data js-data-http js-data-jsonapi js-data-jsonapi-light
Load js-data-jsonapi-light.js
last.
import { DataStore } from 'js-data'
// or `var DataStore = JSDate.DataStore`
import { JsonApiAdapter } from 'js-data-jsonapi-light'
// or `var JsonApiAdapter = JSDataJsonApiLight.JsonApiAdapter`
// Initialize our Store
const store = new DataStore();
// Initialize our Adapter
const jsonApiAdapter = new JsonApiAdapter({
// Same options as DSHttpAdapter
// However, you should use before/after deserialize/serialize instead of serialize/deserialize
});
// Register the Adapter as the default one in the store
store.registerAdapter('jsonApi', jsonApiAdapter, { default: true })
JSData works with mappers which define the communication part and the way your resources are linked together
const UserMapper = window.store.defineMapper('User',{
endpoint: 'users', // optional, it will default to `User`
relations: {
hasOne: {
'UserProfile': {
localField: 'profile',
foreignKey: 'userId'
}
},
hasMany: {
'Article': {
localField: 'articles',
foreignKey: 'authorId'
}
},
belongsTo: {
'UserGroup': {
localField: 'group',
localKey: 'groupId'
}
}
}
})
UserMapper.findAll({ include: 'profile' }).then((records) => {
console.log(records);
})
Once some records has been loaded they are also cached in the DataStore, you can access them synchronously with :
records = store.getAll('User')
Sometime you want to be able to do some custom additional serialization / deserialization.
const jsonApiAdapter = new JsonApiAdapter({
beforeDeserialize: function(mapper, data, opts) { return data; }
afterDeserialize: function(mapper, data, opts) { return data; }
beforeSerialize: function(mapper, data, opts) { return data; }
afterSerialize: function(mapper, data, opts) { return data; }
});
const UserMapper = store.defineMapper('User', {
beforeDeserialize: function(mapper, data, opts) { return data; }
afterDeserialize: function(mapper, data, opts) { return data; }
beforeSerialize: function(mapper, data, opts) { return data; }
afterSerialize: function(mapper, data, opts) { return data; }
});
store.findAll('User', {}, {
beforeDeserialize: function(mapper, data, opts) { return data; }
afterDeserialize: function(mapper, data, opts) { return data; }
});
store.update('User', {}, {
beforeSerialize: function(mapper, data, opts) { return data; }
afterSerialize: function(mapper, data, opts) { return data; }
});
beforeDeserialize
, afterDeserialize
, beforeSerialize
, afterSerialize
Deserialization and serialization hooks, see above.
raw: boolean
Send back raw response
Eg. To retrieve JSONApi Meta :
store.findAll('User', {}, {
raw: true
}).then((response) => {
console.log(response.data); // Return the Records
console.log(response.meta); // Return the JSONApi Metas
console.log(response.rawData); // Return the full response (should be discarded as soon as possible to save memory)
})
all
forceReplace: boolean(false)
On update, force all fields to be sent even if they haven't been changed. It will switch from default PATCH http verb to PUT.
If some properties are passed to the update, only those will be sent. Those who has not changed compared to the record original properties will not be sent. Be careful, because it means, once the update made, the server will return saved datas, and it will erased possible properties that were changed on the record but not given to the update. Nothing wrong with that, it's the correct behavior, but you should know about this logic.
update
, save
forceRelationshipsInAttributes: boolean(false)
On update, force all relationships data to be sent using attributes
structure instead of default relationships
within the resource object.
Note it is not JsonAPI compliant, it is a convenient option to support some specific backends.
update
, save
beforeDeserialize
, afterDeserialize
, beforeSerialize
, afterSerialize
Deserialization and serialization hooks, see above.
The MIT Licence (MIT)
Copyright (c) 2017 WIKODIT SAS
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.