Skip to content

Commit 46f0295

Browse files
committed
Moved Document.updateReferenceProperties() to utils.updateReferenceProperties().
1 parent 7716472 commit 46f0295

File tree

9 files changed

+180
-147
lines changed

9 files changed

+180
-147
lines changed

__tests__/__snapshots__/utils.js.snap

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
exports[`Bleach regex words. 1`] = `"\\\\^\\\\$\\\\*\\\\+\\\\?\\\\{\\\\}\\\\.\\\\[\\\\]\\\\(\\\\)\\\\\\\\hello\\\\|\\\\/"`;
44

5+
exports[`Fetch the reference property of the document. 1`] = `
6+
ArticleModel {
7+
"content": null,
8+
"id": null,
9+
"user": UserModel {
10+
"id": "AWiYXbY_SjjuUM2b1CGI",
11+
"name": "enju",
12+
"version": null,
13+
},
14+
"version": null,
15+
}
16+
`;
17+
518
exports[`Get elasticsearch client. 1`] = `
619
Object {
720
"apiVersion": "2.4",

__tests__/document.coffee

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ generateDataModel = ->
2727

2828
beforeEach ->
2929
DataModel = generateDataModel()
30-
Query.updateReferenceProperties.mockClear?()
30+
utils.updateReferenceProperties.mockClear?()
3131
utils.getIndexPrefix.mockClear?()
3232

3333
test 'Define model.', ->
@@ -129,11 +129,11 @@ test 'Get the document by id with reference.', ->
129129
_version: 0
130130
_source:
131131
name: 'enju'
132-
Query.updateReferenceProperties = jest.fn (documents) -> new Promise (resolve) ->
132+
utils.updateReferenceProperties = jest.fn (documents) -> new Promise (resolve) ->
133133
expect(documents).toMatchSnapshot()
134134
resolve()
135135
DataModel.get('id').then ->
136-
expect(Query.updateReferenceProperties).toBeCalled()
136+
expect(utils.updateReferenceProperties).toBeCalled()
137137

138138
test 'Get documents by ids without reference.', ->
139139
class DataModel extends enju.Document
@@ -168,11 +168,11 @@ test 'Get documents by ids with reference.', ->
168168
_source:
169169
name: 'enju'
170170
]
171-
Query.updateReferenceProperties = jest.fn (documents) -> new Promise (resolve) ->
171+
utils.updateReferenceProperties = jest.fn (documents) -> new Promise (resolve) ->
172172
expect(documents).toMatchSnapshot()
173173
resolve()
174174
DataModel.get(['id']).then ->
175-
expect(Query.updateReferenceProperties).toBeCalled()
175+
expect(utils.updateReferenceProperties).toBeCalled()
176176

177177
test 'Is the document exists.', ->
178178
class DataModel extends enju.Document

__tests__/utils.coffee

+25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
config = require 'config'
22
elasticsearch = require 'elasticsearch'
33
utils = require '../lib/utils'
4+
enju = require '../'
45

56

67
jest.mock 'elasticsearch'
@@ -23,3 +24,27 @@ test 'Get index prefix.', ->
2324
test 'Bleach regex words.', ->
2425
result = utils.bleachRegexWords '^$*+?{}.[]()\\hello|/'
2526
expect(result).toMatchSnapshot()
27+
28+
test 'Fetch the reference property of the document.', ->
29+
class UserModel extends enju.Document
30+
@_index = 'users'
31+
@define
32+
name: new enju.StringProperty()
33+
class ArticleModel extends enju.Document
34+
@_index = 'articles'
35+
@define
36+
content: new enju.StringProperty()
37+
user: new enju.ReferenceProperty
38+
referenceClass: UserModel
39+
UserModel.get = jest.fn (ids) -> new Promise (resolve) ->
40+
expect(ids).toEqual ['AWiYXbY_SjjuUM2b1CGI']
41+
resolve [
42+
new UserModel
43+
id: 'AWiYXbY_SjjuUM2b1CGI'
44+
name: 'enju'
45+
]
46+
article = new ArticleModel
47+
user: 'AWiYXbY_SjjuUM2b1CGI'
48+
utils.updateReferenceProperties([article]).then ->
49+
expect(UserModel.get).toBeCalled()
50+
expect(article).toMatchSnapshot()

lib/document.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178
}
179179
// call resolve()
180180
if (fetchReference) {
181-
return Query.updateReferenceProperties(result).then(function() {
181+
return utils.updateReferenceProperties(result).then(function() {
182182
return resolve(result);
183183
}).catch(function(error) {
184184
return reject(error);
@@ -217,7 +217,7 @@
217217
// call resolve()
218218
document = new this(args);
219219
if (fetchReference) {
220-
return Query.updateReferenceProperties([document]).then(function() {
220+
return utils.updateReferenceProperties([document]).then(function() {
221221
return resolve(document);
222222
}).catch(function(error) {
223223
return reject(error);

lib/query.js

+1-83
Original file line numberDiff line numberDiff line change
@@ -95,88 +95,6 @@
9595
this.queryCells = queryCells;
9696
}
9797

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-
18098
// -----------------------------------------------------
18199
// public methods
182100
// -----------------------------------------------------
@@ -414,7 +332,7 @@
414332
})();
415333
total = response.hits.total;
416334
if (args.fetchReference) {
417-
return Query.updateReferenceProperties(items).then(function() {
335+
return utils.updateReferenceProperties(items).then(function() {
418336
return resolve({
419337
items: items,
420338
total: total

lib/utils.js

+81-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(function() {
2-
var config, elasticsearch, util,
2+
var config, elasticsearch, properties, util,
33
indexOf = [].indexOf;
44

55
util = require('util');
@@ -8,6 +8,8 @@
88

99
elasticsearch = require('elasticsearch');
1010

11+
properties = require('./properties');
12+
1113
module.exports = {
1214
getElasticsearch: function() {
1315
/*
@@ -38,6 +40,84 @@
3840
}
3941
}
4042
return result.join('');
43+
},
44+
updateReferenceProperties: function(documents) {
45+
return new Promise(function(resolve, reject) {
46+
var dataTable, document, documentClassName, documentClasses, documentId, i, items, j, len, len1, property, propertyName, ref, referenceProperties, tasks;
47+
/*
48+
Fetch reference properties of documents.
49+
@param documents {list<Document>}
50+
@returns {promise} The data will direct apply on the arguments.
51+
*/
52+
if (!documents || !documents.length) {
53+
return resolve();
54+
}
55+
dataTable = {}; // {documentClassName: {documentId: {Document}}}
56+
documentClasses = {}; // {documentClassName: documentClass}
57+
referenceProperties = []; // all reference properties in documents
58+
ref = documents[0].constructor._properties;
59+
60+
// scan what kind of documents should be fetched
61+
for (propertyName in ref) {
62+
property = ref[propertyName];
63+
if (property.constructor !== properties.ReferenceProperty) {
64+
continue;
65+
}
66+
if (!(property.referenceClass.name in dataTable)) {
67+
dataTable[property.referenceClass.name] = {};
68+
documentClasses[property.referenceClass.name] = property.referenceClass;
69+
}
70+
referenceProperties.push(property);
71+
}
72+
// scan what id of documents should be fetched
73+
for (i = 0, len = documents.length; i < len; i++) {
74+
document = documents[i];
75+
// loop all reference properties in the document
76+
for (j = 0, len1 = referenceProperties.length; j < len1; j++) {
77+
property = referenceProperties[j];
78+
documentId = document[property.propertyName];
79+
if (documentId) {
80+
dataTable[property.referenceClass.name][documentId] = null;
81+
}
82+
}
83+
}
84+
// fetch documents
85+
tasks = [];
86+
for (documentClassName in dataTable) {
87+
items = dataTable[documentClassName];
88+
tasks.push((function(documentClassName, items) {
89+
return documentClasses[documentClassName].get(Object.keys(items), false).then(function(referenceDocuments) {
90+
var k, len2, referenceDocument, results;
91+
results = [];
92+
for (k = 0, len2 = referenceDocuments.length; k < len2; k++) {
93+
referenceDocument = referenceDocuments[k];
94+
results.push(dataTable[documentClassName][referenceDocument.id] = referenceDocument);
95+
}
96+
return results;
97+
});
98+
})(documentClassName, items));
99+
}
100+
return Promise.all(tasks).then(function() {
101+
var k, l, len2, len3, resolveDocument;
102+
// update reference properties of documents
103+
for (k = 0, len2 = documents.length; k < len2; k++) {
104+
document = documents[k];
105+
// loop all reference properties in the document
106+
for (l = 0, len3 = referenceProperties.length; l < len3; l++) {
107+
property = referenceProperties[l];
108+
resolveDocument = dataTable[property.referenceClass.name][document[property.propertyName]];
109+
if (property.required && !resolveDocument) {
110+
console.log(`There are a reference class can't mapping: ${property.referenceClass.name}::${document[property.propertyName]}`);
111+
continue;
112+
}
113+
document[property.propertyName] = resolveDocument;
114+
}
115+
}
116+
return resolve();
117+
}).catch(function(error) {
118+
return reject(error);
119+
});
120+
});
41121
}
42122
};
43123

