-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
116 lines (97 loc) · 2.2 KB
/
database.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 DataTypes = Sequelize.DataTypes;
const configuration = {
host: "localhost",
dialect: "mysql"
};
const sequelize = new Sequelize("hastenote",
"author", "gQ0WTGHCVWlJNCrpdAS8", configuration);
const dbExports = {sequelize};
function defineModel(name, columns) {
Object.values(columns).forEach((column) => {
if (column.allowNull === undefined) {
column.allowNull = false;
}
});
columns.id = {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
unique: true,
};
return sequelize.define(name, columns, {
timestamps: false
});
}
dbExports.User = defineModel("User", {
username: {
type: DataTypes.STRING,
unique: true
},
password: {
type: DataTypes.STRING
}
});
function addDefaultUserAssociation(model) {
model.belongsTo(dbExports.User, {
foreignKey: "userId",
targetKey: "id"
});
}
dbExports.Note = defineModel("Note", {
userId: {
type: DataTypes.INTEGER
},
name: {
type: DataTypes.STRING,
defaultValue: "Untitled"
},
color: {
type: DataTypes.STRING(6),
defaultValue: "FFFFFF"
},
contents: {
type: DataTypes.TEXT,
defaultValue: ""
},
noteId: {
type: DataTypes.STRING(16),
unique: true
}
});
addDefaultUserAssociation(dbExports.Note);
const Log = defineModel("Log", {
action: {
type: DataTypes.STRING(70)
},
userId: {
type: DataTypes.INTEGER,
allowNull: true
},
ip: {
type: DataTypes.STRING(50),
allowNull: true
},
data: {
type: DataTypes.TEXT,
allowNull: true
},
date: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
});
addDefaultUserAssociation(Log);
dbExports.logAction = (req, action, data) => {
let query = {
action, data
};
if (req) {
if (req.session && req.session.userId) {
query.userId = req.session.userId;
}
query.ip = req.ip;
}
return Log.create(query);
};
module.exports = dbExports;