Skip to content

kdcio/ddb

Repository files navigation

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 size build Known Vulnerabilities Quality Gate Status Code Smells Coverage license

Installation

npm i @kdcio/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

Sonar Cloud Badges

Quality Gate Status Coverage Code Smells Bugs Lines of Code Duplicated Lines (%) Maintainability Rating Reliability Rating Security Rating Technical Debt Vulnerabilities