Skip to content

Commit f676891

Browse files
committed
wip complex schema
1 parent f85357b commit f676891

4 files changed

Lines changed: 11 additions & 6 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"watch": "tsc --watch",
1717
"data-mapper-basic-example": "node example/basic-example/data-mapper-basic-example.js",
1818
"active-record-basic-example": "node example/basic-example/active-record-basic-example.js",
19-
"custom-field-mapping-example": "node example/custom-field-mapping/custom-field-mapping-example.js"
19+
"custom-field-mapping-example": "node example/custom-field-mapping/custom-field-mapping-example.js",
20+
"complex-schema-example": "node example/complex-schema/complex-schema-example.js"
2021
},
2122
"keywords": [
2223
"couchdb",

src/core/ActiveRecordEntity.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Nano, { DocumentScope } from "nano";
1010
abstract class ActiveRecordEntity extends BaseEntity {
1111

1212
private static repoMap: Map<typeof ActiveRecordEntity, CouchRepository> = new Map();
13+
1314

1415
constructor(data: { _id?: string; _rev?: string;[key: string]: any }) {
1516
super(data);

src/core/BaseEntity.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,18 @@ abstract class BaseEntity implements IBaseEntity {
107107

108108
static type: string;
109109
static schemaOrSchemaId: string | object;
110+
static ajvOptions: any = {};
111+
110112
private __data: WeakMap<any, Record<string, any>> = new WeakMap();
111113
private validators: Record<string, ValidateFunction> = {};
112114

113-
114115
// Index signature to allow dynamic properties
115116
[key: string]: any; // This allows dynamic fields to be assigned to the instance
116117

117118
// Map from entity attributes to document fields, type is implicitly handled
118119
static fieldMap: Record<string, string> = { type: "type" }; // Default fieldMap, type is implicitly required
119120

120-
constructor(data: { _id?: string; _rev?: string;[key: string]: any } = {}) {
121+
constructor(data: { _id?: string; _rev?: string;[key: string]: any } = {}, ajvOptions: any = {}) {
121122
this._id = data._id;
122123
this._rev = data._rev;
123124
this.__data.set(this, {});
@@ -129,7 +130,8 @@ abstract class BaseEntity implements IBaseEntity {
129130
return;
130131
}
131132

132-
const ajv = new Ajv({});
133+
const ajvOptionsCtor = ctor.ajvOptions || {};
134+
const ajv = new Ajv(ajvOptionsCtor);
133135
let schema: any;
134136

135137
if (typeof ctor.schemaOrSchemaId === 'string') {
@@ -158,7 +160,7 @@ abstract class BaseEntity implements IBaseEntity {
158160
}
159161

160162
// compile to dereference the schema
161-
const validator =ajv.compile(rawSchema);
163+
const validator = ajv.compile(rawSchema);
162164
const resolvedSchema = validator.schema as AnySchemaObject;
163165
restructureSchemaFromFieldMap(rawSchema, fieldMap);
164166

src/core/CreateEntity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ function createEntityBase(
3232
const DynamicEntityClass = class extends EntityClass {
3333
static type = type;
3434
static schemaOrSchemaId = schemaOrSchemaId;
35-
static fieldMap = fieldMap;
35+
static fieldMap = config.fieldMap;
36+
static ajvOptions = config.ajvOptions;
3637

3738
constructor(data: Record<string, any>) {
3839
super(data);

0 commit comments

Comments
 (0)