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

100% Statements 11/11
100% Branches 2/2
100% Functions 3/3
100% Lines 11/11
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 451x 1x 1x 1x                 71x 1x   70x                   1x                       12x 12x       1x    
const BasicModel = require('./BasicModel');
const _ = require('lodash');
const crypto = require('crypto');
const { Errors } = require('../../../constants');
 
/**
 * PublicModel implementation. It generates random hash identifier on insert as 'uuid' attribute
 * @abstract
 * @augments BasicModel
 */
class PublicModel extends BasicModel {
    constructor() {
        if (new.target === PublicModel) {
            throw new Error(Errors.AbstractClassConstructor('PublicModel'));
        }
        super();
    }
 
    /**
     * JsonSchema getter merge PublicModel schema with super class schema
     * @type {PublicModelSchema}
     * @property {string} uuid unique public identifier
     * @returns PublicModelSchema
     */
    static get jsonSchema() {
        return _.merge(super.jsonSchema, {
            properties: {
                uuid: { type: 'string' },
            },
        });
    }
 
    /**
     * Method called before entity is inserted (sets: uuid) (super class method called too - look at BasicModel)
     * @param context {ReqContext} request context
     */
    $beforeInsert(context) {
        super.$beforeInsert(context);
        this.uuid = crypto.randomBytes(12).toString('base64');
    }
}
 
module.exports = PublicModel;