-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
145 lines (134 loc) · 2.7 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
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
const crypto = require('crypto');
const Sequelize = require('sequelize');
let db;
if (process.env.NODE_ENV === 'production') {
db = new Sequelize(
process.env.DB,
process.env.USER,
process.env.PASSWORD,
{
logging: false,
host: process.env.HOST,
dialect: process.env.DIALECT,
}
);
} else {
db = new Sequelize({
dialect: 'sqlite',
storage: 'db.sqlite',
});
}
const Package = db.define('package', {
name: {
type: Sequelize.STRING,
primaryKey: true,
allowNull: false,
},
description: {
type: Sequelize.STRING,
allowNull: false,
},
downloads: {
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 0,
},
weeklyDownloads: {
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 0,
},
});
const Version = db.define('version', {
id: {
type: Sequelize.STRING,
primaryKey: true,
allowNull: false,
},
package: {
type: Sequelize.STRING,
allowNull: false,
},
version: {
type: Sequelize.STRING,
allowNull: false,
},
readme: {
type: Sequelize.TEXT,
allowNull: false,
defaultValue: 'No readme!',
},
data: {
type: Sequelize.TEXT,
allowNull: false,
},
shasum: {
type: Sequelize.STRING,
allowNull: false,
},
dist: {
type: Sequelize.BLOB,
allowNull: false,
},
cdn: Sequelize.TEXT,
});
const Tag = db.define('tag', {
id: {
primaryKey: true,
allowNull: false,
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
},
name: {
type: Sequelize.STRING,
allowNull: false,
},
});
const User = db.define('user', {
username: {
type: Sequelize.STRING,
primaryKey: true,
allowNull: false,
},
email: {
type: Sequelize.STRING,
allowNull: false,
},
password: {
type: Sequelize.STRING,
allowNull: false,
},
verified: {
type: Sequelize.BOOLEAN,
defaultValue: false,
allowNull: false,
},
verifyToken: {
type: Sequelize.STRING,
allowNull: true,
defaultValue() {
return crypto.randomBytes(32).toString('hex');
},
},
});
const Download = db.define('download', {
date: {
type: Sequelize.DATEONLY,
allowNull: false,
defaultValue() {
return new Date(Date.UTC(new Date().getFullYear(), new Date().getMonth()));
},
},
count: {
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 0,
},
});
Package.hasMany(Version, { as: 'Versions' });
Package.hasMany(Tag, { as: 'Tags' });
Package.hasMany(Download, { as: 'Downloads' });
Tag.belongsTo(Version);
User.belongsToMany(Package, { through: 'UserPackage' });
Package.belongsToMany(User, { through: 'UserPackage', as: 'Authors' });
module.exports = { db, Package, Version, User, Tag };