Start typing to search...

Database

Implements a Koji database for the backend of your Koji app.

A Koji database is included with each Koji project and stores key-value pairs. For more information, see the {@doclink koji-database | Koji database developer guide}.

Constructor

Instantiates the Database class.

Parameters

Example

const database = new KojiBackend.Database({ res });

Methods

.arrayPush(collection, documentName, documentBody, returnDoc)

Adds data onto arrays in an existing database entry.

Parameters

  • collection - String, Name of the collection.

  • documentName - String, Name of the entry.

  • documentBody - Any, Key-value pairs of arrays and the entries to add to them.

  • returnDoc - Boolean (Optional), Whether to return the updated entry as a response.

Returns

Promise<Any>, An HTTP status code indicating whether the request was successful, or the updated entry if `returnDoc` was set to `true`.

Example

const doc = await database.arrayPush('myCollection', 'myDocument', {
 array1: 'newValue1',
 array2: 'newValue2',
}, true);

// Updated document after arrayPush
doc = {
 array1: ['existingValue1', 'newValue1'],
 array2: ['existingValue2', 'newValue2'],
}

Source: backend/database/index.ts#L389

.arrayRemove(collection, documentName, documentBody, returnDoc)

Removes data from an existing database entry.

Parameters

  • collection - String, Name of the collection.

  • documentName - String, Name of the entry.

  • documentBody - Any, Data to remove from the entry.

  • returnDoc - Boolean (Optional), Whether to return the updated entry as a response.

Returns

Promise<Any>, An HTTP status code indicating whether the request was successful, or the updated entry if `returnDoc` was set to `true`.

Example

const isRemoved = await database.arrayRemove('myCollection', 'myDocument', {
 'myData1': 'myValue1',
 'myData2': 'myValue2'
});

Source: backend/database/index.ts#L422

.delete(collection, documentName)

Deletes a database entry from a collection.

Parameters

  • collection - String, Name of the collection.

  • documentName - String, Name of the entry.

Returns

Promise<DatabaseHttpStatusCode>, An HTTP status code indicating whether the request was successful.

Example

const isDeleted = await database.delete('myCollection', 'myDocument');

Source: backend/database/index.ts#L450

.get(collection, documentName)

Gets the specified database entry or collection of entries.

Parameters

  • collection - String, Name of the collection.

  • documentName - String (Optional), Name of the entry.

Returns

Promise<Any>, Data requested from the collection.

Example

const myData = await database.get('myCollection');
const myEntry = await database.get('myCollection','myDoc');

Source: backend/database/index.ts#L139

.getAll(collection, documentNames)

Gets the specified database entries.

Parameters

  • collection - String, Name of the collection.

  • documentNames - String, Array of one or more entry names to retrieve.

Returns

Promise<T>, Data requested from the collection.

Example

const myData = await database.getAll('myCollection', ['doc1', 'doc2']);

Source: backend/database/index.ts#L250

.getAllWhere(collection, predicateKey, predicateOperation, predicateValues)

Searches a collection for records that satisfy the specified predicate. The predicate is specified using predicateKey, predicateOperator, and predicateValues.

Parameters

  • collection - String, Name of the collection.

  • predicateKey - String, Name of a field in the collection.

  • predicateOperation - PredicateOperator, Operator to use for the search.

  • predicateValues - String, Array of one or more search values.

Returns

Promise<T>, Data requested from the collection.

Example

const myData = await database.getAllWhere('myCollection',
 'myField', '==', ['mySearchValue1', 'mySearchValue2']);

Source: backend/database/index.ts#L281

.getCollections()

Gets a list of all collections available in the database.

Returns

Promise<String>, List containing the names of the collections.

Example

const collections = await database.getCollections();

Source: backend/database/index.ts#L162

.getWhere(collection, predicateKey, predicateOperation, predicateValue)

Searches a collection for records that satisfy the specified predicate. The predicate is specified using predicateKey, predicateOperator, and predicateValue.

Parameters

  • collection - String, Name of the collection.

  • predicateKey - String, Name of the field to search.

  • predicateOperation - PredicateOperator, Operator to use for the search.

  • predicateValue - String, Search value.

Returns

Promise<Any>, Data requested from the collection.

Example

const myData = await database.getWhere('myCollection',
 'myField', 'myOperator, 'mySearchValue');

Source: backend/database/index.ts#L219

Searches a collection for records that match the specified search criteria. The search criteria are the search field and the search value.

Parameters

  • collection - String, Name of the collection.

  • queryKey - String, Name of the search field.

  • queryValue - String, Search value.

Returns

Promise<T>, Data requested from the collection.

Example

const myData = await database.search('myCollection', 'myField', 'mySearchValue');

Source: backend/database/index.ts#L187

.set(collection, documentName, documentBody, returnDoc)

Adds an entry to a database collection.

Parameters

  • collection - String, Name of the collection.

  • documentName - String, Name of the entry.

  • documentBody - Any, Data for the entry.

  • returnDoc - Boolean (Optional), Whether to return the updated entry as a response.

Returns

Promise<Any>, An HTTP status code indicating whether the request was successful, or the updated entry if `returnDoc` was set to `true`.

Example

const myData = await database.set('myCollection', 'myDocument', {
 'myData1': 'myValue1',
 'myData2': 'myValue2'
});

Source: backend/database/index.ts#L314

.update(collection, documentName, documentBody, returnDoc)

Updates the specified data for an entry in the database collection.

Note
This method updates only the values that are specified in documentBody. If other values exist in the entry, they are not changed. If no existing entry matches the documentName, a new entry is created with the specified documentName and documentBody.

Parameters

  • collection - String, Name of the collection.

  • documentName - String, Name of the entry.

  • documentBody - Any, New data.

  • returnDoc - Boolean (Optional), Whether to return the updated entry as a response.

Returns

Promise<Any>, An HTTP status code indicating whether the request was successful, or the updated entry if `returnDoc` was set to `true`.

Example

const myData = await database.update('myCollection', 'myDocument', {
 'myData1': 'myValue1',
 'myData2': 'myValue2'
});

Source: backend/database/index.ts#L350

Enums

DatabaseHttpStatusCode

Possible response values when interacting with the database API.

Possible values

  • BAD_REQUEST = '400'
  • INTERNAL_SERVER_ERROR = '500'
  • NOT_FOUND = '404'
  • OK = '200'
  • PRECONDITION_FAILED = '412'
  • SERVICE_UNAVAILABLE = '503'
  • UNAUTHORIZED = '401'

PredicateOperator

Available operator types for database comparisons.

Possible values

  • ARRAY_CONTAINS = 'array-contains'
  • ARRAY_CONTAINS_ANY = 'array-contains-any'
  • EQUAL_TO = '=='
  • GREATER_THAN = '>'
  • GREATER_THAN_OR_EQUAL_TO = '>='
  • IN = 'in'
  • LESS_THAN = '<'
  • LESS_THAN_OR_EQUAL_TO = '<='
  • NOT_EQUAL_TO = '!='
  • NOT_IN = 'not-in'

Interfaces

BackendConfigurationInput

Configuration information for the Koji app.

Properties

  • projectId - String (Optional), Unique identifier for the Koji app. Will override data passed through `res`.

  • projectToken - String (Optional), Secret key for the Koji app. Will override data passed through `res`.

  • res - Response (Optional), Express response object. Used in conjunction with middleware to scope environment variables for customized versions of the original Koji app. For the original definition see [[https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/express/index.d.ts#L127 | @types/express]].