Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 5ebbe64

Browse files
authored
use newer feature store interface to ensure proper order of operations (#4)
1 parent a6ac9a9 commit 5ebbe64

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

.babelrc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"env": {
3+
"test": {
4+
"presets": [
5+
[
6+
"env",
7+
{
8+
"targets": {
9+
"node": "6"
10+
}
11+
}
12+
]
13+
]
14+
}
15+
}
16+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ LaunchDarkly SDK for Node.js - DynamoDB integration
44

55
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.
66

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

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

dynamodb_feature_store.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,25 @@ function dynamoDBFeatureStoreInternal(tableName, options) {
6666
});
6767
};
6868

69-
store.initInternal = function(allData, cb) {
69+
store.initOrderedInternal = function(allData, cb) {
7070
readExistingItems(allData)
7171
.then(function(existingItems) {
7272
var existingNamespaceKeys = {};
7373
for (var i = 0; i < existingItems.length; i++) {
7474
existingNamespaceKeys[makeNamespaceKey(existingItems[i])] = existingItems[i].version;
7575
}
76-
77-
// Always write the initialized token when we initialize.
78-
var ops = [{PutRequest: { TableName: tableName, Item: initializedToken() }}];
7976
delete existingNamespaceKeys[makeNamespaceKey(initializedToken())];
80-
81-
// Write all initial data (with version checks).
82-
for (var kindNamespace in allData) {
83-
for (var key in allData[kindNamespace]) {
77+
78+
// Write all initial data (without version checks).
79+
var ops = [];
80+
allData.forEach(function(collection) {
81+
var kindNamespace = collection.kind.namespace;
82+
collection.items.forEach(function(item) {
83+
var key = item.key;
8484
delete existingNamespaceKeys[kindNamespace + '$' + key];
85-
ops.push({ PutRequest: makePutRequest(dataKind[kindNamespace], allData[kindNamespace][key]) });
86-
}
87-
}
85+
ops.push({ PutRequest: makePutRequest(collection.kind, item) });
86+
});
87+
});
8888

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

104+
// Always write the initialized token when we initialize.
105+
ops.push({PutRequest: { TableName: tableName, Item: initializedToken() }});
106+
104107
var writePromises = helpers.batchWrite(dynamoDBClient, tableName, ops);
105108

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

153156
store.close = function() {
154-
// The node DynamoDB client is stateless, so close isn't a meaningful operation.
157+
// The Node DynamoDB client is stateless, so close isn't a meaningful operation.
155158
};
156159

157160
function queryParamsForNamespace(namespace) {
@@ -163,9 +166,10 @@ function dynamoDBFeatureStoreInternal(tableName, options) {
163166
};
164167
}
165168

166-
function readExistingItems(newData) {
169+
function readExistingItems(allData) {
167170
var p = Promise.resolve([]);
168-
Object.keys(newData).forEach(function(namespace) {
171+
allData.forEach(function(collection) {
172+
var namespace = collection.kind.namespace;
169173
p = p.then(function(previousItems) {
170174
var params = queryParamsForNamespace(namespace);
171175
return helpers.queryHelper(dynamoDBClient, params).then(function (items) {

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,30 @@
88
"test": "jest --ci --forceExit"
99
},
1010
"devDependencies": {
11+
"babel-core": "6.26.0",
12+
"babel-jest": "22.4.3",
13+
"babel-preset-env": "1.6.1",
1114
"eslint": "5.8.0",
1215
"jest": "23.6.0",
1316
"jest-junit": "5.2.0",
14-
"ldclient-node": ">= 5.6.1"
17+
"ldclient-node": ">= 5.7.0"
1518
},
1619
"jest": {
1720
"rootDir": ".",
1821
"testEnvironment": "node",
1922
"testMatch": [
2023
"**/*-test.js"
2124
],
22-
"testResultsProcessor": "jest-junit"
25+
"testResultsProcessor": "jest-junit",
26+
"transformIgnorePatterns": []
2327
},
2428
"dependencies": {
2529
"aws-sdk": "2.349.0",
2630
"node-cache": "4.2.0",
2731
"winston": "2.4.1"
2832
},
2933
"peerDependencies": {
30-
"ldclient-node": ">= 5.6.1"
34+
"ldclient-node": ">= 5.7.0"
3135
},
3236
"engines": {
3337
"node": ">= 0.8.x"

0 commit comments

Comments
 (0)