JsonAPI Adapter based on js-data-http for js-data
This adapter just add a light serialize and deserialize layer over DSHttpAdapter.
| js-data | >= 3.0.0-rc.7 | | js-data-http | >= 3.0.0-rc.2 |
By design it is not compatible with JS-Data v2, for a full integration on v2, you could 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({
// Store needs to be given to the adapter
store: store
// Same options as DSHttpAdapter
// If a serialization option is given, it will be run before JSONApi serialization has occured
// If a deserialization option is given, it will be run after JSONApi deserialization has occured
});
// Register the Adapter as the default one in the store
store.registerAdapter('jsonApi', jsonApiAdapter, { default: true })
- You can find examples of Mapper Definitions in test/resources.
- Fake JsonAPI responses corresponding to those mapper can be found in test/api
- Bootstrap of JSData + JsonAPI could be found in test/ds
- Exemple of use could be found in test/unit
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')
To retrieve JSONApi Meta on every call :
store.findAll('User', {}, {
raw: true
}).then((response) => {
console.log(response.data); // Return the Records
console.log(response.meta); // Return the JSONApi Metas
})
1.0.0-alpha.2
- Deserialization, supporting hasMany, hasOne, belongsTo relationships
- Meta handling
- Serialization
- ManyToMany
- Error handling
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.