jLinux

Untitled

Dec 23rd, 2015
125
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.hasOne( OS );
  13.         return this.belongsTo( OS );
  14.     }
  15.     //, application: function(){ return this.hasMany( Application ).through( AppServer ); }
  16. });
  17.  
  18. var OS = Bookshelf.Model.extend({
  19.     tableName: 'operating_systems',
  20.     server: function() {
  21.         //return this.belongsToMany( Server );
  22.         return this.hasMany( Server );
  23.     }
  24. });
  25.  
  26. var AppServer = Bookshelf.Model.extend({
  27.     tableName: 'servers_applications',
  28.     server: function() {
  29.         return this.belongsTo( Server );
  30.     },
  31.     application: function(){
  32.         return this.belongsTo( Application );
  33.     }
  34. });
  35.  
  36. var Application = Bookshelf.Model.extend({
  37.     tableName: 'applications',
  38.     appServer: function() {
  39.         return this.belongsToMany( Server ).through( AppServer );
  40.     }
  41. });
  42.  
  43. new Server()
  44.     //.where({id: 1})
  45.     .fetch({
  46.         withRelated: ['operating_system']
  47.     })
  48.     .then((result) => {
  49.         console.log('SERVERs:', JSON.stringify(result));
  50.  
  51.         Knex.destroy(() => {
  52.             console.log('Terminated Connection pool');
  53.         });
  54.     })
  55.     .catch((err) => {
  56.         console.log('Query Error:',err);
  57.  
  58.         Knex.destroy(() => {
  59.             console.log('Terminated Connection pool');
  60.         });
  61.     });
Advertisement
Add Comment
Please, Sign In to add comment