Integrations > Databases
Learn how to store user specific data to databases with the Jovo Persistence Layer.
This is an abstraction layer for persisting data across sessions. By default, the file-based system will be used so you can start right away when prototyping locally.
You can add different database integrations in the Jovo app constructor. This is the default configuration:
const config = {
db: {
type: 'file',
localDbFilename: 'db',
},
// Other configurations
}
This will save data with your user's userID
as a mainKey, and a key
and a value
specified by you.
The easiest way to do so is to use the user object for this:
this.user().data.key = value;
// Example
this.user().data.score = 300;
After you saved data, you can use a key
to retrieve a value
from the database.
Again, you can use the user object for this:
let data = this.user().data.key;
This will delete a data point from the database, specified by a key.
deleteData(key, callback)
this.db().deleteData(key, function(err) {
// Do something
});
This will delete your whole user's data (the mainKey
) from the database.
deleteUser(callback)
this.db().deleteUser(function(err) {
// Do something
});
Note: This is the default database integration.
The FilePersistence integration allows you to easily store user session data in a JSON file. This is especially helpful for local development and prototyping. Data will be stored to a db.json file by default.
This sort of data persistence is enabled by default. The db.json
can be found in the the following folder:
index.js
db/
-- db.json
// Other files
And this is an example how the file structure looks like, with the userID
as a mainKey and some persisted data with someKey
and someValue
, which can be added with this.user().data.someKey = 'someValue';
:
// Example for Amazon Alexa
[
{
"userId": "amzn1.ask.account.[some_user_id]",
"userData": {
"data": {
"someKey": "someValue"
},
"metaData": {
"createdAt": "2017-11-13T13:46:37.421Z",
"lastUsedAt": "2017-11-13T14:12:05.738Z",
"sessionsCount": 9
}
}
}
]
Tutorial: Add DynamoDB to Store User Data
The DynamoDB integration allows you to store user session data in the NoSQL service running on AWS. This integration is especially convenient if you're running your voice app on AWS Lambda. Learn more about DynamoDB here: aws.amazon.com/dynamodb.
If you're running on Lambda, you can simply integrate a DynamoDB table like this:
// Using the constructor
const config = {
db: {
type: 'dynamodb',
tableName: 'TableName',
},
// Other configurations
};
// Using the setter
app.setDynamoDb('TableName');
This will create a table with a name specified by you, and use this to store and load data. To make it work, you need to give your Lambda Role DynamoDB permissions.
You can find out more in the official documentation by Amazon: AWS Lambda Permissions Model.
In case you're hosting your voice app somewhere else, you can add DynamoDB with the following:
let awsConfig = {
accessKeyId: 'yourAccessKeyId',
secretAccessKey: 'yourSecretAccessKey',
region: 'yourRegion',
};
// Using the constructor
const config = {
db: {
type: 'dynamodb',
tableName: 'TableName',
awsConfig: awsConfig,
},
// Other configurations
};
// Using the setter
app.setDynamoDb('TableName', awsConfig);
You can find a detailed guide by Amazon about setting up your DynamoDB for programmatic access here: Setting Up DynamoDB (Web Service).
Here are a few things you need to consider when switching from a different database to DynamoDB
- DynamoDB does not allow empty strings (
""
) as values: If you use them, please switch tonull
or a different value