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
5 changes: 4 additions & 1 deletion doc/7/controllers/collection/update-mapping/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ description: Update the collection mapping

# updateMapping

<SinceBadge version="1.7.1" />
<SinceBadge version="Kuzzle 1.7.1" />
<DeprecatedBadge version="Kuzzle 2.1.0"/>

__Use [collection:update](/sdk/js/7/controllers/collection/update/) instead.__

You can define the collection [dynamic mapping policy](/core/2/guides/essentials/database-mappings#dynamic-mapping-policy) by setting the `dynamic` field to the desired value.

Expand Down
57 changes: 57 additions & 0 deletions doc/7/controllers/collection/update/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
code: true
type: page
title: update
description: Update the collection mapping
---

# update

<SinceBadge version="Kuzzle 2.1.0" />

You can define the collection [dynamic mapping policy](/core/2/guides/essentials/database-mappings#dynamic-mapping-policy) by setting the `dynamic` field to the desired value.

You can define [collection additional metadata](/core/2/guides/essentials/database-mappings#collection-metadata) within the `_meta` root field.

<br/>

```js
update(index, collection, mapping);
```

<br/>

| Arguments | Type | Description |
| ------------ | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `index` | <pre>string</pre> | Index name |
| `collection` | <pre>string</pre> | Collection name |
| `mapping` | <pre>object</pre> | Describes the collection mapping |

### mapping

An object representing the collection data mapping.

This object must have a root field `properties` that contain the mapping definition:

```js
const mapping = {
properties: {
field1: { type: 'text' },
field2: {
properties: {
nestedField: { type: 'keyword' }
}
}
}
};
```

More informations about database mappings [here](/core/2/guides/essentials/database-mappings).

## Resolves

Resolve if the collection is successfully updated.

## Usage

<<< ./snippets/update.js
17 changes: 17 additions & 0 deletions doc/7/controllers/collection/update/snippets/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const mapping = {
dynamic: 'false',
_meta: {
area: 'Panipokhari'
},
properties: {
plate: { type: 'keyword' }
}
};

try {
await kuzzle.collection.update('nyc-open-data', 'yellow-taxi', mapping);

console.log('Success');
} catch (error) {
console.error(error.message);
}
10 changes: 10 additions & 0 deletions doc/7/controllers/collection/update/snippets/update.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: collection#update
description: Update the collection mapping
hooks:
before: curl -X POST kuzzle:7512/nyc-open-data/_create && curl -X PUT kuzzle:7512/nyc-open-data/yellow-taxi
after:
template: default
expected: Success

sdk: js
version: 7
11 changes: 11 additions & 0 deletions src/controllers/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ class CollectionController extends BaseController {
.then(response => response.result);
}

update(index, collection, body) {
return this.query({
index,
collection,
body,
action: 'update'
})
.then(response => response.result);
}

// @deprecated
updateMapping (index, collection, body, options = {}) {
return this.query({
index,
Expand Down
4 changes: 4 additions & 0 deletions src/protocols/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@
}
},
"collection": {
"update": {
"url": "/:index/:collection/",
"verb": "POST"
},
"updateMapping": {
"url": "/:index/:collection/_mapping",
"verb": "PUT"
Expand Down
22 changes: 22 additions & 0 deletions test/controllers/collection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,28 @@ describe('Collection Controller', () => {
});
});

describe('update', () => {
it('should call collection/update query with the new mapping and return a Promise which resolves a json object', () => {
kuzzle.query.resolves({ result: { foo: 'bar' } });

const body = { foo: 'bar' };
return kuzzle.collection.update('index', 'collection', body)
.then(res => {
should(kuzzle.query)
.be.calledOnce()
.be.calledWith({
body,
controller: 'collection',
action: 'update',
index: 'index',
collection: 'collection'
});

should(res).match({ foo: 'bar' });
});
});
});

describe('updateSpecifications', () => {
it('should call collection/updateSpecifications query with the new specifications and return a Promise which resolves a json object', () => {
kuzzle.query.resolves({ result: { foo: 'bar' } });
Expand Down