diff --git a/backend/src/object/models/base.model.ts b/backend/src/object/models/base.model.ts index 3df7d97..cb59bed 100644 --- a/backend/src/object/models/base.model.ts +++ b/backend/src/object/models/base.model.ts @@ -1,4 +1,5 @@ import { Model } from 'objection'; +import { randomUUID } from 'crypto'; /** * Base model for all dynamic and system models @@ -10,26 +11,23 @@ export class BaseModel extends Model { tenantId?: string; ownerId?: string; name?: string; - created_at?: Date; - updated_at?: Date; + created_at?: string; + updated_at?: string; // Hook to set system-managed fields - $beforeInsert() { - // created_at and updated_at are handled by the database - // ownerId should be set by the controller/service + 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', ' '); + } } - $beforeUpdate() { - // updated_at is handled by the database - } - - - - /** - * Get the API name for this object - * Override in subclasses - */ - static get objectApiName(): string { - return 'BaseModel'; + async $beforeUpdate() { + this.updated_at = new Date().toISOString().slice(0, 19).replace('T', ' '); } } diff --git a/backend/src/object/models/dynamic-model.factory.ts b/backend/src/object/models/dynamic-model.factory.ts index 3ea871c..046a03b 100644 --- a/backend/src/object/models/dynamic-model.factory.ts +++ b/backend/src/object/models/dynamic-model.factory.ts @@ -1,4 +1,3 @@ -import { randomUUID } from 'crypto'; import { ModelClass, JSONSchema, RelationMappings, Model } from 'objection'; import { BaseModel } from './base.model'; @@ -82,15 +81,8 @@ export class DynamicModelFactory { targetTable: this.getTableName(f.referenceObject), })); - // Create the dynamic model class extending Model directly - class DynamicModel extends Model { - id?: string; - tenantId?: string; - ownerId?: string; - name?: string; - created_at?: string; - updated_at?: string; - + // Create the dynamic model class extending BaseModel + class DynamicModel extends BaseModel { static tableName = tableName; static objectApiName = apiName; @@ -137,22 +129,6 @@ export class DynamicModelFactory { properties, }; } - - 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', ' '); - } } return DynamicModel as any;