-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathseed.js
50 lines (43 loc) · 1.37 KB
/
seed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* eslint-disable no-console */
const { PutCommand } = require("@aws-sdk/lib-dynamodb");
const { buildDynamoClient } = require("../../utils/dynamodb.js");
/**
* Custom handler for seeding deployed environments with required data.
* Simple functionality to add required section base templates to each branch
*/
async function myHandler(_event, _context, _callback) {
if (process.env.seedData !== "true") {
console.log("Seed data not enabled for environment, skipping.");
return;
}
console.log("Seeding Tables...");
const { tables } = require("./tables");
for (const table of tables) {
await runSeed(table);
}
console.log("Seed Finished");
}
/**
* @param {{
* filenames: string[];
* tableNameSuffix: string;
* }} seedInstructions
*/
const runSeed = async (seedInstructions) => {
const { filenames, tableNameSuffix } = seedInstructions;
const dynamoClient = buildDynamoClient();
for (const filename of filenames) {
const TableName = `${process.env.dynamoPrefix}-${tableNameSuffix}`;
if (!filenames || filenames <= 0) continue;
const items = require(filename);
if (!items || items.length <= 0) continue;
try {
for (const Item of items) {
await dynamoClient.send(new PutCommand({ TableName, Item }));
}
} catch (e) {
console.log(` -- ERROR UPLOADING ${TableName}\n`, e);
}
}
};
exports.handler = myHandler;