Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { Model } = require('objection');
  2.  
  3. class Client extends Model {
  4.  
  5.     static get tableName() {
  6.         return 'Client';
  7.     }
  8.  
  9.     static get idColumn() {
  10.         return 'id';
  11.     }
  12.  
  13.     static get jsonSchema() {
  14.         return {
  15.             type: "object",
  16.             properties: {
  17.                 id: { type: "string", format: "uuid" },
  18.                 company_nif: { type: ['integer', 'null'] }
  19.  
  20.             }
  21.         }
  22.     }
  23.  
  24.     static get relationMappings() {
  25.  
  26.         const Company = require('./Company');
  27.         const Subscription = require('./Subscription');
  28.  
  29.         return {
  30.             company: {
  31.                 relation: Model.BelongsToOneRelation,
  32.                 modelClass: Company,
  33.                 join: {
  34.                     from: 'Client.company_nif',
  35.                     to: 'Company.company_nif'
  36.                 }
  37.             },
  38.             subscriptions: {
  39.                 relation: Model.HasManyRelation,
  40.                 modelClass: Subscription,
  41.                 join: {
  42.                     from: 'Client.id',
  43.                     to: 'Subscription.client_id'
  44.                 }
  45.             },
  46.         }
  47.  
  48.     }
  49.  
  50.     $beforeInsert() {
  51.         this.created_at = new Date().toISOString();
  52.     }
  53.  
  54.     $beforeUpdate() {
  55.         this.updated_at = new Date().toISOString();
  56.     }
  57.  
  58. };
  59.  
  60.  
  61.  
  62. module.exports = Client;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement