Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ Collection.prototype.createDocument = function (id, document, options, cb) {
return cb(err);
}

doc = new Document(self, res.result._id, res.result._source);
doc = new Document(self, res.result._id, res.result._source, res.result._kuzzle_info);
doc.version = res.result._version;
cb(null, doc);
});
Expand Down Expand Up @@ -276,7 +276,7 @@ Collection.prototype.fetchDocument = function (documentId, options, cb) {
return cb(err);
}

document = new Document(self, res.result._id, res.result._source);
document = new Document(self, res.result._id, res.result._source, res.result._kuzzle_info);
document.version = res.result._version;
cb(null, document);
});
Expand Down Expand Up @@ -419,7 +419,7 @@ Collection.prototype.replaceDocument = function (documentId, content, options, c
return cb(err);
}

document = new Document(self, res.result._id, res.result._source);
document = new Document(self, res.result._id, res.result._source, res.result._kuzzle_info);
document.version = res.result._version;
cb(null, document);
});
Expand Down Expand Up @@ -462,7 +462,7 @@ Collection.prototype.search = function (filters, options, cb) {
}

result.result.hits.forEach(function (doc) {
var newDocument = new Document(self, doc._id, doc._source);
var newDocument = new Document(self, doc._id, doc._source, doc._kuzzle_info);

newDocument.version = doc._version;

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

result.result.hits.forEach(function (doc) {
var newDocument = new Document(self, doc._id, doc._source);
var newDocument = new Document(self, doc._id, doc._source, doc._kuzzle_info);

newDocument.version = doc._version;

Expand Down
11 changes: 9 additions & 2 deletions src/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
* @param {Collection} collection - an instanciated Collection object
* @param {string} [documentId] - ID of an existing document
* @param {object} [content] - Initializes this document with the provided content
* @param {object} [kuzzleInfo] - Initializes this document with the provided _kuzzle_info
* @constructor
*/
function Document(collection, documentId, content) {
function Document(collection, documentId, content, kuzzleInfo) {
Object.defineProperties(this, {
// read-only properties
collection: {
Expand Down Expand Up @@ -55,6 +56,11 @@ function Document(collection, documentId, content) {
value: undefined,
enumerable: true,
writable: true
},
kuzzleInfo: {
value: kuzzleInfo || {},
enumerable: true,
writable: false
}
});

Expand Down Expand Up @@ -108,6 +114,7 @@ Document.prototype.serialize = function () {
}

data.body = this.content;
data.kuzzleInfo = this.kuzzleInfo;
data._version = this.version;
data = this.kuzzle.addHeaders(data, this.headers);

Expand Down Expand Up @@ -179,7 +186,7 @@ Document.prototype.refresh = function (options, cb) {
return cb(error);
}

newDocument = new Document(self.dataCollection, self.id, res.result._source);
newDocument = new Document(self.dataCollection, self.id, res.result._source, res.result._kuzzle_info);
newDocument.version = res.result._version;

cb(null, newDocument);
Expand Down
2 changes: 1 addition & 1 deletion src/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ function notificationCallback (data) {

if (data.controller === 'document' || (data.controller === 'realtime' && data.action === 'publish')) {
data.type = 'document';
data.document = new Document(this.collection, data.result._id, data.result._source);
data.document = new Document(this.collection, data.result._id, data.result._source, data.result._kuzzle_info);
delete data.result;
}
else if (data.controller === 'realtime') {
Expand Down
11 changes: 10 additions & 1 deletion test/Collection/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,16 @@ describe('Collection methods', function () {
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});
kuzzle.query = queryStub;
emitted = false;
result = { result: { _scroll_id: 'banana', total: 123, hits: [ {_id: 'foobar', _source: { foo: 'bar'}} ], aggregations: {someAggregate: {}}}};
result = {
result: {
_scroll_id: 'banana',
total: 123,
hits: [
{_id: 'foobar', _source: { foo: 'bar'}, _kuzzle_info: {author: 'toto'}}
],
aggregations: {someAggregate: {}}
}
};
error = null;
expectedQuery = {
index: 'bar',
Expand Down
13 changes: 13 additions & 0 deletions test/Document/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,41 @@ describe('Document constructor', function () {
should(refreshed).be.false();
should(document.id).be.undefined();
should(document.content).be.empty();
should(document.kuzzleInfo).be.empty();
should(document.version).be.undefined();
should(document.collection).be.exactly('foo');

document = new Document(collection, { some: 'content' });
should(refreshed).be.false();
should(document.id).be.undefined();
should(document.content).match({some: 'content'});
should(document.kuzzleInfo).be.empty();
should(document.version).be.undefined();
should(document.collection).be.exactly('foo');

document = new Document(collection, 'id', { some: 'content', _version: 123 });
should(refreshed).be.false();
should(document.id).be.exactly('id');
should(document.content).match({some: 'content'});
should(document.kuzzleInfo).be.empty();
should(document.version).be.exactly(123);
should(document.collection).be.exactly('foo');

document = new Document(collection, 'id');
should(refreshed).be.false();
should(document.id).be.exactly('id');
should(document.content).be.empty();
should(document.kuzzleInfo).be.empty();
should(document.version).be.undefined();
should(document.collection).be.exactly('foo');

document = new Document(collection, 'id', { some: 'content', _version: 123 }, {author: 'toto'});
should(refreshed).be.false();
should(document.id).be.exactly('id');
should(document.content).match({some: 'content'});
should(document.kuzzleInfo).match({author: 'toto'});
should(document.version).be.exactly(123);
should(document.collection).be.exactly('foo');
});

it('should expose documented properties with the right permissions', function () {
Expand All @@ -67,6 +79,7 @@ describe('Document constructor', function () {
should(document).have.propertyWithDescriptor('headers', { enumerable: true, writable: true, configurable: false });
should(document).have.propertyWithDescriptor('id', { enumerable: true, writable: true, configurable: false });
should(document).have.propertyWithDescriptor('version', { enumerable: true, writable: true, configurable: false });
should(document).have.propertyWithDescriptor('kuzzleInfo', { enumerable: true, writable: false, configurable: false });
});

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