@kdcsoftware/ddb
TypeScript icon, indicating that this package has built-in type declarations

0.8.5 • Public • Published

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.

ver build codecov size license

Installation

npm i @kdcsoftware/ddb

Quick Start

  1. 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.

  2. Define the fields of the document.

    const fields = {
      id: { required: true },
      name: { required: true },
      type: { required: true },
    };
  3. Define primary and secondary keys

    const pKey = { pk: '{type}#{id}', sk: 'ANIMAL#{type}' };
    const sKey = { pk2: 'ANIMAL', sk2: '{type}#{id}' };
  4. Create schema

    const schema = new DDB.Schema(fields, pKey, sKey);
  5. Create class

    const Animal = DDB.model('Animal', schema);
  6. Create an instance document from data object

    const dog = new Animal({ id: 1, name: 'sparkle', type: 'dog' });
    await dog.save();
  7. Check the tests folder for more examples.

Debugging

This package uses debug.

To enable:

DEBUG=ddb:* node app.js

Package Sidebar

Install

npm i @kdcsoftware/ddb

Weekly Downloads

0

Version

0.8.5

License

MIT

Unpacked Size

376 kB

Total Files

53

Last publish

Collaborators

  • ianpogi5
  • mbungalso