import Knex from 'knex' ; import { ID } from '@api/shared/shared.types' ; import { Model } from 'objection' ; import { isDev } from '@shared/isDev' ; import { generateId } from './db.utils' ; const config = require ( '../../knexfile' ) . development ; declare global { namespace NodeJS { interface Global { __KNEX_DB_CONNECTION__ : Knex ; } } } function getKnexConnection ( ) { if ( ! isDev ( ) ) { return Knex ( config ) ; } if ( ! global . __KNEX_DB_CONNECTION__ ) { global . __KNEX_DB_CONNECTION__ = Knex ( config ) ; } return global . __KNEX_DB_CONNECTION__ ; } export const knex = getKnexConnection ( ) ; Model . knex ( knex ) ; export class BaseModel extends Model { id : ID ; createdAt : Date ; updatedAt : Date ; $beforeInsert ( ) { if ( ! this . id ) { this . id = generateId ( ) ; } this . createdAt = new Date ( ) ; } $beforeUpdate ( ) { this . updatedAt = new Date ( ) ; } }