-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseeders.js
More file actions
116 lines (107 loc) · 2.26 KB
/
Copy pathseeders.js
File metadata and controls
116 lines (107 loc) · 2.26 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
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
#!/usr/bin/node
const { mongoose } = require('./config/mongo');
const { User, Channel, WidgetType, Widget } = require('./models');
const bcrypt = require('bcrypt');
const argv = require('minimist')(process.argv.slice(2));
const saltRounds = 10;
const seedUsers = [
{
email: 'sa@prodigyio.com',
username: 'sa',
password: process.env.SA_PASS,
firstName: 'Super',
lastName: 'Admin',
role: 'sa',
},
{
email: 'lc@prodigyio.com',
username: 'luizcarlos',
password: process.env.SA_PASS,
firstName: 'Luiz',
lastName: 'Carlos',
role: 'user',
},
];
const seedChannels = [
{
channelId: 1802103,
},
{
channelId: 1854957,
},
];
const seedWidgetTypes = [
{
name: 'Display',
slug: 'display',
sortOrder: 1,
},
{
name: 'Time series',
slug: 'time-series',
sortOrder: 2,
},
{
name: 'Light bulb',
slug: 'bulb',
sortOrder: 3,
},
{
name: 'Toggle switch',
slug: 'switch',
sortOrder: 4,
},
{
name: 'Float field',
slug: 'float',
sortOrder: 5,
},
];
(async () => {
try {
// npm run seed -- -d
if (argv.d) {
await Widget.deleteMany({});
await WidgetType.deleteMany({});
await Channel.deleteMany({});
await User.deleteMany({});
}
const asSeedUsers = await Promise.all(
seedUsers.map(async (user) => ({
...user,
password: await bcrypt.hash(user.password, saltRounds),
}))
);
await Promise.all(
asSeedUsers.map((user) =>
User.findOneAndUpdate({ email: user.email }, user, {
upsert: true,
})
)
);
const saUser = await User.findByUsername('sa');
await Promise.all(
seedChannels.map((channel) =>
Channel.findOneAndUpdate(
{ user: saUser.id, channelId: channel.channelId },
{ ...channel, user: saUser.id },
{
upsert: true,
}
)
)
);
await Promise.all(
seedWidgetTypes.map((widgetTypes) =>
WidgetType.findOneAndUpdate({ name: widgetTypes.name }, widgetTypes, {
upsert: true,
})
)
);
} catch (err) {
console.error(err);
}
mongoose.connection.close();
console.log('Seeder successfully executed');
process.exit();
})();