jLinux

Untitled

Dec 23rd, 2015
135
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. Server.on('fetching', ( model, columns ) => {
  34.     console.log('FETCHING!!');
  35. });
  36. /* Triggers this error:
  37.  
  38.  Server.on('fetching', ( model, columns ) => {
  39.  ^
  40.  
  41.  TypeError: Server.on is not a function
  42.  at Object.<anonymous> (/Users/jhyland/Documents/scripts/js/node/bookshelf_knex/server-relates.js:33:8)
  43.  at Module._compile (module.js:399:26)
  44.  */
  45.  
  46.  
  47. Server
  48.     .where({id: 2})
  49.     .fetch({
  50.         withRelated: ['operating_system','application']
  51.     })
  52.     .then((result) => {
  53.         console.log('SERVERs:', JSON.stringify(result));
  54.  
  55.         console.log('Last Query',result._knex.toSQL());
  56.         Knex.destroy(() => {
  57.             console.log('Terminated Connection pool');
  58.         });
  59.     })
  60.     .catch((err) => {
  61.         console.log('Query Error:',err);
  62.  
  63.         Knex.destroy(() => {
  64.             console.log('Terminated Connection pool');
  65.         });
  66.     });
Advertisement
Add Comment
Please, Sign In to add comment