clarkie / dynogels

DynamoDB data mapper for node.js. Originally forked from https://github.com/ryanfitz/vogels
Other
490 stars 110 forks source link

Feature: Create a Dynogels Factory with Independent Configurations #118

Open naerbnic opened 6 years ago

naerbnic commented 6 years ago

Currently when you're configuring dynogels, you call a module-global method to initialize the configuration state:

const dynogels = require('dynogels');
dynogels.AWS.config.update(myConfig);

While convenient, this makes it difficult to run independent configurations, such as may be necessary with either multiple regions or with tests that point to local implementations.

I propose we add a factory method that constructs independent dynogels modules, as many other modules do. With a bike-sheddable interface:

const dynogels = require('dynogels').createWithConfig(myConfig1);

const users = dynogels.define('Users', ...)

This module is independent of others created with createWithConfig(), and global changes to configuration do not affect it.

The dynogels module itself would not change it's API for purposes of compatibility, so the first code block above would still operate as expected.

jkav77 commented 6 years ago

That sounds like a reasonable use case. Can it be solved with the feature to add an external dynamodb instance?

From the README.md:

You can also pass in a custom instance of the aws-sdk DynamoDB client

var dynamodb = new AWS.DynamoDB();
Account.config({dynamodb: dynamodb});

// or globally use custom DynamoDB instance
// all defined models will now use this driver
dynogels.dynamoDriver(dynamodb);
cdhowie commented 6 years ago

This is a useful and interesting feature. Bluebird has a similar thing that we use to augment the promise prototype with our own methods, without polluting Bluebird for any other modules that use it. As much as possible, I think Node modules should provide factories to produce new and independent module instances.

I will try to work on this over the next month or so.

pdeschen commented 6 years ago

FWIW, that would be definitely a nice way to hook in a dynamodb-local instance for testing purposes.