From c369cf1272d100db4db7ec4af0872747e30dd5f8 Mon Sep 17 00:00:00 2001 From: Aschen Date: Wed, 22 Jul 2020 11:47:42 +0200 Subject: [PATCH 01/14] Add Index and Collection --- .eslintignore | 2 + .gitignore | 2 + doc/7/controllers/collection/create/index.md | 54 ++- .../collection/create/snippets/create.js | 4 +- doc/7/controllers/collection/exists/index.md | 2 +- .../collection/get-mapping/index.md | 1 + doc/7/controllers/collection/refresh/index.md | 2 +- .../collection/update-specifications/index.md | 2 +- doc/7/controllers/collection/update/index.md | 63 ++- src/Kuzzle.ts | 4 +- src/controllers/Collection.js | 151 ------- src/controllers/Collection.ts | 405 ++++++++++++++++++ src/controllers/Index.js | 58 --- src/controllers/Index.ts | 102 +++++ src/utils/interfaces.ts | 41 ++ 15 files changed, 659 insertions(+), 234 deletions(-) delete mode 100644 src/controllers/Collection.js create mode 100644 src/controllers/Collection.ts delete mode 100644 src/controllers/Index.js create mode 100644 src/controllers/Index.ts diff --git a/.eslintignore b/.eslintignore index d849ec6cc..2ff078d4e 100644 --- a/.eslintignore +++ b/.eslintignore @@ -5,6 +5,8 @@ src/**/*.js.map src/Kuzzle.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 1a1af6994..48daf2c0b 100644 --- a/.gitignore +++ b/.gitignore @@ -33,6 +33,8 @@ index.js src/Kuzzle.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/doc/7/controllers/collection/create/index.md b/doc/7/controllers/collection/create/index.md index 68ada4bda..13606c653 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, [body], [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..49c6776f1 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', { mapping }); 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..5bbfd9845 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 writed 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..a32c702fb 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. +Create 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..aeaba3580 100644 --- a/doc/7/controllers/collection/update/index.md +++ b/doc/7/controllers/collection/update/index.md @@ -13,28 +13,68 @@ 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, body); ```
-| 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 + + -An object representing the collection data mapping. +### definition -This object must have a root field `properties` that contain the mapping 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: { @@ -48,6 +88,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/src/Kuzzle.ts b/src/Kuzzle.ts index 98a27138e..34e2da878 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 97727ab6b..000000000 --- a/src/controllers/Collection.js +++ /dev/null @@ -1,151 +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); - } -} - -module.exports = { CollectionController }; diff --git a/src/controllers/Collection.ts b/src/controllers/Collection.ts new file mode 100644 index 000000000..6bdbc8821 --- /dev/null +++ b/src/controllers/Collection.ts @@ -0,0 +1,405 @@ +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(() => {}); + } + + /** + * 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(() => {}); + } + + /** + * 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(() => {}); + } + + /** + * 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(() => {}); + } + + /** + * 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(() => {}); + } + + /** + * @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); + } +} diff --git a/src/controllers/Index.js b/src/controllers/Index.js deleted file mode 100644 index 626d8c949..000000000 --- a/src/controllers/Index.js +++ /dev/null @@ -1,58 +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..1f581115d --- /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(() => {}); + } + + /** + * 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(() => {}); + } + + /** + * 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 6062231a3..88df936e8 100644 --- a/src/utils/interfaces.ts +++ b/src/utils/interfaces.ts @@ -185,3 +185,44 @@ 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' +} + +/** + * 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', +} \ No newline at end of file From b9848fc2c7e9f47737e84773a10889a698f445e0 Mon Sep 17 00:00:00 2001 From: Aschen Date: Fri, 31 Jul 2020 10:57:33 +0200 Subject: [PATCH 02/14] Fix doc params and lint --- doc/7/controllers/collection/create/index.md | 2 +- doc/7/controllers/collection/update/index.md | 2 +- .../collection/validate-specifications/index.md | 2 +- doc/7/controllers/index/create/index.md | 7 +++++++ src/controllers/Collection.ts | 11 ++++++----- src/controllers/Index.ts | 4 ++-- 6 files changed, 18 insertions(+), 10 deletions(-) diff --git a/doc/7/controllers/collection/create/index.md b/doc/7/controllers/collection/create/index.md index 13606c653..4ccb7b592 100644 --- a/doc/7/controllers/collection/create/index.md +++ b/doc/7/controllers/collection/create/index.md @@ -22,7 +22,7 @@ You can also provide Elasticsearch [index settings](https:/www.elastic.co/guide/
```js -create(index, collection, [body], [options]); +create(index, collection, [definition], [options]); ```
diff --git a/doc/7/controllers/collection/update/index.md b/doc/7/controllers/collection/update/index.md index aeaba3580..a134e1070 100644 --- a/doc/7/controllers/collection/update/index.md +++ b/doc/7/controllers/collection/update/index.md @@ -21,7 +21,7 @@ You can also provide Elasticsearch [index settings](https:/www.elastic.co/guide/
```js -update(index, collection, body); +update(index, collection, definition); ```
diff --git a/doc/7/controllers/collection/validate-specifications/index.md b/doc/7/controllers/collection/validate-specifications/index.md index cd5cf676b..9ea6d66b3 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 or 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/doc/7/controllers/index/create/index.md b/doc/7/controllers/index/create/index.md index 8cf8d0a53..5c84bcf2c 100644 --- a/doc/7/controllers/index/create/index.md +++ b/doc/7/controllers/index/create/index.md @@ -42,4 +42,11 @@ Resolves if the index does not already exists ## Usage +:::: tabs +::: tab yourTabName <<< ./snippets/create.js +::: +::: tab anotherTab +<<< ./snippets/create.js +::: +:::: \ No newline at end of file diff --git a/src/controllers/Collection.ts b/src/controllers/Collection.ts index 6bdbc8821..43217fb9b 100644 --- a/src/controllers/Collection.ts +++ b/src/controllers/Collection.ts @@ -43,8 +43,9 @@ export class CollectionController extends BaseController { body: definition, action: 'create' }; + return this.query(request, options) - .then(() => {}); + .then(() => undefined); } /** @@ -68,7 +69,7 @@ export class CollectionController extends BaseController { action: 'deleteSpecifications' }; return this.query(request, options) - .then(() => {}); + .then(() => undefined); } /** @@ -115,7 +116,7 @@ export class CollectionController extends BaseController { collection, action: 'refresh' }, options) - .then(() => {}); + .then(() => undefined); } /** @@ -282,7 +283,7 @@ export class CollectionController extends BaseController { action: 'truncate' }; return this.query(request, options) - .then(() => {}); + .then(() => undefined); } /** @@ -321,7 +322,7 @@ export class CollectionController extends BaseController { body: definition, action: 'update' }, options) - .then(() => {}); + .then(() => undefined); } /** diff --git a/src/controllers/Index.ts b/src/controllers/Index.ts index 1f581115d..61fc13c10 100644 --- a/src/controllers/Index.ts +++ b/src/controllers/Index.ts @@ -21,7 +21,7 @@ export class IndexController extends BaseController { action: 'create' }; return this.query(request, options) - .then(() => {}); + .then(() => undefined); } /** @@ -39,7 +39,7 @@ export class IndexController extends BaseController { action: 'delete' }; return this.query(request, options) - .then(() => {}); + .then(() => undefined); } /** From e7005f770f3af5913c51caf64b52c372c06ab85f Mon Sep 17 00:00:00 2001 From: Aschen Date: Fri, 31 Jul 2020 11:04:34 +0200 Subject: [PATCH 03/14] Fix test and lint --- test/controllers/collection.test.js | 16 ++++++++-------- test/controllers/index.test.js | 5 ++--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/test/controllers/collection.test.js b/test/controllers/collection.test.js index 03e83489f..d57d91473 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(); }); }); }); From 8381bdf7a8ac18d555676e29e725227cc6b894f5 Mon Sep 17 00:00:00 2001 From: Aschen Date: Fri, 31 Jul 2020 11:54:43 +0200 Subject: [PATCH 04/14] Add retry to dead link check --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index aa2ac4cef..4c68855dd 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/ && travis_retry HYDRA_MAX_CONCURRENCY=20 ruby .ci/dead-links.rb -p src/sdk/js/7/ - stage: Deployment Doc Dev name: Deploy next-docs.kuzzle.io From c69db5b4ec69fba97a6ae430cc633838d58f5ce0 Mon Sep 17 00:00:00 2001 From: Aschen Date: Fri, 31 Jul 2020 12:41:16 +0200 Subject: [PATCH 05/14] I hate ci dev --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4c68855dd..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/ && travis_retry 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 From 0ace4d84e90795b1a5bf0ffd0489c683bb8e9f6f Mon Sep 17 00:00:00 2001 From: Aschen Date: Fri, 31 Jul 2020 13:53:00 +0200 Subject: [PATCH 06/14] fix dead link --- doc/7/controllers/collection/create/index.md | 4 ++-- doc/7/controllers/collection/update/index.md | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/doc/7/controllers/collection/create/index.md b/doc/7/controllers/collection/create/index.md index 4ccb7b592..a1b80308b 100644 --- a/doc/7/controllers/collection/create/index.md +++ b/doc/7/controllers/collection/create/index.md @@ -17,7 +17,7 @@ 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. +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.
@@ -39,7 +39,7 @@ create(index, collection, [definition], [options]); 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) + - 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 diff --git a/doc/7/controllers/collection/update/index.md b/doc/7/controllers/collection/update/index.md index a134e1070..b1c70d4a8 100644 --- a/doc/7/controllers/collection/update/index.md +++ b/doc/7/controllers/collection/update/index.md @@ -16,7 +16,7 @@ You can define [collection additional metadata](/core/2/guides/essentials/databa -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. +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.
@@ -33,15 +33,14 @@ update(index, collection, definition); | `definition` |
object
| Describes the collection mappings and the ES index settings | | `options` |
object
| Query options | - ### 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: + - Elasticsearch [index settings](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/index-modules.html#index-modules-settings) + ```js const definition = { @@ -56,7 +55,7 @@ const definition = { } }, settings: { - + // index settings (e.g. analyzers) } }; ``` @@ -64,7 +63,6 @@ const definition = { - ### definition From 189553ce17806f0be66f224e87c886fcd4668708 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Cottinet?= Date: Mon, 10 Aug 2020 11:16:55 +0200 Subject: [PATCH 07/14] [doc] typo --- doc/7/controllers/collection/refresh/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/7/controllers/collection/refresh/index.md b/doc/7/controllers/collection/refresh/index.md index 5bbfd9845..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 -Refreshes a collection to reindex the writed and deleted documents so they are 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. From 2599e55e594f6390743f9af2739a7e93d026736b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Cottinet?= Date: Mon, 10 Aug 2020 11:17:05 +0200 Subject: [PATCH 08/14] [doc] typo --- doc/7/controllers/collection/update-specifications/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/7/controllers/collection/update-specifications/index.md b/doc/7/controllers/collection/update-specifications/index.md index a32c702fb..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 -Create or updates 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. From e887f331ffa0a43b79e401a27e3065674af48ab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Cottinet?= Date: Mon, 10 Aug 2020 11:17:13 +0200 Subject: [PATCH 09/14] [doc] typo --- doc/7/controllers/collection/update/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/7/controllers/collection/update/index.md b/doc/7/controllers/collection/update/index.md index b1c70d4a8..7dfd728e2 100644 --- a/doc/7/controllers/collection/update/index.md +++ b/doc/7/controllers/collection/update/index.md @@ -37,7 +37,7 @@ update(index, collection, definition); ### definition -An object containings: +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) From 569b1e9ef3a223afb3347dc06543652a00e1a52f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Cottinet?= Date: Mon, 10 Aug 2020 11:17:24 +0200 Subject: [PATCH 10/14] [doc] typo --- doc/7/controllers/collection/validate-specifications/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/7/controllers/collection/validate-specifications/index.md b/doc/7/controllers/collection/validate-specifications/index.md index 9ea6d66b3..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 -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. From 1023fd38a1cbf9cf857d48ec7e1f95dab3397ac5 Mon Sep 17 00:00:00 2001 From: Adrien Maret Date: Mon, 10 Aug 2020 11:48:50 +0200 Subject: [PATCH 11/14] Update doc/7/controllers/collection/create/snippets/create.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sébastien Cottinet --- doc/7/controllers/collection/create/snippets/create.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/7/controllers/collection/create/snippets/create.js b/doc/7/controllers/collection/create/snippets/create.js index 49c6776f1..89cc8a348 100644 --- a/doc/7/controllers/collection/create/snippets/create.js +++ b/doc/7/controllers/collection/create/snippets/create.js @@ -11,7 +11,7 @@ const mappings = { }; 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) { From 725bf913e47b37d5fb7568b54323167db717c03a Mon Sep 17 00:00:00 2001 From: Aschen Date: Mon, 10 Aug 2020 11:49:35 +0200 Subject: [PATCH 12/14] Allow boolean for dynamic --- src/utils/interfaces.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/interfaces.ts b/src/utils/interfaces.ts index 88df936e8..6dca6f281 100644 --- a/src/utils/interfaces.ts +++ b/src/utils/interfaces.ts @@ -198,7 +198,7 @@ export interface MappingsProperties { * * @see https://docs.kuzzle.io/core/2/guides/essentials/database-mappings/#dynamic-mapping-policy */ - dynamic?: 'true' | 'false' | 'strict' + dynamic?: 'true' | 'false' | 'strict' | boolean } /** From ecf5ccadef5dd2c641c91a32fb88457633ea3e1f Mon Sep 17 00:00:00 2001 From: Adrien Maret Date: Mon, 10 Aug 2020 11:58:15 +0200 Subject: [PATCH 13/14] Update src/utils/interfaces.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sébastien Cottinet --- src/utils/interfaces.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/interfaces.ts b/src/utils/interfaces.ts index 6dca6f281..8d978a584 100644 --- a/src/utils/interfaces.ts +++ b/src/utils/interfaces.ts @@ -224,5 +224,5 @@ export interface CollectionMappings { * * @see https://docs.kuzzle.io/core/2/guides/essentials/database-mappings/#dynamic-mapping-policy */ - dynamic?: 'true' | 'false' | 'strict', -} \ No newline at end of file + dynamic?: 'true' | 'false' | 'strict' | boolean, +} From bbfc272f3f8dfc8c44355384c0701122a94ee939 Mon Sep 17 00:00:00 2001 From: Aschen Date: Tue, 11 Aug 2020 11:54:11 +0200 Subject: [PATCH 14/14] Remove tab --- doc/7/controllers/index/create/index.md | 7 ------- 1 file changed, 7 deletions(-) diff --git a/doc/7/controllers/index/create/index.md b/doc/7/controllers/index/create/index.md index 5c84bcf2c..8cf8d0a53 100644 --- a/doc/7/controllers/index/create/index.md +++ b/doc/7/controllers/index/create/index.md @@ -42,11 +42,4 @@ Resolves if the index does not already exists ## Usage -:::: tabs -::: tab yourTabName <<< ./snippets/create.js -::: -::: tab anotherTab -<<< ./snippets/create.js -::: -:::: \ No newline at end of file