jLinux

Untitled

Dec 24th, 2015
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. const _         = require('lodash');
  4. const Util      = require('util');
  5. const Knex      = require( 'knex' )( require('./config').database );
  6. const Bookshelf = require('bookshelf')( Knex );
  7.  
  8. Bookshelf.plugin( require( 'bookshelf-soft-delete' ) );
  9. Bookshelf.plugin( 'visibility' );
  10. Bookshelf.plugin('virtuals')
  11.  
  12. var Server = Bookshelf.Model.extend({
  13.     tableName: 'servers',
  14.     soft: true,
  15.     hidden: ['root_password'],
  16.     hasTimestamps: true,
  17.     virtuals: {
  18.         environment: function() {
  19.             let env = this.get('server_hostname').match(/^[a-z]{1}([a-z]{1})/im);
  20.  
  21.             switch( env[1] ){
  22.                 case 'p': return 'prod'   break;
  23.                 case 'd': return 'dev';   break;
  24.                 case 's': return 'stage'; break;
  25.                 case 'q': return 'qa';    break;
  26.                 default:  return 'N/A';   break;
  27.             }
  28.         }
  29.     },
  30.     operating_system: function() {
  31.         return this.belongsTo( OS );
  32.     },
  33.     application: function(){
  34.         return this.belongsToMany( Application );
  35.     }
  36. });
  37.  
  38. var OS = Bookshelf.Model.extend({
  39.     tableName: 'operating_systems',
  40.     hasTimestamps: true,
  41.     server: function() {
  42.         return this.hasMany( Server );
  43.     }
  44. });
  45.  
  46. var Application = Bookshelf.Model.extend({
  47.     tableName: 'applications',
  48.     hasTimestamps: true,
  49.     appServer: function() {
  50.         return this.belongsToMany( Server );
  51.     }
  52. });
Advertisement
Add Comment
Please, Sign In to add comment