-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
76 lines (69 loc) · 2.92 KB
/
app.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
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const db = require('./models');
const { User, Blueprint, Space, Visit } = require('./models');
const dummyUsers = require('./dummy/users.json');
const dummyBlueprints = require('./dummy/blueprints.json');
const dummySpaces = require('./dummy/spaces.json');
const dummyVisits = require('./dummy/visits.json');
const dummyAssociations = require('./dummy/associations.json');
// Load from .env file if app is not in prod
if (process.env.NODE_ENV !== 'production'){
require('dotenv').config();
}
const app = express();
db.sequelize.sync({ force: true }).then(() => console.log('synced!'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/api', require('./api'));
app.use(express.static(path.join(__dirname, 'client/build')));
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
//populates users and creates associations
(async () => {
try {
setTimeout(async () => {
const users = await User.bulkCreate(dummyUsers);
const blueprints = await Blueprint.bulkCreate(dummyBlueprints);
const spaces = await Space.bulkCreate(dummySpaces);
const visits = await Visit.bulkCreate(dummyVisits);
const { userToBlueprint, blueprintToSpace, userToSpace } = dummyAssociations;
for (let a of userToBlueprint) {
const { user, blueprint } = a;
console.log('user')
console.log(user)
console.log('blueprint')
console.log(blueprint)
const foundUser = await User.findOne({ where: { username: user }});
const foundBp = await Blueprint.findOne({ where: { name: blueprint }});
if (foundUser || foundBp) {
// foundBp.addUser(foundUser);
foundBp.addUser(foundUser, {
through : 'member',
foreignKey : 'blueprintId'
});
foundUser.addBlueprint(foundBp, { through: 'member' });
} else {
throw 'assocation failed';
}
}
for (let a of blueprintToSpace) {
const { blueprint, space } = a;
const foundBp = await Blueprint.findOne({ where: { name: blueprint }});
const foundSpace = await Space.findOne({ where: { name: space }});
if (foundSpace || foundBp) {
foundBp.addSpace(foundSpace);
} else {
throw 'assocation failed';
}
}
}, 1000)
console.log('bulk creation finished')
} catch (err) {
console.error(err);
}
})();
var PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`listening on ${PORT}`));