1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | exports.PopulateDocuments = exports.PopulateDocument = void 0;
|
13 | const utils = require("./utils");
|
14 | function PopulateDocument(settings, callback, internalSettings) {
|
15 | if (typeof settings === "function") {
|
16 | callback = settings;
|
17 | settings = {};
|
18 | }
|
19 | if (!internalSettings) {
|
20 | internalSettings = {};
|
21 | }
|
22 | const { model } = this;
|
23 | const localSettings = settings;
|
24 | const promise = model.schemaForObject(this).then((schema) => {
|
25 | 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").map((obj) => obj.prop);
|
26 | return { schema, modelAttributes };
|
27 | }).then((obj) => {
|
28 | const { schema, modelAttributes } = obj;
|
29 | return Promise.all(modelAttributes.map((prop) => __awaiter(this, void 0, void 0, function* () {
|
30 | const typeDetails = schema.getAttributeTypeDetails(prop);
|
31 | const typeDetail = Array.isArray(typeDetails) ? typeDetails.find((detail) => detail.name === "Model") : typeDetails;
|
32 | const { typeSettings } = typeDetail;
|
33 |
|
34 | const subModel = typeof typeSettings.model === "object" ? model.Document : typeSettings.model;
|
35 | const doesPopulatePropertyExist = !(typeof this[prop] === "undefined" || this[prop] === null);
|
36 | if (!doesPopulatePropertyExist || this[prop] instanceof subModel) {
|
37 | return;
|
38 | }
|
39 | const key = [internalSettings.parentKey, prop].filter((a) => Boolean(a)).join(".");
|
40 | const populatePropertiesExists = typeof (localSettings === null || localSettings === void 0 ? void 0 : localSettings.properties) !== "undefined" && localSettings.properties !== null;
|
41 | 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];
|
42 | const isPopulatePropertyInSettingProperties = populatePropertiesExists ? utils.dynamoose.wildcard_allowed_check(populateProperties, key) : true;
|
43 | if (!isPopulatePropertyInSettingProperties) {
|
44 | return;
|
45 | }
|
46 | const subDocument = yield subModel.get(this[prop]);
|
47 | const saveDocument = yield PopulateDocument.bind(subDocument)(localSettings, null, { "parentKey": key });
|
48 | this[prop] = saveDocument;
|
49 | })));
|
50 | });
|
51 | if (callback) {
|
52 | promise.then(() => callback(null, this)).catch((err) => callback(err));
|
53 | }
|
54 | else {
|
55 | return (() => __awaiter(this, void 0, void 0, function* () {
|
56 | yield promise;
|
57 | return this;
|
58 | }))();
|
59 | }
|
60 | }
|
61 | exports.PopulateDocument = PopulateDocument;
|
62 | function PopulateDocuments(settings, callback) {
|
63 | if (typeof settings === "function") {
|
64 | callback = settings;
|
65 | settings = {};
|
66 | }
|
67 | const promise = Promise.all(this.map((document, index) => __awaiter(this, void 0, void 0, function* () {
|
68 | this[index] = yield PopulateDocument.bind(document)(settings);
|
69 | })));
|
70 | if (callback) {
|
71 | promise.then(() => callback(null, this)).catch((err) => callback(err));
|
72 | }
|
73 | else {
|
74 | return (() => __awaiter(this, void 0, void 0, function* () {
|
75 | yield promise;
|
76 | return this;
|
77 | }))();
|
78 | }
|
79 | }
|
80 | exports.PopulateDocuments = PopulateDocuments;
|
81 |
|
\ | No newline at end of file |