-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_utils.ts
More file actions
63 lines (55 loc) · 1.42 KB
/
database_utils.ts
File metadata and controls
63 lines (55 loc) · 1.42 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
import { Client } from "https://deno.land/x/postgres@v0.16.1/mod.ts";
// Settings file to share between wormscli and your parasite instance.
import { settings as instanceSettings } from "../parasite/settings.ts";
const db_settings = instanceSettings.database.settings;
const client = new Client(db_settings);
export async function basicSelect(
row: string,
table: string,
name: string,
) {
await client.connect();
const res = await client.queryArray(
`SELECT ${row} FROM ${table} WHERE id = $1`,
[name],
);
await client.end();
if (res.rows.length !== 0) {
return res.rows[0];
}
return { "err": true, "msg": msg };
}
export async function basicUpdate(
category: string,
params = {},
id: string,
) {
await client.connect();
for (const prop in params) {
await client.queryArray(
`UPDATE ${category} SET ${prop} = $1 WHERE id = $2`,
[JSON.stringify(params[prop]), id],
);
}
await client.end();
}
export async function basicDelete(
table: string,
name: string,
) {
await client.connect();
await client.queryArray(
`DELETE FROM ${table} WHERE id = $1`,
[name],
);
if (table === "users") {
const defaultTables = ["comments", "torrents", "lists", "actions"];
for await (const defaultTable of defaultTables) {
await client.queryArray(
`DELETE FROM ${defaultTable} WHERE uploader = $1`,
[id],
);
}
}
await client.end();
}