Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- //const Bookshelf = require('./bookshelf');
- const Knex = require( 'knex' )( require('./config').database );
- const Bookshelf = require('bookshelf')( Knex );
- var Server = Bookshelf.Model.extend({
- tableName: 'servers',
- operating_system: function() {
- return this.belongsTo( OS );
- },
- application: function(){
- return this.belongsToMany( Application );
- }
- });
- var OS = Bookshelf.Model.extend({
- tableName: 'operating_systems',
- server: function() {
- return this.hasMany( Server );
- }
- });
- var Application = Bookshelf.Model.extend({
- tableName: 'applications',
- appServer: function() {
- return this.belongsToMany( Server );
- }
- });
- var server = new Server();
- server.on('fetching', ( model, columns ) => {
- console.log('FETCHING!!');
- });
- server
- .where({id: 2})
- .fetch({
- withRelated: ['operating_system','application']
- })
- .then((result) => {
- console.log('SERVERs:', JSON.stringify(result));
- console.log('Last Query',result._knex.toSQL());
- Knex.destroy(() => {
- console.log('Terminated Connection pool');
- });
- })
- .catch((err) => {
- console.log('Query Error:',err);
- Knex.destroy(() => {
- console.log('Terminated Connection pool');
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment