|
1 | 1 | # firestore-context-store-nodejs
|
2 |
| -Stores SmartApp configuration and auth tokens for use in app-initiated calls |
| 2 | + |
| 3 | +<p> |
| 4 | +<a href="https://npmjs.org/package/@smartthings/firestore-context-store"><img src="https://badge.fury.io/js/@smartthings/firestore-context-store.svg"></a> |
| 5 | +<a href="https://circleci.com/gh/SmartThingsCommunity/firestore-context-store-nodejs.svg?style=svg"><img src="https://circleci.com/gh/SmartThingsCommunity/firestore-context-store-nodejs.svg?style=svg"></a> |
| 6 | +<a href="https://david-dm.org/SmartThingsCommunity/firestore-context-store-nodejs"><img src="https://david-dm.org/SmartThingsCommunity/firestore-context-store-nodejs.svg?theme=shields.io"></a> |
| 7 | +<a href="https://codecov.io/gh/SmartThingsCommunity/firestore-context-store-nodejs"><img src="https://codecov.io/gh/SmartThingsCommunity/firestore-context-store-nodejs/branch/master/graph/badge.svg" /></a> |
| 8 | +</p> |
| 9 | + |
| 10 | +> Stores SmartApp configuration and auth tokens for use in app-initiated calls |
| 11 | +
|
| 12 | +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 |
| 13 | +and retrieves that information for use in asynchronous API calls. The use of a context store |
| 14 | +is only needed when SmartApps have to call the SmartThings API in response to external |
| 15 | +events. SmartApps that only response to lifecycle events from the SmartThings platform |
| 16 | +will automatically have the proper context without the app having to store it. |
| 17 | + |
| 18 | +The context stored by this module consists of the following data elements: |
| 19 | + |
| 20 | +- **installedAppId**: the UUID of the installed app instance. This is the primary key of the table. |
| 21 | +- **locationId**: the UUID of the location in which the app is installed |
| 22 | +- **authToken**: the access token used in calling the API |
| 23 | +- **refreshToken**: the refresh token used in generating a new access token when one expires |
| 24 | +- **clientId**: the SmartApp's client ID, used in generating a new access token |
| 25 | +- **clientSecret**: the SmartApp's client secret, used in generating a new access token |
| 26 | +- **config**: the current installed app instance configuration, i.e. selected devices, options, etc.v |
| 27 | + |
| 28 | +## Installation |
| 29 | + |
| 30 | +```shell |
| 31 | +npm install @smartthings/firestore-context-store --save |
| 32 | +``` |
| 33 | + |
| 34 | +## Usage |
| 35 | + |
| 36 | +### A note about usage |
| 37 | + |
| 38 | +This package currently targets initializing Cloud Firestore from your own server by using a Service Account. If you are hosted on something like [Firebase Cloud Functions](https://firebase.google.com/docs/functions/) or [Google Cloud Platform](https://cloud.google.com/), we don't currently support those methodologies – feel free to contribute! |
| 39 | + |
| 40 | +To use this module to add Firebase Cloud Firestore context storage to your SmartApp, you should: |
| 41 | + |
| 42 | +1. **Generate a service account**. We use the Firebase Admin SDK. Go to IAM & admin > Service accounts in the Cloud Platform Console. Generate a new private key and save the JSON file. Then use the file to initialize the SDK in the next step. |
| 43 | + |
| 44 | +1. **`require()` your Google Services service account JSON file.** Be certain that you don't ever commit this file to a public repository. |
| 45 | + |
| 46 | +1. **Create a context store instance** with the service account object and the document collection name and pass it to the SmartApp SDK object. For example, the following code: |
| 47 | + |
| 48 | +```javascript |
| 49 | +const smartapp = require('@smartthings/smartapp'); |
| 50 | +const FirestoreDBContextStore = require('@smartthings/firestore-context-store'); |
| 51 | +const serviceAccount = require('./googleservices-YOURAPPNAME-AND-ID.json') |
| 52 | + |
| 53 | +smartapp |
| 54 | + .contextStore(new FirestoreDBContextStore(serviceAccount, 'installedapps')) |
| 55 | + .configureI18n() |
| 56 | + .page('mainPage', (page) => { |
| 57 | + ... |
| 58 | +``` |
| 59 | +
|
| 60 | +This will use a document collection named `installedapps` to store documents as keyed by the `installedAppId` value. |
| 61 | +
|
| 62 | +### Firebase Console |
| 63 | +
|
| 64 | +From the Firebase Cloud Firestore console, you will see a new document collection with persisted contexts. |
| 65 | +
|
| 66 | + |
| 67 | +
|
| 68 | +## More about SmartThings |
| 69 | +
|
| 70 | +If you are not familiar with SmartThings, we have |
| 71 | +[extensive on-line documentation](https://smartthings.developer.samsung.com/develop/index.html). |
| 72 | +
|
| 73 | +To create and manage your services and devices on SmartThings, create an account in the |
| 74 | +[developer workspace](https://devworkspace.developer.samsung.com/). |
| 75 | +
|
| 76 | +The [SmartThings Community](https://community.smartthings.com/c/developers/) is a good place share and |
| 77 | +ask questions. |
| 78 | +
|
| 79 | +There is also a [SmartThings reddit community](https://www.reddit.com/r/SmartThings/) where you |
| 80 | +can read and share information. |
| 81 | +
|
| 82 | +## License and Copyright |
| 83 | +
|
| 84 | +Licensed under the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0) |
| 85 | +
|
| 86 | +Copyright 2019 SmartThings, Inc. |
0 commit comments