Skip to content

Commit 02f71e8

Browse files
authored
Merge pull request #200 from kuzzleio/18-kuzzle-info
Add kuzzle info in Document object
2 parents b1d2eb0 + d30617a commit 02f71e8

File tree

5 files changed

+38
-9
lines changed

5 files changed

+38
-9
lines changed

src/Collection.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ Collection.prototype.createDocument = function (id, document, options, cb) {
193193
return cb(err);
194194
}
195195

196-
doc = new Document(self, res.result._id, res.result._source);
196+
doc = new Document(self, res.result._id, res.result._source, res.result._meta);
197197
doc.version = res.result._version;
198198
cb(null, doc);
199199
});
@@ -276,7 +276,7 @@ Collection.prototype.fetchDocument = function (documentId, options, cb) {
276276
return cb(err);
277277
}
278278

279-
document = new Document(self, res.result._id, res.result._source);
279+
document = new Document(self, res.result._id, res.result._source, res.result._meta);
280280
document.version = res.result._version;
281281
cb(null, document);
282282
});
@@ -419,7 +419,7 @@ Collection.prototype.replaceDocument = function (documentId, content, options, c
419419
return cb(err);
420420
}
421421

422-
document = new Document(self, res.result._id, res.result._source);
422+
document = new Document(self, res.result._id, res.result._source, res.result._meta);
423423
document.version = res.result._version;
424424
cb(null, document);
425425
});
@@ -462,7 +462,7 @@ Collection.prototype.search = function (filters, options, cb) {
462462
}
463463

464464
result.result.hits.forEach(function (doc) {
465-
var newDocument = new Document(self, doc._id, doc._source);
465+
var newDocument = new Document(self, doc._id, doc._source, doc._meta);
466466

467467
newDocument.version = doc._version;
468468

@@ -531,7 +531,7 @@ Collection.prototype.scroll = function (scrollId, options, filters, cb) {
531531
}
532532

533533
result.result.hits.forEach(function (doc) {
534-
var newDocument = new Document(self, doc._id, doc._source);
534+
var newDocument = new Document(self, doc._id, doc._source, doc._meta);
535535

536536
newDocument.version = doc._version;
537537

src/Document.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
* @param {Collection} collection - an instanciated Collection object
1919
* @param {string} [documentId] - ID of an existing document
2020
* @param {object} [content] - Initializes this document with the provided content
21+
* @param {object} [meta] - Initializes this document with the provided meta
2122
* @constructor
2223
*/
23-
function Document(collection, documentId, content) {
24+
function Document(collection, documentId, content, meta) {
2425
Object.defineProperties(this, {
2526
// read-only properties
2627
collection: {
@@ -55,6 +56,11 @@ function Document(collection, documentId, content) {
5556
value: undefined,
5657
enumerable: true,
5758
writable: true
59+
},
60+
meta: {
61+
value: meta || {},
62+
enumerable: true,
63+
writable: false
5864
}
5965
});
6066

@@ -108,6 +114,7 @@ Document.prototype.serialize = function () {
108114
}
109115

110116
data.body = this.content;
117+
data.meta = this.meta;
111118
data._version = this.version;
112119
data = this.kuzzle.addHeaders(data, this.headers);
113120

@@ -179,7 +186,7 @@ Document.prototype.refresh = function (options, cb) {
179186
return cb(error);
180187
}
181188

182-
newDocument = new Document(self.dataCollection, self.id, res.result._source);
189+
newDocument = new Document(self.dataCollection, self.id, res.result._source, res.result._meta);
183190
newDocument.version = res.result._version;
184191

185192
cb(null, newDocument);

src/Room.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ function notificationCallback (data) {
322322

323323
if (data.controller === 'document' || (data.controller === 'realtime' && data.action === 'publish')) {
324324
data.type = 'document';
325-
data.document = new Document(this.collection, data.result._id, data.result._source);
325+
data.document = new Document(this.collection, data.result._id, data.result._source, data.result._meta);
326326
delete data.result;
327327
}
328328
else if (data.controller === 'realtime') {

test/Collection/methods.test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,16 @@ describe('Collection methods', function () {
5656
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});
5757
kuzzle.query = queryStub;
5858
emitted = false;
59-
result = { result: { _scroll_id: 'banana', total: 123, hits: [ {_id: 'foobar', _source: { foo: 'bar'}} ], aggregations: {someAggregate: {}}}};
59+
result = {
60+
result: {
61+
_scroll_id: 'banana',
62+
total: 123,
63+
hits: [
64+
{_id: 'foobar', _source: { foo: 'bar'}, _meta: {author: 'toto'}}
65+
],
66+
aggregations: {someAggregate: {}}
67+
}
68+
};
6069
error = null;
6170
expectedQuery = {
6271
index: 'bar',

test/Document/constructor.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,41 @@ describe('Document constructor', function () {
3434
should(refreshed).be.false();
3535
should(document.id).be.undefined();
3636
should(document.content).be.empty();
37+
should(document.meta).be.empty();
3738
should(document.version).be.undefined();
3839
should(document.collection).be.exactly('foo');
3940

4041
document = new Document(collection, { some: 'content' });
4142
should(refreshed).be.false();
4243
should(document.id).be.undefined();
4344
should(document.content).match({some: 'content'});
45+
should(document.meta).be.empty();
4446
should(document.version).be.undefined();
4547
should(document.collection).be.exactly('foo');
4648

4749
document = new Document(collection, 'id', { some: 'content', _version: 123 });
4850
should(refreshed).be.false();
4951
should(document.id).be.exactly('id');
5052
should(document.content).match({some: 'content'});
53+
should(document.meta).be.empty();
5154
should(document.version).be.exactly(123);
5255
should(document.collection).be.exactly('foo');
5356

5457
document = new Document(collection, 'id');
5558
should(refreshed).be.false();
5659
should(document.id).be.exactly('id');
5760
should(document.content).be.empty();
61+
should(document.meta).be.empty();
5862
should(document.version).be.undefined();
5963
should(document.collection).be.exactly('foo');
64+
65+
document = new Document(collection, 'id', { some: 'content', _version: 123 }, {author: 'toto'});
66+
should(refreshed).be.false();
67+
should(document.id).be.exactly('id');
68+
should(document.content).match({some: 'content'});
69+
should(document.meta).match({author: 'toto'});
70+
should(document.version).be.exactly(123);
71+
should(document.collection).be.exactly('foo');
6072
});
6173

6274
it('should expose documented properties with the right permissions', function () {
@@ -67,6 +79,7 @@ describe('Document constructor', function () {
6779
should(document).have.propertyWithDescriptor('headers', { enumerable: true, writable: true, configurable: false });
6880
should(document).have.propertyWithDescriptor('id', { enumerable: true, writable: true, configurable: false });
6981
should(document).have.propertyWithDescriptor('version', { enumerable: true, writable: true, configurable: false });
82+
should(document).have.propertyWithDescriptor('meta', { enumerable: true, writable: false, configurable: false });
7083
});
7184

7285
it('should promisify the right functions', function () {

0 commit comments

Comments
 (0)