Skip to content

Commit fe0529f

Browse files
committed
Merge pull request #42 from kuzzleio/factoryRefactor
made KuzzleDataCollection.documentFactory compliant with the documentation
2 parents 0132b62 + 1a3aa27 commit fe0529f

File tree

6 files changed

+48
-32
lines changed

6 files changed

+48
-32
lines changed

dist/kuzzle.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,7 +1436,11 @@ KuzzleDataCollection.prototype.advancedSearch = function (filters, options, cb)
14361436
}
14371437

14381438
result.result.hits.forEach(function (doc) {
1439-
documents.push(self.documentFactory(doc._id, doc));
1439+
var newDocument = new KuzzleDocument(self, doc._id, doc._source);
1440+
1441+
newDocument.version = doc._version;
1442+
1443+
documents.push(newDocument);
14401444
});
14411445

14421446
cb(null, { total: result.result.total, documents: documents });
@@ -1557,14 +1561,18 @@ KuzzleDataCollection.prototype.createDocument = function (id, document, options,
15571561

15581562
if (cb) {
15591563
self.kuzzle.query(this.buildQueryArgs('write', action), data, options, function (err, res) {
1564+
var doc;
1565+
15601566
if (err) {
15611567
return cb(err);
15621568
}
15631569

1564-
cb(null, self.documentFactory(res.result._id, res.result));
1570+
doc = new KuzzleDocument(self, res.result._id, res.result._source);
1571+
doc.version = res.result._version;
1572+
cb(null, doc);
15651573
});
15661574
} else {
1567-
this.kuzzle.query(this.buildQueryArgs('write', action), data, options);
1575+
self.kuzzle.query(this.buildQueryArgs('write', action), data, options);
15681576
}
15691577

15701578
return this;
@@ -1668,11 +1676,15 @@ KuzzleDataCollection.prototype.fetchDocument = function (documentId, options, cb
16681676
data = self.kuzzle.addHeaders(data, this.headers);
16691677

16701678
self.kuzzle.query(this.buildQueryArgs('read', 'get'), data, options, function (err, res) {
1679+
var document;
1680+
16711681
if (err) {
16721682
return cb(err);
16731683
}
16741684

1675-
cb(null, self.documentFactory(res.result._id, res.result));
1685+
document = new KuzzleDocument(self, res.result._id, res.result._source);
1686+
document.version = res.result._version;
1687+
cb(null, document);
16761688
});
16771689

16781690
return this;
@@ -1801,11 +1813,15 @@ KuzzleDataCollection.prototype.replaceDocument = function (documentId, content,
18011813

18021814
if (cb) {
18031815
self.kuzzle.query(this.buildQueryArgs('write', 'createOrUpdate'), data, options, function (err, res) {
1816+
var document;
1817+
18041818
if (err) {
18051819
return cb(err);
18061820
}
18071821

1808-
cb(null, self.documentFactory(res.result._id, res.result));
1822+
document = new KuzzleDocument(self, res.result._id, res.result._source);
1823+
document.version = res.result._version;
1824+
cb(null, document);
18091825
});
18101826
} else {
18111827
self.kuzzle.query(this.buildQueryArgs('write', 'createOrUpdate'), data, options);
@@ -1916,13 +1932,7 @@ KuzzleDataCollection.prototype.updateDocument = function (documentId, content, o
19161932
* @constructor
19171933
*/
19181934
KuzzleDataCollection.prototype.documentFactory = function (id, content) {
1919-
var document = content._source ? new KuzzleDocument(this, id, content._source) : new KuzzleDocument(this, id, content);
1920-
1921-
if (content._version && content._source) {
1922-
document.version = content._version;
1923-
}
1924-
1925-
return document;
1935+
return new KuzzleDocument(this, id, content);
19261936
};
19271937

19281938
/**

dist/kuzzle.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/kuzzle.min.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "1.3.3",
3+
"version": "1.3.4",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <[email protected]>",
66
"repository": {

src/kuzzleDataCollection.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ KuzzleDataCollection.prototype.advancedSearch = function (filters, options, cb)
106106
}
107107

108108
result.result.hits.forEach(function (doc) {
109-
documents.push(self.documentFactory(doc._id, doc));
109+
var newDocument = new KuzzleDocument(self, doc._id, doc._source);
110+
111+
newDocument.version = doc._version;
112+
113+
documents.push(newDocument);
110114
});
111115

112116
cb(null, { total: result.result.total, documents: documents });
@@ -227,14 +231,18 @@ KuzzleDataCollection.prototype.createDocument = function (id, document, options,
227231

228232
if (cb) {
229233
self.kuzzle.query(this.buildQueryArgs('write', action), data, options, function (err, res) {
234+
var doc;
235+
230236
if (err) {
231237
return cb(err);
232238
}
233239

234-
cb(null, self.documentFactory(res.result._id, res.result));
240+
doc = new KuzzleDocument(self, res.result._id, res.result._source);
241+
doc.version = res.result._version;
242+
cb(null, doc);
235243
});
236244
} else {
237-
this.kuzzle.query(this.buildQueryArgs('write', action), data, options);
245+
self.kuzzle.query(this.buildQueryArgs('write', action), data, options);
238246
}
239247

240248
return this;
@@ -338,11 +346,15 @@ KuzzleDataCollection.prototype.fetchDocument = function (documentId, options, cb
338346
data = self.kuzzle.addHeaders(data, this.headers);
339347

340348
self.kuzzle.query(this.buildQueryArgs('read', 'get'), data, options, function (err, res) {
349+
var document;
350+
341351
if (err) {
342352
return cb(err);
343353
}
344354

345-
cb(null, self.documentFactory(res.result._id, res.result));
355+
document = new KuzzleDocument(self, res.result._id, res.result._source);
356+
document.version = res.result._version;
357+
cb(null, document);
346358
});
347359

348360
return this;
@@ -471,11 +483,15 @@ KuzzleDataCollection.prototype.replaceDocument = function (documentId, content,
471483

472484
if (cb) {
473485
self.kuzzle.query(this.buildQueryArgs('write', 'createOrUpdate'), data, options, function (err, res) {
486+
var document;
487+
474488
if (err) {
475489
return cb(err);
476490
}
477491

478-
cb(null, self.documentFactory(res.result._id, res.result));
492+
document = new KuzzleDocument(self, res.result._id, res.result._source);
493+
document.version = res.result._version;
494+
cb(null, document);
479495
});
480496
} else {
481497
self.kuzzle.query(this.buildQueryArgs('write', 'createOrUpdate'), data, options);
@@ -586,13 +602,7 @@ KuzzleDataCollection.prototype.updateDocument = function (documentId, content, o
586602
* @constructor
587603
*/
588604
KuzzleDataCollection.prototype.documentFactory = function (id, content) {
589-
var document = content._source ? new KuzzleDocument(this, id, content._source) : new KuzzleDocument(this, id, content);
590-
591-
if (content._version && content._source) {
592-
document.version = content._version;
593-
}
594-
595-
return document;
605+
return new KuzzleDocument(this, id, content);
596606
};
597607

598608
/**

test/kuzzleDataCollection/methods.test.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,10 +1002,6 @@ describe('KuzzleDataCollection methods', function () {
10021002
should(kuzzle.dataCollectionFactory('foo').documentFactory('foo', { foo: 'bar'})).be.instanceof(KuzzleDocument);
10031003
});
10041004

1005-
it('documentFactory should handle a _source content', function () {
1006-
should(kuzzle.dataCollectionFactory('foo').documentFactory('foo', { _source: {foo: 'bar'}})).be.instanceof(KuzzleDocument);
1007-
});
1008-
10091005
it('roomFactory should return a new KuzzleRoom object', function () {
10101006
should(kuzzle.dataCollectionFactory('foo').roomFactory()).be.instanceof(KuzzleRoom);
10111007
});

0 commit comments

Comments
 (0)