trailpack-js-data v0.0.5
trailpack-js-data
Trailpack for js-data ORM
Loads Application Models (in api/models
) into the js-data ORM; Integrates with trailpack-router to
generate Footprints for routes.
This ORM works with sqlite
, mysql
, postgres
, mongodb
and can easily be updated to support even more! Such as redis
and rethink
.
Install
with npm:
$ npm install --save trailpack-js-data
with yo:
$ yo trails:trailpack trailpack-js-data
Configure
// config/main.js
module.exports = {
packs: [
// ... other trailpacks
require('trailpack-js-data')
]
}
A basic config/database.js
can be found here : https://github.com/scott-wyatt/trailpack-js-data/blob/master/archetype/config/database.js
Models
module.exports = class User extends Model {
//More about supported schema here : http://www.js-data.io/docs/js-data-schema
static schema (app) {
return {
name: { type: 'string', allowNull: false },
password: 'string',
displayName: 'string'
}
}
static config (app) {
return {
migrate: 'drop', //override default models configurations if needed
store: 'sqlite', //override default models configurations if needed
//More informations about supported models options here: http://www.js-data.io/docs/relations
options: {
relations: {
hasOne: {
profile: {
localField: 'profile',
foreignKey: 'userId'
}
}
}
},
afterCreate: function(resource, attrs, cb){
cb(null,resource)
}
}
}
}
Query
// api/services/UserService.js
module.exports = class UserService extends Service {
/**
* Finds people with the given email.
* @return Promise
* @example {
* name: 'Ludwig Beethoven',
* email: 'someemail@email.com',
* favoriteColors: [
* { name: 'yellow', hex: 'ffff00' },
* { name: 'black', hex: '000000' }
* ]
* }
*/
findUser (email) {
// More info about queries here: http://www.js-data.io/docs/query-syntaxlatest/docs/models-usage/
return this.app.orm.User.findAll({email: email})
},
/**
* Finds people with the given email with footprints.
* @return Promise
* @example {
* name: 'Ludwig Beethoven',
* email: 'someemail@email.com',
* favoriteColors: [
* { name: 'yellow', hex: 'ffff00' },
* { name: 'black', hex: '000000' }
* ]
* }
*/
findUserFootprint (email) {
// This ORM can also use footprints!
retrun this.app.services.FootprintService.find('User',{email: email}, {findOne: true})
}
}
Footprints query options
Some options can be provide as query param for the find
method, example GET /api/v1/user
.
Populate
You can add /api/v1/user?populate=all
to populate all associations or use /api/v1/user?populate=field1,field2
to populate only some association.
Pagination
By settings offset
and limit
you can do some pagination, example /api/v1/user?offset=10&limit=10
will return only 10 items started from 10 (id 10 to 20).
Contributing
We love contributions! Please check out our Contributor's Guide for more information on how our projects are organized and how to get started.