forked from sitnpaws/sit-n-paws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.js
71 lines (65 loc) · 3.57 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
// seed.js is for seeding the database with mock data to work with in the interface
// this function is invoked near the top of the server.js file and can be easily commented out to stop seeding
// import mongoose, config, and listing schema
const mongoose = require('mongoose');
const Listing = require('./db/models/listing');
const User = require('./db/models/users');
// guest e-mail: [email protected]
// host e-mail: [email protected]
// pw: password1
// seed data for listings to populate host listings
const listingsData = [
{"name":"Camp Snoopy","zipcode":94106,"dogSizePreference":"medium","dogBreedPreference":"Corgi","email":"[email protected]","dogActivityPreference":"rutrum","homeAttributes":"Great home with lots of space","hostPictures":"https://randomuser.me/api/portraits/women/44.jpg","homePictures":"https://farm7.staticflickr.com/6076/6080657644_19cfe82456.jpg","cost":35},
{"name":"Kibbles Paradise","zipcode":94110,"dogSizePreference":"super extra large","dogBreedPreference":"Chihuahua","email":"[email protected]","dogActivityPreference":"dapibus","homeAttributes":"Great home with lots of space","hostPictures":"https://randomuser.me/api/portraits/women/45.jpg","homePictures":"https://farm1.staticflickr.com/68/187943195_05de9fe99b.jpg","cost":55},
{"name":"Bow Wowee","zipcode":94123,"dogSizePreference":"small","dogBreedPreference":"Dachshund","email":"[email protected]","dogActivityPreference":"lacus","homeAttributes":"Great home with lots of space","hostPictures":"https://randomuser.me/api/portraits/women/46.jpg","homePictures":"https://farm6.staticflickr.com/5510/14490433662_2745930345.jpg","cost":30},
{"name":"Dog Mansion","zipcode":94106,"dogSizePreference":"teeny weeny","dogBreedPreference":"German Shepherd","email":"[email protected]","dogActivityPreference":"amet","homeAttributes":"Great home with lots of space","hostPictures":"https://randomuser.me/api/portraits/women/47.jpg","homePictures":"https://farm4.staticflickr.com/3062/3046570389_f960000e36.jpg","cost":65}
];
// seed data for user to provide instant login capability
const mockCompleteUser = [
{
email: '[email protected]',
password: 'password1',
name: 'Mary Tester',
phone: '561-123-5155',
address: '14 Main Street'
}
];
// function to clean listings from database and seed with above listings and user
const seedListingDB = () => {
// remove listings from database to start - NOTE THIS REMOVES ALL LISTINGS, SO BE CAREFUL IN PRODUCTION
Listing.remove({}, (err) => {
if(err) {
console.log(err);
} else {
// remove mary444 user prior to adding
User.remove({'username': 'mary444'}, (err) => {
if(err) {
console.log(err);
}
let reformatUser = JSON.stringify(mockCompleteUser[0]);
let newUser = new User(JSON.parse(reformatUser));
const newUserId = newUser._id;
// add user mary444 to database
newUser.save((err) => {
if(err) {
console.log(err);
}
})
// iterate over mock listings, format, and save each listing into the database
listingsData.forEach((listing) => {
// reformat data to strings for parsing before saving
listing.userId = newUserId;
let reformatListing = JSON.stringify(listing);
let newListing = new Listing(JSON.parse(reformatListing));
newListing.save((err) => {
if(err) {
console.log(err);
}
})
})
})
console.log('DATABASE SEEDED');
}
})
}
seedListingDB();