filo_caspar/server/bookshelf.js

70 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-04-10 08:37:05 +00:00
import _ from 'lodash'
import knex from 'knex'
import bookshelf from 'bookshelf'
import config from '../config'
import log from '../log'
let host = config.get('knex:connection')
/* istanbul ignore if */
if (host.match && host.match(/@[^/]+/)) {
host = host.match(/@[^/]+/)[0]
}
log.info(host, 'Connecting to DB')
const client = knex(config.get('knex'))
// Check if we're running tests while connected to
// potential production environment.
/* istanbul ignore if */
if (config.get('NODE_ENV') === 'test' &&
config.get('knex:connection:database') !== 'test' ||
config.get('knex:connection:connection')) {
// There is an offchance that we're running tests on
// production database. Exit NOW!
log.error('Critical: potentially running test on production enviroment. Shutting down.')
process.exit(1)
}
let shelf = bookshelf(client)
// Helper method to create models
shelf.createModel = (attr, opts) => {
// Create default attributes to all models
let attributes = _.defaults(attr, {
initialize() {
this.on('fetching', this.checkFetching)
},
checkFetching(model, columns, options) {
2016-04-14 04:01:51 +00:00
options.query.where({ is_deleted: false })
2016-04-10 08:37:05 +00:00
},
})
// Create default options for all models
let options = _.defaults(opts, {
create(data) {
return this.forge(data).save()
},
2016-04-14 04:01:51 +00:00
getSingle(id, withRelated = [], require = true) {
2016-04-10 08:37:05 +00:00
let where = { id: Number(id) || 0 }
return this.query({ where })
.fetch({ require, withRelated })
},
getAll(where = {}, withRelated = []) {
2016-04-14 04:01:51 +00:00
where.is_deleted = false
2016-04-10 08:37:05 +00:00
return this.query({ where })
.fetchAll({ withRelated })
},
})
return shelf.Model.extend(attributes, options)
}
export default shelf