Javascript 7.x

create

Creates a new collection 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 (check here the mapping capabilities of ElasticSearch).

This method will only update the mapping if the collection already exists.

Available since Kuzzle 2.2.0
Available since 7.4.0

You can also provide Elasticsearch index settings when creating a new collection.


Copied to clipboard!
create(index, collection, [definition], [options]);

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
Available since 7.4.0

definition

An object containings:

Copied to clipboard!
const definition = {
  mappings: {
    properties: {
      field1: { type: 'text' },
      field2: {
        properties: {
          nestedField: { type: 'keyword' }
        }
      }
    }    
  },
  settings: {

  }
};
Deprecated since 7.4.0

definition

An object representing the data mappings of the collection.

The mappings must have a root field properties that contain the mappings properties definition:

Copied to clipboard!
const mappings = {
  properties: {
    field1: { type: 'text' },
    field2: {
      properties: {
        nestedField: { type: 'keyword' }
      }
    }
  }
};

More informations about database mappings here.

options

Additional query options

Property Type
(default)
Description
queuable
boolean

(true)
If true, queues the request during downtime, until connected to Kuzzle again

Resolves

Resolves if the collection is successfully created.

Usage

Copied to clipboard!
const mappings = {
  properties: {
    license: { type: 'keyword' },
    driver: {
      properties: {
        name: { type: 'keyword' },
        curriculum: { type: 'text' }
      }
    }
  }
};
try {
  await kuzzle.collection.create('nyc-open-data', 'yellow-taxi', { mappings });
  console.log('Success');
} catch (error) {
  console.error(error.message);
}