diff --git a/package.json b/package.json index e12fa52c8..33cfb7393 100644 --- a/package.json +++ b/package.json @@ -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 ", "repository": { diff --git a/src/kuzzleDataCollection.js b/src/kuzzleDataCollection.js index 809d353f1..296f1b95f 100644 --- a/src/kuzzleDataCollection.js +++ b/src/kuzzleDataCollection.js @@ -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. * @@ -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. * @@ -389,7 +433,6 @@ 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); @@ -397,6 +440,29 @@ KuzzleDataCollection.prototype.subscribe = function (filters, cb, 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 * diff --git a/src/kuzzleRoom.js b/src/kuzzleRoom.js index c2d5000ad..369a3195a 100644 --- a/src/kuzzleRoom.js +++ b/src/kuzzleRoom.js @@ -26,8 +26,6 @@ function KuzzleRoom(kuzzleDataCollection, options) { throw new Error('KuzzleRoom: missing parameters'); } - kuzzleDataCollection.kuzzle.isValid(); - // Define properties Object.defineProperties(this, { // private properties