-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
fake-data.ts
38 lines (31 loc) · 911 Bytes
/
fake-data.ts
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
import {config} from 'dotenv'
import {faker} from '@faker-js/faker'
const envFile = process.argv[process.argv.length - 1]
config({path: envFile})
import {mongoClient, MONGODB_COLLECTION, MONGODB_DATABASE, User} from './util'
function createRandomUser() {
return {
userId: faker.datatype.uuid(),
fullName: faker.name.fullName(),
email: faker.internet.email(),
avatar: faker.image.avatar(),
registeredAt: faker.date.past(),
country: faker.address.countryCode(),
}
}
async function main() {
try {
const db = mongoClient.db(MONGODB_DATABASE)
const collection = db.collection<User>(MONGODB_COLLECTION)
const users = Array.from({length: 10000}).map((_value, index) => {
faker.seed(index)
return createRandomUser()
})
await collection.insertMany(users)
} catch (err) {
console.log(err)
} finally {
await mongoClient.close()
}
}
main()