src/lib/document.coffee

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ module.exports = class Document
119119

120120
# call resolve()
121121
if fetchReference
122-
Query.updateReferenceProperties(result).then ->
122+
utils.updateReferenceProperties(result).then ->
123123
resolve result
124124
.catch (error) ->
125125
reject error
@@ -148,7 +148,7 @@ module.exports = class Document
148148
# call resolve()
149149
document = new @(args)
150150
if fetchReference
151-
Query.updateReferenceProperties([document]).then ->
151+
utils.updateReferenceProperties([document]).then ->
152152
resolve document
153153
.catch (error) ->
154154
reject error

src/lib/query.coffee

+1-54
Original file line numberDiff line numberDiff line change
@@ -64,59 +64,6 @@ module.exports = class Query
6464
@queryCells = queryCells
6565

6666

67-
# -----------------------------------------------------
68-
# class methods
69-
# -----------------------------------------------------
70-
@updateReferenceProperties = (documents) -> new Promise (resolve, reject) =>
71-
###
72-
Update reference properties of documents.
73-
@param documents {list<Document>}
74-
@returns {promise}
75-
###
76-
if not documents or not documents.length
77-
return resolve()
78-
79-
dataTable = {} # {documentClassName: {documentId: {Document}}}
80-
documentClasses = {} # {documentClassName: documentClass}
81-
referenceProperties = [] # all reference properties in documents
82-
83-
# scan what kind of documents should be fetched
84-
for propertyName, property of documents[0].constructor._properties
85-
if property.constructor isnt properties.ReferenceProperty
86-
continue
87-
if property.referenceClass.name not of dataTable
88-
dataTable[property.referenceClass.name] = {}
89-
documentClasses[property.referenceClass.name] = property.referenceClass
90-
referenceProperties.push property
91-
92-
# scan what id of documents should be fetched
93-
for document in documents
94-
for property in referenceProperties # loop all reference properties in the document
95-
documentId = document[property.propertyName]
96-
if documentId
97-
dataTable[property.referenceClass.name][documentId] = null
98-
99-
# fetch documents
100-
tasks = []
101-
for documentClassName, items of dataTable
102-
tasks.push do (documentClassName, items) ->
103-
documentClasses[documentClassName].get(Object.keys(items), no).then (referenceDocuments) ->
104-
for referenceDocument in referenceDocuments
105-
dataTable[documentClassName][referenceDocument.id] = referenceDocument
106-
Promise.all(tasks).then ->
107-
# update reference properties of documents
108-
for document in documents
109-
for property in referenceProperties # loop all reference properties in the document
110-
resolveDocument = dataTable[property.referenceClass.name][document[property.propertyName]]
111-
if property.required and not resolveDocument
112-
console.log "There are a reference class can't mapping: #{property.referenceClass.name}::#{document[property.propertyName]}"
113-
continue
114-
document[property.propertyName] = resolveDocument
115-
resolve()
116-
.catch (error) ->
117-
reject error
118-
119-
12067
# -----------------------------------------------------
12168
# public methods
12269
# -----------------------------------------------------
@@ -295,7 +242,7 @@ module.exports = class Query
295242
result
296243
total = response.hits.total
297244
if args.fetchReference
298-
Query.updateReferenceProperties(items).then ->
245+
utils.updateReferenceProperties(items).then ->
299246
resolve
300247
items: items
301248
total: total

0 commit comments

Comments
 (0)