-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdb.ts
More file actions
271 lines (253 loc) · 7.91 KB
/
db.ts
File metadata and controls
271 lines (253 loc) · 7.91 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { Database, open } from 'sqlite';
import sqlite3 from 'sqlite3';
import { logger } from '../logger/default';
let db: Database | null = null;
export const openCommandoDB = async (): Promise<Database> =>
await open({ filename: 'db/commando.db', driver: sqlite3.Database });
const initCoffeeChatTables = async (db: Database): Promise<void> => {
//Database to store past matches, with TIMESTAMP being the time matches were written into DB
await db.run(
`
CREATE TABLE IF NOT EXISTS coffee_historic_matches (
first_user_id TEXT NOT NULL,
second_user_id TEXT NOT NULL,
match_date TIMESTAMP NOT NULL
)
`,
);
};
const initInterviewTables = async (db: Database): Promise<void> => {
await db.run(
`
CREATE TABLE IF NOT EXISTS interviewers (
user_id TEXT PRIMARY KEY,
link TEXT NOT NULL,
status INTEGER NOT NULL DEFAULT 0
)
`,
);
await db.run(
`
CREATE TABLE IF NOT EXISTS domains (
user_id TEXT NOT NULL,
domain TEXT NOT NULL
)
`,
);
await db.run('CREATE INDEX IF NOT EXISTS ix_domains_domain ON domains (domain)');
};
const initSuggestionsTable = async (db: Database): Promise<void> => {
await db.run(
`
CREATE TABLE IF NOT EXISTS suggestions (
id INTEGER PRIMARY KEY NOT NULL,
author_id VARCHAR(255) NOT NULL,
author_username TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
suggestion TEXT NOT NULL,
state VARCHAR(255) NOT NULL
)
`,
);
};
const initUserCoinBonusTable = async (db: Database): Promise<void> => {
await db.run(
`
CREATE TABLE IF NOT EXISTS user_coin_bonus (
user_id VARCHAR(255) NOT NULL,
bonus_type INTEGER NOT NULL,
last_granted TIMESTAMP NOT NULL,
PRIMARY KEY (user_id, bonus_type)
)
`,
);
};
const initUserCoinLedgerTable = async (db: Database): Promise<void> => {
await db.run(
`
CREATE TABLE IF NOT EXISTS user_coin_ledger (
id INTEGER PRIMARY KEY NOT NULL,
user_id VARCHAR(255) NOT NULL,
amount INTEGER NOT NULL,
new_balance INTEGER NOT NULL,
event INTEGER NOT NULL,
reason VARCHAR(255),
admin_id VARCHAR(255),
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)
`,
);
await db.run(
'CREATE INDEX IF NOT EXISTS ix_user_coin_ledger_user_id ON user_coin_ledger (user_id)',
);
};
const initUserCoinTable = async (db: Database): Promise<void> => {
await db.run(
`
CREATE TABLE IF NOT EXISTS user_coin (
user_id VARCHAR(255) PRIMARY KEY NOT NULL,
balance INTEGER NOT NULL CHECK(balance>=0)
)
`,
);
};
const initBlackjackPlayerStats = async (db: Database): Promise<void> => {
await db.run(
`
CREATE TABLE IF NOT EXISTS blackjack_player_stats (
user_id VARCHAR(255) PRIMARY KEY NOT NULL,
games_played INTEGER NOT NULL DEFAULT 0,
games_won INTEGER NOT NULL DEFAULT 0,
games_lost INTEGER NOT NULL DEFAULT 0,
net_gain_loss INTEGER NOT NULL DEFAULT 0,
winrate REAL NOT NULL DEFAULT 0.0
)
`,
);
await db.run(
`CREATE INDEX IF NOT EXISTS idx_net_gain_loss ON blackjack_player_stats (net_gain_loss)`,
);
await db.run(`CREATE INDEX IF NOT EXISTS idx_winrate ON blackjack_player_stats (winrate)`);
};
const initUserProfileTable = async (db: Database): Promise<void> => {
await db.run(
`
CREATE TABLE IF NOT EXISTS user_profile_table (
user_id VARCHAR(255) PRIMARY KEY NOT NULL,
about_me TEXT,
birth_date TEXT,
preferred_name VARCHAR(32),
preferred_pronouns VARCHAR(16),
term VARCHAR(16),
year INTEGER,
last_updated TIMESTAMP NOT NULL DEFAULT CURRENT_DATE,
faculty VARCHAR(32),
program VARCHAR(32),
specialization VARCHAR(32),
profile_emoji VARCHAR(32)
)
`,
);
await addSQLColumnIfNotExists(db, 'user_profile_table', 'profile_emoji', 'VARCHAR(32)');
};
const initRpsGameInfo = async (db: Database): Promise<void> => {
// If player 2 ID is null, the game was against Codey
await db.run(
`
CREATE TABLE IF NOT EXISTS rps_game_info (
id INTEGER PRIMARY KEY NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
player1_id VARCHAR(30) NOT NULL,
player2_id VARCHAR(30),
bet INTEGER NOT NULL,
player1_sign INTEGER NOT NULL DEFAULT 0,
player2_sign INTEGER NOT NULL DEFAULT 0,
status INTEGER NOT NULL DEFAULT 0
)
`,
);
};
const initConnectFourGameInfo = async (db: Database): Promise<void> => {
// If player 2 ID is null, the game was against Codey
await db.run(
`
CREATE TABLE IF NOT EXISTS connect_four_game_info (
id INTEGER PRIMARY KEY NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
player1_id VARCHAR(30) NOT NULL,
player2_id VARCHAR(30),
player1_sign INTEGER NOT NULL DEFAULT 0,
player2_sign INTEGER NOT NULL DEFAULT 0,
status INTEGER NOT NULL DEFAULT 0
)
`,
);
};
const initResumePreview = async (db: Database): Promise<void> => {
await db.run(
`
CREATE TABLE IF NOT EXISTS resume_preview_info (
initial_pdf_id VARCHAR(255) PRIMARY KEY NOT NULL,
preview_id VARCHAR(255) NOT NULL
)
`,
);
await db.run(
'CREATE INDEX IF NOT EXISTS ix_resume_preview_info_preview_id ON resume_preview_info (preview_id)',
);
};
/*
function: addSQLColumnIfNotExists
parameters:
* db: the object of SQLite Database
* tableName: the name of the table where the column needs to be added
* columnName: the name of the column that needs to be added to the table
* columnDataType: the SQL data type of the column that needs to be added
Example:
addSQLColumnIfNotExists(db, 'user_profile_table', 'profile_emoji', 'VARCHAR(32)')
*/
const addSQLColumnIfNotExists = async (
db: Database,
tableName: string,
columnName: string,
columnDataType: string,
): Promise<void> => {
const columns = await db.all(
`SELECT *
FROM pragma_table_info('${tableName}')
WHERE name='${columnName}'
`,
);
if (columns.length == 0) {
await db.run(`ALTER TABLE ${tableName} ADD COLUMN ${columnName} ${columnDataType}`);
}
};
const initCompaniesTable = async (db: Database): Promise<void> => {
// the company_id will match the crunchbase entity_id, which is a unique identifier for each company
// see https://app.swaggerhub.com/apis-docs/Crunchbase/crunchbase-enterprise_api/1.0.3#/Entity/get_entities_organizations__entity_id_
await db.run(
`
CREATE TABLE IF NOT EXISTS companies (
company_id VARCHAR(30) PRIMARY KEY NOT NULL,
name VARCHAR(64) NOT NULL,
image_id TEXT,
description TEXT
)`,
);
};
const initPeopleCompaniesTable = async (db: Database): Promise<void> => {
await db.run(`
CREATE TABLE IF NOT EXISTS companies_people (
user_id VARCHAR(255) NOT NULL,
company_id VARCHAR(32) NOT NULL,
role VARCHAR(64) NOT NULL,
FOREIGN KEY(company_id) REFERENCES companies(company_id)
)`);
};
const initTables = async (db: Database): Promise<void> => {
//initialize all relevant tables
await initCoffeeChatTables(db);
await initInterviewTables(db);
await initSuggestionsTable(db);
await initUserCoinBonusTable(db);
await initUserCoinLedgerTable(db);
await initUserCoinTable(db);
await initBlackjackPlayerStats(db);
await initUserProfileTable(db);
await initRpsGameInfo(db);
await initConnectFourGameInfo(db);
await initResumePreview(db);
await initCompaniesTable(db);
await initPeopleCompaniesTable(db);
};
export const openDB = async (): Promise<Database> => {
if (db == null) {
db = await open({
filename: 'db/bot.db',
driver: sqlite3.Database,
});
await initTables(db);
logger.info({ message: 'Initialized database and tables.', where: 'openDB' });
}
return db;
};