Skip to content

Commit

Permalink
Merge pull request #12 from kuzzleio/collectionsOperations
Browse files Browse the repository at this point in the history
Collections operations
  • Loading branch information
scottinet committed Nov 19, 2015
2 parents d914c8c + 5a5708d commit 418e120
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kuzzle-sdk",
"version": "1.0.0-alpha.4",
"version": "1.0.0-alpha.5",
"description": "A connector for the Kuzzle API",
"author": "The Kuzzle Team <support@kuzzle.io>",
"repository": {
Expand Down
68 changes: 67 additions & 1 deletion src/kuzzleDataCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,29 @@ KuzzleDataCollection.prototype.count = function (filters, options, cb) {
return this;
};

/**
* Create a new empty data collection, with no associated mapping.
* Kuzzle automatically creates data collections when storing documents, but there are cases where we
* want to create and prepare data collections before storing documents in it.
*
* @param {object} [options] - Optional parameters
* @param {responseCallback} [cb] - returns Kuzzle's response
* @returns {*} this
*/
KuzzleDataCollection.prototype.create = function (options, cb) {
var data = {};

if (!cb && typeof options === 'function') {
cb = options;
options = null;
}

data = this.kuzzle.addHeaders(data, this.headers);
this.kuzzle.query(this.collection, 'write', 'createCollection', data, options, cb);

return this;
};

/**
* Create a new document in Kuzzle.
*
Expand Down Expand Up @@ -176,6 +199,27 @@ KuzzleDataCollection.prototype.createDocument = function (document, options, cb)
return this;
};

/**
* Delete this data collection and all documents in it.
*
* @param {object} [options] - Optional parameters
* @param {responseCallback} [cb] - returns Kuzzle's response
* @returns {*} this
*/
KuzzleDataCollection.prototype.delete = function (options, cb) {
var data = {};

if (!cb && typeof options === 'function') {
cb = options;
options = null;
}

data = this.kuzzle.addHeaders(data, this.headers);
this.kuzzle.query(this.collection, 'admin', 'deleteCollection', data, options, cb);

return this;
};

/**
* Delete persistent documents.
*
Expand Down Expand Up @@ -389,14 +433,36 @@ KuzzleDataCollection.prototype.replaceDocument = function (documentId, content,
KuzzleDataCollection.prototype.subscribe = function (filters, cb, options) {
var room;

this.kuzzle.isValid();
this.kuzzle.callbackRequired('KuzzleDataCollection.subscribe', cb);

room = new KuzzleRoom(this, options);

return room.renew(filters, cb);
};

/**
* Truncate the data collection, removing all stored documents but keeping all associated mappings.
* This method is a lot faster than removing all documents using a query.
*
* @param {object} [options] - Optional parameters
* @param {responseCallback} [cb] - returns Kuzzle's response
* @returns {*} this
*/
KuzzleDataCollection.prototype.truncate = function (options, cb) {
var data = {};

if (!cb && typeof options === 'function') {
cb = options;
options = null;
}

data = this.kuzzle.addHeaders(data, this.headers);
this.kuzzle.query(this.collection, 'admin', 'truncateCollection', data, options, cb);

return this;
};


/**
* Update parts of a document
*
Expand Down
2 changes: 0 additions & 2 deletions src/kuzzleRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ function KuzzleRoom(kuzzleDataCollection, options) {
throw new Error('KuzzleRoom: missing parameters');
}

kuzzleDataCollection.kuzzle.isValid();

// Define properties
Object.defineProperties(this, {
// private properties
Expand Down

0 comments on commit 418e120

Please sign in to comment.