@orion-js/typed-model 4.0.0-next.3 → 4.0.0-next.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +97 -230
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +24 -21
- package/dist/index.d.ts +24 -21
- package/dist/index.js +94 -227
- package/dist/index.js.map +1 -1
- package/package.json +5 -7
package/dist/index.cjs
CHANGED
|
@@ -30,269 +30,102 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
var index_exports = {};
|
|
31
31
|
__export(index_exports, {
|
|
32
32
|
Prop: () => Prop,
|
|
33
|
-
ResolverProp: () => ResolverProp,
|
|
34
|
-
TypedModel: () => TypedModel,
|
|
35
33
|
TypedSchema: () => TypedSchema,
|
|
36
34
|
cloneSchemaClass: () => cloneSchemaClass,
|
|
37
35
|
getModelForClass: () => getModelForClass,
|
|
38
|
-
getSchemaForClass: () => getSchemaForClass
|
|
36
|
+
getSchemaForClass: () => getSchemaForClass,
|
|
37
|
+
internal_getModelForClassFromMetadata: () => internal_getModelForClassFromMetadata,
|
|
38
|
+
resetModelCache: () => resetModelCache
|
|
39
39
|
});
|
|
40
40
|
module.exports = __toCommonJS(index_exports);
|
|
41
41
|
|
|
42
|
-
// src/errors/CannotDetermineType.ts
|
|
43
|
-
var CannotDetermineTypeError = class extends Error {
|
|
44
|
-
constructor(schemaName, propertyKey) {
|
|
45
|
-
super(
|
|
46
|
-
`Cannot determine type at "${schemaName}.${propertyKey}" field (object/union/ambiguous type was used). Make sure your property decorator defines a "type" option. For example: "@Prop({ type: {name: String, age: Number} })"`
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
// src/errors/CannotUseArray.ts
|
|
52
|
-
var CannotUseArrayError = class extends Error {
|
|
53
|
-
constructor(schemaName, propertyKey) {
|
|
54
|
-
super(
|
|
55
|
-
`Cannot infer type from an Array TypeScript type at "${schemaName}.${propertyKey}" field. Make sure your property decorator defines a "type" option. For example: "@Prop({ type: [String | Number | ...] })"`
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
// src/errors/PropertyAlreadyExists.ts
|
|
61
|
-
var PropertyAlreadyExistsError = class extends Error {
|
|
62
|
-
constructor(schemaName, propertyName) {
|
|
63
|
-
super(`Schema with name "${schemaName}" already contains property "${propertyName}".`);
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
// src/storage/metadataStorage.ts
|
|
68
|
-
var import_helpers = require("@orion-js/helpers");
|
|
69
|
-
var MetadataStorageHandler = class {
|
|
70
|
-
schemas = /* @__PURE__ */ new Map();
|
|
71
|
-
getSchema(target) {
|
|
72
|
-
const schema = this.schemas.get(target.__schemaId);
|
|
73
|
-
if (schema) return schema;
|
|
74
|
-
const schemaId = (0, import_helpers.generateId)();
|
|
75
|
-
target.__schemaId = schemaId;
|
|
76
|
-
const newSchema = {
|
|
77
|
-
schema: target,
|
|
78
|
-
options: {},
|
|
79
|
-
properties: {},
|
|
80
|
-
resolvers: {}
|
|
81
|
-
};
|
|
82
|
-
this.schemas.set(target.__schemaId, newSchema);
|
|
83
|
-
return newSchema;
|
|
84
|
-
}
|
|
85
|
-
addSchemaMetadata({ target, options }) {
|
|
86
|
-
const schema = this.getSchema(target);
|
|
87
|
-
schema.options = options;
|
|
88
|
-
}
|
|
89
|
-
addPropMetadata({
|
|
90
|
-
target,
|
|
91
|
-
propertyKey,
|
|
92
|
-
options
|
|
93
|
-
}) {
|
|
94
|
-
const schema = this.getSchema(target);
|
|
95
|
-
const currProp = schema.properties[propertyKey];
|
|
96
|
-
if (currProp) {
|
|
97
|
-
throw new PropertyAlreadyExistsError(target.name, propertyKey);
|
|
98
|
-
}
|
|
99
|
-
schema.properties[propertyKey] = options;
|
|
100
|
-
}
|
|
101
|
-
addResolverMetadata({
|
|
102
|
-
target,
|
|
103
|
-
propertyKey,
|
|
104
|
-
options
|
|
105
|
-
}) {
|
|
106
|
-
const schema = this.getSchema(target);
|
|
107
|
-
const currResolver = schema.resolvers[propertyKey];
|
|
108
|
-
if (currResolver) {
|
|
109
|
-
throw new PropertyAlreadyExistsError(target.name, propertyKey);
|
|
110
|
-
}
|
|
111
|
-
schema.resolvers[propertyKey] = options;
|
|
112
|
-
}
|
|
113
|
-
getSchemaProps(target) {
|
|
114
|
-
const schema = this.getSchema(target);
|
|
115
|
-
return schema.properties;
|
|
116
|
-
}
|
|
117
|
-
getSchemaResolvers(target) {
|
|
118
|
-
const schema = this.getSchema(target);
|
|
119
|
-
return schema.resolvers;
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
var MetadataStorage = new MetadataStorageHandler();
|
|
123
|
-
|
|
124
|
-
// src/decorators/typedModel.ts
|
|
125
|
-
function TypedModel(options = {}) {
|
|
126
|
-
return (target) => {
|
|
127
|
-
MetadataStorage.addSchemaMetadata({ target, options });
|
|
128
|
-
target.getModel = () => getModelForClass(target);
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
|
|
132
42
|
// src/decorators/typedSchema.ts
|
|
133
|
-
|
|
134
|
-
return (target) => {
|
|
135
|
-
MetadataStorage.addSchemaMetadata({ target, options });
|
|
136
|
-
target.getModel = () => getModelForClass(target);
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// src/decorators/prop.ts
|
|
141
|
-
var import_reflect_metadata = require("reflect-metadata");
|
|
142
|
-
|
|
143
|
-
// src/utils/isClass.ts
|
|
144
|
-
var isClass = (type) => /^class\s/.test(Function.prototype.toString.call(type));
|
|
145
|
-
|
|
146
|
-
// src/decorators/prop.ts
|
|
147
|
-
function Prop(options = {}) {
|
|
148
|
-
return (classDef, propertyKey) => {
|
|
149
|
-
var _a;
|
|
150
|
-
const schemaName = (_a = classDef.constructor) == null ? void 0 : _a.name;
|
|
151
|
-
if (!options.type) {
|
|
152
|
-
const type = Reflect.getMetadata("design:type", classDef, propertyKey);
|
|
153
|
-
if (isClass(type) || type === Object) {
|
|
154
|
-
throw new CannotDetermineTypeError(schemaName, propertyKey);
|
|
155
|
-
}
|
|
156
|
-
if (type === Array) {
|
|
157
|
-
throw new CannotUseArrayError(schemaName, propertyKey);
|
|
158
|
-
}
|
|
159
|
-
if (type) {
|
|
160
|
-
options.type = type;
|
|
161
|
-
} else {
|
|
162
|
-
throw new CannotDetermineTypeError(schemaName, propertyKey);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
MetadataStorage.addPropMetadata({ target: classDef.constructor, propertyKey, options });
|
|
166
|
-
classDef[propertyKey] = options;
|
|
167
|
-
};
|
|
168
|
-
}
|
|
43
|
+
var import_lodash = require("lodash");
|
|
169
44
|
|
|
170
|
-
// src/
|
|
171
|
-
|
|
172
|
-
return (classDef, propertyKey) => {
|
|
173
|
-
MetadataStorage.addResolverMetadata({ target: classDef.constructor, propertyKey, options });
|
|
174
|
-
classDef[propertyKey] = options;
|
|
175
|
-
};
|
|
176
|
-
}
|
|
45
|
+
// src/factories/getModelForClass.ts
|
|
46
|
+
var import_models = require("@orion-js/models");
|
|
177
47
|
|
|
178
|
-
// src/factories/
|
|
48
|
+
// src/factories/processTypeForProp.ts
|
|
179
49
|
var import_isPlainObject = __toESM(require("lodash/isPlainObject"), 1);
|
|
180
|
-
function
|
|
181
|
-
return [Boolean, Number, String, Date].includes(type);
|
|
182
|
-
}
|
|
183
|
-
function processSchemaForProp(prop) {
|
|
50
|
+
function getParamTypeForProp(type) {
|
|
184
51
|
var _a;
|
|
185
|
-
if (
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
if (Array.isArray(prop.type)) {
|
|
189
|
-
return prop.type.length > 0 ? { ...prop, type: [processSchemaForProp(prop.type[0])] } : prop;
|
|
190
|
-
}
|
|
191
|
-
if (typeof prop.type !== "function") {
|
|
192
|
-
if ((0, import_isPlainObject.default)(prop.type)) {
|
|
193
|
-
const subschema = {};
|
|
194
|
-
Object.keys(prop.type).forEach((key) => {
|
|
195
|
-
subschema[key] = processSchemaForProp({ type: prop.type[key] });
|
|
196
|
-
});
|
|
197
|
-
return { ...prop, type: subschema };
|
|
198
|
-
}
|
|
199
|
-
return prop;
|
|
52
|
+
if (Array.isArray(type)) {
|
|
53
|
+
const itemType = type[0];
|
|
54
|
+
return [getParamTypeForProp(itemType)];
|
|
200
55
|
}
|
|
201
|
-
if (
|
|
202
|
-
return
|
|
56
|
+
if ((_a = type == null ? void 0 : type[Symbol.metadata]) == null ? void 0 : _a._getModel) {
|
|
57
|
+
return type[Symbol.metadata]._getModel(type);
|
|
203
58
|
}
|
|
204
|
-
if (
|
|
205
|
-
|
|
206
|
-
return {
|
|
207
|
-
...prop,
|
|
208
|
-
type: schema
|
|
209
|
-
};
|
|
59
|
+
if (type == null ? void 0 : type.getSchema) {
|
|
60
|
+
return getParamTypeForProp(type.getSchema());
|
|
210
61
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
const schema = {};
|
|
215
|
-
let parent = target;
|
|
216
|
-
while (parent.prototype) {
|
|
217
|
-
if (parent === Function.prototype) {
|
|
218
|
-
break;
|
|
219
|
-
}
|
|
220
|
-
const props = MetadataStorage.getSchemaProps(parent);
|
|
221
|
-
if (!props) {
|
|
222
|
-
parent = Object.getPrototypeOf(parent);
|
|
223
|
-
continue;
|
|
62
|
+
if ((0, import_isPlainObject.default)(type)) {
|
|
63
|
+
if (type.__isFieldType) {
|
|
64
|
+
return type;
|
|
224
65
|
}
|
|
225
|
-
|
|
226
|
-
|
|
66
|
+
const subschema = {};
|
|
67
|
+
Object.keys(type).forEach((key) => {
|
|
68
|
+
if (key.startsWith("__")) {
|
|
69
|
+
subschema[key] = type[key];
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
subschema[key] = {
|
|
73
|
+
...type[key],
|
|
74
|
+
type: getParamTypeForProp(type[key].type)
|
|
75
|
+
};
|
|
227
76
|
});
|
|
228
|
-
|
|
77
|
+
return subschema;
|
|
229
78
|
}
|
|
230
|
-
return
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
// src/factories/getSchemaForClass.ts
|
|
234
|
-
function getSchemaForClass(target) {
|
|
235
|
-
return getSchemaForClassRecursive(target);
|
|
79
|
+
return type;
|
|
236
80
|
}
|
|
237
81
|
|
|
238
82
|
// src/factories/getModelForClass.ts
|
|
239
|
-
|
|
83
|
+
Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
|
|
240
84
|
var modelCache = /* @__PURE__ */ new Map();
|
|
241
|
-
function
|
|
242
|
-
|
|
243
|
-
if (((_a = prop.type) == null ? void 0 : _a.__isModel) === true) {
|
|
244
|
-
return prop;
|
|
245
|
-
}
|
|
246
|
-
if (((_b = prop.type) == null ? void 0 : _b._isFieldType) === true) {
|
|
247
|
-
return prop;
|
|
248
|
-
}
|
|
249
|
-
return processSchemaForProp(prop);
|
|
85
|
+
function resetModelCache() {
|
|
86
|
+
modelCache.clear();
|
|
250
87
|
}
|
|
251
88
|
function getModelForClass(target) {
|
|
252
89
|
const targetAsModel = target;
|
|
253
90
|
if (targetAsModel.__isModel) {
|
|
254
91
|
return targetAsModel;
|
|
255
92
|
}
|
|
256
|
-
|
|
257
|
-
if (
|
|
258
|
-
|
|
259
|
-
target = target.prototype.typedModel;
|
|
93
|
+
const metadata = target[Symbol.metadata];
|
|
94
|
+
if (!metadata) {
|
|
95
|
+
return targetAsModel;
|
|
260
96
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
97
|
+
return internal_getModelForClassFromMetadata(metadata);
|
|
98
|
+
}
|
|
99
|
+
function internal_getModelForClassFromMetadata(metadata) {
|
|
100
|
+
const modelName = metadata._modelName;
|
|
101
|
+
if (modelCache.has(modelName)) {
|
|
102
|
+
return modelCache.get(modelName);
|
|
264
103
|
}
|
|
265
104
|
const schema = {};
|
|
266
|
-
const
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
});
|
|
276
|
-
const resolvers = MetadataStorage.getSchemaResolvers(parent) ?? {};
|
|
277
|
-
Object.keys(resolvers).forEach((key) => {
|
|
278
|
-
resolverMap[key] = resolvers[key];
|
|
279
|
-
});
|
|
280
|
-
parent = Object.getPrototypeOf(parent);
|
|
105
|
+
const keys = Object.keys(metadata ?? {});
|
|
106
|
+
const injectionKeys = keys.filter((key) => key.startsWith("_prop:"));
|
|
107
|
+
for (const key of injectionKeys) {
|
|
108
|
+
const prop = metadata[key];
|
|
109
|
+
const schemaProp = key.replace("_prop:", "");
|
|
110
|
+
schema[schemaProp] = {
|
|
111
|
+
...prop,
|
|
112
|
+
type: getParamTypeForProp(prop.type)
|
|
113
|
+
};
|
|
281
114
|
}
|
|
282
115
|
const model = (0, import_models.createModel)({
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
validate: targetAsModel.validate,
|
|
287
|
-
resolvers: {
|
|
288
|
-
...resolverMap,
|
|
289
|
-
...modelResolvers
|
|
290
|
-
}
|
|
116
|
+
...metadata._modelOptions,
|
|
117
|
+
name: modelName,
|
|
118
|
+
schema
|
|
291
119
|
});
|
|
292
|
-
modelCache.set(
|
|
120
|
+
modelCache.set(modelName, model);
|
|
293
121
|
return model;
|
|
294
122
|
}
|
|
295
123
|
|
|
124
|
+
// src/factories/getSchemaForClass.ts
|
|
125
|
+
function getSchemaForClass(target) {
|
|
126
|
+
return getModelForClass(target).getSchema();
|
|
127
|
+
}
|
|
128
|
+
|
|
296
129
|
// src/factories/cloneSchemaClass.ts
|
|
297
130
|
function cloneSchemaClass(schema, options) {
|
|
298
131
|
const model = getModelForClass(schema);
|
|
@@ -304,14 +137,48 @@ function cloneSchemaClass(schema, options) {
|
|
|
304
137
|
});
|
|
305
138
|
return newModel;
|
|
306
139
|
}
|
|
140
|
+
|
|
141
|
+
// src/decorators/typedSchema.ts
|
|
142
|
+
function TypedSchema(options = {}) {
|
|
143
|
+
return (_target, context) => {
|
|
144
|
+
context.metadata._isTypedSchema = true;
|
|
145
|
+
context.metadata._modelName = options.name || context.name;
|
|
146
|
+
context.metadata._modelOptions = (0, import_lodash.omit)(options, "name");
|
|
147
|
+
context.metadata._getModel = () => {
|
|
148
|
+
return internal_getModelForClassFromMetadata(
|
|
149
|
+
context.metadata
|
|
150
|
+
);
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// src/errors/CannotDetermineType.ts
|
|
156
|
+
var CannotDetermineTypeError = class extends Error {
|
|
157
|
+
constructor(propertyKey) {
|
|
158
|
+
super(
|
|
159
|
+
`Cannot determine type at @Prop() "${propertyKey}". type: is required for all props since Orion v4`
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
// src/decorators/prop.ts
|
|
165
|
+
function Prop(options) {
|
|
166
|
+
return (_target, context) => {
|
|
167
|
+
const propertyKey = String(context.name);
|
|
168
|
+
if (!options.type) {
|
|
169
|
+
throw new CannotDetermineTypeError(propertyKey);
|
|
170
|
+
}
|
|
171
|
+
context.metadata[`_prop:${propertyKey}`] = options;
|
|
172
|
+
};
|
|
173
|
+
}
|
|
307
174
|
// Annotate the CommonJS export names for ESM import in node:
|
|
308
175
|
0 && (module.exports = {
|
|
309
176
|
Prop,
|
|
310
|
-
ResolverProp,
|
|
311
|
-
TypedModel,
|
|
312
177
|
TypedSchema,
|
|
313
178
|
cloneSchemaClass,
|
|
314
179
|
getModelForClass,
|
|
315
|
-
getSchemaForClass
|
|
180
|
+
getSchemaForClass,
|
|
181
|
+
internal_getModelForClassFromMetadata,
|
|
182
|
+
resetModelCache
|
|
316
183
|
});
|
|
317
184
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/errors/CannotDetermineType.ts","../src/errors/CannotUseArray.ts","../src/errors/PropertyAlreadyExists.ts","../src/storage/metadataStorage.ts","../src/decorators/typedModel.ts","../src/decorators/typedSchema.ts","../src/decorators/prop.ts","../src/utils/isClass.ts","../src/decorators/resolver.ts","../src/factories/helpers/processSchemaForProp.ts","../src/factories/getSchemaForClass.ts","../src/factories/getModelForClass.ts","../src/factories/cloneSchemaClass.ts"],"sourcesContent":["export * from './decorators'\nexport * from './factories'\n","export class CannotDetermineTypeError extends Error {\n constructor(schemaName: string, propertyKey: string) {\n super(\n `Cannot determine type at \"${schemaName}.${propertyKey}\" field (object/union/ambiguous type was used). Make sure your property decorator defines a \"type\" option. For example: \"@Prop({ type: {name: String, age: Number} })\"`\n )\n }\n}\n","export class CannotUseArrayError extends Error {\n constructor(schemaName: string, propertyKey: string) {\n super(\n `Cannot infer type from an Array TypeScript type at \"${schemaName}.${propertyKey}\" field. Make sure your property decorator defines a \"type\" option. For example: \"@Prop({ type: [String | Number | ...] })\"`\n )\n }\n}\n","export class PropertyAlreadyExistsError extends Error {\n constructor(schemaName: string, propertyName: string) {\n super(`Schema with name \"${schemaName}\" already contains property \"${propertyName}\".`)\n }\n}\n","import {PropOptions} from '..'\nimport {PropertyAlreadyExistsError} from '../errors'\nimport {ModelResolversMap} from '@orion-js/models'\nimport {ModelResolver, ModelResolverResolve} from '@orion-js/resolvers'\nimport {generateId} from '@orion-js/helpers'\n\nexport type PropertiesMap = {[key: string]: PropOptions}\n\ninterface SchemaStorage {\n schema: any\n options: object\n properties: PropertiesMap\n resolvers: ModelResolversMap\n}\n\nexport type TypedModelOptions = Record<string, never>\n\nexport class MetadataStorageHandler {\n private schemas = new Map<any, SchemaStorage>()\n\n private getSchema(target) {\n const schema = this.schemas.get(target.__schemaId)\n if (schema) return schema\n\n const schemaId = generateId()\n\n target.__schemaId = schemaId\n\n const newSchema = {\n schema: target,\n options: {},\n properties: {},\n resolvers: {}\n }\n this.schemas.set(target.__schemaId, newSchema)\n return newSchema\n }\n\n public addSchemaMetadata({target, options}: {target: any; options?: TypedModelOptions}) {\n const schema = this.getSchema(target)\n schema.options = options\n }\n\n public addPropMetadata({\n target,\n propertyKey,\n options\n }: {\n target: any\n propertyKey: string\n options: PropOptions\n }) {\n const schema = this.getSchema(target)\n\n const currProp = schema.properties[propertyKey]\n if (currProp) {\n throw new PropertyAlreadyExistsError(target.name, propertyKey)\n }\n schema.properties[propertyKey] = options\n }\n\n public addResolverMetadata({\n target,\n propertyKey,\n options\n }: {\n target: any\n propertyKey: string\n options: ModelResolver<ModelResolverResolve>\n }) {\n const schema = this.getSchema(target)\n\n const currResolver = schema.resolvers[propertyKey]\n if (currResolver) {\n throw new PropertyAlreadyExistsError(target.name, propertyKey)\n }\n schema.resolvers[propertyKey] = options\n }\n\n public getSchemaProps(target: any): PropertiesMap | undefined {\n const schema = this.getSchema(target)\n\n return schema.properties\n }\n\n public getSchemaResolvers(target: any): ModelResolversMap | undefined {\n const schema = this.getSchema(target)\n\n return schema.resolvers\n }\n}\n\nexport const MetadataStorage = new MetadataStorageHandler()\n","import {getModelForClass} from '..'\nimport {MetadataStorage, TypedModelOptions} from '../storage/metadataStorage'\n\n/**\n * @deprecated Please use @TypedSchema instead\n */\nexport function TypedModel(options: TypedModelOptions = {}): ClassDecorator {\n return target => {\n MetadataStorage.addSchemaMetadata({target, options})\n\n // @ts-expect-error this is a trick to make it work in resolvers without having to call getModelForClass\n target.getModel = () => getModelForClass(target)\n }\n}\n","import {getModelForClass} from '..'\nimport {MetadataStorage, TypedModelOptions} from '../storage/metadataStorage'\n\nexport function TypedSchema(options: TypedModelOptions = {}): ClassDecorator {\n return target => {\n MetadataStorage.addSchemaMetadata({target, options})\n\n // @ts-expect-error this is a trick to make it work in resolvers without having to call getModelForClass\n target.getModel = () => getModelForClass(target)\n }\n}\n","/* eslint-disable @typescript-eslint/ban-types */\nimport {Constructor, SchemaMetaFieldType, SchemaNode} from '@orion-js/schema'\nimport {MetadataStorage} from '../storage/metadataStorage'\nimport 'reflect-metadata'\nimport {CannotDetermineTypeError, CannotUseArrayError} from '../errors'\nimport {isClass} from '../utils/isClass'\nimport {Model} from '@orion-js/models'\n\nexport interface SchemaNodeForClasses extends Omit<SchemaNode, 'type'> {\n type: SchemaMetaFieldType | Constructor<any> | Model | Model[]\n}\n\nexport type PropOptions = Partial<SchemaNodeForClasses>\n\nexport function Prop(options: PropOptions = {}): PropertyDecorator {\n return (classDef: Function, propertyKey: string) => {\n const schemaName = classDef.constructor?.name\n\n if (!options.type) {\n const type = Reflect.getMetadata('design:type', classDef, propertyKey)\n\n if (isClass(type) || type === Object) {\n throw new CannotDetermineTypeError(schemaName, propertyKey)\n }\n\n if (type === Array) {\n throw new CannotUseArrayError(schemaName, propertyKey)\n }\n\n if (type) {\n options.type = type\n } else {\n throw new CannotDetermineTypeError(schemaName, propertyKey)\n }\n }\n\n MetadataStorage.addPropMetadata({target: classDef.constructor, propertyKey, options})\n\n classDef[propertyKey] = options\n }\n}\n","// eslint-disable-next-line @typescript-eslint/ban-types\nexport const isClass = (type: Function) => /^class\\s/.test(Function.prototype.toString.call(type))\n","import {ModelResolver, ModelResolverResolve} from '@orion-js/resolvers'\nimport {MetadataStorage} from '../storage/metadataStorage'\n\n/**\n * @deprecated Please use a @TypedSchema and a @Model a @ModelResolver instead\n */\nexport function ResolverProp(options: ModelResolver<ModelResolverResolve>): PropertyDecorator {\n return (classDef: any, propertyKey: string) => {\n MetadataStorage.addResolverMetadata({target: classDef.constructor, propertyKey, options})\n\n classDef[propertyKey] = options\n }\n}\n","/* eslint-disable @typescript-eslint/ban-types */\nimport isPlainObject from 'lodash/isPlainObject'\nimport {PropOptions} from '../../decorators/prop'\nimport {Schema, SchemaRecursiveNodeType} from '@orion-js/schema'\nimport {MetadataStorage} from '../../storage/metadataStorage'\nimport {isClass} from '../../utils/isClass'\n\nfunction isPrimitive(type: Function) {\n return ([Boolean, Number, String, Date] as Function[]).includes(type)\n}\n\nexport function processSchemaForProp(prop: PropOptions) {\n if (typeof (prop.type as SchemaRecursiveNodeType)?.type === 'function') {\n return processSchemaForProp(prop.type as PropOptions)\n }\n\n if (Array.isArray(prop.type)) {\n return prop.type.length > 0\n ? {...prop, type: [processSchemaForProp(prop.type[0] as PropOptions)]}\n : prop\n }\n\n if (typeof prop.type !== 'function') {\n if (isPlainObject(prop.type)) {\n const subschema = {}\n Object.keys(prop.type).forEach(key => {\n subschema[key] = processSchemaForProp({type: prop.type[key]})\n })\n return {...prop, type: subschema}\n }\n\n return prop\n }\n\n if (isPrimitive(prop.type)) {\n return prop\n }\n\n if (isClass(prop.type)) {\n const schema = getSchemaForClassRecursive(prop.type)\n return {\n ...prop,\n type: schema\n }\n }\n\n return prop\n}\n\nexport function getSchemaForClassRecursive(target): Schema {\n const schema: Schema = {}\n\n let parent: Function = target\n\n while (parent.prototype) {\n if (parent === Function.prototype) {\n break\n }\n\n const props = MetadataStorage.getSchemaProps(parent)\n if (!props) {\n parent = Object.getPrototypeOf(parent)\n continue\n }\n\n Object.keys(props).forEach(key => {\n schema[key] = processSchemaForProp(props[key])\n })\n\n parent = Object.getPrototypeOf(parent)\n }\n\n return schema\n}\n","/* eslint-disable @typescript-eslint/ban-types */\nimport {Schema} from '@orion-js/schema'\nimport {Constructor} from '../utils/interfaces'\nimport {getSchemaForClassRecursive} from './helpers/processSchemaForProp'\n\nexport function getSchemaForClass<TClass>(target: Constructor<TClass>): Schema {\n return getSchemaForClassRecursive(target)\n}\n","/* eslint-disable @typescript-eslint/ban-types */\nimport {createModel, Model, ModelSchema, ModelResolversMap} from '@orion-js/models'\nimport {FieldType} from '@orion-js/schema'\nimport {PropOptions} from '..'\nimport {MetadataStorage} from '../storage/metadataStorage'\nimport {Constructor} from '../utils/interfaces'\nimport {processSchemaForProp} from './helpers/processSchemaForProp'\n\nconst modelCache = new Map<Constructor<any>, Model>()\n\nfunction processModelSchemaForProp(prop: PropOptions) {\n if ((prop.type as Model)?.__isModel === true) {\n return prop\n }\n\n if ((prop.type as FieldType)?._isFieldType === true) {\n return prop\n }\n\n return processSchemaForProp(prop)\n}\n\nexport function getModelForClass<TClass>(target: Constructor<TClass>): Model {\n const targetAsModel = target as any as Model\n if (targetAsModel.__isModel) {\n return targetAsModel\n }\n\n let modelResolvers = null\n\n if (target.prototype.typedModel) {\n modelResolvers = target.prototype.resolvers || {}\n target = target.prototype.typedModel\n }\n\n const schemaId = (target as any).__schemaId\n\n if (modelCache.has(schemaId)) {\n return modelCache.get(schemaId)\n }\n\n const schema: ModelSchema = {}\n const resolverMap: ModelResolversMap = {}\n\n let parent: Function = target\n\n while (parent.prototype) {\n if (parent === Function.prototype) {\n break\n }\n\n const props = MetadataStorage.getSchemaProps(parent) ?? {}\n\n Object.keys(props).forEach(key => {\n schema[key] = processModelSchemaForProp(props[key])\n })\n\n const resolvers = MetadataStorage.getSchemaResolvers(parent) ?? {}\n Object.keys(resolvers).forEach(key => {\n resolverMap[key] = resolvers[key]\n })\n\n parent = Object.getPrototypeOf(parent)\n }\n\n const model = createModel({\n name: targetAsModel.name,\n schema,\n clean: targetAsModel.clean,\n validate: targetAsModel.validate,\n resolvers: {\n ...resolverMap,\n ...modelResolvers\n }\n })\n\n modelCache.set(schemaId, model)\n\n return model\n}\n","import {Model} from '@orion-js/models'\nimport {CloneOptions} from '@orion-js/models'\nimport {Constructor} from '../utils/interfaces'\nimport {getModelForClass} from './getModelForClass'\n\nexport interface CloneSchemaClassOptions<TClass, TFields extends keyof TClass> {\n name: string\n pickFields: readonly TFields[]\n mapFields?: CloneOptions['mapFields']\n extendSchema?: CloneOptions['extendSchema']\n}\n\n/**\n * This function returns a cloned model but the type is a subset of the original Schema.\n * To use the type of the cloned schema use `typeof ClonedModel.type`\n *\n * Example:\n * ```ts\n * const ClonedModel = cloneSchemaClass(Schema, {\n * name: 'ClonedSchema',\n * pickFields: ['name'] as const\n * })\n * type ClonedType = typeof ClonedModel.type\n * ```\n */\nexport function cloneSchemaClass<TClass, TFields extends keyof TClass>(\n schema: Constructor<TClass>,\n options: CloneSchemaClassOptions<TClass, TFields>\n): Model<Pick<TClass, TFields>> {\n const model = getModelForClass(schema)\n\n const newModel: Model<Pick<TClass, TFields>> = model.clone({\n name: options.name,\n pickFields: options.pickFields as any as string[],\n mapFields: options.mapFields,\n extendSchema: options.extendSchema\n })\n\n return newModel\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,2BAAN,cAAuC,MAAM;AAAA,EAClD,YAAY,YAAoB,aAAqB;AACnD;AAAA,MACE,6BAA6B,UAAU,IAAI,WAAW;AAAA,IACxD;AAAA,EACF;AACF;;;ACNO,IAAM,sBAAN,cAAkC,MAAM;AAAA,EAC7C,YAAY,YAAoB,aAAqB;AACnD;AAAA,MACE,uDAAuD,UAAU,IAAI,WAAW;AAAA,IAClF;AAAA,EACF;AACF;;;ACNO,IAAM,6BAAN,cAAyC,MAAM;AAAA,EACpD,YAAY,YAAoB,cAAsB;AACpD,UAAM,qBAAqB,UAAU,gCAAgC,YAAY,IAAI;AAAA,EACvF;AACF;;;ACAA,qBAAyB;AAalB,IAAM,yBAAN,MAA6B;AAAA,EAC1B,UAAU,oBAAI,IAAwB;AAAA,EAEtC,UAAU,QAAQ;AACxB,UAAM,SAAS,KAAK,QAAQ,IAAI,OAAO,UAAU;AACjD,QAAI,OAAQ,QAAO;AAEnB,UAAM,eAAW,2BAAW;AAE5B,WAAO,aAAa;AAEpB,UAAM,YAAY;AAAA,MAChB,QAAQ;AAAA,MACR,SAAS,CAAC;AAAA,MACV,YAAY,CAAC;AAAA,MACb,WAAW,CAAC;AAAA,IACd;AACA,SAAK,QAAQ,IAAI,OAAO,YAAY,SAAS;AAC7C,WAAO;AAAA,EACT;AAAA,EAEO,kBAAkB,EAAC,QAAQ,QAAO,GAA+C;AACtF,UAAM,SAAS,KAAK,UAAU,MAAM;AACpC,WAAO,UAAU;AAAA,EACnB;A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;AAAA,EACxB,CAAC;AAED,SAAO;AACT;","names":["isPlainObject"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/decorators/typedSchema.ts","../src/factories/getModelForClass.ts","../src/factories/processTypeForProp.ts","../src/factories/getSchemaForClass.ts","../src/factories/cloneSchemaClass.ts","../src/errors/CannotDetermineType.ts","../src/decorators/prop.ts"],"sourcesContent":["export * from './decorators'\nexport * from './factories'\n","import {omit} from 'lodash'\nimport {internal_getModelForClassFromMetadata} from '../factories'\nimport {TypedSchemaOptions} from '../storage/metadataStorage'\nimport {Model} from '@orion-js/models'\nimport {PropOptions} from './prop'\n\n/**\n * @deprecated use schema with InferSchemaType<schema as const> instead\n */\nexport function TypedSchema(options: TypedSchemaOptions = {}) {\n return (_target: any, context: ClassDecoratorContext<any>) => {\n context.metadata._isTypedSchema = true\n context.metadata._modelName = options.name || context.name\n context.metadata._modelOptions = omit(options, 'name')\n context.metadata._getModel = () => {\n return internal_getModelForClassFromMetadata(\n context.metadata as SchemaFromTypedSchemaMetadata,\n )\n }\n }\n}\n\nexport type SchemaFromTypedSchemaMetadata = {\n _isTypedSchema: true\n _modelName: string\n _modelOptions: TypedSchemaOptions\n _getModel: () => Model\n [key: `_prop:${string}`]: PropOptions\n}\n","/* eslint-disable @typescript-eslint/ban-types */\nimport {createModel, Model} from '@orion-js/models'\nimport {SchemaFromTypedSchemaMetadata} from '..'\nimport {getParamTypeForProp} from './processTypeForProp'\nimport {Schema} from '@orion-js/schema'\n\n// @ts-ignore polyfill for Symbol.metadata // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#decorator-metadata\nSymbol.metadata ??= Symbol('Symbol.metadata')\n\nconst modelCache = new Map<string, Model>()\n\nexport function resetModelCache() {\n modelCache.clear()\n}\n\nexport function getModelForClass(target: any): Model {\n const targetAsModel = target as any as Model\n if (targetAsModel.__isModel) {\n return targetAsModel\n }\n\n const metadata = target[Symbol.metadata] as SchemaFromTypedSchemaMetadata\n\n if (!metadata) {\n return targetAsModel\n }\n\n return internal_getModelForClassFromMetadata(metadata)\n}\n\nexport function internal_getModelForClassFromMetadata(metadata: SchemaFromTypedSchemaMetadata) {\n const modelName = metadata._modelName\n if (modelCache.has(modelName)) {\n return modelCache.get(modelName)\n }\n\n const schema: Schema = {}\n const keys = Object.keys(metadata ?? {})\n const injectionKeys = keys.filter(key => key.startsWith('_prop:'))\n\n for (const key of injectionKeys) {\n const prop = metadata[key] as Schema\n const schemaProp = key.replace('_prop:', '')\n\n schema[schemaProp] = {\n ...prop,\n type: getParamTypeForProp(prop.type as any),\n }\n }\n\n const model = createModel({\n ...metadata._modelOptions,\n name: modelName,\n schema,\n })\n\n modelCache.set(modelName, model)\n\n return model\n}\n","import {PropOptions} from '../decorators/prop'\nimport isPlainObject from 'lodash/isPlainObject'\n\nexport function getParamTypeForProp(type: PropOptions['type']) {\n if (Array.isArray(type)) {\n const itemType = type[0]\n return [getParamTypeForProp(itemType)]\n }\n\n if (type?.[Symbol.metadata]?._getModel) {\n return type[Symbol.metadata]._getModel(type)\n }\n\n if (type?.getSchema) {\n return getParamTypeForProp(type.getSchema())\n }\n\n if (isPlainObject(type)) {\n if (type.__isFieldType) {\n return type\n }\n\n const subschema = {}\n Object.keys(type).forEach(key => {\n if (key.startsWith('__')) {\n subschema[key] = type[key]\n return\n }\n\n subschema[key] = {\n ...type[key],\n type: getParamTypeForProp(type[key].type),\n }\n })\n\n return subschema\n }\n\n return type\n}\n","/* eslint-disable @typescript-eslint/ban-types */\nimport {Schema} from '@orion-js/schema'\nimport {getModelForClass} from './getModelForClass'\n\nexport function getSchemaForClass(target: any): Schema {\n return getModelForClass(target).getSchema()\n}\n","import {Model} from '@orion-js/models'\nimport {CloneOptions} from '@orion-js/models'\nimport {getModelForClass} from './getModelForClass'\nimport {Constructor} from '../utils/interfaces'\n\nexport interface CloneSchemaClassOptions<TClass, TFields extends keyof TClass> {\n name: string\n pickFields: readonly TFields[]\n mapFields?: CloneOptions['mapFields']\n extendSchema?: CloneOptions['extendSchema']\n}\n\n/**\n * This function returns a cloned model but the type is a subset of the original Schema.\n * To use the type of the cloned schema use `typeof ClonedModel.type`\n *\n * Example:\n * ```ts\n * const ClonedModel = cloneSchemaClass(Schema, {\n * name: 'ClonedSchema',\n * pickFields: ['name'] as const\n * })\n * type ClonedType = typeof ClonedModel.type\n * ```\n */\nexport function cloneSchemaClass<TClass, TFields extends keyof TClass>(\n schema: Constructor<TClass>,\n options: CloneSchemaClassOptions<TClass, TFields>,\n): Model<Pick<TClass, TFields>> {\n const model = getModelForClass(schema)\n\n const newModel: Model<Pick<TClass, TFields>> = model.clone({\n name: options.name,\n pickFields: options.pickFields as any as string[],\n mapFields: options.mapFields,\n extendSchema: options.extendSchema,\n })\n\n return newModel\n}\n","export class CannotDetermineTypeError extends Error {\n constructor(propertyKey: string) {\n super(\n `Cannot determine type at @Prop() \"${propertyKey}\". type: is required for all props since Orion v4`,\n )\n }\n}\n","import {Constructor, SchemaFieldType, SchemaNode} from '@orion-js/schema'\nimport {Model} from '@orion-js/models'\nimport {CannotDetermineTypeError} from '../errors/CannotDetermineType'\n\nexport interface PropOptions extends Omit<SchemaNode, 'type'> {\n type: SchemaFieldType | Constructor<any> | Model | Model[]\n}\n\n/**\n * @deprecated use schema with InferSchemaType<schema as const> instead\n */\nexport function Prop(options: PropOptions) {\n return (_target: any, context: ClassFieldDecoratorContext) => {\n const propertyKey = String(context.name)\n\n if (!options.type) {\n throw new CannotDetermineTypeError(propertyKey)\n }\n\n context.metadata[`_prop:${propertyKey}`] = options\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAmB;;;ACCnB,oBAAiC;;;ACAjC,2BAA0B;AAEnB,SAAS,oBAAoB,MAA2B;AAH/D;AAIE,MAAI,MAAM,QAAQ,IAAI,GAAG;AACvB,UAAM,WAAW,KAAK,CAAC;AACv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AM;AACjB,YAAM,IAAI,yBAAyB,WAAW;AAAA,IAChD;AAEA,YAAQ,SAAS,SAAS,WAAW,EAAE,IAAI;AAAA,EAC7C;AACF;","names":["isPlainObject"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,35 +1,38 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { ModelResolver, ModelResolverResolve } from '@orion-js/resolvers';
|
|
1
|
+
import { CreateModelOptions, Model, CloneOptions } from '@orion-js/models';
|
|
2
|
+
import { SchemaNode, SchemaFieldType, Constructor as Constructor$1, Schema } from '@orion-js/schema';
|
|
4
3
|
|
|
5
|
-
type
|
|
4
|
+
type TypedSchemaOptions = Partial<Omit<CreateModelOptions, 'schema'>>;
|
|
6
5
|
|
|
6
|
+
interface PropOptions extends Omit<SchemaNode, 'type'> {
|
|
7
|
+
type: SchemaFieldType | Constructor$1<any> | Model | Model[];
|
|
8
|
+
}
|
|
7
9
|
/**
|
|
8
|
-
* @deprecated
|
|
10
|
+
* @deprecated use schema with InferSchemaType<schema as const> instead
|
|
9
11
|
*/
|
|
10
|
-
declare function
|
|
11
|
-
|
|
12
|
-
declare function TypedSchema(options?: TypedModelOptions): ClassDecorator;
|
|
13
|
-
|
|
14
|
-
interface SchemaNodeForClasses extends Omit<SchemaNode, 'type'> {
|
|
15
|
-
type: SchemaMetaFieldType | Constructor$1<any> | Model | Model[];
|
|
16
|
-
}
|
|
17
|
-
type PropOptions = Partial<SchemaNodeForClasses>;
|
|
18
|
-
declare function Prop(options?: PropOptions): PropertyDecorator;
|
|
12
|
+
declare function Prop(options: PropOptions): (_target: any, context: ClassFieldDecoratorContext) => void;
|
|
19
13
|
|
|
20
14
|
/**
|
|
21
|
-
* @deprecated
|
|
15
|
+
* @deprecated use schema with InferSchemaType<schema as const> instead
|
|
22
16
|
*/
|
|
23
|
-
declare function
|
|
17
|
+
declare function TypedSchema(options?: TypedSchemaOptions): (_target: any, context: ClassDecoratorContext<any>) => void;
|
|
18
|
+
type SchemaFromTypedSchemaMetadata = {
|
|
19
|
+
_isTypedSchema: true;
|
|
20
|
+
_modelName: string;
|
|
21
|
+
_modelOptions: TypedSchemaOptions;
|
|
22
|
+
_getModel: () => Model;
|
|
23
|
+
[key: `_prop:${string}`]: PropOptions;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
declare function getSchemaForClass(target: any): Schema;
|
|
27
|
+
|
|
28
|
+
declare function resetModelCache(): void;
|
|
29
|
+
declare function getModelForClass(target: any): Model;
|
|
30
|
+
declare function internal_getModelForClassFromMetadata(metadata: SchemaFromTypedSchemaMetadata): Model<any>;
|
|
24
31
|
|
|
25
32
|
interface Constructor<T> extends Function {
|
|
26
33
|
new (...args: unknown[]): T;
|
|
27
34
|
}
|
|
28
35
|
|
|
29
|
-
declare function getSchemaForClass<TClass>(target: Constructor<TClass>): Schema;
|
|
30
|
-
|
|
31
|
-
declare function getModelForClass<TClass>(target: Constructor<TClass>): Model;
|
|
32
|
-
|
|
33
36
|
interface CloneSchemaClassOptions<TClass, TFields extends keyof TClass> {
|
|
34
37
|
name: string;
|
|
35
38
|
pickFields: readonly TFields[];
|
|
@@ -51,4 +54,4 @@ interface CloneSchemaClassOptions<TClass, TFields extends keyof TClass> {
|
|
|
51
54
|
*/
|
|
52
55
|
declare function cloneSchemaClass<TClass, TFields extends keyof TClass>(schema: Constructor<TClass>, options: CloneSchemaClassOptions<TClass, TFields>): Model<Pick<TClass, TFields>>;
|
|
53
56
|
|
|
54
|
-
export { type CloneSchemaClassOptions, Prop, type PropOptions,
|
|
57
|
+
export { type CloneSchemaClassOptions, Prop, type PropOptions, type SchemaFromTypedSchemaMetadata, TypedSchema, cloneSchemaClass, getModelForClass, getSchemaForClass, internal_getModelForClassFromMetadata, resetModelCache };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,35 +1,38 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { ModelResolver, ModelResolverResolve } from '@orion-js/resolvers';
|
|
1
|
+
import { CreateModelOptions, Model, CloneOptions } from '@orion-js/models';
|
|
2
|
+
import { SchemaNode, SchemaFieldType, Constructor as Constructor$1, Schema } from '@orion-js/schema';
|
|
4
3
|
|
|
5
|
-
type
|
|
4
|
+
type TypedSchemaOptions = Partial<Omit<CreateModelOptions, 'schema'>>;
|
|
6
5
|
|
|
6
|
+
interface PropOptions extends Omit<SchemaNode, 'type'> {
|
|
7
|
+
type: SchemaFieldType | Constructor$1<any> | Model | Model[];
|
|
8
|
+
}
|
|
7
9
|
/**
|
|
8
|
-
* @deprecated
|
|
10
|
+
* @deprecated use schema with InferSchemaType<schema as const> instead
|
|
9
11
|
*/
|
|
10
|
-
declare function
|
|
11
|
-
|
|
12
|
-
declare function TypedSchema(options?: TypedModelOptions): ClassDecorator;
|
|
13
|
-
|
|
14
|
-
interface SchemaNodeForClasses extends Omit<SchemaNode, 'type'> {
|
|
15
|
-
type: SchemaMetaFieldType | Constructor$1<any> | Model | Model[];
|
|
16
|
-
}
|
|
17
|
-
type PropOptions = Partial<SchemaNodeForClasses>;
|
|
18
|
-
declare function Prop(options?: PropOptions): PropertyDecorator;
|
|
12
|
+
declare function Prop(options: PropOptions): (_target: any, context: ClassFieldDecoratorContext) => void;
|
|
19
13
|
|
|
20
14
|
/**
|
|
21
|
-
* @deprecated
|
|
15
|
+
* @deprecated use schema with InferSchemaType<schema as const> instead
|
|
22
16
|
*/
|
|
23
|
-
declare function
|
|
17
|
+
declare function TypedSchema(options?: TypedSchemaOptions): (_target: any, context: ClassDecoratorContext<any>) => void;
|
|
18
|
+
type SchemaFromTypedSchemaMetadata = {
|
|
19
|
+
_isTypedSchema: true;
|
|
20
|
+
_modelName: string;
|
|
21
|
+
_modelOptions: TypedSchemaOptions;
|
|
22
|
+
_getModel: () => Model;
|
|
23
|
+
[key: `_prop:${string}`]: PropOptions;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
declare function getSchemaForClass(target: any): Schema;
|
|
27
|
+
|
|
28
|
+
declare function resetModelCache(): void;
|
|
29
|
+
declare function getModelForClass(target: any): Model;
|
|
30
|
+
declare function internal_getModelForClassFromMetadata(metadata: SchemaFromTypedSchemaMetadata): Model<any>;
|
|
24
31
|
|
|
25
32
|
interface Constructor<T> extends Function {
|
|
26
33
|
new (...args: unknown[]): T;
|
|
27
34
|
}
|
|
28
35
|
|
|
29
|
-
declare function getSchemaForClass<TClass>(target: Constructor<TClass>): Schema;
|
|
30
|
-
|
|
31
|
-
declare function getModelForClass<TClass>(target: Constructor<TClass>): Model;
|
|
32
|
-
|
|
33
36
|
interface CloneSchemaClassOptions<TClass, TFields extends keyof TClass> {
|
|
34
37
|
name: string;
|
|
35
38
|
pickFields: readonly TFields[];
|
|
@@ -51,4 +54,4 @@ interface CloneSchemaClassOptions<TClass, TFields extends keyof TClass> {
|
|
|
51
54
|
*/
|
|
52
55
|
declare function cloneSchemaClass<TClass, TFields extends keyof TClass>(schema: Constructor<TClass>, options: CloneSchemaClassOptions<TClass, TFields>): Model<Pick<TClass, TFields>>;
|
|
53
56
|
|
|
54
|
-
export { type CloneSchemaClassOptions, Prop, type PropOptions,
|
|
57
|
+
export { type CloneSchemaClassOptions, Prop, type PropOptions, type SchemaFromTypedSchemaMetadata, TypedSchema, cloneSchemaClass, getModelForClass, getSchemaForClass, internal_getModelForClassFromMetadata, resetModelCache };
|
package/dist/index.js
CHANGED
|
@@ -1,257 +1,90 @@
|
|
|
1
|
-
// src/errors/CannotDetermineType.ts
|
|
2
|
-
var CannotDetermineTypeError = class extends Error {
|
|
3
|
-
constructor(schemaName, propertyKey) {
|
|
4
|
-
super(
|
|
5
|
-
`Cannot determine type at "${schemaName}.${propertyKey}" field (object/union/ambiguous type was used). Make sure your property decorator defines a "type" option. For example: "@Prop({ type: {name: String, age: Number} })"`
|
|
6
|
-
);
|
|
7
|
-
}
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
// src/errors/CannotUseArray.ts
|
|
11
|
-
var CannotUseArrayError = class extends Error {
|
|
12
|
-
constructor(schemaName, propertyKey) {
|
|
13
|
-
super(
|
|
14
|
-
`Cannot infer type from an Array TypeScript type at "${schemaName}.${propertyKey}" field. Make sure your property decorator defines a "type" option. For example: "@Prop({ type: [String | Number | ...] })"`
|
|
15
|
-
);
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
// src/errors/PropertyAlreadyExists.ts
|
|
20
|
-
var PropertyAlreadyExistsError = class extends Error {
|
|
21
|
-
constructor(schemaName, propertyName) {
|
|
22
|
-
super(`Schema with name "${schemaName}" already contains property "${propertyName}".`);
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
// src/storage/metadataStorage.ts
|
|
27
|
-
import { generateId } from "@orion-js/helpers";
|
|
28
|
-
var MetadataStorageHandler = class {
|
|
29
|
-
schemas = /* @__PURE__ */ new Map();
|
|
30
|
-
getSchema(target) {
|
|
31
|
-
const schema = this.schemas.get(target.__schemaId);
|
|
32
|
-
if (schema) return schema;
|
|
33
|
-
const schemaId = generateId();
|
|
34
|
-
target.__schemaId = schemaId;
|
|
35
|
-
const newSchema = {
|
|
36
|
-
schema: target,
|
|
37
|
-
options: {},
|
|
38
|
-
properties: {},
|
|
39
|
-
resolvers: {}
|
|
40
|
-
};
|
|
41
|
-
this.schemas.set(target.__schemaId, newSchema);
|
|
42
|
-
return newSchema;
|
|
43
|
-
}
|
|
44
|
-
addSchemaMetadata({ target, options }) {
|
|
45
|
-
const schema = this.getSchema(target);
|
|
46
|
-
schema.options = options;
|
|
47
|
-
}
|
|
48
|
-
addPropMetadata({
|
|
49
|
-
target,
|
|
50
|
-
propertyKey,
|
|
51
|
-
options
|
|
52
|
-
}) {
|
|
53
|
-
const schema = this.getSchema(target);
|
|
54
|
-
const currProp = schema.properties[propertyKey];
|
|
55
|
-
if (currProp) {
|
|
56
|
-
throw new PropertyAlreadyExistsError(target.name, propertyKey);
|
|
57
|
-
}
|
|
58
|
-
schema.properties[propertyKey] = options;
|
|
59
|
-
}
|
|
60
|
-
addResolverMetadata({
|
|
61
|
-
target,
|
|
62
|
-
propertyKey,
|
|
63
|
-
options
|
|
64
|
-
}) {
|
|
65
|
-
const schema = this.getSchema(target);
|
|
66
|
-
const currResolver = schema.resolvers[propertyKey];
|
|
67
|
-
if (currResolver) {
|
|
68
|
-
throw new PropertyAlreadyExistsError(target.name, propertyKey);
|
|
69
|
-
}
|
|
70
|
-
schema.resolvers[propertyKey] = options;
|
|
71
|
-
}
|
|
72
|
-
getSchemaProps(target) {
|
|
73
|
-
const schema = this.getSchema(target);
|
|
74
|
-
return schema.properties;
|
|
75
|
-
}
|
|
76
|
-
getSchemaResolvers(target) {
|
|
77
|
-
const schema = this.getSchema(target);
|
|
78
|
-
return schema.resolvers;
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
var MetadataStorage = new MetadataStorageHandler();
|
|
82
|
-
|
|
83
|
-
// src/decorators/typedModel.ts
|
|
84
|
-
function TypedModel(options = {}) {
|
|
85
|
-
return (target) => {
|
|
86
|
-
MetadataStorage.addSchemaMetadata({ target, options });
|
|
87
|
-
target.getModel = () => getModelForClass(target);
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
|
|
91
1
|
// src/decorators/typedSchema.ts
|
|
92
|
-
|
|
93
|
-
return (target) => {
|
|
94
|
-
MetadataStorage.addSchemaMetadata({ target, options });
|
|
95
|
-
target.getModel = () => getModelForClass(target);
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// src/decorators/prop.ts
|
|
100
|
-
import "reflect-metadata";
|
|
101
|
-
|
|
102
|
-
// src/utils/isClass.ts
|
|
103
|
-
var isClass = (type) => /^class\s/.test(Function.prototype.toString.call(type));
|
|
104
|
-
|
|
105
|
-
// src/decorators/prop.ts
|
|
106
|
-
function Prop(options = {}) {
|
|
107
|
-
return (classDef, propertyKey) => {
|
|
108
|
-
var _a;
|
|
109
|
-
const schemaName = (_a = classDef.constructor) == null ? void 0 : _a.name;
|
|
110
|
-
if (!options.type) {
|
|
111
|
-
const type = Reflect.getMetadata("design:type", classDef, propertyKey);
|
|
112
|
-
if (isClass(type) || type === Object) {
|
|
113
|
-
throw new CannotDetermineTypeError(schemaName, propertyKey);
|
|
114
|
-
}
|
|
115
|
-
if (type === Array) {
|
|
116
|
-
throw new CannotUseArrayError(schemaName, propertyKey);
|
|
117
|
-
}
|
|
118
|
-
if (type) {
|
|
119
|
-
options.type = type;
|
|
120
|
-
} else {
|
|
121
|
-
throw new CannotDetermineTypeError(schemaName, propertyKey);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
MetadataStorage.addPropMetadata({ target: classDef.constructor, propertyKey, options });
|
|
125
|
-
classDef[propertyKey] = options;
|
|
126
|
-
};
|
|
127
|
-
}
|
|
2
|
+
import { omit } from "lodash";
|
|
128
3
|
|
|
129
|
-
// src/
|
|
130
|
-
|
|
131
|
-
return (classDef, propertyKey) => {
|
|
132
|
-
MetadataStorage.addResolverMetadata({ target: classDef.constructor, propertyKey, options });
|
|
133
|
-
classDef[propertyKey] = options;
|
|
134
|
-
};
|
|
135
|
-
}
|
|
4
|
+
// src/factories/getModelForClass.ts
|
|
5
|
+
import { createModel } from "@orion-js/models";
|
|
136
6
|
|
|
137
|
-
// src/factories/
|
|
7
|
+
// src/factories/processTypeForProp.ts
|
|
138
8
|
import isPlainObject from "lodash/isPlainObject";
|
|
139
|
-
function
|
|
140
|
-
return [Boolean, Number, String, Date].includes(type);
|
|
141
|
-
}
|
|
142
|
-
function processSchemaForProp(prop) {
|
|
9
|
+
function getParamTypeForProp(type) {
|
|
143
10
|
var _a;
|
|
144
|
-
if (
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
if (Array.isArray(prop.type)) {
|
|
148
|
-
return prop.type.length > 0 ? { ...prop, type: [processSchemaForProp(prop.type[0])] } : prop;
|
|
149
|
-
}
|
|
150
|
-
if (typeof prop.type !== "function") {
|
|
151
|
-
if (isPlainObject(prop.type)) {
|
|
152
|
-
const subschema = {};
|
|
153
|
-
Object.keys(prop.type).forEach((key) => {
|
|
154
|
-
subschema[key] = processSchemaForProp({ type: prop.type[key] });
|
|
155
|
-
});
|
|
156
|
-
return { ...prop, type: subschema };
|
|
157
|
-
}
|
|
158
|
-
return prop;
|
|
11
|
+
if (Array.isArray(type)) {
|
|
12
|
+
const itemType = type[0];
|
|
13
|
+
return [getParamTypeForProp(itemType)];
|
|
159
14
|
}
|
|
160
|
-
if (
|
|
161
|
-
return
|
|
15
|
+
if ((_a = type == null ? void 0 : type[Symbol.metadata]) == null ? void 0 : _a._getModel) {
|
|
16
|
+
return type[Symbol.metadata]._getModel(type);
|
|
162
17
|
}
|
|
163
|
-
if (
|
|
164
|
-
|
|
165
|
-
return {
|
|
166
|
-
...prop,
|
|
167
|
-
type: schema
|
|
168
|
-
};
|
|
18
|
+
if (type == null ? void 0 : type.getSchema) {
|
|
19
|
+
return getParamTypeForProp(type.getSchema());
|
|
169
20
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
const schema = {};
|
|
174
|
-
let parent = target;
|
|
175
|
-
while (parent.prototype) {
|
|
176
|
-
if (parent === Function.prototype) {
|
|
177
|
-
break;
|
|
178
|
-
}
|
|
179
|
-
const props = MetadataStorage.getSchemaProps(parent);
|
|
180
|
-
if (!props) {
|
|
181
|
-
parent = Object.getPrototypeOf(parent);
|
|
182
|
-
continue;
|
|
21
|
+
if (isPlainObject(type)) {
|
|
22
|
+
if (type.__isFieldType) {
|
|
23
|
+
return type;
|
|
183
24
|
}
|
|
184
|
-
|
|
185
|
-
|
|
25
|
+
const subschema = {};
|
|
26
|
+
Object.keys(type).forEach((key) => {
|
|
27
|
+
if (key.startsWith("__")) {
|
|
28
|
+
subschema[key] = type[key];
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
subschema[key] = {
|
|
32
|
+
...type[key],
|
|
33
|
+
type: getParamTypeForProp(type[key].type)
|
|
34
|
+
};
|
|
186
35
|
});
|
|
187
|
-
|
|
36
|
+
return subschema;
|
|
188
37
|
}
|
|
189
|
-
return
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// src/factories/getSchemaForClass.ts
|
|
193
|
-
function getSchemaForClass(target) {
|
|
194
|
-
return getSchemaForClassRecursive(target);
|
|
38
|
+
return type;
|
|
195
39
|
}
|
|
196
40
|
|
|
197
41
|
// src/factories/getModelForClass.ts
|
|
198
|
-
|
|
42
|
+
Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
|
|
199
43
|
var modelCache = /* @__PURE__ */ new Map();
|
|
200
|
-
function
|
|
201
|
-
|
|
202
|
-
if (((_a = prop.type) == null ? void 0 : _a.__isModel) === true) {
|
|
203
|
-
return prop;
|
|
204
|
-
}
|
|
205
|
-
if (((_b = prop.type) == null ? void 0 : _b._isFieldType) === true) {
|
|
206
|
-
return prop;
|
|
207
|
-
}
|
|
208
|
-
return processSchemaForProp(prop);
|
|
44
|
+
function resetModelCache() {
|
|
45
|
+
modelCache.clear();
|
|
209
46
|
}
|
|
210
47
|
function getModelForClass(target) {
|
|
211
48
|
const targetAsModel = target;
|
|
212
49
|
if (targetAsModel.__isModel) {
|
|
213
50
|
return targetAsModel;
|
|
214
51
|
}
|
|
215
|
-
|
|
216
|
-
if (
|
|
217
|
-
|
|
218
|
-
target = target.prototype.typedModel;
|
|
52
|
+
const metadata = target[Symbol.metadata];
|
|
53
|
+
if (!metadata) {
|
|
54
|
+
return targetAsModel;
|
|
219
55
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
56
|
+
return internal_getModelForClassFromMetadata(metadata);
|
|
57
|
+
}
|
|
58
|
+
function internal_getModelForClassFromMetadata(metadata) {
|
|
59
|
+
const modelName = metadata._modelName;
|
|
60
|
+
if (modelCache.has(modelName)) {
|
|
61
|
+
return modelCache.get(modelName);
|
|
223
62
|
}
|
|
224
63
|
const schema = {};
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
});
|
|
235
|
-
const resolvers = MetadataStorage.getSchemaResolvers(parent) ?? {};
|
|
236
|
-
Object.keys(resolvers).forEach((key) => {
|
|
237
|
-
resolverMap[key] = resolvers[key];
|
|
238
|
-
});
|
|
239
|
-
parent = Object.getPrototypeOf(parent);
|
|
64
|
+
const keys = Object.keys(metadata ?? {});
|
|
65
|
+
const injectionKeys = keys.filter((key) => key.startsWith("_prop:"));
|
|
66
|
+
for (const key of injectionKeys) {
|
|
67
|
+
const prop = metadata[key];
|
|
68
|
+
const schemaProp = key.replace("_prop:", "");
|
|
69
|
+
schema[schemaProp] = {
|
|
70
|
+
...prop,
|
|
71
|
+
type: getParamTypeForProp(prop.type)
|
|
72
|
+
};
|
|
240
73
|
}
|
|
241
74
|
const model = createModel({
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
validate: targetAsModel.validate,
|
|
246
|
-
resolvers: {
|
|
247
|
-
...resolverMap,
|
|
248
|
-
...modelResolvers
|
|
249
|
-
}
|
|
75
|
+
...metadata._modelOptions,
|
|
76
|
+
name: modelName,
|
|
77
|
+
schema
|
|
250
78
|
});
|
|
251
|
-
modelCache.set(
|
|
79
|
+
modelCache.set(modelName, model);
|
|
252
80
|
return model;
|
|
253
81
|
}
|
|
254
82
|
|
|
83
|
+
// src/factories/getSchemaForClass.ts
|
|
84
|
+
function getSchemaForClass(target) {
|
|
85
|
+
return getModelForClass(target).getSchema();
|
|
86
|
+
}
|
|
87
|
+
|
|
255
88
|
// src/factories/cloneSchemaClass.ts
|
|
256
89
|
function cloneSchemaClass(schema, options) {
|
|
257
90
|
const model = getModelForClass(schema);
|
|
@@ -263,13 +96,47 @@ function cloneSchemaClass(schema, options) {
|
|
|
263
96
|
});
|
|
264
97
|
return newModel;
|
|
265
98
|
}
|
|
99
|
+
|
|
100
|
+
// src/decorators/typedSchema.ts
|
|
101
|
+
function TypedSchema(options = {}) {
|
|
102
|
+
return (_target, context) => {
|
|
103
|
+
context.metadata._isTypedSchema = true;
|
|
104
|
+
context.metadata._modelName = options.name || context.name;
|
|
105
|
+
context.metadata._modelOptions = omit(options, "name");
|
|
106
|
+
context.metadata._getModel = () => {
|
|
107
|
+
return internal_getModelForClassFromMetadata(
|
|
108
|
+
context.metadata
|
|
109
|
+
);
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// src/errors/CannotDetermineType.ts
|
|
115
|
+
var CannotDetermineTypeError = class extends Error {
|
|
116
|
+
constructor(propertyKey) {
|
|
117
|
+
super(
|
|
118
|
+
`Cannot determine type at @Prop() "${propertyKey}". type: is required for all props since Orion v4`
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// src/decorators/prop.ts
|
|
124
|
+
function Prop(options) {
|
|
125
|
+
return (_target, context) => {
|
|
126
|
+
const propertyKey = String(context.name);
|
|
127
|
+
if (!options.type) {
|
|
128
|
+
throw new CannotDetermineTypeError(propertyKey);
|
|
129
|
+
}
|
|
130
|
+
context.metadata[`_prop:${propertyKey}`] = options;
|
|
131
|
+
};
|
|
132
|
+
}
|
|
266
133
|
export {
|
|
267
134
|
Prop,
|
|
268
|
-
ResolverProp,
|
|
269
|
-
TypedModel,
|
|
270
135
|
TypedSchema,
|
|
271
136
|
cloneSchemaClass,
|
|
272
137
|
getModelForClass,
|
|
273
|
-
getSchemaForClass
|
|
138
|
+
getSchemaForClass,
|
|
139
|
+
internal_getModelForClassFromMetadata,
|
|
140
|
+
resetModelCache
|
|
274
141
|
};
|
|
275
142
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/errors/CannotDetermineType.ts","../src/errors/CannotUseArray.ts","../src/errors/PropertyAlreadyExists.ts","../src/storage/metadataStorage.ts","../src/decorators/typedModel.ts","../src/decorators/typedSchema.ts","../src/decorators/prop.ts","../src/utils/isClass.ts","../src/decorators/resolver.ts","../src/factories/helpers/processSchemaForProp.ts","../src/factories/getSchemaForClass.ts","../src/factories/getModelForClass.ts","../src/factories/cloneSchemaClass.ts"],"sourcesContent":["export class CannotDetermineTypeError extends Error {\n constructor(schemaName: string, propertyKey: string) {\n super(\n `Cannot determine type at \"${schemaName}.${propertyKey}\" field (object/union/ambiguous type was used). Make sure your property decorator defines a \"type\" option. For example: \"@Prop({ type: {name: String, age: Number} })\"`\n )\n }\n}\n","export class CannotUseArrayError extends Error {\n constructor(schemaName: string, propertyKey: string) {\n super(\n `Cannot infer type from an Array TypeScript type at \"${schemaName}.${propertyKey}\" field. Make sure your property decorator defines a \"type\" option. For example: \"@Prop({ type: [String | Number | ...] })\"`\n )\n }\n}\n","export class PropertyAlreadyExistsError extends Error {\n constructor(schemaName: string, propertyName: string) {\n super(`Schema with name \"${schemaName}\" already contains property \"${propertyName}\".`)\n }\n}\n","import {PropOptions} from '..'\nimport {PropertyAlreadyExistsError} from '../errors'\nimport {ModelResolversMap} from '@orion-js/models'\nimport {ModelResolver, ModelResolverResolve} from '@orion-js/resolvers'\nimport {generateId} from '@orion-js/helpers'\n\nexport type PropertiesMap = {[key: string]: PropOptions}\n\ninterface SchemaStorage {\n schema: any\n options: object\n properties: PropertiesMap\n resolvers: ModelResolversMap\n}\n\nexport type TypedModelOptions = Record<string, never>\n\nexport class MetadataStorageHandler {\n private schemas = new Map<any, SchemaStorage>()\n\n private getSchema(target) {\n const schema = this.schemas.get(target.__schemaId)\n if (schema) return schema\n\n const schemaId = generateId()\n\n target.__schemaId = schemaId\n\n const newSchema = {\n schema: target,\n options: {},\n properties: {},\n resolvers: {}\n }\n this.schemas.set(target.__schemaId, newSchema)\n return newSchema\n }\n\n public addSchemaMetadata({target, options}: {target: any; options?: TypedModelOptions}) {\n const schema = this.getSchema(target)\n schema.options = options\n }\n\n public addPropMetadata({\n target,\n propertyKey,\n options\n }: {\n target: any\n propertyKey: string\n options: PropOptions\n }) {\n const schema = this.getSchema(target)\n\n const currProp = schema.properties[propertyKey]\n if (currProp) {\n throw new PropertyAlreadyExistsError(target.name, propertyKey)\n }\n schema.properties[propertyKey] = options\n }\n\n public addResolverMetadata({\n target,\n propertyKey,\n options\n }: {\n target: any\n propertyKey: string\n options: ModelResolver<ModelResolverResolve>\n }) {\n const schema = this.getSchema(target)\n\n const currResolver = schema.resolvers[propertyKey]\n if (currResolver) {\n throw new PropertyAlreadyExistsError(target.name, propertyKey)\n }\n schema.resolvers[propertyKey] = options\n }\n\n public getSchemaProps(target: any): PropertiesMap | undefined {\n const schema = this.getSchema(target)\n\n return schema.properties\n }\n\n public getSchemaResolvers(target: any): ModelResolversMap | undefined {\n const schema = this.getSchema(target)\n\n return schema.resolvers\n }\n}\n\nexport const MetadataStorage = new MetadataStorageHandler()\n","import {getModelForClass} from '..'\nimport {MetadataStorage, TypedModelOptions} from '../storage/metadataStorage'\n\n/**\n * @deprecated Please use @TypedSchema instead\n */\nexport function TypedModel(options: TypedModelOptions = {}): ClassDecorator {\n return target => {\n MetadataStorage.addSchemaMetadata({target, options})\n\n // @ts-expect-error this is a trick to make it work in resolvers without having to call getModelForClass\n target.getModel = () => getModelForClass(target)\n }\n}\n","import {getModelForClass} from '..'\nimport {MetadataStorage, TypedModelOptions} from '../storage/metadataStorage'\n\nexport function TypedSchema(options: TypedModelOptions = {}): ClassDecorator {\n return target => {\n MetadataStorage.addSchemaMetadata({target, options})\n\n // @ts-expect-error this is a trick to make it work in resolvers without having to call getModelForClass\n target.getModel = () => getModelForClass(target)\n }\n}\n","/* eslint-disable @typescript-eslint/ban-types */\nimport {Constructor, SchemaMetaFieldType, SchemaNode} from '@orion-js/schema'\nimport {MetadataStorage} from '../storage/metadataStorage'\nimport 'reflect-metadata'\nimport {CannotDetermineTypeError, CannotUseArrayError} from '../errors'\nimport {isClass} from '../utils/isClass'\nimport {Model} from '@orion-js/models'\n\nexport interface SchemaNodeForClasses extends Omit<SchemaNode, 'type'> {\n type: SchemaMetaFieldType | Constructor<any> | Model | Model[]\n}\n\nexport type PropOptions = Partial<SchemaNodeForClasses>\n\nexport function Prop(options: PropOptions = {}): PropertyDecorator {\n return (classDef: Function, propertyKey: string) => {\n const schemaName = classDef.constructor?.name\n\n if (!options.type) {\n const type = Reflect.getMetadata('design:type', classDef, propertyKey)\n\n if (isClass(type) || type === Object) {\n throw new CannotDetermineTypeError(schemaName, propertyKey)\n }\n\n if (type === Array) {\n throw new CannotUseArrayError(schemaName, propertyKey)\n }\n\n if (type) {\n options.type = type\n } else {\n throw new CannotDetermineTypeError(schemaName, propertyKey)\n }\n }\n\n MetadataStorage.addPropMetadata({target: classDef.constructor, propertyKey, options})\n\n classDef[propertyKey] = options\n }\n}\n","// eslint-disable-next-line @typescript-eslint/ban-types\nexport const isClass = (type: Function) => /^class\\s/.test(Function.prototype.toString.call(type))\n","import {ModelResolver, ModelResolverResolve} from '@orion-js/resolvers'\nimport {MetadataStorage} from '../storage/metadataStorage'\n\n/**\n * @deprecated Please use a @TypedSchema and a @Model a @ModelResolver instead\n */\nexport function ResolverProp(options: ModelResolver<ModelResolverResolve>): PropertyDecorator {\n return (classDef: any, propertyKey: string) => {\n MetadataStorage.addResolverMetadata({target: classDef.constructor, propertyKey, options})\n\n classDef[propertyKey] = options\n }\n}\n","/* eslint-disable @typescript-eslint/ban-types */\nimport isPlainObject from 'lodash/isPlainObject'\nimport {PropOptions} from '../../decorators/prop'\nimport {Schema, SchemaRecursiveNodeType} from '@orion-js/schema'\nimport {MetadataStorage} from '../../storage/metadataStorage'\nimport {isClass} from '../../utils/isClass'\n\nfunction isPrimitive(type: Function) {\n return ([Boolean, Number, String, Date] as Function[]).includes(type)\n}\n\nexport function processSchemaForProp(prop: PropOptions) {\n if (typeof (prop.type as SchemaRecursiveNodeType)?.type === 'function') {\n return processSchemaForProp(prop.type as PropOptions)\n }\n\n if (Array.isArray(prop.type)) {\n return prop.type.length > 0\n ? {...prop, type: [processSchemaForProp(prop.type[0] as PropOptions)]}\n : prop\n }\n\n if (typeof prop.type !== 'function') {\n if (isPlainObject(prop.type)) {\n const subschema = {}\n Object.keys(prop.type).forEach(key => {\n subschema[key] = processSchemaForProp({type: prop.type[key]})\n })\n return {...prop, type: subschema}\n }\n\n return prop\n }\n\n if (isPrimitive(prop.type)) {\n return prop\n }\n\n if (isClass(prop.type)) {\n const schema = getSchemaForClassRecursive(prop.type)\n return {\n ...prop,\n type: schema\n }\n }\n\n return prop\n}\n\nexport function getSchemaForClassRecursive(target): Schema {\n const schema: Schema = {}\n\n let parent: Function = target\n\n while (parent.prototype) {\n if (parent === Function.prototype) {\n break\n }\n\n const props = MetadataStorage.getSchemaProps(parent)\n if (!props) {\n parent = Object.getPrototypeOf(parent)\n continue\n }\n\n Object.keys(props).forEach(key => {\n schema[key] = processSchemaForProp(props[key])\n })\n\n parent = Object.getPrototypeOf(parent)\n }\n\n return schema\n}\n","/* eslint-disable @typescript-eslint/ban-types */\nimport {Schema} from '@orion-js/schema'\nimport {Constructor} from '../utils/interfaces'\nimport {getSchemaForClassRecursive} from './helpers/processSchemaForProp'\n\nexport function getSchemaForClass<TClass>(target: Constructor<TClass>): Schema {\n return getSchemaForClassRecursive(target)\n}\n","/* eslint-disable @typescript-eslint/ban-types */\nimport {createModel, Model, ModelSchema, ModelResolversMap} from '@orion-js/models'\nimport {FieldType} from '@orion-js/schema'\nimport {PropOptions} from '..'\nimport {MetadataStorage} from '../storage/metadataStorage'\nimport {Constructor} from '../utils/interfaces'\nimport {processSchemaForProp} from './helpers/processSchemaForProp'\n\nconst modelCache = new Map<Constructor<any>, Model>()\n\nfunction processModelSchemaForProp(prop: PropOptions) {\n if ((prop.type as Model)?.__isModel === true) {\n return prop\n }\n\n if ((prop.type as FieldType)?._isFieldType === true) {\n return prop\n }\n\n return processSchemaForProp(prop)\n}\n\nexport function getModelForClass<TClass>(target: Constructor<TClass>): Model {\n const targetAsModel = target as any as Model\n if (targetAsModel.__isModel) {\n return targetAsModel\n }\n\n let modelResolvers = null\n\n if (target.prototype.typedModel) {\n modelResolvers = target.prototype.resolvers || {}\n target = target.prototype.typedModel\n }\n\n const schemaId = (target as any).__schemaId\n\n if (modelCache.has(schemaId)) {\n return modelCache.get(schemaId)\n }\n\n const schema: ModelSchema = {}\n const resolverMap: ModelResolversMap = {}\n\n let parent: Function = target\n\n while (parent.prototype) {\n if (parent === Function.prototype) {\n break\n }\n\n const props = MetadataStorage.getSchemaProps(parent) ?? {}\n\n Object.keys(props).forEach(key => {\n schema[key] = processModelSchemaForProp(props[key])\n })\n\n const resolvers = MetadataStorage.getSchemaResolvers(parent) ?? {}\n Object.keys(resolvers).forEach(key => {\n resolverMap[key] = resolvers[key]\n })\n\n parent = Object.getPrototypeOf(parent)\n }\n\n const model = createModel({\n name: targetAsModel.name,\n schema,\n clean: targetAsModel.clean,\n validate: targetAsModel.validate,\n resolvers: {\n ...resolverMap,\n ...modelResolvers\n }\n })\n\n modelCache.set(schemaId, model)\n\n return model\n}\n","import {Model} from '@orion-js/models'\nimport {CloneOptions} from '@orion-js/models'\nimport {Constructor} from '../utils/interfaces'\nimport {getModelForClass} from './getModelForClass'\n\nexport interface CloneSchemaClassOptions<TClass, TFields extends keyof TClass> {\n name: string\n pickFields: readonly TFields[]\n mapFields?: CloneOptions['mapFields']\n extendSchema?: CloneOptions['extendSchema']\n}\n\n/**\n * This function returns a cloned model but the type is a subset of the original Schema.\n * To use the type of the cloned schema use `typeof ClonedModel.type`\n *\n * Example:\n * ```ts\n * const ClonedModel = cloneSchemaClass(Schema, {\n * name: 'ClonedSchema',\n * pickFields: ['name'] as const\n * })\n * type ClonedType = typeof ClonedModel.type\n * ```\n */\nexport function cloneSchemaClass<TClass, TFields extends keyof TClass>(\n schema: Constructor<TClass>,\n options: CloneSchemaClassOptions<TClass, TFields>\n): Model<Pick<TClass, TFields>> {\n const model = getModelForClass(schema)\n\n const newModel: Model<Pick<TClass, TFields>> = model.clone({\n name: options.name,\n pickFields: options.pickFields as any as string[],\n mapFields: options.mapFields,\n extendSchema: options.extendSchema\n })\n\n return newModel\n}\n"],"mappings":";AAAO,IAAM,2BAAN,cAAuC,MAAM;AAAA,EAClD,YAAY,YAAoB,aAAqB;AACnD;AAAA,MACE,6BAA6B,UAAU,IAAI,WAAW;AAAA,IACxD;AAAA,EACF;AACF;;;ACNO,IAAM,sBAAN,cAAkC,MAAM;AAAA,EAC7C,YAAY,YAAoB,aAAqB;AACnD;AAAA,MACE,uDAAuD,UAAU,IAAI,WAAW;AAAA,IAClF;AAAA,EACF;AACF;;;ACNO,IAAM,6BAAN,cAAyC,MAAM;AAAA,EACpD,YAAY,YAAoB,cAAsB;AACpD,UAAM,qBAAqB,UAAU,gCAAgC,YAAY,IAAI;AAAA,EACvF;AACF;;;ACAA,SAAQ,kBAAiB;AAalB,IAAM,yBAAN,MAA6B;AAAA,EAC1B,UAAU,oBAAI,IAAwB;AAAA,EAEtC,UAAU,QAAQ;AACxB,UAAM,SAAS,KAAK,QAAQ,IAAI,OAAO,UAAU;AACjD,QAAI,OAAQ,QAAO;AAEnB,UAAM,WAAW,WAAW;AAE5B,WAAO,aAAa;AAEpB,UAAM,YAAY;AAAA,MAChB,QAAQ;AAAA,MACR,SAAS,CAAC;AAAA,MACV,YAAY,CAAC;AAAA,MACb,WAAW,CAAC;AAAA,IACd;AACA,SAAK,QAAQ,IAAI,OAAO,YAAY,SAAS;AAC7C,WAAO;AAAA,EACT;AAAA,EAEO,kBAAkB,EAAC,QAAQ,QAAO,GAA+C;AACtF,UAAM,SAAS,KAAK,UAAU,MAAM;AACpC,WAAO,UAAU;AAAA,EACnB;AAAA,EAEO,gBAAgB;AAAA,IACrB;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAIG;AACD,UAAM,SAAS,KAAK,UAAU,MAAM;AAEpC,UAAM,WAAW,OAAO,WAAW,WAAW;AAC9C,QAAI,UAAU;AACZ,YAAM,IAAI,2BAA2B,OAAO,MAAM,WAAW;A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WAAY,OAAe;AAEjC,MAAI,WAAW,IAAI,QAAQ,GAAG;AAC5B,WAAO,WAAW,IAAI,QAAQ;AAAA,EAChC;AAEA,QAAM,SAAsB,CAAC;AAC7B,QAAM,cAAiC,CAAC;AAExC,MAAI,SAAmB;AAEvB,SAAO,OAAO,WAAW;AACvB,QAAI,WAAW,SAAS,WAAW;AACjC;AAAA,IACF;AAEA,UAAM,QAAQ,gBAAgB,eAAe,MAAM,KAAK,CAAC;AAEzD,WAAO,KAAK,KAAK,EAAE,QAAQ,SAAO;AAChC,aAAO,GAAG,IAAI,0BAA0B,MAAM,GAAG,CAAC;AAAA,IACpD,CAAC;AAED,UAAM,YAAY,gBAAgB,mBAAmB,MAAM,KAAK,CAAC;AACjE,WAAO,KAAK,SAAS,EAAE,QAAQ,SAAO;AACpC,kBAAY,GAAG,IAAI,UAAU,GAAG;AAAA,IAClC,CAAC;AAED,aAAS,OAAO,eAAe,MAAM;AAAA,EACvC;AAEA,QAAM,QAAQ,YAAY;AAAA,IACxB,MAAM,cAAc;AAAA,IACpB;AAAA,IACA,OAAO,cAAc;AAAA,IACrB,UAAU,cAAc;AAAA,IACxB,WAAW;AAAA,MACT,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,EACF,CAAC;AAED,aAAW,IAAI,UAAU,KAAK;AAE9B,SAAO;AACT;;;ACtDO,SAAS,iBACd,QACA,SAC8B;AAC9B,QAAM,QAAQ,iBAAiB,MAAM;AAErC,QAAM,WAAyC,MAAM,MAAM;AAAA,IACzD,MAAM,QAAQ;AAAA,IACd,YAAY,QAAQ;AAAA,IACpB,WAAW,QAAQ;AAAA,IACnB,cAAc,QAAQ;AAAA,EACxB,CAAC;AAED,SAAO;AACT;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/decorators/typedSchema.ts","../src/factories/getModelForClass.ts","../src/factories/processTypeForProp.ts","../src/factories/getSchemaForClass.ts","../src/factories/cloneSchemaClass.ts","../src/errors/CannotDetermineType.ts","../src/decorators/prop.ts"],"sourcesContent":["import {omit} from 'lodash'\nimport {internal_getModelForClassFromMetadata} from '../factories'\nimport {TypedSchemaOptions} from '../storage/metadataStorage'\nimport {Model} from '@orion-js/models'\nimport {PropOptions} from './prop'\n\n/**\n * @deprecated use schema with InferSchemaType<schema as const> instead\n */\nexport function TypedSchema(options: TypedSchemaOptions = {}) {\n return (_target: any, context: ClassDecoratorContext<any>) => {\n context.metadata._isTypedSchema = true\n context.metadata._modelName = options.name || context.name\n context.metadata._modelOptions = omit(options, 'name')\n context.metadata._getModel = () => {\n return internal_getModelForClassFromMetadata(\n context.metadata as SchemaFromTypedSchemaMetadata,\n )\n }\n }\n}\n\nexport type SchemaFromTypedSchemaMetadata = {\n _isTypedSchema: true\n _modelName: string\n _modelOptions: TypedSchemaOptions\n _getModel: () => Model\n [key: `_prop:${string}`]: PropOptions\n}\n","/* eslint-disable @typescript-eslint/ban-types */\nimport {createModel, Model} from '@orion-js/models'\nimport {SchemaFromTypedSchemaMetadata} from '..'\nimport {getParamTypeForProp} from './processTypeForProp'\nimport {Schema} from '@orion-js/schema'\n\n// @ts-ignore polyfill for Symbol.metadata // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#decorator-metadata\nSymbol.metadata ??= Symbol('Symbol.metadata')\n\nconst modelCache = new Map<string, Model>()\n\nexport function resetModelCache() {\n modelCache.clear()\n}\n\nexport function getModelForClass(target: any): Model {\n const targetAsModel = target as any as Model\n if (targetAsModel.__isModel) {\n return targetAsModel\n }\n\n const metadata = target[Symbol.metadata] as SchemaFromTypedSchemaMetadata\n\n if (!metadata) {\n return targetAsModel\n }\n\n return internal_getModelForClassFromMetadata(metadata)\n}\n\nexport function internal_getModelForClassFromMetadata(metadata: SchemaFromTypedSchemaMetadata) {\n const modelName = metadata._modelName\n if (modelCache.has(modelName)) {\n return modelCache.get(modelName)\n }\n\n const schema: Schema = {}\n const keys = Object.keys(metadata ?? {})\n const injectionKeys = keys.filter(key => key.startsWith('_prop:'))\n\n for (const key of injectionKeys) {\n const prop = metadata[key] as Schema\n const schemaProp = key.replace('_prop:', '')\n\n schema[schemaProp] = {\n ...prop,\n type: getParamTypeForProp(prop.type as any),\n }\n }\n\n const model = createModel({\n ...metadata._modelOptions,\n name: modelName,\n schema,\n })\n\n modelCache.set(modelName, model)\n\n return model\n}\n","import {PropOptions} from '../decorators/prop'\nimport isPlainObject from 'lodash/isPlainObject'\n\nexport function getParamTypeForProp(type: PropOptions['type']) {\n if (Array.isArray(type)) {\n const itemType = type[0]\n return [getParamTypeForProp(itemType)]\n }\n\n if (type?.[Symbol.metadata]?._getModel) {\n return type[Symbol.metadata]._getModel(type)\n }\n\n if (type?.getSchema) {\n return getParamTypeForProp(type.getSchema())\n }\n\n if (isPlainObject(type)) {\n if (type.__isFieldType) {\n return type\n }\n\n const subschema = {}\n Object.keys(type).forEach(key => {\n if (key.startsWith('__')) {\n subschema[key] = type[key]\n return\n }\n\n subschema[key] = {\n ...type[key],\n type: getParamTypeForProp(type[key].type),\n }\n })\n\n return subschema\n }\n\n return type\n}\n","/* eslint-disable @typescript-eslint/ban-types */\nimport {Schema} from '@orion-js/schema'\nimport {getModelForClass} from './getModelForClass'\n\nexport function getSchemaForClass(target: any): Schema {\n return getModelForClass(target).getSchema()\n}\n","import {Model} from '@orion-js/models'\nimport {CloneOptions} from '@orion-js/models'\nimport {getModelForClass} from './getModelForClass'\nimport {Constructor} from '../utils/interfaces'\n\nexport interface CloneSchemaClassOptions<TClass, TFields extends keyof TClass> {\n name: string\n pickFields: readonly TFields[]\n mapFields?: CloneOptions['mapFields']\n extendSchema?: CloneOptions['extendSchema']\n}\n\n/**\n * This function returns a cloned model but the type is a subset of the original Schema.\n * To use the type of the cloned schema use `typeof ClonedModel.type`\n *\n * Example:\n * ```ts\n * const ClonedModel = cloneSchemaClass(Schema, {\n * name: 'ClonedSchema',\n * pickFields: ['name'] as const\n * })\n * type ClonedType = typeof ClonedModel.type\n * ```\n */\nexport function cloneSchemaClass<TClass, TFields extends keyof TClass>(\n schema: Constructor<TClass>,\n options: CloneSchemaClassOptions<TClass, TFields>,\n): Model<Pick<TClass, TFields>> {\n const model = getModelForClass(schema)\n\n const newModel: Model<Pick<TClass, TFields>> = model.clone({\n name: options.name,\n pickFields: options.pickFields as any as string[],\n mapFields: options.mapFields,\n extendSchema: options.extendSchema,\n })\n\n return newModel\n}\n","export class CannotDetermineTypeError extends Error {\n constructor(propertyKey: string) {\n super(\n `Cannot determine type at @Prop() \"${propertyKey}\". type: is required for all props since Orion v4`,\n )\n }\n}\n","import {Constructor, SchemaFieldType, SchemaNode} from '@orion-js/schema'\nimport {Model} from '@orion-js/models'\nimport {CannotDetermineTypeError} from '../errors/CannotDetermineType'\n\nexport interface PropOptions extends Omit<SchemaNode, 'type'> {\n type: SchemaFieldType | Constructor<any> | Model | Model[]\n}\n\n/**\n * @deprecated use schema with InferSchemaType<schema as const> instead\n */\nexport function Prop(options: PropOptions) {\n return (_target: any, context: ClassFieldDecoratorContext) => {\n const propertyKey = String(context.name)\n\n if (!options.type) {\n throw new CannotDetermineTypeError(propertyKey)\n }\n\n context.metadata[`_prop:${propertyKey}`] = options\n }\n}\n"],"mappings":";AAAA,SAAQ,YAAW;;;ACCnB,SAAQ,mBAAyB;;;ACAjC,OAAO,mBAAmB;AAEnB,SAAS,oBAAoB,MAA2B;AAH/D;AAIE,MAAI,MAAM,QAAQ,IAAI,GAAG;AACvB,UAAM,WAAW,KAAK,CAAC;AACvB,WAAO,CAAC,oBAAoB,QAAQ,CAAC;AAAA,EACvC;AAEA,OAAI,kCAAO,OAAO,cAAd,mBAAyB,WAAW;AACtC,WAAO,KAAK,OAAO,QAAQ,EAAE,UAAU,IAAI;AAAA,EAC7C;AAEA,MAAI,6BAAM,WAAW;AACnB,WAAO,oBAAoB,KAAK,UAAU,CAAC;AAAA,EAC7C;AAEA,MAAI,cAAc,IAAI,GAAG;AACvB,QAAI,KAAK,eAAe;AACtB,aAAO;AAAA,IACT;AAEA,UAAM,YAAY,CAA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IAAI,WAAW,KAAK;AAE/B,SAAO;AACT;;;AEvDO,SAAS,kBAAkB,QAAqB;AACrD,SAAO,iBAAiB,MAAM,EAAE,UAAU;AAC5C;;;ACmBO,SAAS,iBACd,QACA,SAC8B;AAC9B,QAAM,QAAQ,iBAAiB,MAAM;AAErC,QAAM,WAAyC,MAAM,MAAM;AAAA,IACzD,MAAM,QAAQ;AAAA,IACd,YAAY,QAAQ;AAAA,IACpB,WAAW,QAAQ;AAAA,IACnB,cAAc,QAAQ;AAAA,EACxB,CAAC;AAED,SAAO;AACT;;;AJ9BO,SAAS,YAAY,UAA8B,CAAC,GAAG;AAC5D,SAAO,CAAC,SAAc,YAAwC;AAC5D,YAAQ,SAAS,iBAAiB;AAClC,YAAQ,SAAS,aAAa,QAAQ,QAAQ,QAAQ;AACtD,YAAQ,SAAS,gBAAgB,KAAK,SAAS,MAAM;AACrD,YAAQ,SAAS,YAAY,MAAM;AACjC,aAAO;AAAA,QACL,QAAQ;AAAA,MACV;AAAA,IACF;AAAA,EACF;AACF;;;AKpBO,IAAM,2BAAN,cAAuC,MAAM;AAAA,EAClD,YAAY,aAAqB;AAC/B;AAAA,MACE,qCAAqC,WAAW;AAAA,IAClD;AAAA,EACF;AACF;;;ACKO,SAAS,KAAK,SAAsB;AACzC,SAAO,CAAC,SAAc,YAAwC;AAC5D,UAAM,cAAc,OAAO,QAAQ,IAAI;AAEvC,QAAI,CAAC,QAAQ,MAAM;AACjB,YAAM,IAAI,yBAAyB,WAAW;AAAA,IAChD;AAEA,YAAQ,SAAS,SAAS,WAAW,EAAE,IAAI;AAAA,EAC7C;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/typed-model",
|
|
3
|
-
"version": "4.0.0-next.
|
|
3
|
+
"version": "4.0.0-next.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -17,12 +17,10 @@
|
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"lodash": "^4.17.21",
|
|
20
|
-
"
|
|
21
|
-
"@orion-js/
|
|
22
|
-
"@orion-js/
|
|
23
|
-
"@orion-js/
|
|
24
|
-
"@orion-js/schema": "4.0.0-next.3",
|
|
25
|
-
"@orion-js/services": "4.0.0-next.3"
|
|
20
|
+
"@orion-js/schema": "4.0.0-next.4",
|
|
21
|
+
"@orion-js/models": "4.0.0-next.4",
|
|
22
|
+
"@orion-js/resolvers": "4.0.0-next.4",
|
|
23
|
+
"@orion-js/services": "4.0.0-next.4"
|
|
26
24
|
},
|
|
27
25
|
"devDependencies": {
|
|
28
26
|
"@types/node": "^18.0.0",
|