Is it possible to run with DynamoDB locally? Or to at least have the code intelligently use DynamoDB or FileDB depending on stage?


#1

In https://www.jovo.tech/tutorials/add-dynamodb-database, @jan seems to say we can set things up so development (default) runs with FileDB while production runs with DynamoDB. It seems to imply that to support this, app.js would require and app.use both databases.

If I do that, following the config.prod.js pattern shown there, then initializing DynamoDB in non-prod execution complains that the table name hasn’t been set.

Does the javascript code need to conditionally select one or the other database so dynamodb isn’t initialized when not being used? If so, is there a best practice for writing that conditional… and shouldn’t the tutorial spell that out?

If not, any idea what I’m doing wrong?


#2

You could do something like this:


if (Project.getStage() === 'prod') {
  app.use(
    new DynamoDb({
      tableName: process.env.DATABASE_USER
    }),
  );
} else {
  app.use(new FileDb());
}


#3

Yes, this is something that’s not super intuitive, which we updated in v4: https://v4.jovo.tech/docs/staging#app-staging


#4

Uhm… Looks reasonable, but where do I get Project? It isn’t defined in my code…


#5

Project.getStage() is a shortcut for process.env.JOVO_STAGE

const { App, Project } = require('jovo-framework');


#6

Tnx! (Yep, that did it. Now I need to get the lambda up and running.)


#7

To answer the original question: YES, DynamoDB can be downloaded and run locally. See

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html

That’s going to be helpful for other parts of my application. Lambdas run with an immutable filesystem, so storing any state information requires something like a database service.