Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,28 @@ Collection.prototype.deleteDocument = function (arg, options, cb) {
return this;
};

/**
* Returns a boolean indicating whether or not a document with provided ID exists.
*
* @param {string} documentId - Unique document identifier
* @param {object} options [options] - Optional parameters
* @param {responseCallback} cb - Handles the query response
*/
Collection.prototype.documentExists = function (documentId, options, cb) {
var
data = {_id: documentId},
self = this;

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

self.kuzzle.query(this.buildQueryArgs('document', 'exists'), data, options, function (err, res) {
cb(err, err ? undefined : res.result);
});
};

/**
* Retrieve a single stored document using its unique document ID.
*
Expand Down
24 changes: 24 additions & 0 deletions src/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,30 @@ Document.prototype.delete = function (options, cb) {
});
};

/**
* Checks if this document exists in Kuzzle.
*
* @param {object} [options] - Optional parameters
* @param {responseCallback} [cb] - Handles the query response
* @returns {*} this
*/
Document.prototype.exists = function (options, cb) {
var self = this;

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

if (!self.id) {
throw new Error('Document.exists: cannot check if the document exists if no id has been provided');
}

this.kuzzle.query(this.dataCollection.buildQueryArgs('document', 'exists'), this.serialize(), options, cb && function (err, res) {
cb(err, err ? undefined : res.result);
});
};

/**
* Replaces the current content with the last version of this document stored in Kuzzle.
*
Expand Down
1 change: 1 addition & 0 deletions test/Collection/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('Collection constructor', function () {
should.exist(collection.createPromise);
should.exist(collection.createDocumentPromise);
should.exist(collection.deleteDocumentPromise);
should.exist(collection.documentExistsPromise);
should.exist(collection.fetchDocumentPromise);
should.exist(collection.fetchAllDocumentsPromise);
should.exist(collection.getMappingPromise);
Expand Down
62 changes: 62 additions & 0 deletions test/Collection/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,68 @@ describe('Collection methods', function () {
});
});

describe('#documentExists', function () {
beforeEach(function () {
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});
kuzzle.query = queryStub;
emitted = false;
result = { result: true };
error = null;
expectedQuery = {
index: 'bar',
collection: 'foo',
action: 'exists',
controller: 'document',
body: {}
};
});

it('should send the right documentExists query to Kuzzle', function(done) {
var collection = kuzzle.collection(expectedQuery.collection),
options = { queuable: false };

expectedQuery.options = options;

collection.documentExists(result.result._id, options, function (err, res) {
should(err).be.null();
should(res).be.true();
done();
});
should(emitted).be.true();
});

it('should raise an error if no callback is provided', function () {
var collection = kuzzle.collection(expectedQuery.collection);

should(function () { collection.documentExists(); }).throw(Error);
should(function () { collection.documentExists({}); }).throw(Error);
should(function () { collection.documentExists({}, {}); }).throw(Error);
});

it('should handle the callback argument correctly', function () {
var collection = kuzzle.collection(expectedQuery.collection);

collection.documentExists({}, function () {});
should(emitted).be.true();

emitted = false;
collection.documentExists({}, {}, function () {});
should(emitted).be.true();
});

it('should call the callback with an error if one occurs', function (done) {
var collection = kuzzle.collection(expectedQuery.collection);
error = 'foobar';
this.timeout(50);

collection.documentExists({}, function (err, res) {
should(err).be.exactly('foobar');
should(res).be.undefined();
done();
});
});
});

describe('#fetchDocument', function () {
beforeEach(function () {
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});
Expand Down
1 change: 1 addition & 0 deletions test/Document/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe('Document constructor', function () {
var document = new Document(collection);

should.exist(document.deletePromise);
should.not.exist(document.existsPromise);
should.not.exist(document.publishPromise);
should.exist(document.refreshPromise);
should.exist(document.savePromise);
Expand Down
86 changes: 86 additions & 0 deletions test/Document/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,92 @@ describe('Document methods', function () {
});
});

describe('#exists', function () {
beforeEach(function () {
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});
kuzzle.query = queryStub;
collection = kuzzle.collection('foo');
emitted = false;
result = { result: true };
error = null;
expectedQuery = {
index: 'bar',
collection: 'foo',
action: 'exists',
controller: 'document',
body: {},
_id: 'foo'
};
});

it('should send the right query to Kuzzle', function () {
var
options = { queuable: false },
document = new Document(collection);

expectedQuery.options = options;
document.id = 'foo';
should(document.exists(options));
should(emitted).be.true();
});

it('should handle arguments correctly', function () {
var document = new Document(collection);

document.id = 'foo';
document.exists(function () {});
should(emitted).be.true();

emitted = false;
document.exists();
should(emitted).be.true();

emitted = false;
document.exists({}, function () {});
should(emitted).be.true();

emitted = false;
document.exists({});
should(emitted).be.true();
});

it('should throw an error if no ID has been set', function () {
var document = new Document(collection);

should(function () { document.exists(); }).throw(Error);
should(emitted).be.false();
});

it('should resolve the callback with true as the result', function (done) {
var document = new Document(collection);

this.timeout(50);
document.id = 'foo';

document.exists(function (err, res) {
should(emitted).be.true();
should(err).be.null();
should(res).be.true();
done();
});
});

it('should revolve the callback with an error if one occurs', function (done) {
var document = new Document(collection);

this.timeout(50);
document.id = 'foo';
error = 'foobar';

document.exists(function (err, res) {
should(emitted).be.true();
should(err).be.exactly('foobar');
should(res).be.undefined();
done();
});
});
});

describe('#refresh', function () {
beforeEach(function () {
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});
Expand Down