1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.PopulateDocuments = exports.PopulateDocument = void 0;
|
4 | const utils = require("./utils");
|
5 | function PopulateDocument(settings, callback, internalSettings) {
|
6 | if (typeof settings === "function") {
|
7 | callback = settings;
|
8 | settings = {};
|
9 | }
|
10 | if (!internalSettings) {
|
11 | internalSettings = {};
|
12 | }
|
13 | const { model } = this;
|
14 | const localSettings = settings;
|
15 | const promise = model.schemaForObject(this).then((schema) => {
|
16 |
|
17 | const modelAttributes = utils.array_flatten(schema.attributes().map((prop) => ({ prop, "details": schema.getAttributeTypeDetails(prop) }))).filter((obj) => Array.isArray(obj.details) ? obj.details.some((detail) => detail.name === "Model" ) : obj.details.name === "Model" || obj.details.name === "Model Set").map((obj) => obj.prop);
|
18 | return { schema, modelAttributes };
|
19 | }).then((obj) => {
|
20 | const { schema, modelAttributes } = obj;
|
21 | return Promise.all(modelAttributes.map(async (prop) => {
|
22 | const typeDetails = schema.getAttributeTypeDetails(prop);
|
23 | const typeDetail = Array.isArray(typeDetails) ? typeDetails.find((detail) => detail.name === "Model") : typeDetails;
|
24 | const { typeSettings } = typeDetail;
|
25 |
|
26 | const subModel = typeof typeSettings.model === "object" ? model.Document : typeSettings.model;
|
27 | prop = prop.endsWith(".0") ? prop.substring(0, prop.length - 2) : prop;
|
28 | const documentPropValue = utils.object.get(this, prop);
|
29 | const doesPopulatePropertyExist = !(typeof documentPropValue === "undefined" || documentPropValue === null);
|
30 | if (!doesPopulatePropertyExist || documentPropValue instanceof subModel) {
|
31 | return;
|
32 | }
|
33 | const key = [internalSettings.parentKey, prop].filter((a) => Boolean(a)).join(".");
|
34 | const populatePropertiesExists = typeof (localSettings === null || localSettings === void 0 ? void 0 : localSettings.properties) !== "undefined" && localSettings.properties !== null;
|
35 | const populateProperties = Array.isArray(localSettings === null || localSettings === void 0 ? void 0 : localSettings.properties) || typeof (localSettings === null || localSettings === void 0 ? void 0 : localSettings.properties) === "boolean" ? localSettings.properties : [localSettings === null || localSettings === void 0 ? void 0 : localSettings.properties];
|
36 | const isPopulatePropertyInSettingProperties = populatePropertiesExists ? utils.dynamoose.wildcard_allowed_check(populateProperties, key) : true;
|
37 | if (!isPopulatePropertyInSettingProperties) {
|
38 | return;
|
39 | }
|
40 | const isArray = Array.isArray(documentPropValue);
|
41 | const isSet = documentPropValue instanceof Set;
|
42 | if (isArray || isSet) {
|
43 | const subDocuments = await Promise.all([...documentPropValue].map((val) => subModel.get(val)));
|
44 | const saveDocuments = await Promise.all(subDocuments.map((doc) => PopulateDocument.bind(doc)(localSettings, null, { "parentKey": key })));
|
45 | utils.object.set(this, prop, saveDocuments);
|
46 | }
|
47 | else {
|
48 | const subDocument = await subModel.get(documentPropValue);
|
49 | const saveDocument = await PopulateDocument.bind(subDocument)(localSettings, null, { "parentKey": key });
|
50 | utils.object.set(this, prop, saveDocument);
|
51 | }
|
52 | }));
|
53 | });
|
54 | if (callback) {
|
55 | promise.then(() => callback(null, this)).catch((err) => callback(err));
|
56 | }
|
57 | else {
|
58 | return (async () => {
|
59 | await promise;
|
60 | return this;
|
61 | })();
|
62 | }
|
63 | }
|
64 | exports.PopulateDocument = PopulateDocument;
|
65 | function PopulateDocuments(settings, callback) {
|
66 | if (typeof settings === "function") {
|
67 | callback = settings;
|
68 | settings = {};
|
69 | }
|
70 | const promise = Promise.all(this.map(async (document, index) => {
|
71 | this[index] = await PopulateDocument.bind(document)(settings);
|
72 | }));
|
73 | if (callback) {
|
74 | promise.then(() => callback(null, this)).catch((err) => callback(err));
|
75 | }
|
76 | else {
|
77 | return (async () => {
|
78 | await promise;
|
79 | return this;
|
80 | })();
|
81 | }
|
82 | }
|
83 | exports.PopulateDocuments = PopulateDocuments;
|
84 |
|
\ | No newline at end of file |