-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-fake-data.js
executable file
·169 lines (151 loc) · 3.5 KB
/
create-fake-data.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env node
'use strict'
const { prompt } = require('inquirer')
const faker = require('faker')
const readline = require('readline')
const { promises: fs, createReadStream, createWriteStream } = require('fs')
const { EOL } = require('os')
const DynamoDB = require('aws-sdk/clients/dynamodb')
async function run () {
const { env } = await prompt({
type: 'list',
name: 'env',
message: 'generate fake data for?',
choices: ['local', 'aws']
})
const stage =
env === 'local'
? env
: (
await prompt({
type: 'list',
name: 'stage',
message: 'stage?',
choices: ['dev', 'prod']
})
).stage
const dynamodb = new DynamoDB.DocumentClient({
convertEmptyValues: true,
...(env === 'local' && {
endpoint: 'http://127.0.0.1:8000',
region: 'us-east-1',
accessKeyId: 'x',
secretAccessKey: 'x'
})
})
const { max } = await prompt({
type: 'number',
name: 'max',
message: 'how many users?'
})
const ws = createWriteStream('./events.ldjson')
const ids = []
const telephoneNumbers = {}
await new Promise((resolve, reject) => {
ws.on('error', reject)
ws.on('finish', resolve)
let i = 0
while (i++ < max) {
const signup = createSignup()
ws.write(JSON.stringify(signup) + EOL)
const {
payload: { id }
} = signup
ids.push(id)
telephoneNumbers[id] = faker.phone.phoneNumber()
}
for (const [id, phoneNumber] of Object.entries(telephoneNumbers)) {
ws.write(JSON.stringify(addPhoneNumber(id, phoneNumber)) + EOL)
}
for (const [id, phoneNumber] of Object.entries(telephoneNumbers)) {
ws.write(JSON.stringify(verifyPhoneNumber(id, phoneNumber)) + EOL)
}
for (const id of ids) {
ws.write(JSON.stringify(addLocation(id)) + EOL)
}
ws.end()
})
const pending = []
for await (const { pk, sk, type, log, payload } of readEvents()) {
pending.push(() =>
dynamodb
.put({
TableName: `${stage}-dynamodb-logs`,
Item: {
pk,
sk,
type,
log,
payload
}
})
.promise()
)
if (pending.length > 20) {
await Promise.all(pending.map(fn => fn()))
pending.splice(0, pending.length)
}
}
await Promise.all(pending.map(fn => fn()))
}
run().catch(console.error)
function createSignup () {
const name = faker.name.findName()
const email = faker.internet.email()
const id = faker.datatype.uuid()
return {
pk: `users#${id}#stream`,
sk: 1,
type: 'signup',
log: 'users',
payload: {
id,
name,
email
}
}
}
function addPhoneNumber (id, phoneNumber) {
return {
pk: `users#${id}#stream`,
sk: 2,
type: 'addPhoneNumber',
log: 'users',
payload: {
id,
phoneNumber
}
}
}
function verifyPhoneNumber (id, phoneNumber) {
return {
pk: `users#${id}#stream`,
sk: 3,
type: 'verifyPhoneNumber',
log: 'users',
payload: {
id,
phoneNumber
}
}
}
function addLocation (id) {
return {
pk: `users#${id}#stream`,
sk: 4,
type: 'addLocation',
log: 'users',
payload: {
id,
long: Number(faker.address.longitude()),
lat: Number(faker.address.latitude())
}
}
}
async function * readEvents () {
for await (const line of readline.createInterface({
input: createReadStream('./events.ldjson')
})) {
yield JSON.parse(line)
}
}