All files / lib/objection-utils model.decorator.lib.ts

100% Statements 20/20
66.67% Branches 10/15
100% Functions 7/7
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  4x   4x 7x 7x 7x 7x 12x 12x 12x     7x 4x 4x     7x       4x 9x 9x 9x 4x 4x   4x        
import { Model } from "objection";
import uniqid from "uniqid";
 
export const timestamp = () => {
  return function<T extends Model>(target): any {
    const old$beforeInsert = target.prototype.$beforeInsert;
    const old$beforeUpdate = target.prototype.$beforeUpdate;
    target.prototype.$beforeInsert = function() {
      this.createdAt = new Date();
      this.updatedAt = new Date();
      old$beforeInsert?.call(this);
    };
 
    target.prototype.$beforeUpdate = function() {
      this.updatedAt = new Date();
      old$beforeUpdate?.call(this);
    };
 
    return target;
  };
};
 
export const randomId = (prefix = "") => {
  return function<T extends Model>(target): any {
    const old$beforeInsert = target.prototype.$beforeInsert;
    target.prototype.$beforeInsert = function() {
      Eif (!this[target.idColumn]) {
        this[target.idColumn] = uniqid(prefix);
      }
      old$beforeInsert?.call(this);
    };
  };
};