diff --git a/.eslintignore b/.eslintignore index 4a5064d0d..47d963a48 100644 --- a/.eslintignore +++ b/.eslintignore @@ -6,6 +6,8 @@ src/Kuzzle.js src/KuzzleError.js src/controllers/Auth.js src/controllers/Document.js +src/controllers/Index.js +src/controllers/Collection.js src/controllers/Base.js src/core/security/User.js src/core/security/Profile.js diff --git a/.gitignore b/.gitignore index 875b9e4c4..d8da07f04 100644 --- a/.gitignore +++ b/.gitignore @@ -34,6 +34,8 @@ src/Kuzzle.js src/KuzzleError.js src/controllers/Auth.js src/controllers/Document.js +src/controllers/Index.js +src/controllers/Collection.js src/controllers/Base.js src/core/security/User.js src/core/security/Profile.js diff --git a/.travis.yml b/.travis.yml index aa2ac4cef..a03c9f9e6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -93,7 +93,7 @@ jobs: - $(npm bin)/kuzdoc framework:link -d /sdk/js/7/ -v 7 script: - gem install typhoeus - - cd doc/framework/ && HYDRA_MAX_CONCURRENCY=20 ruby .ci/dead-links.rb -p src/sdk/js/7/ + - cd doc/framework/ && HYDRA_MAX_CONCURRENCY=20 travis_retry ruby .ci/dead-links.rb -p src/sdk/js/7/ - stage: Deployment Doc Dev name: Deploy next-docs.kuzzle.io diff --git a/doc/7/controllers/collection/create/index.md b/doc/7/controllers/collection/create/index.md index 68ada4bda..a1b80308b 100644 --- a/doc/7/controllers/collection/create/index.md +++ b/doc/7/controllers/collection/create/index.md @@ -7,17 +7,22 @@ description: Create a new collection # create -Creates a new [collection](/core/2/guides/essentials/store-access-data) in Kuzzle via the persistence engine, in the provided index. +Creates a new [collection](/core/2/guides/essentials/store-access-data) in the provided index. You can also provide an optional data mapping that allow you to exploit the full capabilities of our -persistent data storage layer, [ElasticSearch](https://www.elastic.co/elastic-stack) (check here the [mapping capabilities of ElasticSearch](https://www.elastic.co/guide/en/elasticsearch/reference/7.3/mapping.html)). +persistent data storage layer, [ElasticSearch](https://www.elastic.co/elastic-stack) (check here the [mapping capabilities of ElasticSearch](/core/2/guides/essentials/database-mappings/)). This method will only update the mapping if the collection already exists. + + + +You can also provide Elasticsearch [index settings](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/index-modules.html#index-modules-settings) when creating a new collection. +
```js -create(index, collection, [mapping], [options]); +create(index, collection, [definition], [options]); ```
@@ -26,17 +31,48 @@ create(index, collection, [mapping], [options]); | ------------ | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `index` |
string
| Index name | | `collection` |
string
| Collection name | -| `mapping` |
object
| Describes the data mapping to associate to the new collection, using Elasticsearch [mapping format](https://www.elastic.co/guide/en/elasticsearch/reference/7.3/mapping.html) | +| `definition` |
object
| Describes the collection mappings and the ES index settings | | `options` |
object
| Query options | + -### mapping - -An object representing the data mapping of the collection. +### definition +An object containings: + - [collection mappings](/core/2/guides/essentials/database-mappings). + - Elasticsearch [index settings](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/index-modules.html#index-modules-settings) The mapping must have a root field `properties` that contain the mapping definition: ```js -const mapping = { +const definition = { + mappings: { + properties: { + field1: { type: 'text' }, + field2: { + properties: { + nestedField: { type: 'keyword' } + } + } + } + }, + settings: { + + } +}; +``` + + + + + + +### definition + +An object representing the data mappings of the collection. + +The mappings must have a root field `properties` that contain the mappings properties definition: + +```js +const mappings = { properties: { field1: { type: 'text' }, field2: { @@ -50,6 +86,8 @@ const mapping = { More informations about database mappings [here](/core/2/guides/essentials/database-mappings). + + ### options Additional query options diff --git a/doc/7/controllers/collection/create/snippets/create.js b/doc/7/controllers/collection/create/snippets/create.js index 4c10502d6..89cc8a348 100644 --- a/doc/7/controllers/collection/create/snippets/create.js +++ b/doc/7/controllers/collection/create/snippets/create.js @@ -1,4 +1,4 @@ -const mapping = { +const mappings = { properties: { license: { type: 'keyword' }, driver: { @@ -11,7 +11,7 @@ const mapping = { }; try { - await kuzzle.collection.create('nyc-open-data', 'yellow-taxi', mapping); + await kuzzle.collection.create('nyc-open-data', 'yellow-taxi', { mappings }); console.log('Success'); } catch (error) { diff --git a/doc/7/controllers/collection/exists/index.md b/doc/7/controllers/collection/exists/index.md index 8a8000677..012fc4300 100644 --- a/doc/7/controllers/collection/exists/index.md +++ b/doc/7/controllers/collection/exists/index.md @@ -7,7 +7,7 @@ description: Check if collection exists # exists -Check if a collection exists in Kuzzle. +Checks if a collection exists in Kuzzle.
diff --git a/doc/7/controllers/collection/get-mapping/index.md b/doc/7/controllers/collection/get-mapping/index.md index 0f0fdcade..c01a9177e 100644 --- a/doc/7/controllers/collection/get-mapping/index.md +++ b/doc/7/controllers/collection/get-mapping/index.md @@ -30,6 +30,7 @@ Additional query options | Property | Type
(default) | Description | | ---------- | ------------------------------- | ---------------------------------------------------------------------------- | | `queuable` |
boolean

(`true`) | If true, queues the request during downtime, until connected to Kuzzle again | +| `includeKuzzleMeta` |
boolean

(`true`) | If true, the returned mappings will contain [Kuzzle metadata](/core/2/guides/essentials/document-metadata/) | ## Resolves diff --git a/doc/7/controllers/collection/refresh/index.md b/doc/7/controllers/collection/refresh/index.md index b79d97a7d..407f0e6b5 100644 --- a/doc/7/controllers/collection/refresh/index.md +++ b/doc/7/controllers/collection/refresh/index.md @@ -7,7 +7,7 @@ description: Forces an Elasticsearch search index update # refresh -When writing or deleting documents in Kuzzle, the update needs to be indexed before being available in search results. +Refreshes a collection to reindex the written and deleted documents so they are available in search results. :::info A refresh operation comes with some performance costs. diff --git a/doc/7/controllers/collection/update-specifications/index.md b/doc/7/controllers/collection/update-specifications/index.md index af9b1981d..b7947f2d7 100644 --- a/doc/7/controllers/collection/update-specifications/index.md +++ b/doc/7/controllers/collection/update-specifications/index.md @@ -7,7 +7,7 @@ description: Update the validation specifications # updateSpecifications -The updateSpecifications method allows you to create or update the validation specifications for a collection. +Creates or updates the validation specifications for a collection. When the validation specification is not formatted correctly, a detailed error message is returned to help you to debug. diff --git a/doc/7/controllers/collection/update/index.md b/doc/7/controllers/collection/update/index.md index f7edd238a..7dfd728e2 100644 --- a/doc/7/controllers/collection/update/index.md +++ b/doc/7/controllers/collection/update/index.md @@ -13,28 +13,66 @@ You can define the collection [dynamic mapping policy](/core/2/guides/essentials You can define [collection additional metadata](/core/2/guides/essentials/database-mappings#collection-metadata) within the `_meta` root field. + + + +You can also provide Elasticsearch [index settings](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/index-modules.html#index-modules-settings) when creating a new collection. +
```js -update(index, collection, mapping); +update(index, collection, definition); ```
-| Arguments | Type | Description | -| ------------ | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `index` |
string
| Index name | -| `collection` |
string
| Collection name | -| `mapping` |
object
| Describes the collection mapping | +| Arguments | Type | Description | +|--------------|-------------------|-------------------------------------------------------------| +| `index` |
string
| Index name | +| `collection` |
string
| Collection name | +| `definition` |
object
| Describes the collection mappings and the ES index settings | +| `options` |
object
| Query options | + + -### mapping +### definition -An object representing the collection data mapping. +An object containing: + - [collection mappings](/core/2/guides/essentials/database-mappings). + - Elasticsearch [index settings](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/index-modules.html#index-modules-settings) -This object must have a root field `properties` that contain the mapping definition: ```js -const mapping = { +const definition = { + mappings: { + properties: { + field1: { type: 'text' }, + field2: { + properties: { + nestedField: { type: 'keyword' } + } + } + } + }, + settings: { + // index settings (e.g. analyzers) + } +}; +``` + + + + + + +### definition + +An object representing the data mappings of the collection. + +The mappings must have a root field `properties` that contain the mappings properties definition: + +```js +const mappings = { properties: { field1: { type: 'text' }, field2: { @@ -48,6 +86,9 @@ const mapping = { More informations about database mappings [here](/core/2/guides/essentials/database-mappings). + + + ## Resolves Resolve if the collection is successfully updated. diff --git a/doc/7/controllers/collection/validate-specifications/index.md b/doc/7/controllers/collection/validate-specifications/index.md index cd5cf676b..f10474abe 100644 --- a/doc/7/controllers/collection/validate-specifications/index.md +++ b/doc/7/controllers/collection/validate-specifications/index.md @@ -7,7 +7,7 @@ description: Validate specifications format # validateSpecifications -The validateSpecifications method checks if a validation specification is well formatted. It does not store or modify the existing specification. +Checks if a validation specification is well formatted. It does not store nor modify the existing specification. When the validation specification is not formatted correctly, a detailed error message is returned to help you to debug. diff --git a/src/Kuzzle.ts b/src/Kuzzle.ts index 6d25a3fde..953d1160b 100644 --- a/src/Kuzzle.ts +++ b/src/Kuzzle.ts @@ -58,9 +58,9 @@ export class Kuzzle extends KuzzleEventEmitter { public auth: AuthController; public bulk: any; - public collection: any; + public collection: CollectionController; public document: DocumentController; - public index: any; + public index: IndexController; public ms: any; public realtime: any; public security: any; diff --git a/src/controllers/Collection.js b/src/controllers/Collection.js deleted file mode 100644 index fec87371b..000000000 --- a/src/controllers/Collection.js +++ /dev/null @@ -1,161 +0,0 @@ -const { BaseController } = require('./Base'); -const { SpecificationsSearchResult } = require('../core/searchResult/Specifications'); - -class CollectionController extends BaseController { - - /** - * @param {Kuzzle} kuzzle - */ - constructor (kuzzle) { - super(kuzzle, 'collection'); - } - - create (index, collection, mappings = {}, options = {}) { - const request = { - index, - collection, - body: mappings, - action: 'create' - }; - return this.query(request, options) - .then(response => response.result); - } - - deleteSpecifications (index, collection, options = {}) { - const request = { - index, - collection, - action: 'deleteSpecifications' - }; - return this.query(request, options) - .then(response => response.result); - } - - exists (index, collection, options = {}) { - return this.query({ - index, - collection, - action: 'exists' - }, options) - .then(response => response.result); - } - - refresh (index, collection, options = {}) { - return this.query({ - index, - collection, - action: 'refresh' - }, options) - .then(response => response.result); - } - - getMapping (index, collection, options = {}) { - const request = { - index, - collection, - action: 'getMapping', - includeKuzzleMeta: options.includeKuzzleMeta || false - }; - - return this.query(request, options) - .then(response => response.result); - } - - getSpecifications (index, collection, options = {}) { - return this.query({ - index, - collection, - action: 'getSpecifications' - }, options) - .then(response => response.result); - } - - list (index, options = {}) { - const request = { - index, - action: 'list', - size: options.size || 0, - from: options.from - }; - - return this.query(request, options) - .then(response => response.result); - } - - searchSpecifications (body = {}, options = {}) { - const request = { - body, - action: 'searchSpecifications' - }; - - for (const opt of ['from', 'size', 'scroll']) { - request[opt] = options[opt]; - } - - return this.query(request, options) - .then(response => new SpecificationsSearchResult(this.kuzzle, request, options, response.result)); - } - - truncate (index, collection, options = {}) { - const request = { - index, - collection, - action: 'truncate' - }; - return this.query(request, options) - .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, - collection, - body, - action: 'updateMapping' - }, options) - .then(response => response.result); - } - - updateSpecifications (index, collection, specifications, options = {}) { - return this.query({ - index, - collection, - body: specifications, - action: 'updateSpecifications' - }, options) - .then(response => response.result); - } - - validateSpecifications (index, collection, specifications, options = {}) { - return this.query({ - index, - collection, - body: specifications, - action: 'validateSpecifications' - }, options) - .then(response => response.result); - } - - delete (index, collection) { - const request = { - index, - collection, - action: 'delete' - }; - return this.query(request) - .then(() => undefined); - } -} - -module.exports = { CollectionController }; diff --git a/src/controllers/Collection.ts b/src/controllers/Collection.ts new file mode 100644 index 000000000..6f1374b22 --- /dev/null +++ b/src/controllers/Collection.ts @@ -0,0 +1,428 @@ +import { BaseController } from './Base'; +import { SpecificationsSearchResult } from '../core/searchResult/Specifications'; +import { CollectionMappings, JSONObject } from '../utils/interfaces'; + +export class CollectionController extends BaseController { + constructor (kuzzle) { + super(kuzzle, 'collection'); + } + + /** + * Creates a new collection in the provided index. + * You can also provide optional mappings and settings that allow you to exploit + * the full capabilities of our persistent data storage layer. + * + * @see https://docs.kuzzle.io/sdk/js/7/controllers/collection/create/ + * @see https://docs.kuzzle.io/core/2/guides/essentials/database-mappings/ + * + * @param index Index name + * @param collection Collection name + * @param definition Collection mappings and settings + * @param options Additional options + * - `queuable` If true, queues the request during downtime, until connected to Kuzzle again + */ + create ( + index: string, + collection: string, + definition: + { + /** + * Mappings definition + */ + mappings?: CollectionMappings; + /** + * Elasticsearch index settings + */ + settings?: JSONObject + } | CollectionMappings, + options: { queuable?: boolean } = {} + ): Promise { + const request = { + index, + collection, + body: definition, + action: 'create' + }; + + return this.query(request, options) + .then(() => undefined); + } + + /** + * Deletes validation specifications for a collection. + * + * @see https://docs.kuzzle.io/sdk/js/7/controllers/collection/delete-specifications/ + * + * @param index Index name + * @param collection Collection name + * @param options Additional options + * - `queuable` If true, queues the request during downtime, until connected to Kuzzle again + */ + deleteSpecifications ( + index: string, + collection: string, + options: { queuable?: boolean } = {} + ): Promise { + const request = { + index, + collection, + action: 'deleteSpecifications' + }; + return this.query(request, options) + .then(() => undefined); + } + + /** + * Checks if a collection exists in Kuzzle. + * + * @see https://docs.kuzzle.io/sdk/js/7/controllers/collection/exists/ + * + * @param index Index name + * @param collection Collection name + * @param options Additional options + * - `queuable` If true, queues the request during downtime, until connected to Kuzzle again + */ + exists ( + index: string, + collection: string, + options: { queuable?: boolean } = {} + ): Promise { + return this.query({ + index, + collection, + action: 'exists' + }, options) + .then(response => response.result); + } + + /** + * Refreshes a collection to reindex the writed and deleted documents + * so they are available in search results. + * + * @see https://docs.kuzzle.io/sdk/js/7/controllers/collection/refresh/ + * + * @param index Index name + * @param collection Collection name + * @param options Additional options + * - `queuable` If true, queues the request during downtime, until connected to Kuzzle again + */ + refresh ( + index: string, + collection: string, + options: { queuable?: boolean } = {} + ): Promise { + return this.query({ + index, + collection, + action: 'refresh' + }, options) + .then(() => undefined); + } + + /** + * Returns the collection mapping. + * + * @see https://docs.kuzzle.io/sdk/js/7/controllers/collection/get-mapping/ + * + * @param index Index name + * @param collection Collection name + * @param options Additional options + * - `includeKuzzleMeta` If true, the returned mappings will contain Kuzzle metadata + * - `queuable` If true, queues the request during downtime, until connected to Kuzzle again + */ + getMapping ( + index: string, + collection: string, + options: { includeKuzzleMeta?: boolean, queuable?: boolean } = {} + ): Promise { + const request = { + index, + collection, + action: 'getMapping', + includeKuzzleMeta: options.includeKuzzleMeta || false + }; + + return this.query(request, options) + .then(response => response.result); + } + + /** + * Returns the validation specifications associated to the given index and collection. + * + * @see https://docs.kuzzle.io/sdk/js/7/controllers/collection/get-specifications/ + * + * @param index Index name + * @param collection Collection name + * @param options Additional options + * - `queuable` If true, queues the request during downtime, until connected to Kuzzle again + * + * @returns The specifications + */ + getSpecifications ( + index: string, + collection: string, + options: { queuable?: boolean } = {} + ): Promise { + return this.query({ + index, + collection, + action: 'getSpecifications' + }, options) + .then(response => response.result); + } + + /** + * Returns the list of collections associated to a provided index. + * + * @see https://docs.kuzzle.io/sdk/js/7/controllers/collection/list/ + * + * @param index Index name + * @param options Additional options + * - `queuable` If true, queues the request during downtime, until connected to Kuzzle again + * + * @returns An object containing the collection list + */ + list ( + index: string, + options: { + queuable?: boolean; + /** + * @deprecated + */ + from?: number; + /** + * @deprecated + */ + size?: number; + } = {} + ): Promise<{ + /** + * Types of returned collections. + */ + type: string; + /** + * List of collections + */ + collections: Array<{ + /** + * Collection name + */ + name: string; + /** + * Collection type + */ + type: 'realtime' | 'stored' + }>; + }> { + const request = { + index, + action: 'list', + size: options.size || 0, + from: options.from + }; + + return this.query(request, options) + .then(response => response.result); + } + + /** + * Searches collection specifications. + * + * @see https://docs.kuzzle.io/sdk/js/7/controllers/collection/search-specifications/ + * + * @param query Search query + * @param options Additional options + * - `queuable` If true, queues the request during downtime, until connected to Kuzzle again + * - `from` Offset of the first document to fetch + * - `size` Maximum number of documents to retrieve per page + * - `scroll` When set, gets a forward-only cursor having its ttl set to the given value (e.g. `30s`) + */ + searchSpecifications ( + query: JSONObject = {}, + options: { + queuable?: boolean; + from?: number; + size?: number; + scroll?: string; + } = {} + ): Promise { + const request = { + body: query, + action: 'searchSpecifications' + }; + + for (const opt of ['from', 'size', 'scroll']) { + request[opt] = options[opt]; + } + + return this.query(request, options) + .then(response => ( + new SpecificationsSearchResult(this.kuzzle, request, options, response.result) + )); + } + + + /** + * Removes all documents from a collection, while keeping the associated mappings. + * + * @see https://docs.kuzzle.io/sdk/js/7/controllers/collection/truncate/ + * + * @param index Index name + * @param collection Collection name + * @param options Additional options + * - `queuable` If true, queues the request during downtime, until connected to Kuzzle again + */ + truncate ( + index: string, + collection: string, + options: { queuable?: boolean } = {} + ): Promise { + const request = { + index, + collection, + action: 'truncate' + }; + return this.query(request, options) + .then(() => undefined); + } + + /** + * Updates a collection informations + * You can also provide optional mappings and settings that allow you to exploit + * the full capabilities of our persistent data storage layer. + * + * @see https://docs.kuzzle.io/sdk/js/7/controllers/collection/update/ + * @see https://docs.kuzzle.io/core/2/guides/essentials/database-mappings/ + * + * @param index Index name + * @param collection Collection name + * @param definition Collection mappings and settings + * @param options Additional options + * - `queuable` If true, queues the request during downtime, until connected to Kuzzle again + */ + update ( + index: string, + collection: string, + definition: + { + /** + * Mappings definition + */ + mappings?: CollectionMappings; + /** + * Elasticsearch index settings + */ + settings?: JSONObject + } | CollectionMappings, + options: { queuable?: boolean } = {} + ): Promise { + return this.query({ + index, + collection, + body: definition, + action: 'update' + }, options) + .then(() => undefined); + } + + /** + * @deprecated Use collection.update instead + */ + updateMapping ( + index: string, + collection: string, + mappings: CollectionMappings, + options: { queuable?: boolean } = {} + ): Promise { + return this.query({ + index, + collection, + body: mappings, + action: 'updateMapping' + }, options) + .then(response => response.result); + } + + /** + * Create or updates the validation specifications for a collection. + * + * @see https://docs.kuzzle.io/sdk/js/7/controllers/collection/update-specifications/ + * + * @param index Index name + * @param collection Collection name + * @param specifications Specifications to update + * @param options Additional options + * - `queuable` If true, queues the request during downtime, until connected to Kuzzle again + * + * @returns The updated specifications + */ + updateSpecifications ( + index: string, + collection: string, + specifications: JSONObject, + options: { queuable?: boolean } = {} + ): Promise { + return this.query({ + index, + collection, + body: specifications, + action: 'updateSpecifications' + }, options) + .then(response => response.result); + } + + /** + * Checks if a validation specification is well formatted. + * It does not store or modify the existing specification. + * + * @see https://docs.kuzzle.io/sdk/js/7/controllers/collection/validate-specifications/ + * + * @param index Index name + * @param collection Collection name + * @param specifications Specifications to validate + * @param options Additional options + * - `queuable` If true, queues the request during downtime, until connected to Kuzzle again + * + * @returns An object which contain information about the specifications validity. + */ + validateSpecifications ( + index: string, + collection: string, + specifications: JSONObject, + options: { queuable?: boolean } = {} + ): Promise<{ + valid: boolean; + details: Array; + description: string; + }> { + return this.query({ + index, + collection, + body: specifications, + action: 'validateSpecifications' + }, options) + .then(response => response.result); + } + + /** + * Deletes a collection. + * + * @see https://docs.kuzzle.io/sdk/js/7/controllers/collection/delete/ + * + * @param index Index name + * @param collection Collection name + */ + delete ( + index: string, + collection: string + ): Promise { + const request = { + index, + collection, + action: 'delete' + }; + + return this.query(request) + .then(() => undefined); + } +} diff --git a/src/controllers/Index.js b/src/controllers/Index.js deleted file mode 100644 index 77642dddc..000000000 --- a/src/controllers/Index.js +++ /dev/null @@ -1,57 +0,0 @@ -const { BaseController } = require('./Base'); - -class IndexController extends BaseController { - - /** - * @param {Kuzzle} kuzzle - */ - constructor (kuzzle) { - super(kuzzle, 'index'); - } - - create (index, options = {}) { - const request = { - index, - action: 'create' - }; - return this.query(request, options) - .then(response => response.result); - } - - delete (index, options = {}) { - const request = { - index, - action: 'delete' - }; - return this.query(request, options) - .then(response => response.result.acknowledged); - } - - exists (index, options = {}) { - return this.query({ - index, - action : 'exists' - }, options) - .then(response => response.result); - } - - list (options) { - return this.query({ - action: 'list' - }, options) - .then(response => response.result.indexes); - } - - mDelete (indexes, options = {}) { - const request = { - action: 'mDelete', - body: { - indexes - } - }; - return this.query(request, options) - .then(response => response.result.deleted); - } -} - -module.exports = { IndexController }; diff --git a/src/controllers/Index.ts b/src/controllers/Index.ts new file mode 100644 index 000000000..61fc13c10 --- /dev/null +++ b/src/controllers/Index.ts @@ -0,0 +1,102 @@ +import { BaseController } from './Base'; + +export class IndexController extends BaseController { + + constructor (kuzzle) { + super(kuzzle, 'index'); + } + + /** + * Creates a new index + * + * @see https://docs.kuzzle.io/sdk/js/7/controllers/index/create/ + * + * @param index Index name + * @param options Additional options + * - `queuable` If true, queues the request during downtime, until connected to Kuzzle again + */ + create (index: string, options: { queuable?: boolean } = {}): Promise { + const request = { + index, + action: 'create' + }; + return this.query(request, options) + .then(() => undefined); + } + + /** + * Deletes an index + * + * @see https://docs.kuzzle.io/sdk/js/7/controllers/index/delete/ + * + * @param index Index name + * @param options Additional options + * - `queuable` If true, queues the request during downtime, until connected to Kuzzle again + */ + delete (index: string, options: { queuable?: boolean } = {}): Promise { + const request = { + index, + action: 'delete' + }; + return this.query(request, options) + .then(() => undefined); + } + + /** + * Checks if the given index exists. + * + * @see https://docs.kuzzle.io/sdk/js/7/controllers/index/exists/ + * + * @param index Index name + * @param options Additional options + * - `queuable` If true, queues the request during downtime, until connected to Kuzzle again + */ + exists (index: string, options: { queuable?: boolean } = {}): Promise { + return this.query({ + index, + action : 'exists' + }, options) + .then(response => response.result); + } + + /** + * Returns the complete list of indexes. + * + * @see https://docs.kuzzle.io/sdk/js/7/controllers/index/list/ + * + * @param options Additional options + * - `queuable` If true, queues the request during downtime, until connected to Kuzzle again + */ + list (options: { queuable?: boolean } = {}): Promise> { + return this.query({ + action: 'list' + }, options) + .then(response => response.result.indexes); + } + + /** + * Deletes multiple indexes + * + * @see https://docs.kuzzle.io/sdk/js/7/controllers/index/m-delete/ + * + * @param indexes List of index names to delete + * @param options Additional options + * - `queuable` If true, queues the request during downtime, until connected to Kuzzle again + * + * @returns Names of successfully deleted indexes + */ + mDelete ( + indexes: Array, + options: { queuable?: boolean } = {} + ): Promise> { + const request = { + action: 'mDelete', + body: { + indexes + } + }; + + return this.query(request, options) + .then(response => response.result.deleted); + } +} diff --git a/src/utils/interfaces.ts b/src/utils/interfaces.ts index 075464398..a7c175da2 100644 --- a/src/utils/interfaces.ts +++ b/src/utils/interfaces.ts @@ -198,12 +198,53 @@ export interface DocumentHit extends Document { _score: number; } +export interface MappingsProperties { + /** + * Properties types definition + * + * @see https://docs.kuzzle.io/core/2/guides/essentials/database-mappings/#properties-types-definition + */ + properties?: MappingsProperties, + /** + * Dynamic mapping policy + * + * @see https://docs.kuzzle.io/core/2/guides/essentials/database-mappings/#dynamic-mapping-policy + */ + dynamic?: 'true' | 'false' | 'strict' | boolean +} + +/** + * Collection mappings definition + * + * @see https://docs.kuzzle.io/core/2/guides/essentials/database-mappings/ + */ +export interface CollectionMappings { + /** + * Collection metadata + * + * @see https://docs.kuzzle.io/core/2/guides/essentials/database-mappings/#collection-metadata + */ + _meta?: JSONObject; + /** + * Properties types definition + * + * @see https://docs.kuzzle.io/core/2/guides/essentials/database-mappings/#properties-types-definition + */ + properties?: MappingsProperties, + /** + * Dynamic mapping policy + * + * @see https://docs.kuzzle.io/core/2/guides/essentials/database-mappings/#dynamic-mapping-policy + */ + dynamic?: 'true' | 'false' | 'strict' | boolean, +} + /** * HTTP routes definition format * @example * { * : { - * : { , } + * : { verb: , url: } * } * } * diff --git a/test/controllers/collection.test.js b/test/controllers/collection.test.js index 3fc5b2df4..ae9ba248a 100644 --- a/test/controllers/collection.test.js +++ b/test/controllers/collection.test.js @@ -16,7 +16,7 @@ describe('Collection Controller', () => { }); describe('create', () => { - it('should call collection/create query and return a Promise which resolves an acknowledgement', () => { + it('should call collection/create query and return a Promise which resolves', () => { kuzzle.query.resolves({result: {acknowledged: true}}); return kuzzle.collection.create('index', 'collection', null, options) @@ -31,7 +31,7 @@ describe('Collection Controller', () => { collection: 'collection' }, options); - should(res.acknowledged).be.a.Boolean().and.be.true(); + should(res).be.undefined(); }); }); @@ -50,13 +50,13 @@ describe('Collection Controller', () => { collection: 'collection' }, options); - should(res.acknowledged).be.a.Boolean().and.be.true(); + should(res).be.undefined(); }); }); }); describe('deleteSpecifications', () => { - it('should call collection/deleteSpecifications query and return a Promise which resolves an acknowledgement', () => { + it('should call collection/deleteSpecifications query and return a Promise which resolves', () => { kuzzle.query.resolves({result: {acknowledged: true}}); return kuzzle.collection.deleteSpecifications('index', 'collection', options) @@ -70,7 +70,7 @@ describe('Collection Controller', () => { collection: 'collection' }, options); - should(res.acknowledged).be.a.Boolean().and.be.true(); + should(res).be.undefined(); }); }); }); @@ -110,7 +110,7 @@ describe('Collection Controller', () => { collection: 'collection' }, options); - should(res).be.Null(); + should(res).be.undefined(); }); }); }); @@ -301,7 +301,7 @@ describe('Collection Controller', () => { collection: 'collection' }, options); - should(res.acknowledged).be.a.Boolean().and.be.true(); + should(res).be.undefined(); }); }); }); @@ -345,7 +345,7 @@ describe('Collection Controller', () => { collection: 'collection' }); - should(res).match({ foo: 'bar' }); + should(res).be.undefined(); }); }); }); diff --git a/test/controllers/index.test.js b/test/controllers/index.test.js index 409a9ed13..8bd2e901a 100644 --- a/test/controllers/index.test.js +++ b/test/controllers/index.test.js @@ -33,8 +33,7 @@ describe('Index Controller', () => { index: 'index' }, options); - should(res.acknowledged).be.a.Boolean().and.be.true(); - should(res.shards_acknowledged).be.a.Boolean().and.be.true(); + should(res).be.undefined(); }); }); }); @@ -55,7 +54,7 @@ describe('Index Controller', () => { index: 'index' }, options); - should(res).be.a.Boolean().and.be.true(); + should(res).be.undefined(); }); }); });