|
1 | 1 | var g = require('strong-globalize')(); |
2 | 2 |
|
| 3 | +/** |
| 4 | + * Data model for key-value databases. |
| 5 | + * |
| 6 | + * @class KeyValueModel |
| 7 | + * @inherits {Model} |
| 8 | + */ |
| 9 | + |
3 | 10 | 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 | + */ |
5 | 23 | KeyValueModel.get = function(key, options, callback) { |
6 | 24 | throwNotAttached(this.modelName, 'get'); |
7 | 25 | }; |
8 | 26 |
|
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 | + */ |
10 | 42 | KeyValueModel.set = function(key, value, options, callback) { |
11 | 43 | throwNotAttached(this.modelName, 'set'); |
12 | 44 | }; |
13 | 45 |
|
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 | + */ |
15 | 59 | KeyValueModel.expire = function(key, ttl, options, callback) { |
16 | 60 | throwNotAttached(this.modelName, 'expire'); |
17 | 61 | }; |
18 | 62 |
|
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 | + */ |
20 | 77 | KeyValueModel.ttl = function(key, options, callback) { |
21 | 78 | throwNotAttached(this.modelName, 'ttl'); |
22 | 79 | }; |
23 | 80 |
|
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 | + */ |
25 | 99 | KeyValueModel.keys = function(filter, options, callback) { |
26 | 100 | throwNotAttached(this.modelName, 'keys'); |
27 | 101 | }; |
28 | 102 |
|
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 | + */ |
30 | 147 | KeyValueModel.iterateKeys = function(filter, options) { |
31 | 148 | throwNotAttached(this.modelName, 'iterateKeys'); |
32 | 149 | }; |
33 | 150 |
|
| 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 | + */ |
34 | 161 | KeyValueModel.setup = function() { |
35 | 162 | KeyValueModel.base.setup.apply(this, arguments); |
36 | 163 |
|
|
0 commit comments