|
95 | 95 | this.queryCells = queryCells;
|
96 | 96 | }
|
97 | 97 |
|
98 |
| - // ----------------------------------------------------- |
99 |
| - // class methods |
100 |
| - // ----------------------------------------------------- |
101 |
| - static updateReferenceProperties(documents) { |
102 |
| - return new Promise((resolve, reject) => { |
103 |
| - var dataTable, document, documentClassName, documentClasses, documentId, i, items, j, len, len1, property, propertyName, ref, referenceProperties, tasks; |
104 |
| - /* |
105 |
| - Update reference properties of documents. |
106 |
| - @param documents {list<Document>} |
107 |
| - @returns {promise} |
108 |
| - */ |
109 |
| - if (!documents || !documents.length) { |
110 |
| - return resolve(); |
111 |
| - } |
112 |
| - dataTable = {}; // {documentClassName: {documentId: {Document}}} |
113 |
| - documentClasses = {}; // {documentClassName: documentClass} |
114 |
| - referenceProperties = []; // all reference properties in documents |
115 |
| - ref = documents[0].constructor._properties; |
116 |
| - |
117 |
| - // scan what kind of documents should be fetched |
118 |
| - for (propertyName in ref) { |
119 |
| - property = ref[propertyName]; |
120 |
| - if (property.constructor !== properties.ReferenceProperty) { |
121 |
| - continue; |
122 |
| - } |
123 |
| - if (!(property.referenceClass.name in dataTable)) { |
124 |
| - dataTable[property.referenceClass.name] = {}; |
125 |
| - documentClasses[property.referenceClass.name] = property.referenceClass; |
126 |
| - } |
127 |
| - referenceProperties.push(property); |
128 |
| - } |
129 |
| -// scan what id of documents should be fetched |
130 |
| - for (i = 0, len = documents.length; i < len; i++) { |
131 |
| - document = documents[i]; |
132 |
| -// loop all reference properties in the document |
133 |
| - for (j = 0, len1 = referenceProperties.length; j < len1; j++) { |
134 |
| - property = referenceProperties[j]; |
135 |
| - documentId = document[property.propertyName]; |
136 |
| - if (documentId) { |
137 |
| - dataTable[property.referenceClass.name][documentId] = null; |
138 |
| - } |
139 |
| - } |
140 |
| - } |
141 |
| - // fetch documents |
142 |
| - tasks = []; |
143 |
| - for (documentClassName in dataTable) { |
144 |
| - items = dataTable[documentClassName]; |
145 |
| - tasks.push((function(documentClassName, items) { |
146 |
| - return documentClasses[documentClassName].get(Object.keys(items), false).then(function(referenceDocuments) { |
147 |
| - var k, len2, referenceDocument, results; |
148 |
| - results = []; |
149 |
| - for (k = 0, len2 = referenceDocuments.length; k < len2; k++) { |
150 |
| - referenceDocument = referenceDocuments[k]; |
151 |
| - results.push(dataTable[documentClassName][referenceDocument.id] = referenceDocument); |
152 |
| - } |
153 |
| - return results; |
154 |
| - }); |
155 |
| - })(documentClassName, items)); |
156 |
| - } |
157 |
| - return Promise.all(tasks).then(function() { |
158 |
| - var k, l, len2, len3, resolveDocument; |
159 |
| -// update reference properties of documents |
160 |
| - for (k = 0, len2 = documents.length; k < len2; k++) { |
161 |
| - document = documents[k]; |
162 |
| -// loop all reference properties in the document |
163 |
| - for (l = 0, len3 = referenceProperties.length; l < len3; l++) { |
164 |
| - property = referenceProperties[l]; |
165 |
| - resolveDocument = dataTable[property.referenceClass.name][document[property.propertyName]]; |
166 |
| - if (property.required && !resolveDocument) { |
167 |
| - console.log(`There are a reference class can't mapping: ${property.referenceClass.name}::${document[property.propertyName]}`); |
168 |
| - continue; |
169 |
| - } |
170 |
| - document[property.propertyName] = resolveDocument; |
171 |
| - } |
172 |
| - } |
173 |
| - return resolve(); |
174 |
| - }).catch(function(error) { |
175 |
| - return reject(error); |
176 |
| - }); |
177 |
| - }); |
178 |
| - } |
179 |
| - |
180 | 98 | // -----------------------------------------------------
|
181 | 99 | // public methods
|
182 | 100 | // -----------------------------------------------------
|
|
414 | 332 | })();
|
415 | 333 | total = response.hits.total;
|
416 | 334 | if (args.fetchReference) {
|
417 |
| - return Query.updateReferenceProperties(items).then(function() { |
| 335 | + return utils.updateReferenceProperties(items).then(function() { |
418 | 336 | return resolve({
|
419 | 337 | items: items,
|
420 | 338 | total: total
|
|
0 commit comments