Skip to content

Commit

Permalink
Recreate dynamo db instance on .local()
Browse files Browse the repository at this point in the history
The current solution requires .local() to be called first in order to
use dynamo db locally.

Recreating dynamo db instance when calling .local() solves issues like
script import order.
  • Loading branch information
Renan Ferreira committed May 7, 2018
1 parent 310da5c commit 02d4cc5
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
var Schema = require('./Schema');
var Model = require('./Model');
var https = require('https');
var AWS = require('aws-sdk');

var debug = require('debug')('dynamoose');

function createLocalDb(endpointURL) {
return new AWS.DynamoDB({
endpoint: new AWS.Endpoint(endpointURL)
});
}

function Dynamoose () {
this.models = {};

Expand Down Expand Up @@ -50,10 +57,15 @@ Dynamoose.prototype.model = function(name, schema, options) {

Dynamoose.prototype.VirtualType = require('./VirtualType');

Dynamoose.prototype.AWS = require('aws-sdk');
Dynamoose.prototype.AWS = AWS;

Dynamoose.prototype.driver = function driver(driver) {
this.dynamoDB = driver;
};

Dynamoose.prototype.local = function (url) {
this.endpointURL = url || 'http://localhost:8000';
this.driver(createLocalDb(url));
debug('Setting DynamoDB to local (%s)', this.endpointURL);
};

Expand All @@ -80,9 +92,7 @@ Dynamoose.prototype.ddb = function () {
}
if(this.endpointURL) {
debug('Setting DynamoDB to %s', this.endpointURL);
this.dynamoDB = new this.AWS.DynamoDB({
endpoint: new this.AWS.Endpoint(this.endpointURL)
});
this.driver(createLocalDb(url));
} else {
debug('Getting default DynamoDB');
this.dynamoDB = new this.AWS.DynamoDB({
Expand Down

0 comments on commit 02d4cc5

Please sign in to comment.