Skip to content

Commit 418e120

Browse files
committed
Merge pull request #12 from kuzzleio/collectionsOperations
Collections operations
2 parents d914c8c + 5a5708d commit 418e120

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "1.0.0-alpha.4",
3+
"version": "1.0.0-alpha.5",
44
"description": "A connector for the Kuzzle API",
55
"author": "The Kuzzle Team <[email protected]>",
66
"repository": {

src/kuzzleDataCollection.js

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,29 @@ KuzzleDataCollection.prototype.count = function (filters, options, cb) {
122122
return this;
123123
};
124124

125+
/**
126+
* Create a new empty data collection, with no associated mapping.
127+
* Kuzzle automatically creates data collections when storing documents, but there are cases where we
128+
* want to create and prepare data collections before storing documents in it.
129+
*
130+
* @param {object} [options] - Optional parameters
131+
* @param {responseCallback} [cb] - returns Kuzzle's response
132+
* @returns {*} this
133+
*/
134+
KuzzleDataCollection.prototype.create = function (options, cb) {
135+
var data = {};
136+
137+
if (!cb && typeof options === 'function') {
138+
cb = options;
139+
options = null;
140+
}
141+
142+
data = this.kuzzle.addHeaders(data, this.headers);
143+
this.kuzzle.query(this.collection, 'write', 'createCollection', data, options, cb);
144+
145+
return this;
146+
};
147+
125148
/**
126149
* Create a new document in Kuzzle.
127150
*
@@ -176,6 +199,27 @@ KuzzleDataCollection.prototype.createDocument = function (document, options, cb)
176199
return this;
177200
};
178201

202+
/**
203+
* Delete this data collection and all documents in it.
204+
*
205+
* @param {object} [options] - Optional parameters
206+
* @param {responseCallback} [cb] - returns Kuzzle's response
207+
* @returns {*} this
208+
*/
209+
KuzzleDataCollection.prototype.delete = function (options, cb) {
210+
var data = {};
211+
212+
if (!cb && typeof options === 'function') {
213+
cb = options;
214+
options = null;
215+
}
216+
217+
data = this.kuzzle.addHeaders(data, this.headers);
218+
this.kuzzle.query(this.collection, 'admin', 'deleteCollection', data, options, cb);
219+
220+
return this;
221+
};
222+
179223
/**
180224
* Delete persistent documents.
181225
*
@@ -389,14 +433,36 @@ KuzzleDataCollection.prototype.replaceDocument = function (documentId, content,
389433
KuzzleDataCollection.prototype.subscribe = function (filters, cb, options) {
390434
var room;
391435

392-
this.kuzzle.isValid();
393436
this.kuzzle.callbackRequired('KuzzleDataCollection.subscribe', cb);
394437

395438
room = new KuzzleRoom(this, options);
396439

397440
return room.renew(filters, cb);
398441
};
399442

443+
/**
444+
* Truncate the data collection, removing all stored documents but keeping all associated mappings.
445+
* This method is a lot faster than removing all documents using a query.
446+
*
447+
* @param {object} [options] - Optional parameters
448+
* @param {responseCallback} [cb] - returns Kuzzle's response
449+
* @returns {*} this
450+
*/
451+
KuzzleDataCollection.prototype.truncate = function (options, cb) {
452+
var data = {};
453+
454+
if (!cb && typeof options === 'function') {
455+
cb = options;
456+
options = null;
457+
}
458+
459+
data = this.kuzzle.addHeaders(data, this.headers);
460+
this.kuzzle.query(this.collection, 'admin', 'truncateCollection', data, options, cb);
461+
462+
return this;
463+
};
464+
465+
400466
/**
401467
* Update parts of a document
402468
*

src/kuzzleRoom.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ function KuzzleRoom(kuzzleDataCollection, options) {
2626
throw new Error('KuzzleRoom: missing parameters');
2727
}
2828

29-
kuzzleDataCollection.kuzzle.isValid();
30-
3129
// Define properties
3230
Object.defineProperties(this, {
3331
// private properties

0 commit comments

Comments
 (0)