UNPKG

2.95 kBJavaScriptView Raw
1"use strict";
2require("source-map-support/register");
3const Model_1 = require("./Model");
4const Schema_1 = require("./Schema");
5const Condition_1 = require("./Condition");
6const Transaction_1 = require("./Transaction");
7const aws = require("./aws");
8const Internal = require("./Internal");
9const utils = require("./utils");
10const logger = require("./logger");
11const ModelStore = require("./ModelStore");
12const model = (name, schema, options = {}) => {
13 let model;
14 let storedSchema;
15 if (name) {
16 storedSchema = ModelStore(name);
17 }
18 // TODO: this is something I'd like to do. But is a breaking change. Need to enable this and uncomment it in a breaking release. Also will need to fix the tests as well.
19 /* if (schema && storedSchema) {
20 throw new CustomError.InvalidParameter(`Model with name ${name} has already been registered.`);
21 } else */
22 if (!schema && storedSchema) {
23 model = storedSchema;
24 }
25 else {
26 model = new Model_1.Model(name, schema, options);
27 }
28 const returnObject = model.Document;
29 const keys = utils.array_flatten([
30 Object.keys(model),
31 Object.keys(Object.getPrototypeOf(model)),
32 Object.getOwnPropertyNames(Object.getPrototypeOf(model))
33 ]).filter((key) => !["constructor", "name"].includes(key));
34 keys.forEach((key) => {
35 if (typeof model[key] === "object") {
36 const main = (key) => {
37 utils.object.set(returnObject, key, {});
38 const value = utils.object.get(model, key);
39 if (value === null || value.constructor !== Object && value.constructor !== Array) {
40 utils.object.set(returnObject, key, value);
41 }
42 else {
43 Object.keys(value).forEach((subKey) => {
44 const newKey = `${key}.${subKey}`;
45 const subValue = utils.object.get(model, newKey);
46 if (typeof subValue === "object") {
47 main(newKey);
48 }
49 else {
50 utils.object.set(returnObject, newKey, typeof subValue === "function" ? subValue.bind(model) : subValue);
51 }
52 });
53 }
54 };
55 main(key);
56 }
57 else if (typeof model[key] === "function") {
58 returnObject[key] = model[key].bind(model);
59 }
60 else {
61 returnObject[key] = model[key];
62 }
63 });
64 return returnObject;
65};
66model.defaults = Object.assign({}, require("./Model/defaults").custom);
67module.exports = {
68 model,
69 Schema: Schema_1.Schema,
70 Condition: Condition_1.Condition,
71 transaction: Transaction_1.default,
72 aws,
73 logger,
74 "UNDEFINED": Internal.Public.undefined,
75 "THIS": Internal.Public.this,
76 "NULL": Internal.Public.null
77};
78//# sourceMappingURL=index.js.map
\No newline at end of file