https://github.com/smartthingscommunity/file-context-store-nodejs
File-based context store for SmartApps
https://github.com/smartthingscommunity/file-context-store-nodejs
Last synced: 12 months ago
JSON representation
File-based context store for SmartApps
- Host: GitHub
- URL: https://github.com/smartthingscommunity/file-context-store-nodejs
- Owner: SmartThingsCommunity
- License: apache-2.0
- Created: 2020-12-10T14:42:26.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-07-19T16:58:30.000Z (over 1 year ago)
- Last Synced: 2025-04-10T03:06:29.332Z (12 months ago)
- Language: JavaScript
- Size: 146 KB
- Stars: 1
- Watchers: 8
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Javascript Local File Context Store
Used by the [SmartApp SDK](https://github.com/SmartThingsCommunity/smartapp-sdk-nodejs) to store IDs and access tokens for an installed instance of a SmartApp and retrieves that information for use in asynchronous API calls. The use of a context store is only needed when SmartApps have to call the SmartThings API in response to external events. SmartApps that only response to lifecycle events from the SmartThings platform will automatically have the proper context without the app having to store it.
The context stored by this module consists of the following data elements:
* **installedAppId**: the UUID of the installed app instance. This is the primary key of the table.
* **locationId**: the UUID of the location in which the app is installed
* **locale**: the locale client used to install the app
* **authToken**: the access token used in calling the API
* **refreshToken**: the refresh token used in generating a new access token when one expires
* **config**: the current installed app instance configuration, i.e. selected devices, options, etc.
## Installation
```bash
npm install @smartthings/file-context-store
```
## Usage
Create a `FileContextStore` object and pass it to the SmartApp connector to store the context files in
directory on the local machine.
```javascript
smartapp.contextStore(new FileContextStore())
```
The default storage is in a directory named `data` in the project location.
To locate the directory elsewhere specify the path to the directory to the
constructor.
```javascript
smartapp.contextStore(new FileContextStore('/opt/data/smartapp'))
```
## Storage Formats
### Installed App Context
Each installedApp instance context record is stored as a JSON string in a file with the name
`.json` in the data directory. For example:
```json
{
"installedAppId": "b643d57e-e2eb-40e4-b2ef-ff43519941cc",
"locationId": "8ea7ab21-932d-4256-80c6-abc53932dd3a",
"authToken": "f4b3b75c-091f-4b31-9833-7b52fe875ffb",
"refreshToken": "e980829a-9763-4105-b986-2d94114b1e80",
"clientId": "12475d16-ec68-490a-a708-6d390c112c7c",
"clientSecret": "2888d8f4-88b6-4741-a98e-54a267e6373b",
"config": {
"scenes": [
{
"valueType": "STRING",
"stringConfig": {
"value": "true"
}
}
],
"switches": [
{
"valueType": "STRING",
"stringConfig": {
"value": "true"
}
}
],
"locks": [
{
"valueType": "STRING",
"stringConfig": {
"value": "true"
}
}
]
}
}
```
### State Storage
State storage is a name-value store for the installed app instance. This is useful for storing information
between invocations of the SmartApp. Each state property is stored in a separate file with the name
`/.json` in the data directory. For example a numeric state property named
`count` with a value of `5` would be stored in a file named `b643d57e-e2eb-40e4-b2ef-ff43519941cc/count.json`:
```json
5
```
## Caveats
This data store is intended for development testing use only, not in a production
environment where scalability and redundancy is of primary concern.