-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
116 lines (106 loc) · 2.75 KB
/
db.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
const Sequelize = require("sequelize");
const JWT = require("jsonwebtoken");
const bcrypt = require("bcrypt");
const { STRING } = Sequelize;
const config = {
logging: false,
};
if (process.env.LOGGING) {
delete config.logging;
}
const conn = new Sequelize(
process.env.DATABASE_URL || "postgres://localhost/acme_db",
config
);
const User = conn.define("user", {
username: STRING,
password: STRING,
});
const Note = conn.define("note", {
text: STRING,
});
Note.belongsTo(User);
User.hasMany(Note);
User.byToken = async (token) => {
try {
const payload = JWT.verify(token, process.env.JWT);
const user = await User.findByPk(payload.id);
if (user.id === payload.id) {
return user;
}
const error = Error("bad credentials");
error.status = 401;
throw error;
} catch (ex) {
const error = Error("bad credentials");
error.status = 401;
throw error;
}
};
User.beforeCreate(async (user) => {
user.password = await bcrypt.hash(user.password, 5);
});
User.authenticate = async ({ username, password }) => {
const user = await User.findOne({
where: {
username,
},
});
if (user && await bcrypt.compare(password, user.password)) {
return JWT.sign({ id: user.id }, process.env.JWT);
}
const error = Error("bad credentials");
error.status = 401;
throw error;
};
// User.getNotes = async (id) => {
// const user = await User.findByPk(id);
// if (user) {
// return user.getNotes();
// }
// const error = Error("Cant get notes");
// error.status = 401;
// throw error;
// };
const syncAndSeed = async () => {
await conn.sync({ force: true });
const credentials = [
{ username: "lucy", password: "lucy_pw" },
{ username: "moe", password: "moe_pw" },
{ username: "larry", password: "larry_pw" },
];
const notes = [
{
text: "Lorem ipsum dolor sit amet, eu quo consetetur voluptatum, eu iusto posidonium adversarium sea, cum cibo persius facilis at. Ius recteque posidonium te",
},
{
text: "Lorem ipsum dolor sit amet, eu quo consetetur voluptatum, eu iusto posidonium adversarium sea, cum cibo persius facilis at. Ius recteque posidonium te",
},
{
text: "Lorem ipsum dolor sit amet, eu quo consetetur voluptatum, eu iusto posidonium adversarium sea, cum cibo persius facilis at. Ius recteque posidonium te",
},
];
const [lucy, moe, larry] = await Promise.all(
credentials.map((credential) => User.create(credential))
);
const [text1, text2, text3] = await Promise.all(
notes.map((note) => Note.create(note))
);
lucy.setNotes(text1);
moe.setNotes(text2);
larry.setNotes(text3);
return {
users: {
lucy,
moe,
larry,
},
};
};
module.exports = {
syncAndSeed,
models: {
User,
Note,
},
};