|
| 1 | +import ActiveRecordEntity from "./ActiveRecordEntity"; |
| 2 | +import BaseEntity from "./BaseEntity"; |
| 3 | +import DataMapperEntity from "./DataMapperEntity"; |
| 4 | +import DataSource from "./DataSource"; |
| 5 | +import Ajv, { } from 'ajv'; // Import Ajv types |
| 6 | + |
| 7 | + |
| 8 | +type DataMapperEntityConfig = { |
| 9 | + ajvOptions?: any; // Optional |
| 10 | + fieldMap?: Record<string, string>; |
| 11 | +}; |
| 12 | + |
| 13 | +type EntityConfig = { |
| 14 | + methods?: Record<string, Function>; // Optional |
| 15 | + ajvOptions?: any; // Optional |
| 16 | + fieldMap?: Record<string, string>; |
| 17 | +}; |
| 18 | + |
| 19 | +type Constructor<T = {}> = new (...args: any[]) => T; |
| 20 | + |
| 21 | +function createEntityBase( |
| 22 | + name: string, |
| 23 | + type: string, |
| 24 | + schemaOrSchemaId: string | object, |
| 25 | + config: EntityConfig, |
| 26 | + EntityClass: typeof ActiveRecordEntity | typeof DataMapperEntity |
| 27 | +) { |
| 28 | + const { methods, ajvOptions, fieldMap = {} } = config; |
| 29 | + |
| 30 | + // Merge user-provided fieldMap with the one extracted from the schema |
| 31 | + const mergedFieldMap = { |
| 32 | + ...EntityClass.extractFieldMapFromSchema(schemaOrSchemaId, ajvOptions), |
| 33 | + ...{ type: fieldMap?.type } |
| 34 | + }; |
| 35 | + |
| 36 | + // Initialize the AJV instance with options |
| 37 | + const ajv = new Ajv(ajvOptions || {}); |
| 38 | + let schema: any = schemaOrSchemaId; |
| 39 | + |
| 40 | + // If schemaOrSchemaId is a string (schema ID), fetch the schema |
| 41 | + if (typeof schema === "string") { |
| 42 | + schema = ajv.getSchema(schema)?.schema; // Retrieve the schema |
| 43 | + if (!schema) { |
| 44 | + throw new Error(`Schema with ID ${schema} not found.`); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + const schemaProperties = schema?.properties || {}; |
| 49 | + |
| 50 | + // Create a dynamic subclass of the selected entity class |
| 51 | + const DynamicEntityClass = class extends EntityClass { |
| 52 | + static type = type; |
| 53 | + static schemaOrSchemaId = schemaOrSchemaId; |
| 54 | + static fieldMap = mergedFieldMap; |
| 55 | + |
| 56 | + [key: string]: any; // This allows dynamic fields to be assigned to the instance |
| 57 | + |
| 58 | + constructor(data: { _id?: string; _rev?: string;[key: string]: any }) { |
| 59 | + super(data); |
| 60 | + |
| 61 | + // Initialize properties from schema |
| 62 | + Object.keys(schemaProperties).forEach(property => { |
| 63 | + this.privateData.get(this)[property] = data[property]; |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + }; |
| 70 | + |
| 71 | + |
| 72 | + // Assign the actual class name to the dynamically created class |
| 73 | + Object.defineProperty(DynamicEntityClass, 'name', { value: name }); |
| 74 | + |
| 75 | + // Dynamically add getters and setters based on schema |
| 76 | + Object.keys(schemaProperties).forEach(property => { |
| 77 | + Object.defineProperty(DynamicEntityClass.prototype, property, { |
| 78 | + get() { |
| 79 | + return this.privateData.get(this)[property]; // Retrieve the field value |
| 80 | + }, |
| 81 | + set(value) { |
| 82 | + this.privateData.get(this)[property] = value; // Assign value to internal variable |
| 83 | + }, |
| 84 | + enumerable: true, |
| 85 | + configurable: true |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + // Attach static methods if provided |
| 90 | + if (methods) { |
| 91 | + Object.assign(DynamicEntityClass, methods); |
| 92 | + } |
| 93 | + |
| 94 | + return DynamicEntityClass as unknown; |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +export function createActiveRecordEntity( |
| 99 | + name: string, |
| 100 | + type: string, |
| 101 | + schemaOrSchemaId: string | object, |
| 102 | + dataSource: DataSource, |
| 103 | + config: EntityConfig |
| 104 | +) { |
| 105 | + // Explicitly type the EntityClass as typeof ActiveRecordEntity |
| 106 | + let EntityClass = createEntityBase(name, type, schemaOrSchemaId, config, ActiveRecordEntity) as typeof ActiveRecordEntity; |
| 107 | + |
| 108 | + // Automatically attach the dataSource with optional ajvOptions |
| 109 | + EntityClass['attachDataSource'](dataSource, config.ajvOptions || {}); |
| 110 | + |
| 111 | + return EntityClass as typeof ActiveRecordEntity; |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +export function createDataMapperEntity( |
| 117 | + name: string, |
| 118 | + type: string, |
| 119 | + schemaOrSchemaId: string | object, |
| 120 | + config: DataMapperEntityConfig |
| 121 | +) { |
| 122 | + // Use the shared base function for creating the class |
| 123 | + const EntityClass = createEntityBase(name, type, schemaOrSchemaId, config, DataMapperEntity) as typeof DataMapperEntity; |
| 124 | + |
| 125 | + |
| 126 | + return EntityClass as typeof DataMapperEntity; |
| 127 | +} |
0 commit comments