Skip to content

Commit ec13b7f

Browse files
samniisanGilles Ballini
authored andcommitted
Add support for Document:m* methods (#224)
* Add support for Document:m* methods * Renamed m* methods * Modified error handling * Made mGet method not chainable (read action)
1 parent 9066a3b commit ec13b7f

File tree

3 files changed

+528
-1
lines changed

3 files changed

+528
-1
lines changed

src/Collection.js

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,233 @@ Collection.prototype.getMapping = function (options, cb) {
381381
kuzzleMapping.refresh(options, cb);
382382
};
383383

384+
/**
385+
* Create the provided documents
386+
*
387+
* @param {Array.<document>} documents - Array of documents to create
388+
* @param {object} [options] - Optional parameters
389+
* @param {responseCallback} cb - Returns an instantiated CollectionMapping object
390+
* @returns {object} this
391+
*/
392+
Collection.prototype.mCreateDocument = function (documents, options, cb) {
393+
var
394+
data = {
395+
body: {},
396+
},
397+
self = this;
398+
399+
if (!cb && typeof options === 'function') {
400+
cb = options;
401+
options = null;
402+
}
403+
404+
if (!Array.isArray(documents)) {
405+
return cb(new Error('Collection.mCreateDocument: documents parameter format is invalid (should be an array of documents)'));
406+
}
407+
408+
self.kuzzle.callbackRequired('Collection.mCreate', cb);
409+
410+
data.body.documents = documents.map(function (doc) {
411+
return (doc instanceof Document) ? doc.serialize() : doc;
412+
});
413+
414+
data = self.kuzzle.addHeaders(data, this.headers);
415+
416+
self.kuzzle.query(this.buildQueryArgs('document', 'mCreate'), data, options, cb && function (err, res) {
417+
cb(err, err ? undefined : res.result);
418+
});
419+
420+
return self;
421+
};
422+
423+
/**
424+
* Create or replace the provided documents
425+
*
426+
* @param {Array.<document>} documents - Array of documents to create or replace
427+
* @param {object} [options] - Optional parameters
428+
* @param {responseCallback} cb - Returns an instantiated CollectionMapping object
429+
* @returns {object} this
430+
*/
431+
Collection.prototype.mCreateOrReplaceDocument = function (documents, options, cb) {
432+
var
433+
data = {
434+
body: {},
435+
},
436+
self = this;
437+
438+
if (!cb && typeof options === 'function') {
439+
cb = options;
440+
options = null;
441+
}
442+
443+
if (!Array.isArray(documents)) {
444+
return cb(new Error('Collection.mCreateOrReplaceDocument: documents parameter format is invalid (should be an array of documents)'));
445+
}
446+
447+
self.kuzzle.callbackRequired('Collection.mCreateOrReplace', cb);
448+
449+
data.body.documents = documents.map(function (doc) {
450+
return (doc instanceof Document) ? doc.serialize() : doc;
451+
});
452+
453+
data = self.kuzzle.addHeaders(data, this.headers);
454+
455+
self.kuzzle.query(this.buildQueryArgs('document', 'mCreateOrReplace'), data, options, cb && function (err, res) {
456+
cb(err, err ? undefined : res.result);
457+
});
458+
459+
return self;
460+
};
461+
462+
/**
463+
* Delete specific documents according to given IDs
464+
*
465+
* @param {Array.<string>} documentIds - IDs of the documents to delete
466+
* @param {object} [options] - Optional parameters
467+
* @param {responseCallback} cb - Returns an instantiated CollectionMapping object
468+
* @returns {object} this
469+
*/
470+
Collection.prototype.mDeleteDocument = function (documentIds, options, cb) {
471+
var
472+
data = {
473+
body: {
474+
ids: documentIds
475+
}
476+
},
477+
self = this;
478+
479+
if (!cb && typeof options === 'function') {
480+
cb = options;
481+
options = null;
482+
}
483+
484+
if (!Array.isArray(documentIds)) {
485+
return cb(new Error('Collection.mDeleteDocument: documentIds parameter format is invalid (should be an array of IDs)'));
486+
}
487+
488+
self.kuzzle.callbackRequired('Collection.mDelete', cb);
489+
490+
data = self.kuzzle.addHeaders(data, this.headers);
491+
492+
self.kuzzle.query(this.buildQueryArgs('document', 'mDelete'), data, options, cb && function (err, res) {
493+
cb(err, err ? undefined : res.result);
494+
});
495+
496+
return self;
497+
};
498+
499+
/**
500+
* Get specific documents according to given IDs
501+
*
502+
* @param {Array.<string>} documentIds - IDs of the documents to retrieve
503+
* @param {object} [options] - Optional parameters
504+
* @param {responseCallback} cb - Returns an instantiated CollectionMapping object
505+
*/
506+
Collection.prototype.mGetDocument = function (documentIds, options, cb) {
507+
var
508+
data = {
509+
body: {
510+
ids: documentIds
511+
}
512+
},
513+
self = this;
514+
515+
if (!cb && typeof options === 'function') {
516+
cb = options;
517+
options = null;
518+
}
519+
520+
if (!Array.isArray(documentIds)) {
521+
return cb(new Error('Collection.mGetDocument: documentIds parameter format is invalid (should be an array of IDs)'));
522+
}
523+
524+
self.kuzzle.callbackRequired('Collection.mGet', cb);
525+
526+
data = self.kuzzle.addHeaders(data, this.headers);
527+
528+
self.kuzzle.query(this.buildQueryArgs('document', 'mGet'), data, options, cb && function (err, res) {
529+
cb(err, err ? undefined : res.result);
530+
});
531+
};
532+
533+
/**
534+
* Replace the provided documents
535+
*
536+
* @param {Array.<document>} documents - Array of documents to replace
537+
* @param {object} [options] - Optional parameters
538+
* @param {responseCallback} cb - Returns an instantiated CollectionMapping object
539+
* @returns {object} this
540+
*/
541+
Collection.prototype.mReplaceDocument = function (documents, options, cb) {
542+
var
543+
data = {
544+
body: {}
545+
},
546+
self = this;
547+
548+
if (!cb && typeof options === 'function') {
549+
cb = options;
550+
options = null;
551+
}
552+
553+
if (!Array.isArray(documents)) {
554+
return cb(new Error('Collection.mReplaceDocument: documents parameter format is invalid (should be an array of documents)'));
555+
}
556+
557+
self.kuzzle.callbackRequired('Collection.mReplace', cb);
558+
559+
data.body.documents = documents.map(function (doc) {
560+
return (doc instanceof Document) ? doc.serialize() : doc;
561+
});
562+
563+
data = self.kuzzle.addHeaders(data, this.headers);
564+
565+
self.kuzzle.query(this.buildQueryArgs('document', 'mReplace'), data, options, cb && function (err, res) {
566+
cb(err, err ? undefined : res.result);
567+
});
568+
569+
return self;
570+
};
571+
572+
/**
573+
* Update the provided documents
574+
*
575+
* @param {Array.<document>} documents - Array of documents to update
576+
* @param {object} [options] - Optional parameters
577+
* @param {responseCallback} cb - Returns an instantiated CollectionMapping object
578+
* @returns {object} this
579+
*/
580+
Collection.prototype.mUpdateDocument = function (documents, options, cb) {
581+
var
582+
data = {
583+
body: {}
584+
},
585+
self = this;
586+
587+
if (!cb && typeof options === 'function') {
588+
cb = options;
589+
options = null;
590+
}
591+
592+
if (!Array.isArray(documents)) {
593+
return cb(new Error('Collection.mUpdateDocument: documents parameter format is invalid (should be an array of documents)'));
594+
}
595+
596+
self.kuzzle.callbackRequired('Collection.mUpdate', cb);
597+
598+
data.body.documents = documents.map(function (doc) {
599+
return (doc instanceof Document) ? doc.serialize() : doc;
600+
});
601+
602+
data = self.kuzzle.addHeaders(data, this.headers);
603+
604+
self.kuzzle.query(this.buildQueryArgs('document', 'mUpdate'), data, options, cb && function (err, res) {
605+
cb(err, err ? undefined : res.result);
606+
});
607+
608+
return self;
609+
};
610+
384611
/**
385612
* Publish a realtime message
386613
*

test/Collection/constructor.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ describe('Collection constructor', function () {
4747
should.exist(collection.fetchDocumentPromise);
4848
should.exist(collection.fetchAllDocumentsPromise);
4949
should.exist(collection.getMappingPromise);
50+
should.exist(collection.mCreateDocumentPromise);
51+
should.exist(collection.mCreateOrReplaceDocumentPromise);
52+
should.exist(collection.mDeleteDocumentPromise);
53+
should.exist(collection.mGetDocumentPromise);
54+
should.exist(collection.mReplaceDocumentPromise);
55+
should.exist(collection.mUpdateDocumentPromise);
5056
should.not.exist(collection.publishPromise);
5157
should.exist(collection.replaceDocumentPromise);
5258
should.not.exist(collection.setHeadersPromise);

0 commit comments

Comments
 (0)