-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.js
More file actions
148 lines (141 loc) · 3.87 KB
/
seed.js
File metadata and controls
148 lines (141 loc) · 3.87 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
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
147
148
var db = require("./models")
var faker = require('faker')
var users_list = [
{
username: 'stephanie',
password: '12345',
households: [],
items: []
},
{
username: "steven",
password: '12345',
households: [],
items: []
}
]
var snowgillHouse = {
name: "Snowgill",
members: [],
items: []
}
var items_list = [
{
title: "roller blades",
description: 'black and grey',
category: "sports",
image: "nothing yet",
value: 5,
toKeep: false,
owner: 'stephanie'
},
{
title: "monstera plant",
description: 'large and healthy',
category: 'plant',
image: 'nothing yet',
value: 20,
toKeep: false,
owner: 'stephanie'
},
{
title: 'hockey stick 1',
description: 'white and black',
category: 'sports',
image: 'nothing yet',
value: 100,
toKeep: true,
owner: 'steven'
},
{
title: "hockey net",
description: 'hockey net with white net and red posts',
category: 'sports',
image: 'nothing yet',
value: 300,
toKeep: true,
owner: 'steven'
}
]
function makeUsers(){
return db.User.remove({}).then(function(){
console.log("1 TRYING TO CREATE USERS <<<<")
return db.User.create(users_list).then(function(newUsers){
console.log("1 created", newUsers.length, 'users!' )
})
}).catch(function(error){
console.log('error creating users!', error)
})
}
function makeHouseholds() {
return db.Household.remove({}).then(function(){
console.log("2 TRYING TO CREATE HOUSEHOLD <<<");
return new db.Household({
name: snowgillHouse.name,
members: snowgillHouse.members
})
}).then(function(ourHouse){
console.log('2 our house name', ourHouse.name)
return db.User.findOne({username: "stephanie"})
.then(function(foundUser1){
console.log("2 finding user1 for household:", foundUser1)
foundUser1.households.push(ourHouse)
return foundUser1.save().then(function(savedUser){
console.log("TRYING TO SAVE HOUSEHOLD TO USER", savedUser)
return ourHouse
})
})
}).then(function(ourHouse){
console.log('3 this is our house!', ourHouse);
return db.User.findOne({username: 'steven'})
.then(function(foundUser2){
console.log('3 finding user for household:', foundUser2.username)
foundUser2.households.push(ourHouse)
return foundUser2.save().then(function(savedUser){
console.log("TRYING TO SAVE HOUSE HOLD TO USER", savedUser);
return ourHouse
})
console.log('3 house members', ourHouse.members);
})
}).then(function(ourHouse){
console.log("4 ready to save house")
return ourHouse.save().then(function(savedHouse){
console.log('4 saved', savedHouse.name, "house")
})
})
}
function makeItems(){
return db.Item.remove({}).then(function(){
console.log('5 removed all items')
}).then(async function(){
for(let itemData of items_list){
var item = new db.Item({
title: itemData.title,
description: itemData.description,
category: itemData.category,
image: itemData.image,
value: itemData.value,
toKeep: itemData.toKeep
})
await db.User.findOne({username: itemData.owner}).then(function(foundUser){
console.log("6A finding user for item", foundUser.username, "<<<<<<<<")
item.owner = foundUser
})
await db.Household.findOne({name: "Snowgill"}).then(function(foundHouse){
console.log("6B finding household for item", foundHouse.name)
item.household = foundHouse
console.log("6B owner and household!", item.owner, item.household)
})
await item.save().then(function(savedItem){
console.log(savedItem)
console.log("6C saved item", savedItem.title, savedItem.owner.username)
})
}
})
}
async function seedDB(){
await makeUsers();
await makeHouseholds();
await makeItems();
}
seedDB();