jLinux

Untitled

Dec 23rd, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. //const Bookshelf = require('./bookshelf');
  4.  
  5. const Knex      = require( 'knex' )( require('./config').database );
  6. const Bookshelf = require('bookshelf')( Knex );
  7.  
  8.  
  9. var Server = Bookshelf.Model.extend({
  10.     tableName: 'servers',
  11.     operating_system: function() {
  12.         return this.belongsTo( OS );
  13.     },
  14.     application: function(){
  15.         return this.belongsToMany( Application );
  16.     }
  17. });
  18.  
  19. var OS = Bookshelf.Model.extend({
  20.     tableName: 'operating_systems',
  21.     server: function() {
  22.         return this.hasMany( Server );
  23.     }
  24. });
  25.  
  26. var Application = Bookshelf.Model.extend({
  27.     tableName: 'applications',
  28.     appServer: function() {
  29.         return this.belongsToMany( Server );
  30.     }
  31. });
  32.  
  33. var server = new Server();
  34.  
  35. server.on('fetching', ( model, columns ) => {
  36.     console.log('FETCHING!!');
  37. });
  38.  
  39. server
  40.     .where({id: 2})
  41.     .fetch({
  42.         withRelated: ['operating_system','application']
  43.     })
  44.     .then((result) => {
  45.         console.log('SERVERs:', JSON.stringify(result));
  46.  
  47.         console.log('Last Query',result._knex.toSQL());
  48.         Knex.destroy(() => {
  49.             console.log('Terminated Connection pool');
  50.         });
  51.     })
  52.     .catch((err) => {
  53.         console.log('Query Error:',err);
  54.  
  55.         Knex.destroy(() => {
  56.             console.log('Terminated Connection pool');
  57.         });
  58.     });
Advertisement
Add Comment
Please, Sign In to add comment