dynastar

2.0.1 • Public • Published

dynastar

Build Status

A simple compatibility layer for dynamodb models to be compatible with the datastar model API

Install

npm install dynastar --save

Usage

When defining your dynamodb models, you use dynastar to expose them with a datastar API. You can optionally pass functions you would like to attach to the Dynastar class.

const Dynastar = require('dynastar');
const Joi = require('joi');

function defineMyModel(dynamo) {

  const model = dynamo.define('mymodel', {
    hashKey: 'hashme',
    rangeKey: 'ranger',
    schema: {
      hashme: Joi.string(),
      ranger: dynamo.types.timeUUID()
    }
  });
  //
  // A sync function must have a length of less than 2 if you want
  // to use the AwaitWrap wrapper
  //
  function exampleSyncFn(data) {
    // do something sync
    return someSyncResult;
  }

  function exampleAsyncFn(data, next) {
    // do something async
    next(null, someAsyncResult);
  }

  return new Dynastar({ model, hashKey: 'hashme', rangeKey: 'ranger', exampleSyncFn, exampleAsyncFn });
}

const mymodel = defineMyModel(require('dynamodb'));

AwaitWrap

If you would like to enable an awaitable model, we have a class for that.

Building on the previous example...

const { AwaitWrap } = require('dynastar');

const myAwaitModel = new AwaitWrap(mymodel);

// In this circumstance we have a sync function and async function that was
// added as "extra" onto the model itself. In this context the sync function
// is left untouched but the async callback function is made to be a `thenable`
// that can be awaited

const asyncResult = await myAwaitModel.exampleAsyncFn(data);
const syncResult = myAwaitModel.exampleSyncFn(data);

Key creation

Dynastar supports key builders for the hash and range keys. These are useful for combining multiple values into one.

createHashKey

createHashKey (or simply createKey) can be used to build a compound hash key.

const Dynastar = require('dynastar');
const Joi = require('joi');

function defineMyModel(dynamo) {
  const model = dynamo.define('mymodel', {
    hashKey: 'key',
    rangeKey: 'ranger',
    schema: {
      key: Joi.string(),
      ranger: Joi.string(),
      firstName: Joi.string(),
      lastName: Joi.string(),
      birthday: Joi.date()
    }
  });

  return new Dynastar({ 
    model, 
    hashKey: 'key', 
    rangeKey: 'ranger', 
    createHashKey: ({ firstName, lastName, birthday }) => `${firstName}!${lastName}!${birthday}`
  });
}

createRangeKey

createRangeKey can be used to build a compound range key.

const Dynastar = require('dynastar');
const Joi = require('joi');

function defineMyModel(dynamo) {

  const model = dynamo.define('mymodel', {
    hashKey: 'hashme',
    rangeKey: 'ranger',
    schema: {
      hashme: Joi.string(),
      ranger: Joi.string(),
      time: Joi.date().iso(),
      uuid: dynamo.types.uuid()
    }
  });

  return new Dynastar({ 
    model, 
    hashKey: 'hashme', 
    rangeKey: 'ranger', 
    createRangeKey: ({ time, uuid }) => `${time}#${uuid}`
  });
}

test

Run localstack locally in one terminal

npm run localstack

Run npm tests

npm test

Readme

Keywords

Package Sidebar

Install

npm i dynastar

Weekly Downloads

44

Version

2.0.1

License

MIT

Unpacked Size

39.3 kB

Total Files

11

Last publish

Collaborators

  • swaagie
  • mmason2
  • jpina1-godaddy
  • jgowdy
  • ibrandao
  • jpage
  • kinetifex
  • rxmarbles
  • kquerna
  • 3rdeden
  • jcrugzz
  • dcousineau
  • indexzero
  • fritzmonkey
  • msluther