2016-04-10 08:37:05 +00:00
|
|
|
import knex from 'knex'
|
|
|
|
import bookshelf from 'bookshelf'
|
|
|
|
|
2020-04-06 22:47:58 +00:00
|
|
|
import defaults from './defaults.mjs'
|
|
|
|
import config from './config.mjs'
|
|
|
|
import log from './log.mjs'
|
2016-04-10 08:37:05 +00:00
|
|
|
|
|
|
|
let host = config.get('knex:connection')
|
|
|
|
|
|
|
|
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
|
2020-04-06 22:47:58 +00:00
|
|
|
let attributes = defaults(attr, {
|
2017-12-03 11:34:43 +00:00
|
|
|
/**
|
|
|
|
* Initialize a new instance of model. This does not get called when
|
|
|
|
* relations to this model is being fetched though.
|
|
|
|
*/
|
2016-04-10 08:37:05 +00:00
|
|
|
initialize() {
|
|
|
|
this.on('fetching', this.checkFetching)
|
|
|
|
},
|
|
|
|
|
2017-12-03 11:34:43 +00:00
|
|
|
/**
|
|
|
|
* Event handler when fetch() is called. This gets called for both
|
|
|
|
* when getSingle() or just manual fetch() is called as well as
|
|
|
|
* when relation models through belongsTo() resources get fetched.
|
|
|
|
*
|
|
|
|
* @param {Model} model - The model instance if fetch() was used. For
|
|
|
|
* belongsTo this is the relation model thingy.
|
|
|
|
* @param {Array} columns - Array of columns to select if fetch() was used.
|
|
|
|
* Otherwise this is null.
|
|
|
|
* @param {Object} options - Options for the fetch. Includes the query
|
|
|
|
* builder object.
|
|
|
|
*/
|
2016-04-10 08:37:05 +00:00
|
|
|
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
|
2020-04-06 22:47:58 +00:00
|
|
|
let options = defaults(opts, {
|
2017-12-03 11:34:43 +00:00
|
|
|
/**
|
|
|
|
* Create new model object in database.
|
|
|
|
*
|
|
|
|
* @param {Object} data - The values the new model should have
|
|
|
|
* @return {Model} The resulted model
|
|
|
|
*/
|
2016-04-10 08:37:05 +00:00
|
|
|
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 })
|
|
|
|
},
|
|
|
|
|
2018-06-26 18:35:12 +00:00
|
|
|
getAll(where = {}, withRelated = [], orderBy = 'id') {
|
2016-04-14 04:01:51 +00:00
|
|
|
where.is_deleted = false
|
|
|
|
|
2016-04-10 08:37:05 +00:00
|
|
|
return this.query({ where })
|
2018-06-26 18:35:12 +00:00
|
|
|
.orderBy(orderBy, 'ASC')
|
2016-04-10 08:37:05 +00:00
|
|
|
.fetchAll({ withRelated })
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return shelf.Model.extend(attributes, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default shelf
|