-
Notifications
You must be signed in to change notification settings - Fork 7
/
db.js
123 lines (111 loc) · 3.8 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
class Database {
constructor(databaseConnection) {
this.db = databaseConnection;
}
async getSetting(settingName) {
return await this.db.prepare("SELECT value FROM settings WHERE name = ?")
.bind(settingName)
.first('value');
}
async getLatestUpdateId() {
let result = await this.db.prepare("SELECT updateId FROM messages ORDER BY updateId DESC LIMIT 1")
.first('updateId');
return Number(result);
}
async setSetting(settingName, settingValue) {
return await this.db.prepare(
`INSERT
INTO settings (createdDate, updatedDate, name, value)
VALUES (DATETIME('now'), DATETIME('now'), ?, ?)
ON CONFLICT(name) DO UPDATE SET
updatedDate = DATETIME('now'),
value = excluded.value
WHERE excluded.value <> settings.value`
)
.bind(settingName, settingValue)
.run();
}
async addMessage(message, updateId) {
return await this.db.prepare(
`INSERT
INTO messages (createdDate, updatedDate, message, updateId)
VALUES (DATETIME('now'), DATETIME('now'), ?, ?)`
)
.bind(message, updateId)
.run();
}
async getUser(telegramId) {
return await this.db.prepare("SELECT * FROM users WHERE telegramId = ?")
.bind(telegramId)
.first();
}
async saveUser(user, authTimestamp) {
console.log(user);
console.log(authTimestamp);
// the following is an upsert, see https://sqlite.org/lang_upsert.html for more info
return await this.db.prepare(
`INSERT
INTO users (createdDate, updatedDate, lastAuthTimestamp,
telegramId, isBot, firstName, lastName, username, languageCode,
isPremium, addedToAttachmentMenu, allowsWriteToPm, photoUrl
)
VALUES (DATETIME('now'), DATETIME('now'), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(telegramId) DO UPDATE SET
updatedDate = DATETIME('now'),
lastAuthTimestamp = COALESCE(excluded.lastAuthTimestamp, lastAuthTimestamp),
isBot = COALESCE(excluded.isBot, isBot),
firstName = excluded.firstName,
lastName = excluded.lastName,
username = excluded.username,
languageCode = COALESCE(excluded.languageCode, languageCode),
isPremium = COALESCE(excluded.isPremium, isPremium),
addedToAttachmentMenu = COALESCE(excluded.addedToAttachmentMenu, addedToAttachmentMenu),
allowsWriteToPm = COALESCE(excluded.allowsWriteToPm, allowsWriteToPm),
photoUrl = COALESCE(excluded.photoUrl, photoUrl)
WHERE excluded.lastAuthTimestamp > users.lastAuthTimestamp`
)
.bind(authTimestamp,
user.id, +user.is_bot, user.first_name||null, user.last_name||null, user.username||null, user.language_code||null,
+user.is_premium, +user.added_to_attachment_menu, +user.allows_write_to_pm, user.photo_url||null
)
.run();
}
async saveToken(telegramId, tokenHash) {
const user = await this.getUser(telegramId);
console.log(user.id, tokenHash);
return await this.db.prepare(
`INSERT
INTO tokens (createdDate, updatedDate, expiredDate, userId, tokenHash)
VALUES (DATETIME('now'), DATETIME('now'), DATETIME('now', '+1 day'), ?, ?)`
)
.bind(user.id, tokenHash)
.run();
}
async getUserByTokenHash(tokenHash) {
return await this.db.prepare(
`SELECT users.* FROM tokens
INNER JOIN users ON tokens.userId = users.id
WHERE tokenHash = ? AND DATETIME('now') < expiredDate`
)
.bind(tokenHash)
.first();
}
async saveCalendar(calendarJson, calendarRef, userId) {
return await this.db.prepare(
`INSERT
INTO calendars (createdDate, updatedDate, calendarJson, calendarRef, userId)
VALUES (DATETIME('now'), DATETIME('now'), ?, ?, ?)`
)
.bind(calendarJson, calendarRef, userId)
.run();
}
async getCalendarByRef(calendarRef) {
return await this.db.prepare(
`SELECT calendarJson FROM calendars
WHERE calendarRef = ?`
)
.bind(calendarRef)
.first('calendarJson');
}
}
export { Database }