@orion-js/typed-model 4.0.0-next.2 → 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/LICENSE +21 -0
- package/dist/index.cjs +97 -267
- 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 -266
- package/dist/index.js.map +1 -1
- package/package.json +5 -7
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Orionjs Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
CHANGED
|
@@ -4,7 +4,6 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __getProtoOf = Object.getPrototypeOf;
|
|
6
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
7
|
var __export = (target, all) => {
|
|
9
8
|
for (var name in all)
|
|
10
9
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -31,303 +30,101 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
30
|
var index_exports = {};
|
|
32
31
|
__export(index_exports, {
|
|
33
32
|
Prop: () => Prop,
|
|
34
|
-
ResolverProp: () => ResolverProp,
|
|
35
|
-
TypedModel: () => TypedModel,
|
|
36
33
|
TypedSchema: () => TypedSchema,
|
|
37
34
|
cloneSchemaClass: () => cloneSchemaClass,
|
|
38
35
|
getModelForClass: () => getModelForClass,
|
|
39
|
-
getSchemaForClass: () => getSchemaForClass
|
|
36
|
+
getSchemaForClass: () => getSchemaForClass,
|
|
37
|
+
internal_getModelForClassFromMetadata: () => internal_getModelForClassFromMetadata,
|
|
38
|
+
resetModelCache: () => resetModelCache
|
|
40
39
|
});
|
|
41
40
|
module.exports = __toCommonJS(index_exports);
|
|
42
41
|
|
|
43
|
-
// src/errors/CannotDetermineType.ts
|
|
44
|
-
var _CannotDetermineTypeError = class _CannotDetermineTypeError extends Error {
|
|
45
|
-
constructor(schemaName, propertyKey) {
|
|
46
|
-
super(`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
|
-
__name(_CannotDetermineTypeError, "CannotDetermineTypeError");
|
|
50
|
-
var CannotDetermineTypeError = _CannotDetermineTypeError;
|
|
51
|
-
|
|
52
|
-
// src/errors/CannotUseArray.ts
|
|
53
|
-
var _CannotUseArrayError = class _CannotUseArrayError extends Error {
|
|
54
|
-
constructor(schemaName, propertyKey) {
|
|
55
|
-
super(`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
|
-
__name(_CannotUseArrayError, "CannotUseArrayError");
|
|
59
|
-
var CannotUseArrayError = _CannotUseArrayError;
|
|
60
|
-
|
|
61
|
-
// src/errors/PropertyAlreadyExists.ts
|
|
62
|
-
var _PropertyAlreadyExistsError = class _PropertyAlreadyExistsError extends Error {
|
|
63
|
-
constructor(schemaName, propertyName) {
|
|
64
|
-
super(`Schema with name "${schemaName}" already contains property "${propertyName}".`);
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
__name(_PropertyAlreadyExistsError, "PropertyAlreadyExistsError");
|
|
68
|
-
var PropertyAlreadyExistsError = _PropertyAlreadyExistsError;
|
|
69
|
-
|
|
70
|
-
// src/storage/metadataStorage.ts
|
|
71
|
-
var import_helpers = require("@orion-js/helpers");
|
|
72
|
-
var _MetadataStorageHandler = class _MetadataStorageHandler {
|
|
73
|
-
schemas = /* @__PURE__ */ new Map();
|
|
74
|
-
getSchema(target) {
|
|
75
|
-
const schema = this.schemas.get(target.__schemaId);
|
|
76
|
-
if (schema) return schema;
|
|
77
|
-
const schemaId = (0, import_helpers.generateId)();
|
|
78
|
-
target.__schemaId = schemaId;
|
|
79
|
-
const newSchema = {
|
|
80
|
-
schema: target,
|
|
81
|
-
options: {},
|
|
82
|
-
properties: {},
|
|
83
|
-
resolvers: {}
|
|
84
|
-
};
|
|
85
|
-
this.schemas.set(target.__schemaId, newSchema);
|
|
86
|
-
return newSchema;
|
|
87
|
-
}
|
|
88
|
-
addSchemaMetadata({ target, options }) {
|
|
89
|
-
const schema = this.getSchema(target);
|
|
90
|
-
schema.options = options;
|
|
91
|
-
}
|
|
92
|
-
addPropMetadata({ target, propertyKey, options }) {
|
|
93
|
-
const schema = this.getSchema(target);
|
|
94
|
-
const currProp = schema.properties[propertyKey];
|
|
95
|
-
if (currProp) {
|
|
96
|
-
throw new PropertyAlreadyExistsError(target.name, propertyKey);
|
|
97
|
-
}
|
|
98
|
-
schema.properties[propertyKey] = options;
|
|
99
|
-
}
|
|
100
|
-
addResolverMetadata({ target, propertyKey, options }) {
|
|
101
|
-
const schema = this.getSchema(target);
|
|
102
|
-
const currResolver = schema.resolvers[propertyKey];
|
|
103
|
-
if (currResolver) {
|
|
104
|
-
throw new PropertyAlreadyExistsError(target.name, propertyKey);
|
|
105
|
-
}
|
|
106
|
-
schema.resolvers[propertyKey] = options;
|
|
107
|
-
}
|
|
108
|
-
getSchemaProps(target) {
|
|
109
|
-
const schema = this.getSchema(target);
|
|
110
|
-
return schema.properties;
|
|
111
|
-
}
|
|
112
|
-
getSchemaResolvers(target) {
|
|
113
|
-
const schema = this.getSchema(target);
|
|
114
|
-
return schema.resolvers;
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
__name(_MetadataStorageHandler, "MetadataStorageHandler");
|
|
118
|
-
var MetadataStorageHandler = _MetadataStorageHandler;
|
|
119
|
-
var MetadataStorage = new MetadataStorageHandler();
|
|
120
|
-
|
|
121
|
-
// src/decorators/typedModel.ts
|
|
122
|
-
function TypedModel(options = {}) {
|
|
123
|
-
return (target) => {
|
|
124
|
-
MetadataStorage.addSchemaMetadata({
|
|
125
|
-
target,
|
|
126
|
-
options
|
|
127
|
-
});
|
|
128
|
-
target.getModel = () => getModelForClass(target);
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
__name(TypedModel, "TypedModel");
|
|
132
|
-
|
|
133
42
|
// src/decorators/typedSchema.ts
|
|
134
|
-
|
|
135
|
-
return (target) => {
|
|
136
|
-
MetadataStorage.addSchemaMetadata({
|
|
137
|
-
target,
|
|
138
|
-
options
|
|
139
|
-
});
|
|
140
|
-
target.getModel = () => getModelForClass(target);
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
__name(TypedSchema, "TypedSchema");
|
|
144
|
-
|
|
145
|
-
// src/decorators/prop.ts
|
|
146
|
-
var import_reflect_metadata = require("reflect-metadata");
|
|
147
|
-
|
|
148
|
-
// src/utils/isClass.ts
|
|
149
|
-
var isClass = /* @__PURE__ */ __name((type) => /^class\s/.test(Function.prototype.toString.call(type)), "isClass");
|
|
150
|
-
|
|
151
|
-
// src/decorators/prop.ts
|
|
152
|
-
function Prop(options = {}) {
|
|
153
|
-
return (classDef, propertyKey) => {
|
|
154
|
-
var _a;
|
|
155
|
-
const schemaName = (_a = classDef.constructor) == null ? void 0 : _a.name;
|
|
156
|
-
if (!options.type) {
|
|
157
|
-
const type = Reflect.getMetadata("design:type", classDef, propertyKey);
|
|
158
|
-
if (isClass(type) || type === Object) {
|
|
159
|
-
throw new CannotDetermineTypeError(schemaName, propertyKey);
|
|
160
|
-
}
|
|
161
|
-
if (type === Array) {
|
|
162
|
-
throw new CannotUseArrayError(schemaName, propertyKey);
|
|
163
|
-
}
|
|
164
|
-
if (type) {
|
|
165
|
-
options.type = type;
|
|
166
|
-
} else {
|
|
167
|
-
throw new CannotDetermineTypeError(schemaName, propertyKey);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
MetadataStorage.addPropMetadata({
|
|
171
|
-
target: classDef.constructor,
|
|
172
|
-
propertyKey,
|
|
173
|
-
options
|
|
174
|
-
});
|
|
175
|
-
classDef[propertyKey] = options;
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
__name(Prop, "Prop");
|
|
43
|
+
var import_lodash = require("lodash");
|
|
179
44
|
|
|
180
|
-
// src/
|
|
181
|
-
|
|
182
|
-
return (classDef, propertyKey) => {
|
|
183
|
-
MetadataStorage.addResolverMetadata({
|
|
184
|
-
target: classDef.constructor,
|
|
185
|
-
propertyKey,
|
|
186
|
-
options
|
|
187
|
-
});
|
|
188
|
-
classDef[propertyKey] = options;
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
__name(ResolverProp, "ResolverProp");
|
|
45
|
+
// src/factories/getModelForClass.ts
|
|
46
|
+
var import_models = require("@orion-js/models");
|
|
192
47
|
|
|
193
|
-
// src/factories/
|
|
48
|
+
// src/factories/processTypeForProp.ts
|
|
194
49
|
var import_isPlainObject = __toESM(require("lodash/isPlainObject"), 1);
|
|
195
|
-
function
|
|
196
|
-
return [
|
|
197
|
-
Boolean,
|
|
198
|
-
Number,
|
|
199
|
-
String,
|
|
200
|
-
Date
|
|
201
|
-
].includes(type);
|
|
202
|
-
}
|
|
203
|
-
__name(isPrimitive, "isPrimitive");
|
|
204
|
-
function processSchemaForProp(prop) {
|
|
50
|
+
function getParamTypeForProp(type) {
|
|
205
51
|
var _a;
|
|
206
|
-
if (
|
|
207
|
-
|
|
52
|
+
if (Array.isArray(type)) {
|
|
53
|
+
const itemType = type[0];
|
|
54
|
+
return [getParamTypeForProp(itemType)];
|
|
208
55
|
}
|
|
209
|
-
if (
|
|
210
|
-
return
|
|
211
|
-
...prop,
|
|
212
|
-
type: [
|
|
213
|
-
processSchemaForProp(prop.type[0])
|
|
214
|
-
]
|
|
215
|
-
} : prop;
|
|
216
|
-
}
|
|
217
|
-
if (typeof prop.type !== "function") {
|
|
218
|
-
if ((0, import_isPlainObject.default)(prop.type)) {
|
|
219
|
-
const subschema = {};
|
|
220
|
-
Object.keys(prop.type).forEach((key) => {
|
|
221
|
-
subschema[key] = processSchemaForProp({
|
|
222
|
-
type: prop.type[key]
|
|
223
|
-
});
|
|
224
|
-
});
|
|
225
|
-
return {
|
|
226
|
-
...prop,
|
|
227
|
-
type: subschema
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
return prop;
|
|
231
|
-
}
|
|
232
|
-
if (isPrimitive(prop.type)) {
|
|
233
|
-
return prop;
|
|
56
|
+
if ((_a = type == null ? void 0 : type[Symbol.metadata]) == null ? void 0 : _a._getModel) {
|
|
57
|
+
return type[Symbol.metadata]._getModel(type);
|
|
234
58
|
}
|
|
235
|
-
if (
|
|
236
|
-
|
|
237
|
-
return {
|
|
238
|
-
...prop,
|
|
239
|
-
type: schema
|
|
240
|
-
};
|
|
59
|
+
if (type == null ? void 0 : type.getSchema) {
|
|
60
|
+
return getParamTypeForProp(type.getSchema());
|
|
241
61
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
function getSchemaForClassRecursive(target) {
|
|
246
|
-
const schema = {};
|
|
247
|
-
let parent = target;
|
|
248
|
-
while (parent.prototype) {
|
|
249
|
-
if (parent === Function.prototype) {
|
|
250
|
-
break;
|
|
62
|
+
if ((0, import_isPlainObject.default)(type)) {
|
|
63
|
+
if (type.__isFieldType) {
|
|
64
|
+
return type;
|
|
251
65
|
}
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
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
|
+
};
|
|
259
76
|
});
|
|
260
|
-
|
|
77
|
+
return subschema;
|
|
261
78
|
}
|
|
262
|
-
return
|
|
79
|
+
return type;
|
|
263
80
|
}
|
|
264
|
-
__name(getSchemaForClassRecursive, "getSchemaForClassRecursive");
|
|
265
|
-
|
|
266
|
-
// src/factories/getSchemaForClass.ts
|
|
267
|
-
function getSchemaForClass(target) {
|
|
268
|
-
return getSchemaForClassRecursive(target);
|
|
269
|
-
}
|
|
270
|
-
__name(getSchemaForClass, "getSchemaForClass");
|
|
271
81
|
|
|
272
82
|
// src/factories/getModelForClass.ts
|
|
273
|
-
|
|
83
|
+
Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
|
|
274
84
|
var modelCache = /* @__PURE__ */ new Map();
|
|
275
|
-
function
|
|
276
|
-
|
|
277
|
-
if (((_a = prop.type) == null ? void 0 : _a.__isModel) === true) {
|
|
278
|
-
return prop;
|
|
279
|
-
}
|
|
280
|
-
if (((_b = prop.type) == null ? void 0 : _b._isFieldType) === true) {
|
|
281
|
-
return prop;
|
|
282
|
-
}
|
|
283
|
-
return processSchemaForProp(prop);
|
|
85
|
+
function resetModelCache() {
|
|
86
|
+
modelCache.clear();
|
|
284
87
|
}
|
|
285
|
-
__name(processModelSchemaForProp, "processModelSchemaForProp");
|
|
286
88
|
function getModelForClass(target) {
|
|
287
89
|
const targetAsModel = target;
|
|
288
90
|
if (targetAsModel.__isModel) {
|
|
289
91
|
return targetAsModel;
|
|
290
92
|
}
|
|
291
|
-
|
|
292
|
-
if (
|
|
293
|
-
|
|
294
|
-
target = target.prototype.typedModel;
|
|
93
|
+
const metadata = target[Symbol.metadata];
|
|
94
|
+
if (!metadata) {
|
|
95
|
+
return targetAsModel;
|
|
295
96
|
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
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);
|
|
299
103
|
}
|
|
300
104
|
const schema = {};
|
|
301
|
-
const
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
});
|
|
311
|
-
const resolvers = MetadataStorage.getSchemaResolvers(parent) ?? {};
|
|
312
|
-
Object.keys(resolvers).forEach((key) => {
|
|
313
|
-
resolverMap[key] = resolvers[key];
|
|
314
|
-
});
|
|
315
|
-
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
|
+
};
|
|
316
114
|
}
|
|
317
115
|
const model = (0, import_models.createModel)({
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
validate: targetAsModel.validate,
|
|
322
|
-
resolvers: {
|
|
323
|
-
...resolverMap,
|
|
324
|
-
...modelResolvers
|
|
325
|
-
}
|
|
116
|
+
...metadata._modelOptions,
|
|
117
|
+
name: modelName,
|
|
118
|
+
schema
|
|
326
119
|
});
|
|
327
|
-
modelCache.set(
|
|
120
|
+
modelCache.set(modelName, model);
|
|
328
121
|
return model;
|
|
329
122
|
}
|
|
330
|
-
|
|
123
|
+
|
|
124
|
+
// src/factories/getSchemaForClass.ts
|
|
125
|
+
function getSchemaForClass(target) {
|
|
126
|
+
return getModelForClass(target).getSchema();
|
|
127
|
+
}
|
|
331
128
|
|
|
332
129
|
// src/factories/cloneSchemaClass.ts
|
|
333
130
|
function cloneSchemaClass(schema, options) {
|
|
@@ -340,15 +137,48 @@ function cloneSchemaClass(schema, options) {
|
|
|
340
137
|
});
|
|
341
138
|
return newModel;
|
|
342
139
|
}
|
|
343
|
-
|
|
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
|
+
}
|
|
344
174
|
// Annotate the CommonJS export names for ESM import in node:
|
|
345
175
|
0 && (module.exports = {
|
|
346
176
|
Prop,
|
|
347
|
-
ResolverProp,
|
|
348
|
-
TypedModel,
|
|
349
177
|
TypedSchema,
|
|
350
178
|
cloneSchemaClass,
|
|
351
179
|
getModelForClass,
|
|
352
|
-
getSchemaForClass
|
|
180
|
+
getSchemaForClass,
|
|
181
|
+
internal_getModelForClassFromMetadata,
|
|
182
|
+
resetModelCache
|
|
353
183
|
});
|
|
354
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;;;;;;;;;;;;;ACAO,IAAMA,4BAAN,MAAMA,kCAAiCC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cAAcR,QAAQQ;EACxB,CAAA;AAEA,SAAOL;AACT;AAdgBL;","names":["CannotDetermineTypeError","Error","constructor","schemaName","propertyKey","CannotUseArrayError","Error","constructor","schemaName","propertyKey","PropertyAlreadyExistsError","Error","constructor","schemaName","propertyName","MetadataStorageHandler","schemas","Map","getSchema","target","schema","get","__schemaId","schemaId","generateId","newSchema","options","properties","resolvers","set","addSchemaMetadata","addPropMetadata","propertyKey","currProp","PropertyAlreadyExistsError","name","addResolverMetadata","currResolver","getSchemaProps","getSchemaResolvers","MetadataStorage","TypedModel","options","target","MetadataStorage","addSchemaMetadata","getModel","getModelForClass","TypedSchema","options","target","MetadataStorage","addSchemaMetadata","getModel","getModelForClass","isClass","type","test","Function","prototype","toString","call","Prop","options","classDef","propertyKey","schemaName","constructor","name","type","Reflect","getMetadata","isClass","Object","CannotDetermineTypeError","Array","CannotUseArrayError","MetadataStorage","addPropMetadata","target","ResolverProp","options","classDef","propertyKey","MetadataStorage","addResolverMetadata","target","constructor","isPrimitive","type","Boolean","Number","String","Date","includes","processSchemaForProp","prop","Array","isArray","length","isPlainObject","subschema","Object","keys","forEach","key","isClass","schema","getSchemaForClassRecursive","target","parent","prototype","Function","props","MetadataStorage","getSchemaProps","getPrototypeOf","getSchemaForClass","target","getSchemaForClassRecursive","modelCache","Map","processModelSchemaForProp","prop","type","__isModel","_isFieldType","processSchemaForProp","getModelForClass","target","targetAsModel","modelResolvers","prototype","typedModel","resolvers","schemaId","__schemaId","has","get","schema","resolverMap","parent","Function","props","MetadataStorage","getSchemaProps","Object","keys","forEach","key","getSchemaResolvers","getPrototypeOf","model","createModel","name","clean","validate","set","cloneSchemaClass","schema","options","model","getModelForClass","newModel","clone","name","pickFields","mapFields","extendSchema"]}
|
|
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;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,KAA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|
|
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,294 +1,89 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
-
|
|
4
|
-
// src/errors/CannotDetermineType.ts
|
|
5
|
-
var _CannotDetermineTypeError = class _CannotDetermineTypeError extends Error {
|
|
6
|
-
constructor(schemaName, propertyKey) {
|
|
7
|
-
super(`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} })"`);
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
__name(_CannotDetermineTypeError, "CannotDetermineTypeError");
|
|
11
|
-
var CannotDetermineTypeError = _CannotDetermineTypeError;
|
|
12
|
-
|
|
13
|
-
// src/errors/CannotUseArray.ts
|
|
14
|
-
var _CannotUseArrayError = class _CannotUseArrayError extends Error {
|
|
15
|
-
constructor(schemaName, propertyKey) {
|
|
16
|
-
super(`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 | ...] })"`);
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
__name(_CannotUseArrayError, "CannotUseArrayError");
|
|
20
|
-
var CannotUseArrayError = _CannotUseArrayError;
|
|
21
|
-
|
|
22
|
-
// src/errors/PropertyAlreadyExists.ts
|
|
23
|
-
var _PropertyAlreadyExistsError = class _PropertyAlreadyExistsError extends Error {
|
|
24
|
-
constructor(schemaName, propertyName) {
|
|
25
|
-
super(`Schema with name "${schemaName}" already contains property "${propertyName}".`);
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
__name(_PropertyAlreadyExistsError, "PropertyAlreadyExistsError");
|
|
29
|
-
var PropertyAlreadyExistsError = _PropertyAlreadyExistsError;
|
|
30
|
-
|
|
31
|
-
// src/storage/metadataStorage.ts
|
|
32
|
-
import { generateId } from "@orion-js/helpers";
|
|
33
|
-
var _MetadataStorageHandler = class _MetadataStorageHandler {
|
|
34
|
-
schemas = /* @__PURE__ */ new Map();
|
|
35
|
-
getSchema(target) {
|
|
36
|
-
const schema = this.schemas.get(target.__schemaId);
|
|
37
|
-
if (schema) return schema;
|
|
38
|
-
const schemaId = generateId();
|
|
39
|
-
target.__schemaId = schemaId;
|
|
40
|
-
const newSchema = {
|
|
41
|
-
schema: target,
|
|
42
|
-
options: {},
|
|
43
|
-
properties: {},
|
|
44
|
-
resolvers: {}
|
|
45
|
-
};
|
|
46
|
-
this.schemas.set(target.__schemaId, newSchema);
|
|
47
|
-
return newSchema;
|
|
48
|
-
}
|
|
49
|
-
addSchemaMetadata({ target, options }) {
|
|
50
|
-
const schema = this.getSchema(target);
|
|
51
|
-
schema.options = options;
|
|
52
|
-
}
|
|
53
|
-
addPropMetadata({ target, propertyKey, options }) {
|
|
54
|
-
const schema = this.getSchema(target);
|
|
55
|
-
const currProp = schema.properties[propertyKey];
|
|
56
|
-
if (currProp) {
|
|
57
|
-
throw new PropertyAlreadyExistsError(target.name, propertyKey);
|
|
58
|
-
}
|
|
59
|
-
schema.properties[propertyKey] = options;
|
|
60
|
-
}
|
|
61
|
-
addResolverMetadata({ target, propertyKey, options }) {
|
|
62
|
-
const schema = this.getSchema(target);
|
|
63
|
-
const currResolver = schema.resolvers[propertyKey];
|
|
64
|
-
if (currResolver) {
|
|
65
|
-
throw new PropertyAlreadyExistsError(target.name, propertyKey);
|
|
66
|
-
}
|
|
67
|
-
schema.resolvers[propertyKey] = options;
|
|
68
|
-
}
|
|
69
|
-
getSchemaProps(target) {
|
|
70
|
-
const schema = this.getSchema(target);
|
|
71
|
-
return schema.properties;
|
|
72
|
-
}
|
|
73
|
-
getSchemaResolvers(target) {
|
|
74
|
-
const schema = this.getSchema(target);
|
|
75
|
-
return schema.resolvers;
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
__name(_MetadataStorageHandler, "MetadataStorageHandler");
|
|
79
|
-
var MetadataStorageHandler = _MetadataStorageHandler;
|
|
80
|
-
var MetadataStorage = new MetadataStorageHandler();
|
|
81
|
-
|
|
82
|
-
// src/decorators/typedModel.ts
|
|
83
|
-
function TypedModel(options = {}) {
|
|
84
|
-
return (target) => {
|
|
85
|
-
MetadataStorage.addSchemaMetadata({
|
|
86
|
-
target,
|
|
87
|
-
options
|
|
88
|
-
});
|
|
89
|
-
target.getModel = () => getModelForClass(target);
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
__name(TypedModel, "TypedModel");
|
|
93
|
-
|
|
94
1
|
// src/decorators/typedSchema.ts
|
|
95
|
-
|
|
96
|
-
return (target) => {
|
|
97
|
-
MetadataStorage.addSchemaMetadata({
|
|
98
|
-
target,
|
|
99
|
-
options
|
|
100
|
-
});
|
|
101
|
-
target.getModel = () => getModelForClass(target);
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
__name(TypedSchema, "TypedSchema");
|
|
105
|
-
|
|
106
|
-
// src/decorators/prop.ts
|
|
107
|
-
import "reflect-metadata";
|
|
108
|
-
|
|
109
|
-
// src/utils/isClass.ts
|
|
110
|
-
var isClass = /* @__PURE__ */ __name((type) => /^class\s/.test(Function.prototype.toString.call(type)), "isClass");
|
|
111
|
-
|
|
112
|
-
// src/decorators/prop.ts
|
|
113
|
-
function Prop(options = {}) {
|
|
114
|
-
return (classDef, propertyKey) => {
|
|
115
|
-
var _a;
|
|
116
|
-
const schemaName = (_a = classDef.constructor) == null ? void 0 : _a.name;
|
|
117
|
-
if (!options.type) {
|
|
118
|
-
const type = Reflect.getMetadata("design:type", classDef, propertyKey);
|
|
119
|
-
if (isClass(type) || type === Object) {
|
|
120
|
-
throw new CannotDetermineTypeError(schemaName, propertyKey);
|
|
121
|
-
}
|
|
122
|
-
if (type === Array) {
|
|
123
|
-
throw new CannotUseArrayError(schemaName, propertyKey);
|
|
124
|
-
}
|
|
125
|
-
if (type) {
|
|
126
|
-
options.type = type;
|
|
127
|
-
} else {
|
|
128
|
-
throw new CannotDetermineTypeError(schemaName, propertyKey);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
MetadataStorage.addPropMetadata({
|
|
132
|
-
target: classDef.constructor,
|
|
133
|
-
propertyKey,
|
|
134
|
-
options
|
|
135
|
-
});
|
|
136
|
-
classDef[propertyKey] = options;
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
__name(Prop, "Prop");
|
|
2
|
+
import { omit } from "lodash";
|
|
140
3
|
|
|
141
|
-
// src/
|
|
142
|
-
|
|
143
|
-
return (classDef, propertyKey) => {
|
|
144
|
-
MetadataStorage.addResolverMetadata({
|
|
145
|
-
target: classDef.constructor,
|
|
146
|
-
propertyKey,
|
|
147
|
-
options
|
|
148
|
-
});
|
|
149
|
-
classDef[propertyKey] = options;
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
__name(ResolverProp, "ResolverProp");
|
|
4
|
+
// src/factories/getModelForClass.ts
|
|
5
|
+
import { createModel } from "@orion-js/models";
|
|
153
6
|
|
|
154
|
-
// src/factories/
|
|
7
|
+
// src/factories/processTypeForProp.ts
|
|
155
8
|
import isPlainObject from "lodash/isPlainObject";
|
|
156
|
-
function
|
|
157
|
-
return [
|
|
158
|
-
Boolean,
|
|
159
|
-
Number,
|
|
160
|
-
String,
|
|
161
|
-
Date
|
|
162
|
-
].includes(type);
|
|
163
|
-
}
|
|
164
|
-
__name(isPrimitive, "isPrimitive");
|
|
165
|
-
function processSchemaForProp(prop) {
|
|
9
|
+
function getParamTypeForProp(type) {
|
|
166
10
|
var _a;
|
|
167
|
-
if (
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
if (Array.isArray(prop.type)) {
|
|
171
|
-
return prop.type.length > 0 ? {
|
|
172
|
-
...prop,
|
|
173
|
-
type: [
|
|
174
|
-
processSchemaForProp(prop.type[0])
|
|
175
|
-
]
|
|
176
|
-
} : prop;
|
|
177
|
-
}
|
|
178
|
-
if (typeof prop.type !== "function") {
|
|
179
|
-
if (isPlainObject(prop.type)) {
|
|
180
|
-
const subschema = {};
|
|
181
|
-
Object.keys(prop.type).forEach((key) => {
|
|
182
|
-
subschema[key] = processSchemaForProp({
|
|
183
|
-
type: prop.type[key]
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
return {
|
|
187
|
-
...prop,
|
|
188
|
-
type: subschema
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
return prop;
|
|
11
|
+
if (Array.isArray(type)) {
|
|
12
|
+
const itemType = type[0];
|
|
13
|
+
return [getParamTypeForProp(itemType)];
|
|
192
14
|
}
|
|
193
|
-
if (
|
|
194
|
-
return
|
|
15
|
+
if ((_a = type == null ? void 0 : type[Symbol.metadata]) == null ? void 0 : _a._getModel) {
|
|
16
|
+
return type[Symbol.metadata]._getModel(type);
|
|
195
17
|
}
|
|
196
|
-
if (
|
|
197
|
-
|
|
198
|
-
return {
|
|
199
|
-
...prop,
|
|
200
|
-
type: schema
|
|
201
|
-
};
|
|
18
|
+
if (type == null ? void 0 : type.getSchema) {
|
|
19
|
+
return getParamTypeForProp(type.getSchema());
|
|
202
20
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
function getSchemaForClassRecursive(target) {
|
|
207
|
-
const schema = {};
|
|
208
|
-
let parent = target;
|
|
209
|
-
while (parent.prototype) {
|
|
210
|
-
if (parent === Function.prototype) {
|
|
211
|
-
break;
|
|
212
|
-
}
|
|
213
|
-
const props = MetadataStorage.getSchemaProps(parent);
|
|
214
|
-
if (!props) {
|
|
215
|
-
parent = Object.getPrototypeOf(parent);
|
|
216
|
-
continue;
|
|
21
|
+
if (isPlainObject(type)) {
|
|
22
|
+
if (type.__isFieldType) {
|
|
23
|
+
return type;
|
|
217
24
|
}
|
|
218
|
-
|
|
219
|
-
|
|
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
|
+
};
|
|
220
35
|
});
|
|
221
|
-
|
|
36
|
+
return subschema;
|
|
222
37
|
}
|
|
223
|
-
return
|
|
224
|
-
}
|
|
225
|
-
__name(getSchemaForClassRecursive, "getSchemaForClassRecursive");
|
|
226
|
-
|
|
227
|
-
// src/factories/getSchemaForClass.ts
|
|
228
|
-
function getSchemaForClass(target) {
|
|
229
|
-
return getSchemaForClassRecursive(target);
|
|
38
|
+
return type;
|
|
230
39
|
}
|
|
231
|
-
__name(getSchemaForClass, "getSchemaForClass");
|
|
232
40
|
|
|
233
41
|
// src/factories/getModelForClass.ts
|
|
234
|
-
|
|
42
|
+
Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
|
|
235
43
|
var modelCache = /* @__PURE__ */ new Map();
|
|
236
|
-
function
|
|
237
|
-
|
|
238
|
-
if (((_a = prop.type) == null ? void 0 : _a.__isModel) === true) {
|
|
239
|
-
return prop;
|
|
240
|
-
}
|
|
241
|
-
if (((_b = prop.type) == null ? void 0 : _b._isFieldType) === true) {
|
|
242
|
-
return prop;
|
|
243
|
-
}
|
|
244
|
-
return processSchemaForProp(prop);
|
|
44
|
+
function resetModelCache() {
|
|
45
|
+
modelCache.clear();
|
|
245
46
|
}
|
|
246
|
-
__name(processModelSchemaForProp, "processModelSchemaForProp");
|
|
247
47
|
function getModelForClass(target) {
|
|
248
48
|
const targetAsModel = target;
|
|
249
49
|
if (targetAsModel.__isModel) {
|
|
250
50
|
return targetAsModel;
|
|
251
51
|
}
|
|
252
|
-
|
|
253
|
-
if (
|
|
254
|
-
|
|
255
|
-
target = target.prototype.typedModel;
|
|
52
|
+
const metadata = target[Symbol.metadata];
|
|
53
|
+
if (!metadata) {
|
|
54
|
+
return targetAsModel;
|
|
256
55
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
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);
|
|
260
62
|
}
|
|
261
63
|
const schema = {};
|
|
262
|
-
const
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
});
|
|
272
|
-
const resolvers = MetadataStorage.getSchemaResolvers(parent) ?? {};
|
|
273
|
-
Object.keys(resolvers).forEach((key) => {
|
|
274
|
-
resolverMap[key] = resolvers[key];
|
|
275
|
-
});
|
|
276
|
-
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
|
+
};
|
|
277
73
|
}
|
|
278
74
|
const model = createModel({
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
validate: targetAsModel.validate,
|
|
283
|
-
resolvers: {
|
|
284
|
-
...resolverMap,
|
|
285
|
-
...modelResolvers
|
|
286
|
-
}
|
|
75
|
+
...metadata._modelOptions,
|
|
76
|
+
name: modelName,
|
|
77
|
+
schema
|
|
287
78
|
});
|
|
288
|
-
modelCache.set(
|
|
79
|
+
modelCache.set(modelName, model);
|
|
289
80
|
return model;
|
|
290
81
|
}
|
|
291
|
-
|
|
82
|
+
|
|
83
|
+
// src/factories/getSchemaForClass.ts
|
|
84
|
+
function getSchemaForClass(target) {
|
|
85
|
+
return getModelForClass(target).getSchema();
|
|
86
|
+
}
|
|
292
87
|
|
|
293
88
|
// src/factories/cloneSchemaClass.ts
|
|
294
89
|
function cloneSchemaClass(schema, options) {
|
|
@@ -301,14 +96,47 @@ function cloneSchemaClass(schema, options) {
|
|
|
301
96
|
});
|
|
302
97
|
return newModel;
|
|
303
98
|
}
|
|
304
|
-
|
|
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
|
+
}
|
|
305
133
|
export {
|
|
306
134
|
Prop,
|
|
307
|
-
ResolverProp,
|
|
308
|
-
TypedModel,
|
|
309
135
|
TypedSchema,
|
|
310
136
|
cloneSchemaClass,
|
|
311
137
|
getModelForClass,
|
|
312
|
-
getSchemaForClass
|
|
138
|
+
getSchemaForClass,
|
|
139
|
+
internal_getModelForClassFromMetadata,
|
|
140
|
+
resetModelCache
|
|
313
141
|
};
|
|
314
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,IAAMA,4BAAN,MAAMA,kCAAiCC,MAAAA;EAC5CC,YAAYC,YAAoBC,aAAqB;AACnD,UACE,6BAA6BD,UAAAA,IAAcC,WAAAA,wKAAmL;EAElO;AACF;AAN8CH;AAAvC,IAAMD,2BAAN;;;ACAA,IAAMK,uBAAN,MAAMA,6BAA4BC,MAAAA;EACvCC,YAAYC,YAAoBC,aAAqB;AACnD,UACE,u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AAAA,GAAW;AAC5B,WAAOf,WAAWkB,IAAIH,QAAAA;EACxB;AAEA,QAAMI,SAAsB,CAAC;AAC7B,QAAMC,cAAiC,CAAC;AAExC,MAAIC,SAAmBZ;AAEvB,SAAOY,OAAOT,WAAW;AACvB,QAAIS,WAAWC,SAASV,WAAW;AACjC;IACF;AAEA,UAAMW,QAAQC,gBAAgBC,eAAeJ,MAAAA,KAAW,CAAC;AAEzDK,WAAOC,KAAKJ,KAAAA,EAAOK,QAAQC,CAAAA,QAAAA;AACzBV,aAAOU,GAAAA,IAAO3B,0BAA0BqB,MAAMM,GAAAA,CAAI;IACpD,CAAA;AAEA,UAAMf,YAAYU,gBAAgBM,mBAAmBT,MAAAA,KAAW,CAAC;AACjEK,WAAOC,KAAKb,SAAAA,EAAWc,QAAQC,CAAAA,QAAAA;AAC7BT,kBAAYS,GAAAA,IAAOf,UAAUe,GAAAA;IAC/B,CAAA;AAEAR,aAASK,OAAOK,eAAeV,MAAAA;EACjC;AAEA,QAAMW,QAAQC,YAAY;IACxBC,MAAMxB,cAAcwB;IACpBf;IACAgB,OAAOzB,cAAcyB;IACrBC,UAAU1B,cAAc0B;IACxBtB,WAAW;MACT,GAAGM;MACH,GAAGT;IACL;EACF,CAAA;AAEAX,aAAWqC,IAAItB,UAAUiB,KAAAA;AAEzB,SAAOA;AACT;AAzDgBxB;;;ACGT,SAAS8B,iBACdC,QACAC,SAAiD;AAEjD,QAAMC,QAAQC,iBAAiBH,MAAAA;AAE/B,QAAMI,WAAyCF,MAAMG,MAAM;IACzDC,MAAML,QAAQK;IACdC,YAAYN,QAAQM;IACpBC,WAAWP,QAAQO;IACnBC,cAAcR,QAAQQ;EACxB,CAAA;AAEA,SAAOL;AACT;AAdgBL;","names":["CannotDetermineTypeError","Error","constructor","schemaName","propertyKey","CannotUseArrayError","Error","constructor","schemaName","propertyKey","PropertyAlreadyExistsError","Error","constructor","schemaName","propertyName","generateId","MetadataStorageHandler","schemas","Map","getSchema","target","schema","get","__schemaId","schemaId","generateId","newSchema","options","properties","resolvers","set","addSchemaMetadata","addPropMetadata","propertyKey","currProp","PropertyAlreadyExistsError","name","addResolverMetadata","currResolver","getSchemaProps","getSchemaResolvers","MetadataStorage","TypedModel","options","target","MetadataStorage","addSchemaMetadata","getModel","getModelForClass","TypedSchema","options","target","MetadataStorage","addSchemaMetadata","getModel","getModelForClass","isClass","type","test","Function","prototype","toString","call","Prop","options","classDef","propertyKey","schemaName","constructor","name","type","Reflect","getMetadata","isClass","Object","CannotDetermineTypeError","Array","CannotUseArrayError","MetadataStorage","addPropMetadata","target","ResolverProp","options","classDef","propertyKey","MetadataStorage","addResolverMetadata","target","constructor","isPlainObject","isPrimitive","type","Boolean","Number","String","Date","includes","processSchemaForProp","prop","Array","isArray","length","isPlainObject","subschema","Object","keys","forEach","key","isClass","schema","getSchemaForClassRecursive","target","parent","prototype","Function","props","MetadataStorage","getSchemaProps","getPrototypeOf","getSchemaForClass","target","getSchemaForClassRecursive","createModel","modelCache","Map","processModelSchemaForProp","prop","type","__isModel","_isFieldType","processSchemaForProp","getModelForClass","target","targetAsModel","modelResolvers","prototype","typedModel","resolvers","schemaId","__schemaId","has","get","schema","resolverMap","parent","Function","props","MetadataStorage","getSchemaProps","Object","keys","forEach","key","getSchemaResolvers","getPrototypeOf","model","createModel","name","clean","validate","set","cloneSchemaClass","schema","options","model","getModelForClass","newModel","clone","name","pickFields","mapFields","extendSchema"]}
|
|
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,CAAC;AACnB,WAAO,KAAK,IAAI,EAAE,QAAQ,SAAO;AAC/B,UAAI,IAAI,WAAW,IAAI,GAAG;AACxB,kBAAU,GAAG,IAAI,KAAK,GAAG;AACzB;AAAA,MACF;AAEA,gBAAU,GAAG,IAAI;AAAA,QACf,GAAG,KAAK,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C,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/services": "4.0.0-next.2",
|
|
25
|
-
"@orion-js/resolvers": "4.0.0-next.2"
|
|
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",
|