Skip to content

Commit a6c1a87

Browse files
chore(release): 1.2.0 [skip ci]
# [1.2.0](mooyoul/dynamodb-actions@v1.1.3...v1.2.0) (2020-06-23) ### Features * **batch-put:** implement batch-put operation ([534873e](mooyoul@534873e)), closes [mooyoul#1](mooyoul#1) * **put:** add file input support ([d71159a](mooyoul@d71159a))
1 parent 534873e commit a6c1a87

File tree

9 files changed

+134
-11
lines changed

9 files changed

+134
-11
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# [1.2.0](https://github.com/mooyoul/dynamodb-actions/compare/v1.1.3...v1.2.0) (2020-06-23)
2+
3+
4+
### Features
5+
6+
* **batch-put:** implement batch-put operation ([534873e](https://github.com/mooyoul/dynamodb-actions/commit/534873e18f9671da9d1c0676680eabb71cb5f6dc)), closes [#1](https://github.com/mooyoul/dynamodb-actions/issues/1)
7+
* **put:** add file input support ([d71159a](https://github.com/mooyoul/dynamodb-actions/commit/d71159a4d7606f8de833565ece1f498271ea73aa))
8+
19
## [1.1.3](https://github.com/mooyoul/dynamodb-actions/compare/v1.1.2...v1.1.3) (2020-05-02)
210

311

dist/helpers/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.forgivingJSONParse = exports.createClient = void 0;
34
const dynamodb_1 = require("aws-sdk/clients/dynamodb");
45
const vm = require("vm");
56
function createClient(endpoint) {

dist/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ const processor = new processor_1.Processor();
77
(async () => {
88
var _a;
99
const input = {
10+
// Common
1011
operation: (_a = core.getInput("operation")) === null || _a === void 0 ? void 0 : _a.toLowerCase(),
1112
region: core.getInput("region"),
1213
table: core.getInput("table"),
14+
// Get / Delete Operation
1315
key: helpers_1.forgivingJSONParse(core.getInput("key")),
14-
item: helpers_1.forgivingJSONParse(core.getInput("item")),
1516
consistent: helpers_1.forgivingJSONParse(core.getInput("consistent")),
17+
// Put Operation
18+
item: helpers_1.forgivingJSONParse(core.getInput("item")),
19+
file: core.getInput("file"),
20+
// BatchPut Operation
21+
items: helpers_1.forgivingJSONParse(core.getInput("items")),
22+
files: core.getInput("files"),
1623
};
1724
const output = await processor.process(input);
1825
if (output) {

dist/operations/batch-put.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.BatchPutOperation = void 0;
4+
const glob = require("@actions/glob");
5+
const Joi = require("@hapi/joi");
6+
const fs_1 = require("fs");
7+
const helpers_1 = require("../helpers");
8+
const BaseInputSchema = Joi.object({
9+
operation: Joi.string().lowercase().valid("batch-put").required(),
10+
region: Joi.string().lowercase().required(),
11+
table: Joi.string().required(),
12+
});
13+
const InputSchema = Joi.alternatives([
14+
BaseInputSchema.append({
15+
items: Joi.array().items(Joi.object().required()).min(1).required(),
16+
}),
17+
BaseInputSchema.append({
18+
files: Joi.string().required(),
19+
}),
20+
]).required();
21+
class BatchPutOperation {
22+
constructor() {
23+
this.name = "batch-put";
24+
}
25+
async validate(input) {
26+
const validationResult = InputSchema.validate(input, {
27+
stripUnknown: true,
28+
});
29+
if (validationResult.error) {
30+
throw validationResult.error;
31+
}
32+
return validationResult.value;
33+
}
34+
async execute(input) {
35+
var _a, _b;
36+
const ddb = helpers_1.createClient(input.region);
37+
const items = input.items || await this.read(input.files);
38+
const chunks = this.chunk(items, 20);
39+
for (const chunk of chunks) {
40+
const res = await ddb.batchWrite({
41+
RequestItems: {
42+
[input.table]: chunk.map((item) => ({
43+
PutRequest: {
44+
Item: item,
45+
},
46+
})),
47+
},
48+
}).promise();
49+
const failedItems = (_b = (_a = res.UnprocessedItems) === null || _a === void 0 ? void 0 : _a[input.table]) !== null && _b !== void 0 ? _b : [];
50+
if (failedItems.length > 0) {
51+
console.error("UnprocessedItems: ", res.UnprocessedItems); // tslint:disable-line
52+
throw new Error("Got UnprocessedItems from DynamoDB");
53+
}
54+
}
55+
}
56+
async read(globs) {
57+
const globber = await glob.create(globs);
58+
const files = await globber.glob();
59+
if (files.length === 0) {
60+
throw new Error("Given glob does not match any files");
61+
}
62+
return Promise.all(files.map(async (file) => {
63+
const content = await fs_1.promises.readFile(file, { encoding: "utf8" });
64+
return JSON.parse(content);
65+
}));
66+
}
67+
chunk(items, size) {
68+
return items.reduce((collection, item) => {
69+
const lastChunk = collection[collection.length - 1];
70+
if (lastChunk.length < 20) {
71+
lastChunk.push(item);
72+
}
73+
else {
74+
collection.push([item]);
75+
}
76+
return collection;
77+
}, [[]]);
78+
}
79+
}
80+
exports.BatchPutOperation = BatchPutOperation;

dist/operations/delete.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.DeleteOperation = void 0;
34
const Joi = require("@hapi/joi");
45
const helpers_1 = require("../helpers");
56
const InputSchema = Joi.object({

dist/operations/get.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.GetOperation = void 0;
34
const Joi = require("@hapi/joi");
45
const helpers_1 = require("../helpers");
56
const InputSchema = Joi.object({

dist/operations/index.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
"use strict";
2-
function __export(m) {
3-
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4-
}
2+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3+
if (k2 === undefined) k2 = k;
4+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5+
}) : (function(o, m, k, k2) {
6+
if (k2 === undefined) k2 = k;
7+
o[k2] = m[k];
8+
}));
9+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
10+
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
11+
};
512
Object.defineProperty(exports, "__esModule", { value: true });
6-
__export(require("./delete"));
7-
__export(require("./get"));
8-
__export(require("./put"));
13+
__exportStar(require("./base"), exports);
14+
__exportStar(require("./batch-put"), exports);
15+
__exportStar(require("./delete"), exports);
16+
__exportStar(require("./get"), exports);
17+
__exportStar(require("./put"), exports);

dist/operations/put.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.PutOperation = void 0;
34
const Joi = require("@hapi/joi");
5+
const fs_1 = require("fs");
46
const helpers_1 = require("../helpers");
5-
const InputSchema = Joi.object({
7+
const BaseInputSchema = Joi.object({
68
operation: Joi.string().lowercase().valid("put").required(),
79
region: Joi.string().lowercase().required(),
810
table: Joi.string().required(),
9-
item: Joi.object().required(),
10-
}).required();
11+
});
12+
const InputSchema = Joi.alternatives([
13+
BaseInputSchema.append({
14+
item: Joi.object().required(),
15+
}),
16+
BaseInputSchema.append({
17+
file: Joi.string().required(),
18+
}),
19+
]).required();
1120
class PutOperation {
1221
constructor() {
1322
this.name = "put";
@@ -23,10 +32,15 @@ class PutOperation {
2332
}
2433
async execute(input) {
2534
const ddb = helpers_1.createClient(input.region);
35+
const item = input.item || await this.read(input.file);
2636
await ddb.put({
2737
TableName: input.table,
28-
Item: input.item,
38+
Item: item,
2939
}).promise();
3040
}
41+
async read(path) {
42+
const content = await fs_1.promises.readFile(path, { encoding: "utf8" });
43+
return JSON.parse(content);
44+
}
3145
}
3246
exports.PutOperation = PutOperation;

dist/processor.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.Processor = void 0;
34
const operations_1 = require("./operations");
45
class Processor {
56
constructor() {
67
this.operations = [
8+
new operations_1.BatchPutOperation(),
79
new operations_1.DeleteOperation(),
810
new operations_1.GetOperation(),
911
new operations_1.PutOperation(),

0 commit comments

Comments
 (0)