34 lines
823 B
TypeScript
34 lines
823 B
TypeScript
import { Model } from 'objection';
|
|
import { randomUUID } from 'crypto';
|
|
|
|
/**
|
|
* Base model for all dynamic and system models
|
|
* Provides common functionality for all objects
|
|
*/
|
|
export class BaseModel extends Model {
|
|
// Common fields
|
|
id?: string;
|
|
tenantId?: string;
|
|
ownerId?: string;
|
|
name?: string;
|
|
created_at?: string;
|
|
updated_at?: string;
|
|
|
|
// Hook to set system-managed fields
|
|
async $beforeInsert() {
|
|
if (!this.id) {
|
|
this.id = randomUUID();
|
|
}
|
|
if (!this.created_at) {
|
|
this.created_at = new Date().toISOString().slice(0, 19).replace('T', ' ');
|
|
}
|
|
if (!this.updated_at) {
|
|
this.updated_at = new Date().toISOString().slice(0, 19).replace('T', ' ');
|
|
}
|
|
}
|
|
|
|
async $beforeUpdate() {
|
|
this.updated_at = new Date().toISOString().slice(0, 19).replace('T', ' ');
|
|
}
|
|
}
|