Core
Write Plugins v1.x
1

You are currently looking at the documentation of a previous version of Kuzzle. We strongly recommend that you use the latest version. You can also use the version selector in the top menu.

Repository #

Provides access to a collection inside the plugin's dedicated and secure storage.

If this is not already the case, the collection must first be created, using the storage accessor.


Constructor #

Copied to clipboard!
new context.Repository(collection, [ObjectConstructor]);

Arguments Type Description
collection
string
The repository's collection to link to this class instance
ObjectConstructor
object
If an ObjectConstructor class is provided, fetched data will be returned as instances of that class, instead of plain objects

create #

Creates a document.

Arguments #

Copied to clipboard!
create(document, [options]);

Arguments Type Description
document
object
The document to create. The provided object must contain a _id property, which is the document unique identifier
options
object
Optional arguments

options #

The options argument accepts the following parameters:

Options Type Description
refresh
string
If set with the wait_for string value, then the function will respond only after the document has been indexed

Return #

The create function returns a promise, resolving to the document creation result.

Example #

Copied to clipboard!
const content = {
  _id: '<unique id>',
  someField: 'some content',
  anotherField: 'another content'
};

try {
  const result = await repository.create(content);
  /*
   * Outputs:
   * { _index: '%<plugin name>',
   *   _type: '<collection>',
   *   _id: '<a unique id>',
   *   _version: 1,
   *   result: 'created',
   *   _shards: { total: 2, successful: 1, failed: 0 },
   *   created: true,
   *   _source: {
   *     someField: 'some content',
   *     anotherField: 'another content'
   *   }
   * }
   */
  console.dir(result, { depth: null });
} catch (error) {
  // "error" is a KuzzleError object
}

createOrReplace #

Creates or replaces a document.

Arguments #

Copied to clipboard!
createOrReplace(document, [options]);

Arguments Type Description
document
object
The document to create. The provided object must contain a _id property, which is the document unique identifier
options
object
Optional arguments

options #

The options argument accepts the following parameters:

Options Type Description
refresh
string
If set with the wait_for string value, then the function will respond only after the document has been indexed

Return #

The createOrReplace function returns a promise, resolving to the document creation/replacement result.

Example #

Copied to clipboard!
const content = {
  _id: '<unique id>',
  someField: 'some content',
  anotherField: 'another content'
};

try {
  const result = await repository.createOrReplace(content);
  /*
   * Outputs:
   * { _index: '%<plugin name>',
   *   _type: '<collection>',
   *   _id: '<a unique id>',
   *   _version: 3,
   *   result: 'created',
   *   _shards: { total: 2, successful: 1, failed: 0 },
   *   created: false,
   *   _source: {
   *     someField: 'some content',
   *     anotherField: 'another content'
   *   }
   * }
   */
  console.dir(result, { depth: null });
} catch (error) {
  // "error" is a KuzzleError object
}

delete #

Deletes a document.

Arguments #

Copied to clipboard!
delete (id, [options]);

Arguments Type Description
id
string
Document unique identifier
options
object
Optional arguments

options #

The options argument accepts the following parameters:

Options Type Description
refresh
string
If set with the wait_for string value, then the function will respond only after the document has been indexed

Return #

The delete function returns a promise, resolving to the document deletion result.

Example #

Copied to clipboard!
try {
  await repository.delete('someDocumentId');
  /*
   * Outputs:
   *  { found: true,
   *    _index: '%<plugin name>',
   *    _type: '<collection>',
   *    _id: 'someDocumentId',
   *    _version: 3,
   *    result: 'deleted',
   *    _shards: { total: 2, successful: 1, failed: 0 }
   *  }
   */
  console.dir(result, { depth: null });
} catch (error) {
  // "error" is a KuzzleError object
}

get #

Gets a document.

Arguments #

Copied to clipboard!
get(id);

Arguments Type Description
id
string
Document unique identifier

Return #

The get function returns a promise, resolving to the retrieved document's content.

If an ObjectConstructor argument was provided to the repository constructor, then the content is returned as an instance of that class instead of a raw object.


mGet #

Gets multiple documents.

