forked from laardee/components
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
49 lines (43 loc) · 1.39 KB
/
index.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
const { readFile } = require('fs')
module.exports = {
async deploy(inputs, context) {
// TODO: move this functionality into aws-dynamodb component itself
const productsDb = await context.children.productsDb
const products = await new Promise((resolve, reject) =>
readFile('data/products.json', (err, data) => {
if (err) {
reject(err)
} else {
resolve(JSON.parse(data))
}
})
)
if (products.length > 0) {
const tablename = `products-${context.serviceId}`
context.log(`Seeding ${products.length} items into table ${tablename}.`)
const insertItem = (triesLeft, wait) => (product) =>
productsDb.fns
.insert(productsDb.inputs, {
log: context.log,
state: productsDb.state,
options: {
tablename,
itemdata: product
}
})
.catch(async (error) => {
if (triesLeft > 0) {
return new Promise((resolve, reject) => {
setTimeout(() => {
const doInsert = insertItem(triesLeft - 1, wait)(product)
doInsert.then(resolve, reject)
}, wait)
})
}
throw error
})
const insertions = products.map(JSON.stringify).map(insertItem(30, 8000))
await Promise.all(insertions)
}
}
}