📦 Mongoose.js Trailpack http://mongoosejs.com
Loads Application Models (in api/models
) into the Mongoose ORM; Integrates with trailpack-router to
generate Footprints for routes.
// config/main.js
module.exports = {
// ...
packs: [
require('trailpack-mongoose')
]
}
// config/database.js
module.exports = {
/**
* Define the database stores. A store is typically a single database.
*
* Use the SQLite3 by default for development purposes.
*
* Set production connection info in config/env/production.js
*/
stores: {
/**
* Define a store called "local" which uses SQLite3 to persist data.
*/
someteststore: {
//migration
migrate: 'create',
// Mongodb URI
uri: 'mongodb://localhost:27017/test',
// Mongoose connection options
options: {
}
}
},
models: {
defaultStore: 'someteststore',
migrate: 'drop'
}
}
See Mongoose Documentation for Schemas.
Option | Type | Default | Description |
---|---|---|---|
tableName | String | modelName.toLowerCase() |
Name of collection to use with this model. For example: MyModel.js defaults to collection named mymodel |
store | String | app.config.database.models.defaultStore |
Datastore to use for this model; specify the name of one of the stores in app.config.database.stores . |
migrate | String | Migrate must be set to one of [ 'none', 'drop', 'create' ] |
|
schema | Object | {} |
Schema Options to pass into Mongoose's Schema constructor. |
statics | Object | {} |
Static methods to add to the Model. |
methods | Object | {} |
Instance methods to add to this model's documents. |
onSchema | Function | undefined | Funcion which is useful to for adding schema middleware, virtuals, or indexes. |
// Use default Schema from Mongoose. See http://mongoosejs.com/docs/schematypes.html
module.exports = class User extends Model {
static schema (app, Mongoose) {
return {
username: String,
childs: [{
type: Mongoose.Schema.ObjectId,
ref: 'UserSchema'
}],
email: {
type: String,
required: true,
unique: true,
lowercase: true,
trim: true,
validate: {
validator: function (val) {
return isEmail(val)
},
message: '{VALUE} is not a valid email'
}
}
}
}
static config (app, Mongoose) {
return {
// Collection name
tableName: 'users',
// Schema options
schema: {
timestamps: true,
versionKey: false,
toObject: {
virtuals: true
},
toJSON: {
virtuals: true
}
},
// Schema statics
statics: {
getByEmail: function (email) {
return this
.findOne({
email: _.trim(email)
})
.exec()
},
},
// Schema methods
methods: {
toJSON: function () {
const user = this.toObject()
delete user.password
return user
}
},
/**
* After Trails.js will create model Schema you could add anything you want here
* @param {TrailsApp} app TrailsJs app object
* @param {mongoose.Schema} schema mongoose new Schema object
*/
onSchema (app, schema) {
// virtuals
schema.virtual('name.full').get(function () {
return this.name.first + ' ' + this.name.last
})
// lifecircle events
schema.pre('save', function (next) {
// performing actions
next()
})
// text indexes
schema.index(
{
username: 'text',
email: 'text'
},
{
weights : {
username : 10,
email: 5
},
name: "usersIndex"
}
)
}
}
}
}
// 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) {
return this.orm.User.find({ email: email })
.exec()
}
}
We love contributions! Please check out our Contributor's Guide for more information on how our projects are organized and how to get started.