-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.mongo.js
121 lines (113 loc) · 3.07 KB
/
seed.mongo.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
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */
// TODO: Fix the problem of the date not matching mongodb timestamps[]
const bcrypt = require('bcrypt')
const MongoClient = require('mongodb').MongoClient
const connectDb = async () => {
const uri = 'mongodb://admin:admin@localhost:27017/'
const client = new MongoClient(uri)
return await client.connect()
}
connectDb().then(async (mongo) => {
const resourcesCollection = mongo.db('test').collection('resources')
const rolesCollection = mongo.db('test').collection('roles')
const usersCollection = mongo.db('test').collection('users')
console.log('connected to Mongodb 🥭 -- seeding db')
//* seeding resources
const resources = [
{
name: 'users',
permissions: {
read: true,
write: true,
update: true,
delete: true,
},
createdAt: new Date().toJSON(),
updatedAt: new Date().toJSON(),
},
{
name: 'roles',
permissions: {
read: true,
write: true,
update: true,
delete: true,
},
createdAt: new Date().toJSON(),
updatedAt: new Date().toJSON(),
},
{
name: 'resources',
permissions: {
read: true,
write: true,
update: true,
delete: true,
},
createdAt: new Date().toJSON(),
updatedAt: new Date().toJSON(),
},
{
name: 'self',
permissions: {
read: true,
write: true,
update: true,
delete: true,
},
createdAt: new Date().toJSON(),
updatedAt: new Date().toJSON(),
},
]
console.log('🌱 Seeding resources collection 🍀')
const { insertedIds } = await resourcesCollection.insertMany(resources)
//* seeding roles
const roles = [
{
name: 'admin',
resources: [insertedIds[0], insertedIds[1], insertedIds[2]],
createdAt: new Date().toJSON(),
updatedAt: new Date().toJSON(),
},
{
name: 'student',
resources: [],
createdAt: new Date().toJSON(),
updatedAt: new Date().toJSON(),
},
{
name: 'instructor',
resources: [],
createdAt: new Date().toJSON(),
updatedAt: new Date().toJSON(),
},
{
name: 'content_creator',
resources: [],
createdAt: new Date().toJSON(),
updatedAt: new Date().toJSON(),
},
{
name: 'all',
resources: [insertedIds[3]],
createdAt: new Date().toJSON(),
updatedAt: new Date().toJSON(),
},
]
console.log('🌱 Seeding roles collection 🍀')
const { insertedIds: insertedRolesIds } = await rolesCollection.insertMany(roles)
//* seeding user -- create an admin
const admin = {
email: '[email protected]',
password: await bcrypt.hash('platform_password2022', 12),
firstName: 'admin',
lastName: 'platform',
roles: [insertedRolesIds[0], insertedRolesIds[4]],
createdAt: new Date().toJSON(),
updatedAt: new Date().toJSON(),
}
console.log('🌱 Seeding users collection 🍀')
await await usersCollection.insertOne(admin)
await mongo.close()
})