Skip to content

Commit 845b73d

Browse files
committed
Add docs for KeyValue model
1 parent 6752dd3 commit 845b73d

File tree

2 files changed

+134
-6
lines changed

2 files changed

+134
-6
lines changed

common/models/key-value-model.js

Lines changed: 133 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,163 @@
11
var g = require('strong-globalize')();
22

3+
/**
4+
* Data model for key-value databases.
5+
*
6+
* @class KeyValueModel
7+
* @inherits {Model}
8+
*/
9+
310
module.exports = function(KeyValueModel) {
4-
// TODO add api docs
11+
/**
12+
* Return the value associated with a given key.
13+
*
14+
* @param {String} key Key to use when searching the database.
15+
* @options {Object} options
16+
* @callback {Function} callback
17+
* @param {Error} err Error object.
18+
* @param {Any} result Value associated with the given key.
19+
* @promise
20+
*
21+
* @header KeyValueModel.get(key, cb)
22+
*/
523
KeyValueModel.get = function(key, options, callback) {
624
throwNotAttached(this.modelName, 'get');
725
};
826

9-
// TODO add api docs
27+
/**
28+
* Persist a value and associate it with the given key.
29+
*
30+
* @param {String} key Key to associate with the given value.
31+
* @param {Any} value Value to persist.
32+
* @options {Number|Object} options Optional settings for the key-value
33+
* pair. If a Number is provided, it is set as the TTL (time to live) in ms
34+
* (milliseconds) for the key-value pair.
35+
* @property {Number} ttl TTL for the key-value pair in ms.
36+
* @callback {Function} callback
37+
* @param {Error} err Error object.
38+
* @promise
39+
*
40+
* @header KeyValueModel.set(key, value, cb)
41+
*/
1042
KeyValueModel.set = function(key, value, options, callback) {
1143
throwNotAttached(this.modelName, 'set');
1244
};
1345

14-
// TODO add api docs
46+
/**
47+
* Set the TTL (time to live) in ms (milliseconds) for a given key. TTL is the
48+
* remaining time before a key-value pair is discarded from the database.
49+
*
50+
* @param {String} key Key to use when searching the database.
51+
* @param {Number} ttl TTL in ms to set for the key.
52+
* @options {Object} options
53+
* @callback {Function} callback
54+
* @param {Error} err Error object.
55+
* @promise
56+
*
57+
* @header KeyValueModel.expire(key, ttl, cb)
58+
*/
1559
KeyValueModel.expire = function(key, ttl, options, callback) {
1660
throwNotAttached(this.modelName, 'expire');
1761
};
1862

19-
// TODO add api docs
63+
/**
64+
* Return the TTL (time to live) for a given key. TTL is the remaining time
65+
* before a key-value pair is discarded from the database.
66+
*
67+
* @param {String} key Key to use when searching the database.
68+
* @options {Object} options
69+
* @callback {Function} callback
70+
* @param {Error} error
71+
* @param {Number} ttl Expiration time for the key-value pair. `undefined` if
72+
* TTL was not initially set.
73+
* @promise
74+
*
75+
* @header KeyValueModel.ttl(key, cb)
76+
*/
2077
KeyValueModel.ttl = function(key, options, callback) {
2178
throwNotAttached(this.modelName, 'ttl');
2279
};
2380

24-
// TODO add api docs
81+
/**
82+
* Return all keys in the database.
83+
*
84+
* **WARNING**: This method is not suitable for large data sets as all
85+
* key-values pairs are loaded into memory at once. For large data sets,
86+
* use `iterateKeys()` instead.
87+
*
88+
* @param {Object} filter An optional filter object with the following
89+
* @param {String} filter.match Glob string used to filter returned
90+
* keys (i.e. `userid.*`). All connectors are required to support `*` and
91+
* `?`, but may also support additional special characters specific to the
92+
* database.
93+
* @param {Object} options
94+
* @callback {Function} callback
95+
* @promise
96+
*
97+
* @header KeyValueModel.keys(filter, cb)
98+
*/
2599
KeyValueModel.keys = function(filter, options, callback) {
26100
throwNotAttached(this.modelName, 'keys');
27101
};
28102

29-
// TODO add api docs
103+
/**
104+
* Asynchronously iterate all keys in the database. Similar to `.keys()` but
105+
* instead allows for iteration over large data sets without having to load
106+
* everything into memory at once.
107+
*
108+
* Callback example:
109+
* ```js
110+
* // Given a model named `Color` with two keys `red` and `blue`
111+
* var iterator = Color.iterateKeys();
112+
* it.next(function(err, key) {
113+
* // key contains `red`
114+
* it.next(function(err, key) {
115+
* // key contains `blue`
116+
* });
117+
* });
118+
* ```
119+
*
120+
* Promise example:
121+
* ```js
122+
* // Given a model named `Color` with two keys `red` and `blue`
123+
* var iterator = Color.iterateKeys();
124+
* Promise.resolve().then(function() {
125+
* return it.next();
126+
* })
127+
* .then(function(key) {
128+
* // key contains `red`
129+
* return it.next();
130+
* });
131+
* .then(function(key) {
132+
* // key contains `blue`
133+
* });
134+
* ```
135+
*
136+
* @param {Object} filter An optional filter object with the following
137+
* @param {String} filter.match Glob string to use to filter returned
138+
* keys (i.e. `userid.*`). All connectors are required to support `*` and
139+
* `?`. They may also support additional special characters that are
140+
* specific to the backing database.
141+
* @param {Object} options
142+
* @returns {AsyncIterator} An Object implementing `next(cb) -> Promise`
143+
* function that can be used to iterate all keys.
144+
*
145+
* @header KeyValueModel.iterateKeys(filter)
146+
*/
30147
KeyValueModel.iterateKeys = function(filter, options) {
31148
throwNotAttached(this.modelName, 'iterateKeys');
32149
};
33150

151+
/*!
152+
* Set up remoting metadata for this model.
153+
*
154+
* **Notes**:
155+
* - The method is called automatically by `Model.extend` and/or
156+
* `app.registry.createModel`
157+
* - In general, base models use call this to ensure remote methods are
158+
* inherited correctly, see bug at
159+
* https://github.com/strongloop/loopback/issues/2350
160+
*/
34161
KeyValueModel.setup = function() {
35162
KeyValueModel.base.setup.apply(this, arguments);
36163

docs.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"common/models/application.js",
2525
"common/models/change.js",
2626
"common/models/email.js",
27+
"common/models/key-value-model.js",
2728
"common/models/role.js",
2829
"common/models/role-mapping.js",
2930
"common/models/scope.js",

0 commit comments

Comments
 (0)