2018-05-02 23:21:49 +00:00
|
|
|
// Copyright IBM Corp. 2018. All Rights Reserved.
|
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
|
|
|
import {Callback, Options, PromiseOrVoid} from './common';
|
|
|
|
import {ModelBase, ModelData} from './model';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data object for KV models
|
|
|
|
*/
|
2018-07-13 15:28:56 +00:00
|
|
|
export type KVData<T extends ModelBase = ModelBase> = ModelData<T>;
|
2018-05-02 23:21:49 +00:00
|
|
|
|
|
|
|
/**
|
2018-07-13 15:28:56 +00:00
|
|
|
* Key/Value model. Strictly speaking, KeyValueModel is not a class
|
|
|
|
* but a mixin into existing model classes
|
2018-05-02 23:21:49 +00:00
|
|
|
*/
|
|
|
|
export declare class KeyValueModel extends ModelBase {
|
|
|
|
/**
|
|
|
|
* Return the value associated with a given key.
|
|
|
|
*
|
2018-07-13 15:28:56 +00:00
|
|
|
* @param {string} key Key to use when searching the database.
|
2018-05-02 23:21:49 +00:00
|
|
|
* @options {Object} options
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err Error object.
|
2018-07-13 15:28:56 +00:00
|
|
|
* @param {any} result Value associated with the given key.
|
2018-05-02 23:21:49 +00:00
|
|
|
* @promise
|
|
|
|
*
|
|
|
|
* @header KeyValueModel.get(key, cb)
|
|
|
|
*/
|
2018-07-13 15:28:56 +00:00
|
|
|
static get(
|
2018-05-02 23:21:49 +00:00
|
|
|
key: string,
|
|
|
|
options?: Options,
|
|
|
|
callback?: Callback<KVData>,
|
|
|
|
): PromiseOrVoid<KVData>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Persist a value and associate it with the given key.
|
|
|
|
*
|
2018-07-13 15:28:56 +00:00
|
|
|
* @param {string} key Key to associate with the given value.
|
|
|
|
* @param {any} value Value to persist.
|
2018-05-02 23:21:49 +00:00
|
|
|
* @options {Number|Object} options Optional settings for the key-value
|
|
|
|
* pair. If a Number is provided, it is set as the TTL (time to live) in ms
|
|
|
|
* (milliseconds) for the key-value pair.
|
|
|
|
* @property {Number} ttl TTL for the key-value pair in ms.
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err Error object.
|
|
|
|
* @promise
|
|
|
|
*
|
|
|
|
* @header KeyValueModel.set(key, value, cb)
|
|
|
|
*/
|
2018-07-13 15:28:56 +00:00
|
|
|
static set(
|
2018-05-02 23:21:49 +00:00
|
|
|
key: string,
|
|
|
|
value: KVData,
|
|
|
|
options?: Options,
|
2018-07-13 15:28:56 +00:00
|
|
|
callback?: Callback<void>,
|
|
|
|
): PromiseOrVoid<void>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the key-value pair associated to the given key.
|
|
|
|
*
|
|
|
|
* @param {string} key Key to use when searching the database.
|
|
|
|
* @options {Object} options
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err Error object.
|
|
|
|
* @param {*} result Value associated with the given key.
|
|
|
|
* @promise
|
|
|
|
*/
|
|
|
|
static delete(
|
|
|
|
key: string,
|
|
|
|
options?: Options,
|
|
|
|
callback?: Callback<void>,
|
|
|
|
): PromiseOrVoid<void>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all keys (and values) associated to the current model.
|
|
|
|
*
|
|
|
|
* @options {Object} options Unused ATM, placeholder for future options.
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err Error object.
|
|
|
|
* @promise
|
|
|
|
*/
|
|
|
|
static deleteAll(
|
|
|
|
options?: Options,
|
|
|
|
callback?: Callback<void>,
|
|
|
|
): PromiseOrVoid<void>;
|
2018-05-02 23:21:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the TTL (time to live) in ms (milliseconds) for a given key. TTL is the
|
|
|
|
* remaining time before a key-value pair is discarded from the database.
|
|
|
|
*
|
2018-07-13 15:28:56 +00:00
|
|
|
* @param {string} key Key to use when searching the database.
|
2018-05-02 23:21:49 +00:00
|
|
|
* @param {Number} ttl TTL in ms to set for the key.
|
|
|
|
* @options {Object} options
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err Error object.
|
|
|
|
* @promise
|
|
|
|
*
|
|
|
|
* @header KeyValueModel.expire(key, ttl, cb)
|
|
|
|
*/
|
2018-07-13 15:28:56 +00:00
|
|
|
static expire(
|
2018-05-02 23:21:49 +00:00
|
|
|
key: string,
|
|
|
|
ttl: number,
|
|
|
|
options?: Options,
|
2018-07-13 15:28:56 +00:00
|
|
|
callback?: Callback<void>,
|
|
|
|
): PromiseOrVoid<void>;
|
2018-05-02 23:21:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the TTL (time to live) for a given key. TTL is the remaining time
|
|
|
|
* before a key-value pair is discarded from the database.
|
|
|
|
*
|
2018-07-13 15:28:56 +00:00
|
|
|
* @param {string} key Key to use when searching the database.
|
2018-05-02 23:21:49 +00:00
|
|
|
* @options {Object} options
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} error
|
|
|
|
* @param {Number} ttl Expiration time for the key-value pair. `undefined` if
|
|
|
|
* TTL was not initially set.
|
|
|
|
* @promise
|
|
|
|
*
|
|
|
|
* @header KeyValueModel.ttl(key, cb)
|
|
|
|
*/
|
2018-07-13 15:28:56 +00:00
|
|
|
static ttl(
|
2018-05-02 23:21:49 +00:00
|
|
|
key: string,
|
|
|
|
options?: Options,
|
|
|
|
callback?: Callback<number>,
|
|
|
|
): PromiseOrVoid<number>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all keys in the database.
|
|
|
|
*
|
|
|
|
* **WARNING**: This method is not suitable for large data sets as all
|
|
|
|
* key-values pairs are loaded into memory at once. For large data sets,
|
|
|
|
* use `iterateKeys()` instead.
|
|
|
|
*
|
|
|
|
* @param {Object} filter An optional filter object with the following
|
2018-07-13 15:28:56 +00:00
|
|
|
* @param {string} filter.match Glob string used to filter returned
|
2018-05-02 23:21:49 +00:00
|
|
|
* keys (i.e. `userid.*`). All connectors are required to support `*` and
|
|
|
|
* `?`, but may also support additional special characters specific to the
|
|
|
|
* database.
|
|
|
|
* @param {Object} options
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @promise
|
|
|
|
*
|
|
|
|
* @header KeyValueModel.keys(filter, cb)
|
|
|
|
*/
|
2018-07-13 15:28:56 +00:00
|
|
|
static keys(
|
|
|
|
filter?: KVFilter,
|
2018-05-02 23:21:49 +00:00
|
|
|
options?: Options,
|
|
|
|
callback?: Callback<string[]>,
|
|
|
|
): PromiseOrVoid<string[]>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously iterate all keys in the database. Similar to `.keys()` but
|
|
|
|
* instead allows for iteration over large data sets without having to load
|
|
|
|
* everything into memory at once.
|
|
|
|
*
|
|
|
|
* Callback example:
|
|
|
|
* ```js
|
|
|
|
* // Given a model named `Color` with two keys `red` and `blue`
|
|
|
|
* var iterator = Color.iterateKeys();
|
|
|
|
* it.next(function(err, key) {
|
|
|
|
* // key contains `red`
|
|
|
|
* it.next(function(err, key) {
|
|
|
|
* // key contains `blue`
|
|
|
|
* });
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Promise example:
|
|
|
|
* ```js
|
|
|
|
* // Given a model named `Color` with two keys `red` and `blue`
|
|
|
|
* var iterator = Color.iterateKeys();
|
|
|
|
* Promise.resolve().then(function() {
|
|
|
|
* return it.next();
|
|
|
|
* })
|
|
|
|
* .then(function(key) {
|
|
|
|
* // key contains `red`
|
|
|
|
* return it.next();
|
|
|
|
* });
|
|
|
|
* .then(function(key) {
|
|
|
|
* // key contains `blue`
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param {Object} filter An optional filter object with the following
|
2018-07-13 15:28:56 +00:00
|
|
|
* @param {string} filter.match
|
2018-05-02 23:21:49 +00:00
|
|
|
* @param {Object} options
|
2018-07-13 15:28:56 +00:00
|
|
|
* @returns {AsyncKeyIterator} An Object implementing `next(cb) -> Promise`
|
2018-05-02 23:21:49 +00:00
|
|
|
* function that can be used to iterate all keys.
|
|
|
|
*
|
|
|
|
* @header KeyValueModel.iterateKeys(filter)
|
|
|
|
*/
|
2018-07-13 15:28:56 +00:00
|
|
|
static iterateKeys(filter?: KVFilter, options?: Options): AsyncKeyIterator;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type KVFilter = {
|
|
|
|
/**
|
|
|
|
* Glob string to use to filter returned keys (i.e. `userid.*`). All connectors
|
|
|
|
* are required to support `*` and `?`. They may also support additional special
|
|
|
|
* characters that are specific to the backing database.
|
|
|
|
*/
|
|
|
|
match: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Async iterator to return keys one by one. The value will be undefined if there is
|
|
|
|
* no more keys
|
|
|
|
*/
|
|
|
|
export interface AsyncKeyIterator {
|
|
|
|
/**
|
|
|
|
* Try to fetch the next key
|
|
|
|
* @param callback Callback function. If not provided, the return value will be
|
|
|
|
* a promise
|
|
|
|
*/
|
|
|
|
next(
|
|
|
|
callback?: Callback<string | undefined>,
|
|
|
|
): PromiseOrVoid<string | undefined>;
|
2018-05-02 23:21:49 +00:00
|
|
|
}
|