-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.ts
51 lines (42 loc) · 1.79 KB
/
db.ts
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
import { DB } from "./deps.ts"
export const db = new DB("./data/tags.sqlite");
export interface Tag {
id: number
user: string
guild: string
uname: string
name: string
content: string
created: string
}
export function init() {
db.query(`CREATE TABLE IF NOT EXISTS tags(id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT, guild TEXT, name TEXT, uname TEXT, content TEXT, created TEXT)`)
}
export function getUserTags(guild: string, user: string) {
return [...db.query(`SELECT * FROM tags WHERE user = ? AND guild = ?`, [user, guild]).asObjects()]
}
export function getGuildTags(guild: string) {
return [...db.query(`SELECT * FROM tags WHERE guild = ?`, [guild]).asObjects()]
}
export function getTag(guild: string, name: string) {
return [...db.query(`SELECT * FROM tags WHERE guild = ? AND uname = ?`, [guild, name.toLowerCase()]).asObjects()][0]
}
export function deleteTag(guild: string, name: string): boolean {
if (!getTag(guild, name)) return false
db.query(`DELETE FROM tags WHERE guild = ? AND uname = ?`, [guild, name.toLowerCase()])
return true
}
export function editTag(guild: string, name: string, update: string) {
if (!getTag(guild, name)) return false
db.query(`UPDATE tags SET content = ? WHERE guild = ? AND uname = ?`, [update, guild, name.toLowerCase()])
return getTag(guild, name)
}
export function addTag(guild: string, user: string, name: string, content: string) {
if (getTag(guild, name)) return null
db.query(`INSERT INTO tags (user, guild, uname, name, content, created) VALUES (?, ?, ?, ?, ?, ?)`, [user, guild, name.toLowerCase(), name, content, new Date().getTime()])
return getTag(guild, name)
}
if(import.meta.main) {
console.log('Initializing DB');
init();
}