-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.js
99 lines (90 loc) · 2.46 KB
/
seed.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
// Import Externals
const mongoose = require('mongoose');
require('dotenv').config();
const db = require('./models');
const seedData = {
accounts: [
{
email: 'seannyphoenix@gmail.com',
name: 'Boy Phoenix, Jack of Hearts',
},
{
email: 'test@test.com',
name: 'Testing Person',
},
{
email: 'seannytrash@gmail.com',
name: 'sillyboy',
},
],
organizations: [
{
name: 'Grand Ducal Council',
members: [
{
email: 'seannyphoenix@gmail.com',
},
{
email: 'test@test.com',
},
],
},
{
name: 'Hamburger Marys',
},
],
events: [
{
name: 'Heart Court Presents',
organization: 'Grand Ducal Council',
},
],
};
async function seed() {
try {
// Accounts
let deleted = await db.Account.deleteMany({});
console.log('Deleted', deleted.deletedCount, 'accounts.');
let created = await db.Account.create(seedData.accounts);
console.log('Created', created.length, 'accounts.');
// Organizations
deleted = await db.Organization.deleteMany({});
console.log('Deleted', deleted.deletedCount, 'organizations.');
const organizations = [];
for (let x = 0; x < seedData.organizations.length; x++) {
const thisOrg = seedData.organizations[x];
if (thisOrg.members) {
for (let y = 0; y < thisOrg.members.length; y++) {
null;
}
}
organizations.push(
seedData.organizations[x],
);
}
created = await db.Organization.create(organizations);
console.log('Created', created.length, 'organizations.');
// Events
deleted = await db.Event.deleteMany({});
console.log('Deleted', deleted.deletedCount, 'events.');
for (let i = 0; i < seedData.events.length; i++) {
const element = seedData.events[i];
const org = db.Organization.find({ name: element.organization });
element.organization = org.id;
}
created = await db.Event.create(seedData.events);
console.log('Created', created.length, 'events.');
// Sessions
deleted = await db.Session.deleteMany({});
console.log('Deleted', deleted.deletedCount, 'sessions.');
// Magic Link Tokens
deleted = await db.Token.deleteMany({});
console.log('Deleted', deleted.deletedCount, 'tokens.');
} catch (err) {
console.log(err);
} finally {
// Always Close Connection
mongoose.connection.close();
}
}
seed().catch(console.warn);