All files / framework/src/api/db/model BasicModel.js

82.61% Statements 19/23
66.67% Branches 4/6
87.5% Functions 7/8
81.82% Lines 18/22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 1001x 1x 1x 1x               120x 1x   119x                   1x                                             4x                                 19x 19x 19x       19x                   14x 14x     14x                   23x       1x  
const { Model } = require('objection');
const objection = require('objection');
const { Errors } = require('../../../constants');
const debug = require('../../../log')('BasicModel');
 
/**
 * BasicModel implementation with default properties and transactional query creator
 * @abstract
 */
class BasicModel extends Model {
    constructor() {
        if (new.target === BasicModel) {
            throw new Error(Errors.AbstractClassConstructor('BasicModel'));
        }
        super();
    }
 
    /**
     * Table name getter
     * @abstract
     * @throws {AbstractClassConstructor}
     * @return {String} table name
     */
    static get tableName() {
        throw new Error(Errors.AbstractClassConstructor('BasicModel'));
    }
 
    /**
     * Relations getter
     * @abstract
     * @return {String} list of relations names
     */
    static get relations() {
        return '';
    }
 
    /**
     * Json Schema initializer
     * @type {BasicSchema}
     * @property {null | number} id unique database identifier (primary key)
     * @property {null | Date} createdAt Date-time of object creation
     * @property {null | Date} updatedAt Date-time of object last update
     * @property {null | number} createdBy user ID (creator)
     * @property {null | number} updatedBy user ID (updater)
     * @return {BasicSchema} BasicModel schema
     */
    static get jsonSchema() {
        return {
            type: 'object',
            properties: {
                id: { type: 'integer' },
                createdAt: { anyOf: [{ type: 'null' }, { type: 'string', format: 'date-time' }] },
                updatedAt: { anyOf: [{ type: 'null' }, { type: 'string', format: 'date-time' }] },
                createdBy: { anyOf: [{ type: 'null' }, { type: 'integer' }] },
                updatedBy: { anyOf: [{ type: 'null' }, { type: 'integer' }] },
            },
        };
    }
 
    /**
     * Method called before entity is inserted (sets: createdAt, updatedAt, createdBy, updatedBy)
     * @param context {ReqContext} request context
     */
    $beforeInsert(context) {
        this.createdAt = new Date().toISOString();
        this.updatedAt = new Date().toISOString();
        Iif (context.user) {
            this.createdBy = context.user.id;
            this.updatedBy = context.user.id;
        } else {
            debug('koex:BasicModel No user in context');
        }
    }
 
    /**
     * Method called before entity is updated (sets: updatedAt, updatedBy)
     * @param options
     * @param context {ReqContext} request context
     */
    $beforeUpdate(options, context) {
        this.updatedAt = new Date().toISOString();
        Iif (context.user) {
            this.updatedBy = context.user.id;
        } else {
            debug('koex:BasicModel  No user in context');
        }
    }
 
    /**
     * Executes query
     * @param callback
     * @return {Promise<BasicModel | BasicModel[]>}
     */
    static makeQuery(callback) {
        return objection.transaction(this.knex(), transaction => callback(transaction));
    }
}
 
module.exports = BasicModel;