-
Notifications
You must be signed in to change notification settings - Fork 1
/
addExampleData.js
43 lines (37 loc) · 1.26 KB
/
addExampleData.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
import { readFileSync } from 'fs';
import sequelize from './config/database.js';
import { KendoUIAppointment, KendoUIResource } from './models/index.js';
async function setupDatabase() {
// Wait for all models to synchronize with the database
await sequelize.sync();
// Now add example data
await addExampleData();
}
async function addExampleData() {
try {
// Read and parse the JSON data
const resourcesData = JSON.parse(
readFileSync('./initialData/kendouiResources.json')
);
const appointmentsData = JSON.parse(
readFileSync('./initialData/kendouiAppointments.json')
);
await sequelize.transaction(async(t) => {
const resources = await KendoUIResource.bulkCreate(resourcesData, {
transaction : t
});
const appointments = await KendoUIAppointment.bulkCreate(
appointmentsData,
{ transaction : t }
);
return { resources, appointments };
});
console.log(
'resources and appointments added to database successfully.'
);
}
catch (error) {
console.error('Failed to add data to database due to an error: ', error);
}
}
setupDatabase();