-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmockDataGenerator.js
More file actions
68 lines (61 loc) · 1.7 KB
/
mockDataGenerator.js
File metadata and controls
68 lines (61 loc) · 1.7 KB
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
const sql = require('mssql');
const faker = require('faker');
const config = {
server: 'devdates.database.windows.net',
database: 'DevDatesDB',
user: 'master',
password: '************',
options: {
enableArithAbort: true,
trustServerCertificate: true,
},
};
async function connect() {
try {
await sql.connect(config);
console.log('Connected to the database');
} catch (error) {
console.error(error);
}
}
async function insertGenders() {
const genders = [
{ DisplayName: 'Male' },
{ DisplayName: 'Female' },
{ DisplayName: 'Non-binary' },
];
for (let gender of genders) {
try {
await sql.query`
INSERT INTO Genders (DisplayName, Created, Modified, ModifiedBy)
VALUES (${gender.DisplayName}, GETDATE(), GETDATE(), 'Admin')
`;
console.log(`Inserted gender ${gender.DisplayName}`);
} catch (error) {
console.error(error);
}
}
}
async function insertUsers() {
const genders = await sql.query`SELECT Id FROM Genders`;
for (let i = 0; i < 10; i++) {
const name = faker.name.findName();
const dob = faker.date.past(50);
const genderId = faker.random.arrayElement(genders.recordset).Id;
const bio = faker.lorem.sentence();
const email = faker.internet.email();
try {
await sql.query`
INSERT INTO Users (Name, DateOfBirth, GenderId, Bio, Email, Created, Modified, ModifiedBy)
VALUES (${name}, ${dob.toISOString()}, ${genderId}, ${bio}, ${email}, GETDATE(), GETDATE(), 'Admin')
`;
console.log(`Inserted user ${name}`);
} catch (error) {
console.error(error);
}
}
}
connect()
.then(insertGenders)
.then(insertUsers)
.catch((error) => console.error(error));