@certik/skynet 0.15.1 → 0.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/availability.js +1 -1
- package/dynamodb.js +36 -4
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.16.1
|
|
4
|
+
|
|
5
|
+
- Removed verbose log from exponentialRetry (@certik/skynet/availablity)
|
|
6
|
+
|
|
7
|
+
## 0.16.0
|
|
8
|
+
|
|
9
|
+
- Added: dynamodb read command to get record
|
|
10
|
+
- Added: dynamodb transactWrite wrapper
|
|
11
|
+
|
|
3
12
|
## 0.15.1
|
|
4
13
|
|
|
5
14
|
- BREAKING: changed api restart behavior to never fail the allocation
|
package/availability.js
CHANGED
|
@@ -32,9 +32,9 @@ export async function exponentialRetry(func, { maxRetry, initialDuration, growFa
|
|
|
32
32
|
while (!test(result) && retries > 0) {
|
|
33
33
|
if (verbose) {
|
|
34
34
|
console.log("failed attempt result", result);
|
|
35
|
+
console.log(`sleep for ${duration}ms after failed attempt, remaining ${retries} attempts`);
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
console.log(`sleep for ${duration}ms after failed attempt, remaining ${retries} attempts`);
|
|
38
38
|
retries = retries - 1;
|
|
39
39
|
|
|
40
40
|
await wait(duration);
|
package/dynamodb.js
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
PutCommand,
|
|
7
7
|
QueryCommand,
|
|
8
8
|
UpdateCommand,
|
|
9
|
+
TransactWriteCommand,
|
|
9
10
|
} from "@aws-sdk/lib-dynamodb";
|
|
10
11
|
import { DynamoDBClient, DescribeTableCommand } from "@aws-sdk/client-dynamodb";
|
|
11
12
|
import { getAWSSDKConfig } from "./env.js";
|
|
@@ -153,6 +154,21 @@ async function createRecord(tableName, fields, verbose = false) {
|
|
|
153
154
|
return docClient.send(new PutCommand(params));
|
|
154
155
|
}
|
|
155
156
|
|
|
157
|
+
async function readRecord(tableName, key, verbose = false) {
|
|
158
|
+
if (verbose) {
|
|
159
|
+
console.log("reading", tableName, key);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const docClient = getDocClient();
|
|
163
|
+
|
|
164
|
+
const record = await docClient.send(new GetCommand({
|
|
165
|
+
TableName: tableName,
|
|
166
|
+
Key: key,
|
|
167
|
+
}));
|
|
168
|
+
|
|
169
|
+
return record.Item;
|
|
170
|
+
}
|
|
171
|
+
|
|
156
172
|
async function getRecordsByKey(tableName, keys, indexName) {
|
|
157
173
|
const docClient = getDocClient();
|
|
158
174
|
|
|
@@ -199,13 +215,18 @@ async function getRecordsByKey(tableName, keys, indexName) {
|
|
|
199
215
|
}
|
|
200
216
|
}
|
|
201
217
|
|
|
218
|
+
// Dual purpose for compatibility. If indexName is provided, it will use query command to get the record; if not, use get command which is most efficient.
|
|
202
219
|
async function getRecordByKey(tableName, keys, indexName) {
|
|
203
|
-
|
|
220
|
+
if (indexName) {
|
|
221
|
+
const records = await getRecordsByKey(tableName, keys, indexName);
|
|
204
222
|
|
|
205
|
-
|
|
206
|
-
|
|
223
|
+
if (records) {
|
|
224
|
+
return records[0];
|
|
225
|
+
} else {
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
207
228
|
} else {
|
|
208
|
-
return
|
|
229
|
+
return readRecord(tableName, keys);
|
|
209
230
|
}
|
|
210
231
|
}
|
|
211
232
|
|
|
@@ -451,6 +472,16 @@ async function deleteRecordsByHashKey(tableName, indexName, hashKeyValue, verbos
|
|
|
451
472
|
return totalDeleted;
|
|
452
473
|
}
|
|
453
474
|
|
|
475
|
+
async function transactWrite(items) {
|
|
476
|
+
const docClient = getDocClient();
|
|
477
|
+
|
|
478
|
+
return docClient.send(
|
|
479
|
+
new TransactWriteCommand({
|
|
480
|
+
TransactItems: items,
|
|
481
|
+
}),
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
|
|
454
485
|
export {
|
|
455
486
|
getDocClient,
|
|
456
487
|
// export common dynamodb commands to make it easier to use
|
|
@@ -469,4 +500,5 @@ export {
|
|
|
469
500
|
batchDeleteRecords,
|
|
470
501
|
deleteRecordsByHashKey,
|
|
471
502
|
createTableIfNotExist,
|
|
503
|
+
transactWrite,
|
|
472
504
|
};
|