Skip to content

Commit 05a6c78

Browse files
committed
added "version" property in KuzzleDocument
1 parent 7bfb3db commit 05a6c78

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

src/kuzzleDataCollection.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ KuzzleDataCollection.prototype.advancedSearch = function (filters, cb) {
7272
}
7373

7474
result.hits.hits.forEach(function (doc) {
75-
documents.push(new KuzzleDocument(self, doc._id, doc._source));
75+
documents.push(this.documentFactory(doc._id, doc));
7676
});
7777

7878
cb(null, { total: result.hits.total, documents: documents });
@@ -155,7 +155,7 @@ KuzzleDataCollection.prototype.createDocument = function (document, options, cb)
155155
return cb(err);
156156
}
157157

158-
cb(null, new KuzzleDocument(self, res._id, res._source));
158+
cb(null, self.documentFactory(res._id, res));
159159
});
160160
} else {
161161
this.kuzzle.query(this.collection, 'write', action, data, options);
@@ -239,7 +239,7 @@ KuzzleDataCollection.prototype.fetchDocument = function (documentId, cb) {
239239
return cb(err);
240240
}
241241

242-
cb(null, new KuzzleDocument(self, res._id, res._source));
242+
cb(null, self.documentFactory(res._id, res));
243243
});
244244

245245
return this;
@@ -338,7 +338,7 @@ KuzzleDataCollection.prototype.replaceDocument = function (documentId, content,
338338
return cb(err);
339339
}
340340

341-
cb(null, new KuzzleDocument(self, res._id, res._source));
341+
cb(null, self.documentFactory(res._id, res));
342342
});
343343
} else {
344344
self.kuzzle.query(this.collection, 'write', 'createOrUpdate', data, options);
@@ -417,7 +417,13 @@ KuzzleDataCollection.prototype.updateDocument = function (documentId, content, o
417417
* @constructor
418418
*/
419419
KuzzleDataCollection.prototype.documentFactory = function (id, content) {
420-
return new KuzzleDocument(this, id, content);
420+
var document = content._source ? new KuzzleDocument(this, id, content._source) : new KuzzleDocument(this, id, content);
421+
422+
if (content._version) {
423+
document.version = content._version;
424+
}
425+
426+
return document;
421427
};
422428

423429
/**

src/kuzzleDocument.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,19 @@ function KuzzleDocument(kuzzleDataCollection, documentId, content) {
5555
},
5656
// writable properties
5757
content: {
58-
enumerable: true,
59-
set: function (data) {
60-
if (!content) {
61-
content = {};
62-
}
63-
64-
return this.setContent(data, false);
65-
},
66-
get: function () {
67-
return content;
68-
}
58+
value: {},
59+
writable: true,
60+
enumerable: true
6961
},
7062
headers: {
7163
value: JSON.parse(JSON.stringify(kuzzleDataCollection.headers)),
7264
enumerable: true,
7365
writable: true
66+
},
67+
version: {
68+
value: undefined,
69+
enumerable: true,
70+
writable: true
7471
}
7572
});
7673

@@ -117,6 +114,7 @@ KuzzleDocument.prototype.toJSON = function () {
117114
}
118115

119116
data.body = this.content;
117+
data._version = this.version;
120118
data = this.kuzzle.addHeaders(data, this.headers);
121119

122120
return data;
@@ -195,18 +193,21 @@ KuzzleDocument.prototype.refresh = function (cb) {
195193
self.refreshing = true;
196194

197195
self.kuzzle.query(self.collection, 'read', 'get', {_id: self.id}, function (error, result) {
198-
self.refreshing = false;
199-
dequeue.call(self);
200-
201196
if (error) {
197+
self.refreshing = false;
198+
self.queue = [];
202199
return cb ? cb(error) : false;
203200
}
204201

202+
self.version = result._version;
205203
self.content = result._source;
206204

207205
if (cb) {
208206
cb(null, self);
209207
}
208+
209+
self.refreshing = false;
210+
dequeue.call(self);
210211
});
211212

212213
return this;

0 commit comments

Comments
 (0)