Skip to content

Commit e556d06

Browse files
committed
wip
1 parent e3ac5ae commit e556d06

3 files changed

Lines changed: 49 additions & 40 deletions

File tree

src/core/BaseEntity.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11

22
type FieldMap = Record<string, string>;
3+
interface IBaseEntity {
4+
id?: string;
5+
rev?: string;
6+
7+
// Allow dynamic properties
8+
[key: string]: any;
9+
10+
// Convert entity to JSON object
11+
toJSON(): Record<string, any>;
12+
}
313
import Ajv, { } from 'ajv'; // Import Ajv types
414

515
// src/BaseEntity.ts
6-
abstract class BaseEntity {
16+
abstract class BaseEntity implements IBaseEntity {
717
private _id?: string;
818
private _rev?: string;
919

@@ -34,10 +44,43 @@ abstract class BaseEntity {
3444
throw new Error(`${this.constructor.name} must define type`);
3545
}
3646

47+
// Initialize the AJV instance with options
48+
const ajv = new Ajv({});
49+
let schema: any = this.schemaOrSchemaId;
50+
51+
// If schemaOrSchemaId is a string (schema ID), fetch the schema
52+
if (typeof schema === "string") {
53+
schema = ajv.getSchema(schema)?.schema; // Retrieve the schema
54+
if (!schema) {
55+
throw new Error(`Schema with ID ${schema} not found.`);
56+
}
57+
}
58+
59+
const schemaProperties = schema?.properties || {};
60+
61+
// Dynamically add getters and setters based on schema
62+
Object.keys(schemaProperties).forEach(property => {
63+
Object.defineProperty(this, property, {
64+
get() {
65+
return this.privateData.get(this)[property]; // Retrieve the field value
66+
},
67+
set(value) {
68+
this.privateData.get(this)[property] = value; // Assign value to internal variable
69+
},
70+
enumerable: true,
71+
configurable: true
72+
});
73+
});
74+
75+
// Initialize properties from schema
76+
Object.keys(schemaProperties).forEach(property => {
77+
this.privateData.get(this)[property] = data[property];
78+
});
79+
3780
}
3881

3982
toJSON() {
40-
const data = { ...this.privateData.get(this) } ;
83+
const data = { ...this.privateData.get(this) };
4184
if (this._id) data._id = this._id;
4285
if (this._rev) data._rev = this._rev;
4386
return data;

src/core/CreateEntity.ts

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,10 @@ function createEntityBase(
3030
// Merge user-provided fieldMap with the one extracted from the schema
3131
const mergedFieldMap = {
3232
...EntityClass.extractFieldMapFromSchema(schemaOrSchemaId, ajvOptions),
33-
...{ type: fieldMap?.type }
33+
...{ type: fieldMap ? fieldMap.type : "type" }
3434
};
3535

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 || {};
36+
4937

5038
// Create a dynamic subclass of the selected entity class
5139
const DynamicEntityClass = class extends EntityClass {
@@ -55,36 +43,14 @@ function createEntityBase(
5543

5644
[key: string]: any; // This allows dynamic fields to be assigned to the instance
5745

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-
6846

6947
};
7048

7149

7250
// Assign the actual class name to the dynamically created class
7351
Object.defineProperty(DynamicEntityClass, 'name', { value: name });
7452

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-
});
53+
8854

8955
// Attach static methods if provided
9056
if (methods) {

test/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe('NoSQLax Testing Suite', () => {
102102
"TestEntitySchemaId",
103103
"TestEntity",
104104
schema,
105-
dataSource
105+
{fieldMap}
106106
);
107107

108108
const testEntitySchemaIdRepo = createRepository(TestEntitySchemaId,

0 commit comments

Comments
 (0)