How to use the objection.Model.knex function in objection

To help you get started, we’ve selected a few objection examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Vincit / objection.js / examples / express / app.js View on Github external
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();
github strues / boldr / packages / boldr-api / src / core / bootstrap.js View on Github external
static initDb() {
    logger.info('initDb: Binding to Knex instance and making a test query.');
    // bind Objection models to db instance.
    Model.knex(knex);
  }
}
github SelfKeyFoundation / Identity-Wallet / src / main / db / db.js View on Github external
/* 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
			);
github damian-pastorini / reldens / packages / game / server / data-server.js View on Github external
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);
    }
github kba / anno-common / anno-store-sql / store-sql.js View on Github external
_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()
    }
github puncoz-official / nodejs-express-mvc / src / bootstrap / app.js View on Github external
database() {
        const knex = Knex(DBConfig)
        Model.knex(knex)
    }
github ncrmro / aeon / server / src / database / index.ts View on Github external
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
    }
  }
github feathersjs / generator-feathers / generators / connection / templates / js / objection.js View on Github external
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);
};