Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var Knex = require('knex');
var morgan = require('morgan');
var express = require('express');
var bodyParser = require('body-parser');
var knexConfig = require('./knexfile');
var registerApi = require('./api');
var Model = require('objection').Model;
// Initialize knex.
var knex = Knex(knexConfig.development);
// Bind all Models to a knex instance. If you only have one database in
// your server this is all you have to do. For multi database systems, see
// the Model.bindKnex method.
Model.knex(knex);
var app = express()
.use(bodyParser.json())
.use(morgan('dev'))
.set('json spaces', 2);
// Register our REST API.
registerApi(app);
// Error handling. The `ValidationError` instances thrown by objection.js have a `statusCode`
// property that is sent as the status code of the response.
app.use(function (err, req, res, next) {
if (err) {
res.status(err.statusCode || err.status || 500).send(err.data || err.message || {});
} else {
next();
static initDb() {
logger.info('initDb: Binding to Knex instance and making a test query.');
// bind Objection models to db instance.
Model.knex(knex);
}
}
/* istanbul ignore file */
import { Logger } from 'common/logger';
import fs from 'fs';
import { Model } from 'objection';
import { db as config } from 'common/config';
import Knex from 'knex';
const log = new Logger('db');
const knex = Knex(config);
Model.knex(knex);
const init = async () => {
try {
await createInitialDb();
} catch (e) {
log.error(e);
const backupPath = `${config.connection.filename}.bkp`;
// Tear down connections connected to existing file
await knex.destroy();
try {
createBackup(config.connection.filename, backupPath);
} catch (backupError) {
log.error(
'Automatic recovery has already been attempted and failed. Aborting. %s',
backupError
);
prepareObjection()
{
// initialize knex, the query builder:
this.knex = Knex({client: this.client, connection: this.config});
// give the knex instance to Objection.
this.model = Model.knex(this.knex);
}
_init(options, cb) {
if (typeof options === 'function') [cb, options] = [options, {}]
const {KNEXFILE} = envyConf('ANNO')
const knexConfig = require(KNEXFILE)
this.knex = knex(knexConfig.development)
Model.knex(this.knex)
this.models = require('./models')
return cb()
}
beforeAll(() => {
Model.knex(db);
});
database() {
const knex = Knex(DBConfig)
Model.knex(knex)
}
connect() {
if (!this.initialized) {
log.logTwoTone ("Database:", " Initializing...");
this.knex = Knex (this.knexConfig);
this.model = Model.knex (this.knex);
log.logTwoTone ("Database:", " Connection success!");
this.initialized = true
}
}
module.exports = function (app) {
const { client, connection } = app.get('<%= database %>');
const knex = require('knex')({ client, connection, useNullAsDefault: false });
Model.knex(knex);
app.set('knex', knex);
};