ASK Lowdb Persistence Adapter
An Alexa Skills Kit
persistence
adapter
for lowdb.
Why?
For deployed Alexa Skills, you should use a proper persistence layer (for example Amazon
S3 using the
ask-sdk-s3-persistence-adapter),
but for local development S3 can be awkward to use. Using lowdb is great because it saves
the data locally making it easy to debug and work with.
Installation
Install adapter:
npm i ask-lowdb-persistence-adapter
Install peer dependencies:
npm i lowdb
Usage
Basic usage:
const LowdbPersistenceAdapter = require("ask-lowdb-persistence-adapter");
const options = {
dbName: "persistenceAttributes.db.json",
attributesKey: "persistenceAttributes",
};
Alexa.SkillBuilders.custom()
.withPersistenceAdapter(new LowdbPersistenceAdapter(options))
.addRequestHandlers()
.lambda();
Since the primary purpose of this adapter is for use locally when a proper persistence
layer is unavailable, a nice pattern for creating your persistence adapter is to use a
factory. For example, if one wanted to use S3 when it was available and lowdb otherwise,
one could implement it like so:
const LowdbPersistenceAdapter = require("ask-lowdb-persistence-adapter");
const S3PersistenceAdapter = require("ask-sdk-s3-persistence-adapter")
.S3PersistenceAdapter;
class PersistenceAdapterFactory {
constructor(s3BucketName) {
this.s3BucketName = s3BucketName;
}
createPersistenceAdapter() {
if (this.s3BucketName) {
return new S3PersistenceAdapter({
bucketName: this.s3BucketName,
});
} else {
return new LowdbPersistenceAdapter();
}
}
}
const persistenceAdapterFactory = new PersistenceAdapterFactory(
process.env.S3_PERSISTENCE_BUCKET
);
Alexa.SkillBuilders.custom()
.withPersistenceAdapter(persistenceAdapterFactory.createPersistenceAdapter())
.addRequestHandlers()
.lambda();