jLinux

Untitled

Dec 23rd, 2015
337
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.     },
  14.     application: function(){
  15.         return this.hasMany( Application ).through( AppServer );
  16.     }
  17. });
  18.  
  19. var OS = Bookshelf.Model.extend({
  20.     tableName: 'operating_systems',
  21.     server: function() {
  22.         return this.belongsToMany( 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', 'application']
  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