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