const database = new KojiBackend.Database({ res });
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}.
Instantiates the Database class.
config
- BackendConfigurationInput, Configuration information for the Koji app.
const database = new KojiBackend.Database({ res });
Adds data onto arrays in an existing database entry.
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.
Promise<Any>, An HTTP status code indicating whether the request was successful, or the updated entry if `returnDoc` was set to `true`.
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
Removes data from an existing database entry.
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.
Promise<Any>, An HTTP status code indicating whether the request was successful, or the updated entry if `returnDoc` was set to `true`.
const isRemoved = await database.arrayRemove('myCollection', 'myDocument', {
'myData1': 'myValue1',
'myData2': 'myValue2'
});
Source: backend/database/index.ts#L422
Deletes a database entry from a collection.
collection
- String, Name of the collection.
documentName
- String, Name of the entry.
Promise<DatabaseHttpStatusCode>, An HTTP status code indicating whether the request was successful.
const isDeleted = await database.delete('myCollection', 'myDocument');
Source: backend/database/index.ts#L450
Gets the specified database entry or collection of entries.
collection
- String, Name of the collection.
documentName
- String (Optional), Name of the entry.
Promise<Any>, Data requested from the collection.
const myData = await database.get('myCollection');
const myEntry = await database.get('myCollection','myDoc');
Source: backend/database/index.ts#L139
Gets the specified database entries.
collection
- String, Name of the collection.
documentNames
- String, Array of one or more entry names to retrieve.
Promise<T>, Data requested from the collection.
const myData = await database.getAll('myCollection', ['doc1', 'doc2']);
Source: backend/database/index.ts#L250
Searches a collection for records that satisfy the specified predicate. The predicate is specified using predicateKey, predicateOperator, and predicateValues.
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.
Promise<T>, Data requested from the collection.
const myData = await database.getAllWhere('myCollection',
'myField', '==', ['mySearchValue1', 'mySearchValue2']);
Source: backend/database/index.ts#L281
Gets a list of all collections available in the database.
Promise<String>, List containing the names of the collections.
const collections = await database.getCollections();
Source: backend/database/index.ts#L162
Searches a collection for records that satisfy the specified predicate. The predicate is specified using predicateKey, predicateOperator, and predicateValue.
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.
Promise<Any>, Data requested from the collection.
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.
collection
- String, Name of the collection.
queryKey
- String, Name of the search field.
queryValue
- String, Search value.
Promise<T>, Data requested from the collection.
const myData = await database.search('myCollection', 'myField', 'mySearchValue');
Source: backend/database/index.ts#L187
Adds an entry to a database collection.
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.
Promise<Any>, An HTTP status code indicating whether the request was successful, or the updated entry if `returnDoc` was set to `true`.
const myData = await database.set('myCollection', 'myDocument', {
'myData1': 'myValue1',
'myData2': 'myValue2'
});
Source: backend/database/index.ts#L314
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 .
|
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.
Promise<Any>, An HTTP status code indicating whether the request was successful, or the updated entry if `returnDoc` was set to `true`.
const myData = await database.update('myCollection', 'myDocument', {
'myData1': 'myValue1',
'myData2': 'myValue2'
});
Source: backend/database/index.ts#L350
Possible response values when interacting with the database API.
Available operator types for database comparisons.
Configuration information for the Koji app.
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]].