Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dynastar

Package Overview
Dependencies
Maintainers
15
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dynastar

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

  • 2.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7
increased by250%
Maintainers
15
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 13 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc