Skip to content

Commit

Permalink
use newer feature store interface to ensure proper order of operations (
Browse files Browse the repository at this point in the history
  • Loading branch information
eli-darkly authored Jan 14, 2019
1 parent a6ac9a9 commit 5ebbe64
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
16 changes: 16 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"env": {
"test": {
"presets": [
[
"env",
{
"targets": {
"node": "6"
}
}
]
]
}
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ LaunchDarkly SDK for Node.js - DynamoDB integration

This library provides a DynamoDB-backed persistence mechanism (feature store) for the [LaunchDarkly Node.js SDK](https://github.com/launchdarkly/node-client), replacing the default in-memory feature store. It uses the AWS SDK for Node.js.

The minimum version of the LaunchDarkly Node.js SDK for use with this library is 5.6.1.
The minimum version of the LaunchDarkly Node.js SDK for use with this library is 5.7.0.

For more information, see also: [Using a persistent feature store](https://docs.launchdarkly.com/v2.0/docs/using-a-persistent-feature-store).

Expand Down
32 changes: 18 additions & 14 deletions dynamodb_feature_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,25 @@ function dynamoDBFeatureStoreInternal(tableName, options) {
});
};

store.initInternal = function(allData, cb) {
store.initOrderedInternal = function(allData, cb) {
readExistingItems(allData)
.then(function(existingItems) {
var existingNamespaceKeys = {};
for (var i = 0; i < existingItems.length; i++) {
existingNamespaceKeys[makeNamespaceKey(existingItems[i])] = existingItems[i].version;
}

// Always write the initialized token when we initialize.
var ops = [{PutRequest: { TableName: tableName, Item: initializedToken() }}];
delete existingNamespaceKeys[makeNamespaceKey(initializedToken())];

// Write all initial data (with version checks).
for (var kindNamespace in allData) {
for (var key in allData[kindNamespace]) {

// Write all initial data (without version checks).
var ops = [];
allData.forEach(function(collection) {
var kindNamespace = collection.kind.namespace;
collection.items.forEach(function(item) {
var key = item.key;
delete existingNamespaceKeys[kindNamespace + '$' + key];
ops.push({ PutRequest: makePutRequest(dataKind[kindNamespace], allData[kindNamespace][key]) });
}
}
ops.push({ PutRequest: makePutRequest(collection.kind, item) });
});
});

// Remove existing data that is not in the new list.
for (var namespaceKey in existingNamespaceKeys) {
Expand All @@ -101,6 +101,9 @@ function dynamoDBFeatureStoreInternal(tableName, options) {
}});
}

// Always write the initialized token when we initialize.
ops.push({PutRequest: { TableName: tableName, Item: initializedToken() }});

var writePromises = helpers.batchWrite(dynamoDBClient, tableName, ops);

return Promise.all(writePromises).then(function() { cb && cb(); });
Expand Down Expand Up @@ -151,7 +154,7 @@ function dynamoDBFeatureStoreInternal(tableName, options) {
};

store.close = function() {
// The node DynamoDB client is stateless, so close isn't a meaningful operation.
// The Node DynamoDB client is stateless, so close isn't a meaningful operation.
};

function queryParamsForNamespace(namespace) {
Expand All @@ -163,9 +166,10 @@ function dynamoDBFeatureStoreInternal(tableName, options) {
};
}

function readExistingItems(newData) {
function readExistingItems(allData) {
var p = Promise.resolve([]);
Object.keys(newData).forEach(function(namespace) {
allData.forEach(function(collection) {
var namespace = collection.kind.namespace;
p = p.then(function(previousItems) {
var params = queryParamsForNamespace(namespace);
return helpers.queryHelper(dynamoDBClient, params).then(function (items) {
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,30 @@
"test": "jest --ci --forceExit"
},
"devDependencies": {
"babel-core": "6.26.0",
"babel-jest": "22.4.3",
"babel-preset-env": "1.6.1",
"eslint": "5.8.0",
"jest": "23.6.0",
"jest-junit": "5.2.0",
"ldclient-node": ">= 5.6.1"
"ldclient-node": ">= 5.7.0"
},
"jest": {
"rootDir": ".",
"testEnvironment": "node",
"testMatch": [
"**/*-test.js"
],
"testResultsProcessor": "jest-junit"
"testResultsProcessor": "jest-junit",
"transformIgnorePatterns": []
},
"dependencies": {
"aws-sdk": "2.349.0",
"node-cache": "4.2.0",
"winston": "2.4.1"
},
"peerDependencies": {
"ldclient-node": ">= 5.6.1"
"ldclient-node": ">= 5.7.0"
},
"engines": {
"node": ">= 0.8.x"
Expand Down

0 comments on commit 5ebbe64

Please sign in to comment.