1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.rejectNavigationalPropertiesInData = exports.Event = exports.Entity = exports.ValueObject = exports.Model = exports.ModelDefinition = void 0;
|
8 | const index_1 = require("./index");
|
9 |
|
10 |
|
11 |
|
12 | class ModelDefinition {
|
13 | constructor(nameOrDef) {
|
14 | if (typeof nameOrDef === 'string') {
|
15 | nameOrDef = { name: nameOrDef };
|
16 | }
|
17 | const { name, properties, settings, relations } = nameOrDef;
|
18 | this.name = name;
|
19 | this.properties = {};
|
20 | if (properties) {
|
21 | for (const p in properties) {
|
22 | this.addProperty(p, properties[p]);
|
23 | }
|
24 | }
|
25 | this.settings = settings !== null && settings !== void 0 ? settings : new Map();
|
26 | this.relations = relations !== null && relations !== void 0 ? relations : {};
|
27 | }
|
28 | |
29 |
|
30 |
|
31 |
|
32 |
|
33 | addProperty(name, definitionOrType) {
|
34 | const definition = definitionOrType.type
|
35 | ? definitionOrType
|
36 | : { type: definitionOrType };
|
37 | if (definition.id === true &&
|
38 | definition.generated === true &&
|
39 | definition.type !== undefined &&
|
40 | definition.useDefaultIdType === undefined) {
|
41 | definition.useDefaultIdType = false;
|
42 | }
|
43 | this.properties[name] = definition;
|
44 | return this;
|
45 | }
|
46 | |
47 |
|
48 |
|
49 |
|
50 |
|
51 | addSetting(name, value) {
|
52 | this.settings[name] = value;
|
53 | return this;
|
54 | }
|
55 | |
56 |
|
57 |
|
58 |
|
59 | addRelation(definition) {
|
60 | this.relations[definition.name] = definition;
|
61 | return this;
|
62 | }
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 | belongsTo(name, definition) {
|
69 | const meta = {
|
70 | ...definition,
|
71 | name,
|
72 | type: index_1.RelationType.belongsTo,
|
73 | targetsMany: false,
|
74 | };
|
75 | return this.addRelation(meta);
|
76 | }
|
77 | |
78 |
|
79 |
|
80 |
|
81 |
|
82 | hasOne(name, definition) {
|
83 | const meta = {
|
84 | ...definition,
|
85 | name,
|
86 | type: index_1.RelationType.hasOne,
|
87 | targetsMany: false,
|
88 | };
|
89 | return this.addRelation(meta);
|
90 | }
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 | hasMany(name, definition) {
|
97 | const meta = {
|
98 | ...definition,
|
99 | name,
|
100 | type: index_1.RelationType.hasMany,
|
101 | targetsMany: true,
|
102 | };
|
103 | return this.addRelation(meta);
|
104 | }
|
105 | |
106 |
|
107 |
|
108 |
|
109 |
|
110 | referencesMany(name, definition) {
|
111 | const meta = {
|
112 | ...definition,
|
113 | name,
|
114 | type: index_1.RelationType.referencesMany,
|
115 | targetsMany: true,
|
116 | };
|
117 | return this.addRelation(meta);
|
118 | }
|
119 | |
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | idProperties() {
|
139 | if (typeof this.settings.id === 'string') {
|
140 | return [this.settings.id];
|
141 | }
|
142 | else if (Array.isArray(this.settings.id)) {
|
143 | return this.settings.id;
|
144 | }
|
145 | const idProps = Object.keys(this.properties).filter(prop => this.properties[prop].id);
|
146 | return idProps;
|
147 | }
|
148 | }
|
149 | exports.ModelDefinition = ModelDefinition;
|
150 | function asJSON(value) {
|
151 | if (value == null)
|
152 | return value;
|
153 | if (typeof value.toJSON === 'function') {
|
154 | return value.toJSON();
|
155 | }
|
156 |
|
157 | if (Array.isArray(value)) {
|
158 | return value.map(item => asJSON(item));
|
159 | }
|
160 | return value;
|
161 | }
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | function asObject(value, options) {
|
174 | if (value == null)
|
175 | return value;
|
176 | if (typeof value.toObject === 'function') {
|
177 | return value.toObject(options);
|
178 | }
|
179 | if (Array.isArray(value)) {
|
180 | return value.map(item => asObject(item, options));
|
181 | }
|
182 | return value;
|
183 | }
|
184 |
|
185 |
|
186 |
|
187 | class Model {
|
188 | static get modelName() {
|
189 | var _a;
|
190 | return ((_a = this.definition) === null || _a === void 0 ? void 0 : _a.name) || this.name;
|
191 | }
|
192 | |
193 |
|
194 |
|
195 | toJSON() {
|
196 | const def = this.constructor.definition;
|
197 | if (def == null || def.settings.strict === false) {
|
198 | return this.toObject({ ignoreUnknownProperties: false });
|
199 | }
|
200 | const copyPropertyAsJson = (key) => {
|
201 | const val = asJSON(this[key]);
|
202 | if (val !== undefined) {
|
203 | json[key] = val;
|
204 | }
|
205 | };
|
206 | const json = {};
|
207 | const hiddenProperties = def.settings.hiddenProperties || [];
|
208 | for (const p in def.properties) {
|
209 | if (p in this && !hiddenProperties.includes(p)) {
|
210 | copyPropertyAsJson(p);
|
211 | }
|
212 | }
|
213 | for (const r in def.relations) {
|
214 | const relName = def.relations[r].name;
|
215 | if (relName in this) {
|
216 | copyPropertyAsJson(relName);
|
217 | }
|
218 | }
|
219 | return json;
|
220 | }
|
221 | |
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | toObject(options) {
|
231 | const def = this.constructor.definition;
|
232 | const obj = {};
|
233 | if ((options === null || options === void 0 ? void 0 : options.ignoreUnknownProperties) === false) {
|
234 | const hiddenProperties = (def === null || def === void 0 ? void 0 : def.settings.hiddenProperties) || [];
|
235 | for (const p in this) {
|
236 | if (!hiddenProperties.includes(p)) {
|
237 | const val = this[p];
|
238 | obj[p] = asObject(val, options);
|
239 | }
|
240 | }
|
241 | return obj;
|
242 | }
|
243 | if (def === null || def === void 0 ? void 0 : def.relations) {
|
244 | for (const r in def.relations) {
|
245 | const relName = def.relations[r].name;
|
246 | if (relName in this) {
|
247 | obj[relName] = asObject(this[relName], {
|
248 | ...options,
|
249 | ignoreUnknownProperties: false,
|
250 | });
|
251 | }
|
252 | }
|
253 | }
|
254 | const props = def.properties;
|
255 | const keys = Object.keys(props);
|
256 | for (const i in keys) {
|
257 | const propertyName = keys[i];
|
258 | const val = this[propertyName];
|
259 | if (val === undefined)
|
260 | continue;
|
261 | obj[propertyName] = asObject(val, options);
|
262 | }
|
263 | return obj;
|
264 | }
|
265 | constructor(data) {
|
266 | Object.assign(this, data);
|
267 | }
|
268 | }
|
269 | exports.Model = Model;
|
270 |
|
271 |
|
272 |
|
273 |
|
274 | class ValueObject extends Model {
|
275 | }
|
276 | exports.ValueObject = ValueObject;
|
277 |
|
278 |
|
279 |
|
280 | class Entity extends Model {
|
281 | |
282 |
|
283 |
|
284 | static getIdProperties() {
|
285 | return this.definition.idProperties();
|
286 | }
|
287 | |
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | static getIdOf(entityOrData) {
|
294 | if (typeof entityOrData.getId === 'function') {
|
295 | return entityOrData.getId();
|
296 | }
|
297 | const idName = this.getIdProperties()[0];
|
298 | return entityOrData[idName];
|
299 | }
|
300 | |
301 |
|
302 |
|
303 |
|
304 | getId() {
|
305 | const definition = this.constructor.definition;
|
306 | const idProps = definition.idProperties();
|
307 | if (idProps.length === 1) {
|
308 | return this[idProps[0]];
|
309 | }
|
310 | if (!idProps.length) {
|
311 | throw new Error(`Invalid Entity ${this.constructor.name}:` +
|
312 | 'missing primary key (id) property');
|
313 | }
|
314 | return this.getIdObject();
|
315 | }
|
316 | |
317 |
|
318 |
|
319 |
|
320 | getIdObject() {
|
321 | const definition = this.constructor.definition;
|
322 | const idProps = definition.idProperties();
|
323 | const idObj = {};
|
324 | for (const idProp of idProps) {
|
325 | idObj[idProp] = this[idProp];
|
326 | }
|
327 | return idObj;
|
328 | }
|
329 | |
330 |
|
331 |
|
332 |
|
333 | static buildWhereForId(id) {
|
334 | const where = {};
|
335 | const idProps = this.definition.idProperties();
|
336 | if (idProps.length === 1) {
|
337 | where[idProps[0]] = id;
|
338 | }
|
339 | else {
|
340 | for (const idProp of idProps) {
|
341 | where[idProp] = id[idProp];
|
342 | }
|
343 | }
|
344 | return where;
|
345 | }
|
346 | }
|
347 | exports.Entity = Entity;
|
348 |
|
349 |
|
350 |
|
351 | class Event {
|
352 | }
|
353 | exports.Event = Event;
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 | function rejectNavigationalPropertiesInData(modelClass, data) {
|
363 | const def = modelClass.definition;
|
364 | const props = def.properties;
|
365 | for (const r in def.relations) {
|
366 | const relName = def.relations[r].name;
|
367 | if (!(relName in data))
|
368 | continue;
|
369 | let msg = 'Navigational properties are not allowed in model data ' +
|
370 | `(model "${modelClass.modelName}" property "${relName}"), ` +
|
371 | 'please remove it.';
|
372 | if (relName in props) {
|
373 | msg +=
|
374 | ' The error might be invoked by belongsTo relations, please make' +
|
375 | ' sure the relation name is not the same as the property name.';
|
376 | }
|
377 | throw new Error(msg);
|
378 | }
|
379 | }
|
380 | exports.rejectNavigationalPropertiesInData = rejectNavigationalPropertiesInData;
|
381 |
|
\ | No newline at end of file |