Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 10 additions & 8 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var
Document = require('./Document'),
CollectionMapping = require('./CollectionMapping'),
Room = require('./Room'),
KuzzleSubscribeResult = require('./SubscribeResult');
SubscribeResult = require('./SubscribeResult');

/**
* This is a global callback pattern, called by all asynchronous functions of the Kuzzle object.
Expand Down Expand Up @@ -314,7 +314,7 @@ Collection.prototype.fetchAllDocuments = function (options, cb) {

this.kuzzle.callbackRequired('Collection.fetchAllDocuments', cb);

this.search(filters, options, function getNextDocuments (error, searchResult) {
this.search(filters, options, function fetchNextDocuments (error, searchResult) {
if (error) {
return cb(error);
}
Expand All @@ -328,7 +328,7 @@ Collection.prototype.fetchAllDocuments = function (options, cb) {
searchResult.documents.forEach(function(document) {
documents.push(document);
});
searchResult.next(getNextDocuments);
searchResult.fetchNext(fetchNextDocuments);
}
else {
cb(null, documents);
Expand Down Expand Up @@ -477,8 +477,9 @@ Collection.prototype.search = function (filters, options, cb) {
self,
result.result.total,
documents,
result.result.aggregations ? result.result.aggregations : [],
{options: options, filters: filters},
result.result.aggregations ? result.result.aggregations : {},
options,
filters,
options.previous || null
));
});
Expand Down Expand Up @@ -545,8 +546,9 @@ Collection.prototype.scroll = function (scrollId, options, filters, cb) {
self,
result.result.total,
documents,
result.result.aggregations ? result.result.aggregations : [],
{options: options, filters: filters},
{},
options,
filters,
options.previous || null
));
});
Expand Down Expand Up @@ -575,7 +577,7 @@ Collection.prototype.subscribe = function (filters, options, cb) {

this.kuzzle.callbackRequired('Collection.subscribe', cb);

subscribeResult = new KuzzleSubscribeResult();
subscribeResult = new SubscribeResult();
room = new Room(this, options);

room.renew(filters, cb, subscribeResult.done.bind(subscribeResult));
Expand Down
12 changes: 6 additions & 6 deletions src/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@
* - providing a documentID to the constructor will automatically call refresh, unless a content is also provided
*
*
* @param {object} kuzzleDataCollection - an instanciated Collection object
* @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
* @constructor
*/
function Document(kuzzleDataCollection, documentId, content) {
function Document(collection, documentId, content) {
Object.defineProperties(this, {
// read-only properties
collection: {
value: kuzzleDataCollection.collection,
value: collection.collection,
enumerable: true
},
dataCollection: {
value: kuzzleDataCollection,
value: collection,
enumerable: false
},
kuzzle: {
value: kuzzleDataCollection.kuzzle,
value: collection.kuzzle,
enumerable: false
},
// writable properties
Expand All @@ -47,7 +47,7 @@ function Document(kuzzleDataCollection, documentId, content) {
enumerable: true
},
headers: {
value: JSON.parse(JSON.stringify(kuzzleDataCollection.headers)),
value: JSON.parse(JSON.stringify(collection.headers)),
enumerable: true,
writable: true
},
Expand Down
10 changes: 5 additions & 5 deletions src/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ var
* Once you have subscribed, if a pub/sub message is published matching your filters, or if a matching stored
* document change (because it is created, updated or deleted), then you’ll receive a notification about it.
*
* @param {object} kuzzleDataCollection - an instantiated and valid kuzzle object
* @param {object} collection - an instantiated and valid kuzzle object
* @param {object} [options] - subscription optional configuration
* @constructor
*/
function Room(kuzzleDataCollection, options) {
function Room(collection, options) {
// Define properties
Object.defineProperties(this, {
// private properties
Expand Down Expand Up @@ -73,11 +73,11 @@ function Room(kuzzleDataCollection, options) {
},
// read-only properties
collection: {
value: kuzzleDataCollection,
value: collection,
enumerable: true
},
kuzzle: {
value: kuzzleDataCollection.kuzzle,
value: collection.kuzzle,
enumerable: true
},
// writable properties
Expand All @@ -87,7 +87,7 @@ function Room(kuzzleDataCollection, options) {
writable: true
},
headers: {
value: JSON.parse(JSON.stringify(kuzzleDataCollection.headers)),
value: JSON.parse(JSON.stringify(collection.headers)),
enumerable: true,
writable: true
},
Expand Down
104 changes: 82 additions & 22 deletions src/SearchResult.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
/**
* @param {Collection} dataCollection
* @param {Collection} collection
* @param {int} total
* @param {Document[]} documents
* @param {object} [aggregations]
* @param {object} [searchArgs]
* @param previous
* @property {Collection} dataCollection
* @param {object} aggregations
* @param {object} options
* @param {object} filters
* @param {SearchResult} previous
* @property {Collection} collection
* @property {number} total
* @property {Document[]} documents
* @property {object} aggregations
* @property {object} options
* @property {object} filters
* @property {number} fetchedDocument
* @constructor
*/
function KuzzleSearchResult (dataCollection, total, documents, aggregations, searchArgs, previous) {
function SearchResult (collection, total, documents, aggregations, options, filters, previous) {
Object.defineProperties(this, {
// read-only properties
dataCollection: {
value: dataCollection,
collection: {
value: collection,
enumerable: true
},
total: {
value: total,
Expand All @@ -27,24 +34,28 @@ function KuzzleSearchResult (dataCollection, total, documents, aggregations, sea
value: aggregations || {},
enumerable: true
},
searchArgs: {
value: searchArgs || {},
options: {
value: options || {},
enumerable: true
},
filters: {
value: filters || {},
enumerable: true
},
// writable properties
fetchedDocument: {
value: previous instanceof KuzzleSearchResult ? documents.length + previous.fetchedDocument : documents.length,
value: previous instanceof SearchResult ? documents.length + previous.fetchedDocument : documents.length,
enumerable: true,
writable: true
}
});

// promisifying
if (this.dataCollection.kuzzle.bluebird) {
return this.dataCollection.kuzzle.bluebird.promisifyAll(this, {
if (this.collection.kuzzle.bluebird) {
return this.collection.kuzzle.bluebird.promisifyAll(this, {
suffix: 'Promise',
filter: function (name, func, target, passes) {
var whitelist = ['next'];
var whitelist = ['fetchNext'];

return passes && whitelist.indexOf(name) !== -1;
}
Expand All @@ -57,16 +68,16 @@ function KuzzleSearchResult (dataCollection, total, documents, aggregations, sea
/**
* @param {function} cb
*/
KuzzleSearchResult.prototype.next = function (cb) {
SearchResult.prototype.fetchNext = function (cb) {
var
filters,
options = Object.assign({}, this.searchArgs.options);
options = Object.assign({}, this.options);

options.previous = this;

// retrieve next results with scroll if original search use it
if (options.scrollId) {
if (this.fetchedDocument >= this.total) {
if (this.fetchedDocument >= this.getTotal()) {
cb(null, null);
return;
}
Expand All @@ -80,30 +91,79 @@ KuzzleSearchResult.prototype.next = function (cb) {
delete options.size;
}

this.dataCollection.scroll(options.scrollId, options, this.searchArgs.filters || {}, cb);
this.collection.scroll(options.scrollId, options, this.filters || {}, cb);

return;
}

// retrieve next results with from/size if original search use it
if (options.from !== undefined && options.size !== undefined) {
filters = Object.assign({}, this.searchArgs.filters);
filters = Object.assign({}, this.filters);

// check if we need to do next request to fetch all matching documents
options.from += options.size;

if (options.from >= this.total) {
if (options.from >= this.getTotal()) {
cb(null, null);

return;
}

this.dataCollection.search(filters, options, cb);
this.collection.search(filters, options, cb);

return;
}

cb(new Error('Unable to retrieve next results from search: missing scrollId or from/size params'));
};

module.exports = KuzzleSearchResult;
/**
* @returns {Document[]}
*/
SearchResult.prototype.getDocuments = function () {
return this.documents;
};

/**
* @returns {number}
*/
SearchResult.prototype.getTotal = function () {
return this.total;
};

/**
* @returns {object}
*/
SearchResult.prototype.getAggregations = function () {
return this.aggregations;
};

/**
* @returns {Object}
*/
SearchResult.prototype.getOptions = function() {
return this.options;
};

/**
* @returns {object}
*/
SearchResult.prototype.getFilters = function() {
return this.filters;
};

/**
* @returns {object}
*/
SearchResult.prototype.getCollection = function () {
return this.collection;
};

/**
* @returns {number}
*/
SearchResult.prototype.getFetchedDocument = function () {
return this.fetchedDocument;
};

module.exports = SearchResult;
8 changes: 4 additions & 4 deletions src/SubscribeResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Sugar-code handling the result of a Room.renew call
* @constructor
*/
function KuzzleSubscribeResult() {
function SubscribeResult() {
this.cbs = [];
this.error = null;
this.room = null;
Expand All @@ -12,7 +12,7 @@ function KuzzleSubscribeResult() {
* Registers a callback to be called with a subscription result
* @param {Function} cb
*/
KuzzleSubscribeResult.prototype.onDone = function (cb) {
SubscribeResult.prototype.onDone = function (cb) {
if (this.error || this.room) {
cb(this.error, this.room);
}
Expand All @@ -29,7 +29,7 @@ KuzzleSubscribeResult.prototype.onDone = function (cb) {
* @param {Object} error object
* @param {Room} room
*/
KuzzleSubscribeResult.prototype.done = function (error, room) {
SubscribeResult.prototype.done = function (error, room) {
this.error = error;
this.room = room;

Expand All @@ -38,4 +38,4 @@ KuzzleSubscribeResult.prototype.done = function (error, room) {
});
};

module.exports = KuzzleSubscribeResult;
module.exports = SubscribeResult;
3 changes: 1 addition & 2 deletions src/security/Profile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var
SecurityDocument = require('./SecurityDocument');
var SecurityDocument = require('./SecurityDocument');

function Profile(Security, id, content) {

Expand Down
6 changes: 3 additions & 3 deletions src/security/Role.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var KuzzleSecurityDocument = require('./SecurityDocument');
var SecurityDocument = require('./SecurityDocument');

function Role(Security, id, content) {

KuzzleSecurityDocument.call(this, Security, id, content);
SecurityDocument.call(this, Security, id, content);

// Define properties
Object.defineProperties(this, {
Expand All @@ -29,7 +29,7 @@ function Role(Security, id, content) {

}

Role.prototype = Object.create(KuzzleSecurityDocument.prototype, {
Role.prototype = Object.create(SecurityDocument.prototype, {
constructor: {
value: Role
}
Expand Down
Loading