diff --git a/README.md b/README.md index b83ad00..c2b7068 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ +# Adobe I/O Lib State + [![Version](https://img.shields.io/npm/v/@adobe/aio-lib-state.svg)](https://npmjs.org/package/@adobe/aio-lib-state) [![Downloads/week](https://img.shields.io/npm/dw/@adobe/aio-lib-state.svg)](https://npmjs.org/package/@adobe/aio-lib-state) ![Node.js CI](https://github.com/adobe/aio-lib-state/workflows/Node.js%20CI/badge.svg) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![Codecov Coverage](https://img.shields.io/codecov/c/github/adobe/aio-lib-state/master.svg?style=flat-square)](https://codecov.io/gh/adobe/aio-lib-state/) -# Adobe I/O Lib State - A Node JavaScript abstraction on top of distributed/cloud DBs that exposes a simple state persistence API. You can initialize the lib with your Adobe I/O Runtime (a.k.a OpenWhisk) credentials. -Alternatively, you can bring your own cloud db keys. As of now we only support Azure Cosmos. - Please note that currently you must be a customer of [Adobe Developer App Builder](https://www.adobe.io/apis/experienceplatform/project-firefly.html) to use this library. App Builder is a complete framework that enables enterprise developers to build and deploy custom web applications that extend Adobe Experience Cloud solutions and run on Adobe infrastructure. ## Install @@ -39,8 +37,6 @@ npm install @adobe/aio-lib-state // init when running in an Adobe I/O Runtime action (OpenWhisk) (uses env vars __OW_API_KEY and __OW_NAMESPACE automatically) const state = await stateLib.init() - // or if you want to use your own cloud DB account (make sure your partition key path is /partitionKey) - const state = await stateLib.init({ cosmos: { endpoint, masterKey, databaseId, containerId, partitionKey } }) // get const res = await state.get('key') // res = { value, expiration } @@ -48,10 +44,16 @@ npm install @adobe/aio-lib-state // put await state.put('key', 'value') - await state.put('key', { anObject: 'value' }, { ttl: -1 }) // -1 for no expiry, defaults to 86400 (24 hours) + await state.put('another key', 'another value', { ttl: -1 }) // -1 for no expiry, defaults to 86400 (24 hours) // delete await state.delete('key') + + // delete all keys and values + await state.deleteAll() + + // returns true if you have at least one key and value + await state.any() ``` ## Explore diff --git a/doc/api.md b/doc/api.md index 2a1b248..5e629ae 100644 --- a/doc/api.md +++ b/doc/api.md @@ -1,7 +1,7 @@ ## Classes
Cloud State Management
Promise.<StateStore>
object
Validates json according to a schema.
+Promise.<AdobeState>
Initializes and returns the key-value-store SDK.
To use the SDK you must either provide your
OpenWhisk credentials in
-config.ow
or your own
-Azure Cosmos credentials in config.cosmos
.
config.ow
or your own
OpenWhisk credentials can also be read from environment variables __OW_NAMESPACE
and __OW_API_KEY
.
object
An object holding the OpenWhisk credentials
+object
AdobeStateCredentials
object
An object holding the Azure Cosmos resource credentials with permissions on a single partition and container
+object
AdobeState put options
object
An object holding the Azure Cosmos account master key
+object
AdobeState get return object
object
StateStore put options
-object
StateStore get return object
+object
An object holding the OpenWhisk credentials
Object
object
object
State lib custom errors. +
object
Adobe State lib custom errors.
e.sdkDetails
provides additional context for each error (e.g. function parameter)
Promise.<StateStoreGetReturnValue>
](#StateStoreGetReturnValue)*
- * *[.put(key, value, [options])](#StateStore+put) ⇒ Promise.<string>
*
- * *[.delete(key)](#StateStore+delete) ⇒ Promise.<string>
*
- * *[._get(key)](#StateStore+_get) ⇒ [Promise.<StateStoreGetReturnValue>
](#StateStoreGetReturnValue)*
- * *[._put(key, value, options)](#StateStore+_put) ⇒ Promise.<string>
*
- * *[._delete(key)](#StateStore+_delete) ⇒ Promise.<string>
*
+* *[AdobeState](#AdobeState)*
+ * *[.get(key)](#AdobeState+get) ⇒ [Promise.<AdobeStateGetReturnValue>
](#AdobeStateGetReturnValue)*
+ * *[.put(key, value, [options])](#AdobeState+put) ⇒ Promise.<string>
*
+ * *[.delete(key)](#AdobeState+delete) ⇒ Promise.<string>
*
+ * *[.deleteAll()](#AdobeState+deleteAll) ⇒ Promise.<boolean>
*
+ * *[.any()](#AdobeState+any) ⇒ Promise.<boolean>
*
-
+
-### *stateStore.get(key) ⇒ [Promise.<StateStoreGetReturnValue>
](#StateStoreGetReturnValue)*
+### *adobeState.get(key) ⇒ [Promise.<AdobeStateGetReturnValue>
](#AdobeStateGetReturnValue)*
Retrieves the state value for given key.
If the key doesn't exist returns undefined.
-**Kind**: instance method of [StateStore
](#StateStore)
-**Returns**: [Promise.<StateStoreGetReturnValue>
](#StateStoreGetReturnValue) - get response holding value and additional info
+**Kind**: instance method of [AdobeState
](#AdobeState)
+**Returns**: [Promise.<AdobeStateGetReturnValue>
](#AdobeStateGetReturnValue) - get response holding value and additional info
| Param | Type | Description |
| --- | --- | --- |
| key | string
| state key identifier |
-
+
-### *stateStore.put(key, value, [options]) ⇒ Promise.<string>
*
+### *adobeState.put(key, value, [options]) ⇒ Promise.<string>
*
Creates or updates a state key-value pair
-**Kind**: instance method of [StateStore
](#StateStore)
+**Kind**: instance method of [AdobeState
](#AdobeState)
**Returns**: Promise.<string>
- key
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| key | string
| | state key identifier |
-| value | any
| | state value |
-| [options] | [StateStorePutOptions
](#StateStorePutOptions) | {}
| put options |
+| value | string
| | state value |
+| [options] | [AdobeStatePutOptions
](#AdobeStatePutOptions) | {}
| put options |
-
+
-### *stateStore.delete(key) ⇒ Promise.<string>
*
+### *adobeState.delete(key) ⇒ Promise.<string>
*
Deletes a state key-value pair
-**Kind**: instance method of [StateStore
](#StateStore)
+**Kind**: instance method of [AdobeState
](#AdobeState)
**Returns**: Promise.<string>
- key of deleted state or `null` if state does not exists
| Param | Type | Description |
| --- | --- | --- |
| key | string
| state key identifier |
-
+
-### *stateStore.\_get(key) ⇒ [Promise.<StateStoreGetReturnValue>
](#StateStoreGetReturnValue)*
-**Kind**: instance method of [StateStore
](#StateStore)
-**Returns**: [Promise.<StateStoreGetReturnValue>
](#StateStoreGetReturnValue) - get response holding value and additional info
-**Access**: protected
+### *adobeState.deleteAll() ⇒ Promise.<boolean>
*
+Deletes all key-values
-| Param | Type | Description |
-| --- | --- | --- |
-| key | string
| state key identifier |
+**Kind**: instance method of [AdobeState
](#AdobeState)
+**Returns**: Promise.<boolean>
- true if deleted, false if not
+
-
+### *adobeState.any() ⇒ Promise.<boolean>
*
+There exists key-values.
-### *stateStore.\_put(key, value, options) ⇒ Promise.<string>
*
-**Kind**: instance method of [StateStore
](#StateStore)
-**Returns**: Promise.<string>
- key
-**Access**: protected
+**Kind**: instance method of [AdobeState
](#AdobeState)
+**Returns**: Promise.<boolean>
- true if exists, false if not
+
-| Param | Type | Description |
-| --- | --- | --- |
-| key | string
| state key identifier |
-| value | any
| state value |
-| options | object
| state put options |
-
-
+## validate(schema, data) ⇒ object
+Validates json according to a schema.
-### *stateStore.\_delete(key) ⇒ Promise.<string>
*
-**Kind**: instance method of [StateStore
](#StateStore)
-**Returns**: Promise.<string>
- key of deleted state or `null` if state does not exists
-**Access**: protected
+**Kind**: global function
+**Returns**: object
- the result
| Param | Type | Description |
| --- | --- | --- |
-| key | string
| state key identifier |
+| schema | object
| the AJV schema |
+| data | object
| the json data to test |
-## init([config]) ⇒ [Promise.<StateStore>
](#StateStore)
+## init([config]) ⇒ [Promise.<AdobeState>
](#AdobeState)
Initializes and returns the key-value-store SDK.
To use the SDK you must either provide your
[OpenWhisk credentials](#OpenWhiskCredentials) in
`config.ow` or your own
-[Azure Cosmos credentials](#AzureCosmosMasterCredentials) in `config.cosmos`.
OpenWhisk credentials can also be read from environment variables `__OW_NAMESPACE` and `__OW_API_KEY`.
**Kind**: global function
-**Returns**: [Promise.<StateStore>
](#StateStore) - A StateStore instance
+**Returns**: [Promise.<AdobeState>
](#AdobeState) - An AdobeState instance
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [config] | object
| {}
| used to init the sdk |
| [config.ow] | [OpenWhiskCredentials
](#OpenWhiskCredentials) | | [OpenWhiskCredentials](#OpenWhiskCredentials). Set those if you want to use ootb credentials to access the state management service. OpenWhisk namespace and auth can also be passed through environment variables: `__OW_NAMESPACE` and `__OW_API_KEY` |
-| [config.cosmos] | [AzureCosmosMasterCredentials
](#AzureCosmosMasterCredentials) \| [AzureCosmosPartitionResourceCredentials
](#AzureCosmosPartitionResourceCredentials) | | [Azure Cosmos resource credentials](#AzureCosmosPartitionResourceCredentials) or [Azure Cosmos account credentials](#AzureCosmosMasterCredentials) |
-| [config.tvm] | object
| | tvm configuration, applies only when passing OpenWhisk credentials |
-| [config.tvm.apiUrl] | string
| | alternative tvm api url. |
-| [config.tvm.cacheFile] | string
| | alternative tvm cache file, set to `false` to disable caching of temporary credentials. |
-
+
-## OpenWhiskCredentials : object
-An object holding the OpenWhisk credentials
+## AdobeStateCredentials : object
+AdobeStateCredentials
**Kind**: global typedef
**Properties**
| Name | Type | Description |
| --- | --- | --- |
-| namespace | string
| user namespace |
-| auth | string
| auth key |
-
-
-
-## AzureCosmosPartitionResourceCredentials : object
-An object holding the Azure Cosmos resource credentials with permissions on a single partition and container
-
-**Kind**: global typedef
-**Properties**
+| namespace | string
| the state store namespace |
+| apikey | string
| the state store api key |
-| Name | Type | Description |
-| --- | --- | --- |
-| endpoint | string
| cosmosdb resource endpoint |
-| resourceToken | string
| cosmosdb resource token restricted to the partitionKey |
-| databaseId | string
| id for cosmosdb database |
-| containerId | string
| id for cosmosdb container within database |
-| partitionKey | string
| key for cosmosdb partition within container authorized by resource token |
+
-
-
-## AzureCosmosMasterCredentials : object
-An object holding the Azure Cosmos account master key
+## AdobeStatePutOptions : object
+AdobeState put options
**Kind**: global typedef
**Properties**
| Name | Type | Description |
| --- | --- | --- |
-| endpoint | string
| cosmosdb resource endpoint |
-| masterKey | string
| cosmosdb account masterKey |
-| databaseId | string
| id for cosmosdb database |
-| containerId | string
| id for cosmosdb container within database |
-| partitionKey | string
| key for cosmosdb partition where data will be stored |
+| ttl | number
| time-to-live for key-value pair in seconds, defaults to 24 hours (86400s). Set to < 0 for no expiry. A value of 0 sets default. |
-
+
-## StateStorePutOptions : object
-StateStore put options
+## AdobeStateGetReturnValue : object
+AdobeState get return object
**Kind**: global typedef
**Properties**
| Name | Type | Description |
| --- | --- | --- |
-| ttl | number
| time-to-live for key-value pair in seconds, defaults to 24 hours (86400s). Set to < 0 for no expiry. A value of 0 sets default. |
+| expiration | string
\| null
| ISO date string of expiration time for the key-value pair, if the ttl is infinite expiration=null |
+| value | any
| the value set by put |
-
+
-## StateStoreGetReturnValue : object
-StateStore get return object
+## OpenWhiskCredentials : object
+An object holding the OpenWhisk credentials
**Kind**: global typedef
**Properties**
| Name | Type | Description |
| --- | --- | --- |
-| expiration | string
\| null
| ISO date string of expiration time for the key-value pair, if the ttl is infinite expiration=null |
-| value | any
| the value set by put |
+| namespace | string
| user namespace |
+| auth | string
| auth key |
-
+
-## StateLibError : Object
+## AdobeStateLibError : object
**Kind**: global typedef
**Properties**
@@ -239,12 +205,12 @@ StateStore get return object
| message | string
| The message for the Error |
| code | string
| The code for the Error |
| sdk | string
| The SDK associated with the Error |
-| sdkDetails | Object
| The SDK details associated with the Error |
+| sdkDetails | object
| The SDK details associated with the Error |
-
+
-## StateLibErrors : object
-State lib custom errors.
+## AdobeStateLibErrors : object
+Adobe State lib custom errors.
`e.sdkDetails` provides additional context for each error (e.g. function parameter)
**Kind**: global typedef
@@ -252,10 +218,11 @@ State lib custom errors.
| Name | Type | Description |
| --- | --- | --- |
-| ERROR_BAD_ARGUMENT | [StateLibError
](#StateLibError) | this error is thrown when an argument is missing or has invalid type |
-| ERROR_BAD_REQUEST | [StateLibError
](#StateLibError) | this error is thrown when an argument has an illegal value. |
-| ERROR_NOT_IMPLEMENTED | [StateLibError
](#StateLibError) | this error is thrown when a method is not implemented or when calling methods directly on the abstract class (StateStore). |
-| ERROR_PAYLOAD_TOO_LARGE | [StateLibError
](#StateLibError) | this error is thrown when the state key, state value or underlying request payload size exceeds the specified limitations. |
-| ERROR_BAD_CREDENTIALS | [StateLibError
](#StateLibError) | this error is thrown when the supplied init credentials are invalid. |
-| ERROR_INTERNAL | [StateLibError
](#StateLibError) | this error is thrown when an unknown error is thrown by the underlying DB provider or TVM server for credential exchange. More details can be found in `e.sdkDetails._internal`. |
+| ERROR_BAD_ARGUMENT | [AdobeStateLibError
](#AdobeStateLibError) | this error is thrown when an argument is missing, has invalid type, or includes invalid characters. |
+| ERROR_BAD_REQUEST | [AdobeStateLibError
](#AdobeStateLibError) | this error is thrown when an argument has an illegal value. |
+| ERROR_PAYLOAD_TOO_LARGE | [AdobeStateLibError
](#AdobeStateLibError) | this error is thrown when the state key, state value or underlying request payload size exceeds the specified limitations. |
+| ERROR_BAD_CREDENTIALS | [AdobeStateLibError
](#AdobeStateLibError) | this error is thrown when the supplied init credentials are invalid. |
+| ERROR_UNAUTHORIZED | [AdobeStateLibError
](#AdobeStateLibError) | this error is thrown when the credentials are unauthorized to access the resource |
+| ERROR_INTERNAL | [AdobeStateLibError
](#AdobeStateLibError) | this error is thrown when an unknown error is thrown by the underlying DB provider or TVM server for credential exchange. More details can be found in `e.sdkDetails._internal`. |
+| ERROR_REQUEST_RATE_TOO_HIGH | [AdobeStateLibError
](#AdobeStateLibError) | this error is thrown when the request rate for accessing state is too high. |
diff --git a/lib/AdobeState.js b/lib/AdobeState.js
index 2b0081c..562c077 100644
--- a/lib/AdobeState.js
+++ b/lib/AdobeState.js
@@ -329,7 +329,7 @@ class AdobeState {
* Deletes all key-values
*
* @returns {Promise