Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ For example, for the `create` action of the `document` controller ([document#cre
```js
const options = { refresh: 'wait_for' };
const documentBody = { hello: 'world' };
kuzzle.document.create('my-index', 'my-collection', 'my-uniq-id', documentBody, options)
kuzzle.document.create('my-index', 'my-collection', documentBody, 'my-uniq-id', options)
```

The parameters of each method differ according to the parameters expected in the API.
Expand Down
16 changes: 9 additions & 7 deletions features/steps/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ Given(/^the collection doesn't have a document with id '(.*?)'$/, async function
});

Given('the collection has a document with id {string}', async function (id) {
this.content = await this.kuzzle.document.create(this.index, this.collection, id, {
a: 'document'
}, {
refresh: 'wait_for'
});
this.content = await this.kuzzle.document.create(
this.index,
this.collection,
{ a: 'document' },
id,
{ refresh: 'wait_for'}
);
});


Expand All @@ -31,8 +33,8 @@ When('I create a document in {string}', async function (collection) {
this.content = await this.kuzzle.document.create(
this.index,
this.collection,
'some-id',
{a: 'document'},
'some-id',
{refresh: true}
);
});
Expand All @@ -44,8 +46,8 @@ When('I create a document with id {string}', async function (id) {
this.content = await this.kuzzle.document.create(
this.index,
this.collection,
id,
{a: 'document'},
id,
{refresh: true}
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ class DocumentController {
.then(response => response.result.count);
}

create (index, collection, _id, body, options = {}) {
create (index, collection, document, _id = null, options = {}) {
if (!index) {
throw new Error('Kuzzle.document.create: index is required');
}
if (!collection) {
throw new Error('Kuzzle.document.create: collection is required');
}
if (!body) {
throw new Error('Kuzzle.document.create: body is required');
if (!document) {
throw new Error('Kuzzle.document.create: document is required');
}

const request = {
index,
collection,
_id,
body,
body: document,
controller: 'document',
action: 'create',
refresh: options.refresh
Expand Down
14 changes: 7 additions & 7 deletions test/controllers/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,20 @@ describe('Document Controller', () => {
describe('create', () => {
it('should throw an error if the "index" argument is not provided', () => {
should(function () {
kuzzle.document.create(undefined, 'collection', 'document-id', {foo: 'bar'}, options);
kuzzle.document.create(undefined, 'collection', {foo: 'bar'}, 'document-id', options);
}).throw('Kuzzle.document.create: index is required');
});

it('should throw an error if the "collection" argument is not provided', () => {
should(function () {
kuzzle.document.create('index', undefined, 'document-id', {foo: 'bar'}, options);
kuzzle.document.create('index', undefined, {foo: 'bar'}, 'document-id', options);
}).throw('Kuzzle.document.create: collection is required');
});

it('should throw an error if the "body" argument is not provided', () => {
it('should throw an error if the "document" argument is not provided', () => {
should(function () {
kuzzle.document.create('index', 'collection', 'document-id', undefined, options);
}).throw('Kuzzle.document.create: body is required');
kuzzle.document.create('index', 'collection', undefined, 'document-id', options);
}).throw('Kuzzle.document.create: document is required');
});

it('should call document/create query and return a Promise which resolves the created document', () => {
Expand All @@ -96,7 +96,7 @@ describe('Document Controller', () => {
};
kuzzle.query.resolves({result});

return kuzzle.document.create('index', 'collection', 'document-id', {foo: 'bar'}, options)
return kuzzle.document.create('index', 'collection', {foo: 'bar'}, 'document-id', options)
.then(res => {
should(kuzzle.query)
.be.calledOnce()
Expand All @@ -122,7 +122,7 @@ describe('Document Controller', () => {
};
kuzzle.query.resolves({result});

return kuzzle.document.create('index', 'collection', 'document-id', {foo: 'bar'}, {refresh: true})
return kuzzle.document.create('index', 'collection', {foo: 'bar'}, 'document-id', {refresh: true})
.then(res => {
should(kuzzle.query)
.be.calledOnce()
Expand Down