|
| 1 | +--- |
| 2 | +layout: docs |
| 3 | +toc: api-toc.html |
| 4 | +title: Context Store API |
| 5 | +--- |
| 6 | + |
| 7 | +**New in 0.19** |
| 8 | + |
| 9 | +A Context Storage plugin is a node.js module that exposes a function on its `module.exports` |
| 10 | +that can be used to create new instances of the plugin. The object returned by the |
| 11 | +function must have the following functions: |
| 12 | + |
| 13 | + Function | Description |
| 14 | +---------------------------------------------------------------|------------------------- |
| 15 | +[ContextStore.open()](#contextstoreopen) | Open the storage ready for use |
| 16 | +[ContextStore.close()](#contextstoreclose) | Close the storage |
| 17 | +[ContextStore.get(scope, key, callback)](#contextstoregetscope-key-callback) | Get values from the store |
| 18 | +[ContextStore.set(scope, key, value, callback)](#contextstoresetscope-key-value-callback) | Set values in the store |
| 19 | +[ContextStore.keys(scope, calback)](#contextstorekeysscope-callback) | Get a list of all keys in the store |
| 20 | +[ContextStore.delete(scope)](#contextstoredeletescope) | Delete all keys for a given scope |
| 21 | +[ContextStore.clean(activeNodes)](#contextstorecleanactivenodes) | Clean the context store |
| 22 | + |
| 23 | +### ContextStore.open() |
| 24 | + |
| 25 | +Open the storage ready for use. This is called before any store values are accessed. |
| 26 | + |
| 27 | +Returns a `Promise` that resolves when the store is ready for access. |
| 28 | + |
| 29 | +### ContextStore.close() |
| 30 | + |
| 31 | +Called when the runtime is stopped so no further key values will be accessed. |
| 32 | + |
| 33 | +Returns a `Promise` that resolves with the store is closed. |
| 34 | + |
| 35 | +### ContextStore.get(scope, key, [callback]) |
| 36 | + |
| 37 | +Argument | Description |
| 38 | +---------|------------------------------ |
| 39 | +scope | the scope of the key |
| 40 | +key | the key, or array of keys, to return the value(s) for. |
| 41 | +callback | *optional* a callback function to invoke with the key value |
| 42 | + |
| 43 | +The `key` argument can be either a String identifying a single key, or an Array |
| 44 | +of Strings identifying multiple keys to return the values for. |
| 45 | + |
| 46 | + |
| 47 | +If the optional `callback` argument is provided, it must be a function that takes |
| 48 | +two or more arguments: |
| 49 | + |
| 50 | +``` |
| 51 | +function callback(error, value1, value2, ... ) { |
| 52 | +
|
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +If no callback is provided, and the store supports synchronous access, the |
| 57 | +`get` function should return the individual value, or array of values for the keys. |
| 58 | +If the store does not support synchronous access it should throw an error. |
| 59 | + |
| 60 | +### ContextStore.set(scope, key, value, [callback]) |
| 61 | + |
| 62 | +Argument | Description |
| 63 | +---------|------------------------------ |
| 64 | +scope | the scope of the key |
| 65 | +key | the key, or array of keys, to set the value(s) for. |
| 66 | +value | the value, or array of values |
| 67 | +callback | *optional* a callback function to invoke when the value is set |
| 68 | + |
| 69 | +The `key` argument can be either a String identifying a single key, or an Array |
| 70 | +of Strings identifying multiple keys to set. |
| 71 | + |
| 72 | +`key` | `value` | Action |
| 73 | +-------------|----------------|---------------- |
| 74 | +String | Any | Stores `value` under `key` |
| 75 | +Array | Array | Stores each element of the `value` array under the corresponding `key` value. If `value` has fewer elements than `key`, it sets the missing values to `null`. |
| 76 | +Array | not-Array | Stores `value` as the value of the first key - uses `null` for any remaining keys. |
| 77 | + |
| 78 | + |
| 79 | +If the optional `callback` argument is provided, it will be called when the value |
| 80 | +has been stored. It takes a single argument, `error`, to indicate any errors hit |
| 81 | +whilst storing the values. |
| 82 | + |
| 83 | +``` |
| 84 | +function callback(error) { |
| 85 | +
|
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +If no callback is provided, and the store supports synchronous access, the |
| 90 | +`set` function should return once the value is stored. If the store does not support |
| 91 | +synchronous access it should throw an error. |
| 92 | + |
| 93 | +### ContextStore.keys(scope, [callback]) |
| 94 | + |
| 95 | +Argument | Description |
| 96 | +------------|------------------------ |
| 97 | +scope | the scope of the keys to return |
| 98 | +callback | *optional* a callback function to invoke with the list of keys |
| 99 | + |
| 100 | +Gets a list of all keys under the given scope. |
| 101 | + |
| 102 | +If the optional `callback` argument is provided, it must be a function that takes |
| 103 | +two or more arguments: |
| 104 | + |
| 105 | +``` |
| 106 | +function callback(error, keys) { |
| 107 | +
|
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +If no callback is provided, and the store supports synchronous access, the |
| 112 | +`keys` function should return the array of keys. If the store does not support |
| 113 | +synchronous access it should throw an error. |
| 114 | + |
| 115 | + |
| 116 | +### ContextStore.delete(scope) |
| 117 | + |
| 118 | +Argument | Description |
| 119 | +------------|------------------------ |
| 120 | +scope | the scope to delete |
| 121 | + |
| 122 | + |
| 123 | +### ContextStore.clean(activeNodes) |
| 124 | + |
| 125 | +Argument | Description |
| 126 | +------------|------------------------ |
| 127 | +activeNodes | a list of all node/flow ids that are still active |
| 128 | + |
| 129 | +Returns a promise that resolves when store has removed any context scopes that |
| 130 | +are no longer required. The `activeNodes` list can be used to identify what nodes |
| 131 | +and flows are still considered active. |
0 commit comments