File size: 915 Bytes
3206347
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { AjvValidator, Model, snakeCaseMappers } from 'objection';
import addFormats from 'ajv-formats';

import ExtendedQueryBuilder from './query-builder.js';

class Base extends Model {
  static QueryBuilder = ExtendedQueryBuilder;

  static get columnNameMappers() {
    return snakeCaseMappers();
  }

  static createValidator() {
    return new AjvValidator({
      onCreateAjv: (ajv) => {
        addFormats.default(ajv);
      },
      options: {
        allErrors: true,
        validateSchema: true,
        ownProperties: true,
      },
    });
  }

  async $beforeInsert(queryContext) {
    await super.$beforeInsert(queryContext);

    this.createdAt = new Date().toISOString();
    this.updatedAt = new Date().toISOString();
  }

  async $beforeUpdate(opts, queryContext) {
    this.updatedAt = new Date().toISOString();

    await super.$beforeUpdate(opts, queryContext);
  }
}

export default Base;