Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: node_js
node_js:
- '4'
- 7
- 8
- 9
- stable
services: mongodb
deploy:
Expand Down
2 changes: 1 addition & 1 deletion api/services/FootprintService.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const _ = require('lodash')
const Service = require('trails/service')
const Service = require('trails/lib/Service')

/**
* Trails Service that maps abstract ORM methods to their respective Waterine
Expand Down
2 changes: 1 addition & 1 deletion api/services/SchemaMigrationService.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const _ = require('lodash')
const Service = require('trails/service')
const Service = require('trails/lib/Service')

/**
* @module SchemaMigrationService
Expand Down
30 changes: 15 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const _ = require('lodash')
const mongoose = require('mongoose')
const DatastoreTrailpack = require('trailpack-datastore')
const DatastoreTrailpack = require('trailpack/datastore')
const lib = require('./lib')

/**
Expand All @@ -14,25 +14,25 @@ module.exports = class MongooseTrailpack extends DatastoreTrailpack {
/**
* Ensure that this trailpack supports the configured migration
*/
validate () {
if (!_.includes([ 'none', 'drop', 'create' ], this.app.config.database.models.migrate)) {
validate() {
if (!_.includes(['none', 'drop', 'create'], this.app.config.get('database.models.migrate'))) {
throw new Error('Migrate must be configured to either "create" or "drop"')
}
}

/**
* Create default configuration
*/
configure () {
this.app.config.database.orm = 'mongoose'
configure() {
this.app.config.set('database.orm', 'mongoose')

this.mongoose = mongoose
}

/**
* Initialize mongoose connections, and perform migrations.
*/
initialize () {
async initialize() {
super.initialize()

// Binding promise library
Expand All @@ -43,10 +43,10 @@ module.exports = class MongooseTrailpack extends DatastoreTrailpack {

this.orm = this.orm || {}
// Need to pick only mongoose stores
const stores = lib.Transformer.pickStores(this.app.config.database.stores)
const stores = lib.Transformer.pickStores(this.app.config.get('database.stores'))
// iterating only through mongo stores
this.connections = _.mapValues(stores, (_store, storeName) => {
const store = _.merge({ }, _store)
const store = _.merge({}, _store)
if (!_.isString(store.uri))
throw new Error('Store have to contain "uri" option')

Expand All @@ -66,7 +66,7 @@ module.exports = class MongooseTrailpack extends DatastoreTrailpack {

//create model
this.orm[model.globalId] = connection.model(model.globalId, schema, model.tableName)
this.packs.mongoose.orm[model.identity] = this.orm[model.globalId]
this.app.packs.mongoose.orm[model.identity] = this.orm[model.globalId]
})

return connection
Expand All @@ -80,7 +80,7 @@ module.exports = class MongooseTrailpack extends DatastoreTrailpack {
/**
* Close all database connections
*/
unload () {
unload() {
return Promise.all(
_.map(this.connections, connection => {
return new Promise((resolve, reject) => {
Expand All @@ -95,7 +95,7 @@ module.exports = class MongooseTrailpack extends DatastoreTrailpack {
)
}

constructor (app) {
constructor(app) {
super(app, {
config: require('./config'),
api: require('./api'),
Expand All @@ -106,15 +106,15 @@ module.exports = class MongooseTrailpack extends DatastoreTrailpack {
/**
* Run migrations
*/
migrate () {
async migrate() {
const SchemaMigrationService = this.app.services.SchemaMigrationService
const database = this.app.config.database
const migrate = this.app.config.get('database.models.migrate')

if (database.models.migrate == 'none') return
if (migrate === 'none') return

return Promise.all(
_.map(this.connections, connection => {
if (database.models.migrate == 'drop') {
if (migrate === 'drop') {
return SchemaMigrationService.drop(connection)
}
}))
Expand Down
13 changes: 6 additions & 7 deletions lib/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
* @param {Object} stores
* @return {Object}
*/
pickStores (stores) {
pickStores(stores) {
return _.pickBy(stores, (_store, name) => {
return (_.isString(_store.uri) && _.startsWith(_store.uri, 'mongodb://'))
})
Expand All @@ -20,20 +20,19 @@ module.exports = {
* @param {TrailsApp} app
* @return {Object}
*/
transformModels (app) {
transformModels(app) {
const models = app.models
const dbConfig = app.config.database
return _.mapValues(models, (model, modelName) => {
const config = model.constructor.config(app, require('mongoose')) || { }
const schema = model.constructor.schema(app, require('mongoose')) || { }
const config = model.constructor.config(app, require('mongoose')) || {}
const schema = model.constructor.schema(app, require('mongoose')) || {}
const onSchema = config.onSchema || _.noop

return {
identity: modelName.toLowerCase(),
globalId: modelName,
tableName: config.tableName || modelName.toLowerCase(),
connection: config.store || dbConfig.models.defaultStore,
migrate: config.migrate || dbConfig.models.migrate,
connection: config.store || app.config.get('database.models.defaultStore'),
migrate: config.migrate || app.config.get('database.models.migrate'),
schema: schema,
schemaOptions: config.schema,
statics: config.statics || {},
Expand Down
23 changes: 11 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "trailpack-mongoose",
"version": "2.1.4",
"version": "3.0.0",
"description": "Mongoose Trailpack",
"main": "index.js",
"keywords": [
Expand All @@ -21,25 +21,24 @@
}
],
"dependencies": {
"lodash": "^4.0.0",
"mongoose": "^4.7.7",
"trailpack": "2.1.0",
"trailpack-datastore": "2.0.0"
"lodash": "^4.17.5",
"mongoose": "^5.0.10",
"trailpack": "^3.0.0"
},
"devDependencies": {
"chai": "^3.5.0",
"eslint": "3.13.1",
"eslint-config-trails": "2.0.6",
"mocha": "3.2.0",
"smokesignals": "2.1.0",
"trails": "2.0.0"
"chai": "^4.1.2",
"eslint": "^4.19.0",
"eslint-config-trails": "^3.1.0",
"mocha": "^5.0.4",
"smokesignals": "^3.0.4",
"trails": "^3.2.1"
},
"scripts": {
"test": "eslint --fix . && NODE_ENV=test mocha",
"mocha": "NODE_ENV=test mocha"
},
"engines": {
"node": ">= 4.0.0"
"node": ">= 7.0.0"
},
"eslintConfig": {
"extends": "trails"
Expand Down
2 changes: 1 addition & 1 deletion test/lib/transformer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('lib.Transformer', () => {
describe('#pickStores', () => {

it('should pick only stores for mongo', () => {
const stores = lib.Transformer.pickStores(global.app.config.database.stores)
const stores = lib.Transformer.pickStores(global.app.config.get('database.stores'))
assert.equal(typeof stores, 'object')
assert.deepEqual(Object.keys(stores), [ 'teststore', 'storeoverride' ])
})
Expand Down
1 change: 0 additions & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
--reporter spec
--recursive
--no-exit
--slow 50
--check-leaks
--globals app
Expand Down
2 changes: 1 addition & 1 deletion test/model/Role.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict'
const Model = require('trails/model')
const Model = require('trails/lib/Model')
const Schema = require('mongoose').Schema

module.exports = class Role extends Model {
Expand Down
2 changes: 1 addition & 1 deletion test/model/User.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict'
const Model = require('trails/model')
const Model = require('trails/lib/Model')
const Schema = require('mongoose').Schema

module.exports = class User extends Model {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/services/FootprintService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('FootprintService', () => {
})

it('should exist', () => {
expect(global.app.api.services['FootprintService']).to.be.defined
expect(global.app.api.services['FootprintService']).exist
expect(global.app.services.FootprintService).to.be.an('object')
})

Expand Down
Loading