KDC DDB
A lightweight object modeling for DynamoDB inspired by Mongoose.
The default database operations of this package assumes single table design but all can be overridden by writing your own.
Installation
npm i @kdcsoftware/ddb
Quick Start
-
Define database table name in environment variable.
In code:
process.env.DDB_TABLE = 'my-dynamodb-table';
or in serverless.yml:
provider: environment: DDB_TABLE: 'my-dynamodb-table'
or in package.json:
"scripts": { "start": "DDB_TABLE='my-dynamodb-table' node index.js" }
Note that table creation needs to be done outside this package to allow flexibility.
-
Define the fields of the document.
const fields = { id: { required: true }, name: { required: true }, type: { required: true }, };
-
Define primary and secondary keys
const pKey = { pk: '{type}#{id}', sk: 'ANIMAL#{type}' }; const sKey = { pk2: 'ANIMAL', sk2: '{type}#{id}' };
-
Create schema
const schema = new DDB.Schema(fields, pKey, sKey);
-
Create class
const Animal = DDB.model('Animal', schema);
-
Create an instance document from data object
const dog = new Animal({ id: 1, name: 'sparkle', type: 'dog' }); await dog.save();
-
Check the tests folder for more examples.
Debugging
This package uses debug.
To enable:
DEBUG=ddb:* node app.js