Arguments #

Copied to clipboard!
mGet(ids);

Arguments Type Description
ids
string[]
List of document unique identifiers

Return #

The mGet function returns a promise, resolving to the list of documents contents

If an ObjectConstructor argument was provided to the repository constructor, then each content is returned as an instance of that class instead of a raw object.


replace #

Replaces the content of a document.

Arguments #

Copied to clipboard!
replace(document, [options]);

Arguments Type Description
document
object
The document to create. The provided object must contain a _id property, which is the document unique identifier
options
object
Optional arguments

options #

The options argument accepts the following parameters:

Options Type Description
refresh
string
If set with the wait_for string value, then the function will respond only after the document has been indexed

Return #

The replace function returns a promise, resolving to the document replacement result.

Example #

Copied to clipboard!
const content = {
  _id: '<unique id>',
  someField: 'some content',
  anotherField: 'another content'
};

try {
  const result = await repository.replace(content);
  /*
   * Outputs:
   * { _index: '%<plugin name>',
   *   _type: '<collection>',
   *   _id: '<a unique id>',
   *   _version: 3,
   *   _shards: { total: 2, successful: 1, failed: 0 },
   *   created: false,
   *   _source: {
   *     someField: 'some content',
   *     anotherField: 'another content'
   *   }
   * }
   */
  console.dir(result, { depth: null });
} catch (error) {
  // "error" is a KuzzleError object
}

Searches documents.

Arguments #

Copied to clipboard!
search(query, [options]);

Arguments Type Description
query
object
Search query, using Elasticsearch query format
options
object
Optional arguments

options #

The options argument accepts the following parameters:

Options Type Description
from
integer
Paginates search results by defining the offset from the first result you want to fetch. Usually used with the size option
scroll
string
Creates a forward-only result cursor. This option must be set with a time duration, at the end of which the cursor is destroyed.
If set, a cursor identifier named scrollId is returned in the results. This cursor can then be moved forward using the scroll function
size
integer
Sets the maximum number of documents returned per result page

Return #

The search function returns a promise resolving to a search result object, with the following properties:

Field Type Description
hits
object[]
Found documents. If a ObjectConstructor argument was provided to the repository constructor, then each hit is an instance of that class instead of a raw object
total
integer
Total number of found documents. Can be greater than the number of documents returned in this result set

scroll #

Moves a search cursor forward.

A search cursor is created by a search function call, with a scroll option value provided.

Arguments #

Copied to clipboard!
scroll(scrollId, [ttl]);

Arguments Type Description
scrollId
string
Scroll unique identifier, obtained by the last search/scroll function call (scroll identifiers may change from page to page)
ttl
string
Refreshes the cursor duration, using the time to live syntax

Return #

The scroll function returns a promise resolving to a search result object, with the following properties:

Field Type Description
hits
object[]
Found documents. If a ObjectConstructor argument was provided to the repository constructor, then each hit is an instance of that class instead of a raw object
total
integer
Total number of found documents. Can be greater than the number of documents returned in this result set

update #

Updates parts of a document's content.

Arguments #

Copied to clipboard!
update(document, [options]);

Arguments Type Description
document
object
Parts of the document to update. The provided object must contain a _id property, which is the document unique identifier
options
object
Optional arguments

options #

The options argument accepts the following parameters:

Options Type Description
refresh
string
If set with the wait_for string value, then the function will respond only after the document has been indexed

Return #

The update function returns a promise , resolving to the document update result.

Example #

Copied to clipboard!
const content = {
  _id: '<unique id>',
  someField: 'some content',
  anotherField: 'another content'
};

try {
  const result = repository.update(content);
  /*
   * Outputs:
   * { _index: '%<plugin name>',
   *   _type: '<collection>',
   *   _id: '<a unique id>',
   *   _version: 1,
   *   result: 'updated',
   *   _shards: { total: 2, successful: 1, failed: 0 },
   *   _source: {
   *     someField: 'some content',
   *     anotherField: 'another content'
   *   }
   * }
   */
  console.dir(result, { depth: null });
} catch (error) {
  // "error" is a KuzzleError object
}