Skip to content

Instantly share code, notes, and snippets.

@germanviscuso
Last active February 24, 2020 00:01
Show Gist options
  • Save germanviscuso/bb227655de976b61ba2d38740a175047 to your computer and use it in GitHub Desktop.
Save germanviscuso/bb227655de976b61ba2d38740a175047 to your computer and use it in GitHub Desktop.
Example on how to user personId as primary key in an Alexa Persistence adapter
function getPersistenceAdapter() {
// This function is an indirect way to detect if this is part of an Alexa-Hosted skill
function isAlexaHosted() {
return !!process.env.S3_PERSISTENCE_BUCKET;
if (isAlexaHosted()) {
const {S3PersistenceAdapter} = require('ask-sdk-s3-persistence-adapter');
return new S3PersistenceAdapter({
bucketName: process.env.S3_PERSISTENCE_BUCKET,
objectKeyGenerator: keyGenerator
});
} else {
// IMPORTANT: don't forget to give DynamoDB access to the role you're using to run this lambda (via IAM policy)
const {DynamoDbPersistenceAdapter} = require('ask-sdk-dynamodb-persistence-adapter');
return new DynamoDbPersistenceAdapter({
tableName: 'person_db',
createTable: true,
partitionKeyGenerator: keyGenerator
});
}
}
// This function establishes the primary key of the database as the skill id (hence you get global persistence, not per user id)
function keyGenerator(requestEnvelope) {
if (requestEnvelope.context.System.person
&& requestEnvelope.context.System.person.personId) {
return requestEnvelope.context.System.person.personId;
} else {
return requestEnvelope.context.System.user.userId;
}
}
// don't forget to pass to skill builder the persistentes adapter -> .withPersistenceAdapter(persistenceAdapter)
{
"name": "persons_table",
"version": "0.4.0",
"description": "happy birthday skill",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Amazon Alexa",
"dependencies": {
"ask-sdk-core": "^2.7.0",
"ask-sdk-model": "^1.18.0",
"i18next": "^15.0.5",
"ask-sdk-s3-persistence-adapter": "^2.7.0",
"ask-sdk-dynamodb-persistence-adapter": "^2.7.0",
"moment-timezone": "^0.5.23"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment