-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeder.js
146 lines (125 loc) · 4.86 KB
/
seeder.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
const { sequelize, Setting, Block, BlockOption, BlockImage } = require('./models');
const { settings, blocks } = require('./samples/datas.js');
const seedDatabase = async () => {
let forceRefresh = false;
if (process.argv.length > 2) {
forceRefresh = process.argv[2] === '--force';
}
await sequelize.sync({ force: forceRefresh });
// await Setting.create({ key: 'businessName', value: 'Toko' });
for (const setting of settings) {
// await Setting.create({
// key: setting.key,
// value: setting.value
// });
// await Setting.findOrCreate({
// where: { key: setting.key },
// defaults: { value: setting.value }
// });
// find or create or update
const [instance, created] = await Setting.findOrCreate({
where: { key: setting.key },
defaults: { value: setting.value }
});
if (!created) {
instance.value = setting.value;
await instance.save();
}
}
for (const block of blocks) {
// await Block.create({
// type: block.type,
// text: block.text,
// // nextId: block.next || null,
// input: block.input || null,
// isStartPoint: block.isStartPoint || false,
// matchRules: block.matchRules || null
// });
// await Block.findOrCreate({
// where: { text: block.text, type: block.type },
// defaults: {
// type: block.type,
// input: block.input || null,
// isStartPoint: block.isStartPoint || false,
// matchRules: block.matchRules || null
// }
// });
// find or create or update
const [instance, created] = await Block.findOrCreate({
where: { id: block.id, type: block.type },
defaults: {
text: block.text,
type: block.type,
input: block.input || null,
isStartPoint: block.isStartPoint || false,
// matchRules: block.matchRules || null
matchRules: block.matchRules ? JSON.parse(JSON.stringify(block.matchRules)) : null
}
});
if (!created) {
instance.text = block.text;
instance.input = block.input || null;
instance.isStartPoint = block.isStartPoint || false;
// instance.matchRules = block.matchRules || null;
instance.matchRules = block.matchRules ? JSON.parse(JSON.stringify(block.matchRules)) : null
await instance.save()
}
}
// Fetch all blocks from the database
const allBlocks = await Block.findAll();
const blocksMap = {};
allBlocks.forEach(block => {
// blocksMap[block.type + block.text] = block;
// blocksMap[block.type + block.text.trim()] = block;
blocksMap[block.type + block.id] = block;
blocksMap[block.id] = block;
});
// console.log('blocksMap:', blocksMap);
for (const block of blocks) {
// const currentBlock = await Block.findOne({ where: { type: block.type, text: block.text } });
// let currentBlock = blocksMap[block.type + block.text.trim()];
let currentBlock = blocksMap[block.type + block.id];
// console.log('currentBlock:', currentBlock);
if (!currentBlock) {
console.error('Block not found:', block.id, block.type);
continue;
}
if (block.next) {
// const nextBlock = await Block.findOne({ where: { id: block.next } });
const nextBlock = blocksMap[block.next];
currentBlock.nextId = nextBlock.id;
await currentBlock.save();
}
if (block.options) {
// delete all existing options
await BlockOption.destroy({ where: { blockId: currentBlock.id } });
for (const option of block.options) {
// if (!option.next) {
// continue;
// }
const nextBlock = await Block.findOne({ where: { id: option.next } });
await BlockOption.create({
text: option.text.trim(),
nextId: nextBlock ? nextBlock.id : null,
blockId: currentBlock.id
});
}
}
if (block.image) {
// delete all existing images
await BlockImage.destroy({ where: { blockId: currentBlock.id } });
await BlockImage.create({
type: 'url',
image: block.image,
blockId: currentBlock.id
});
}
}
};
seedDatabase().then(() => {
console.log('Database seeded successfully.');
sequelize.close();
}).catch(err => {
console.error('Error seeding database:', err);
sequelize.close();
});