-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdb.js
119 lines (101 loc) · 3.4 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
const { Pool } = require('pg');
const { PGSQLConnection } = require('./tokens');
const DBPool = new Pool(PGSQLConnection);
async function addShopHistory(item_id) {
const client = await DBPool.connect();
try {
let now = new Date();
return client.query("INSERT INTO shop_history (item_id, date_stamp) VALUES ($1, $2) ON CONFLICT DO NOTHING", [item_id, now]);
} catch (e) {
console.error(e);
} finally {
client.release();
}
}
exports.addShopHistory = addShopHistory;
async function getLastAppeared(item_id) {
const client = await DBPool.connect();
try {
let now = new Date();
let rows = await client.query('SELECT date_stamp FROM shop_history WHERE item_id = $1 AND date_stamp < $2 ORDER BY date_stamp DESC LIMIT 1', [item_id, now]);
if (rows.rows.length <= 0) return false;
return rows.rows[0].date_stamp;
} catch (e) {
console.error(e);
} finally {
client.release();
}
}
exports.getLastAppeared = getLastAppeared;
async function getAppearanceCount(item_id) {
const client = await DBPool.connect();
try {
let rows = await client.query("SELECT COUNT(item_id) AS item_count FROM shop_history WHERE item_id = $1", [item_id]);
return rows.rows[0].item_count;
} catch (e) {
console.error(e);
} finally {
client.release();
}
}
exports.getAppearanceCount = getAppearanceCount;
async function getLocaleString(namespace, key, lang_key) {
const client = await DBPool.connect();
let query = {
name: 'retrieve-locale',
text: 'SELECT content FROM localization WHERE namespace = $1 AND string_key = $2 AND lang_key = $3',
values: [namespace, key, lang_key],
};
try {
let rows = await client.query(query);
return rows.rows[0].content;
} catch (e) {
console.error(e);
} finally {
client.release();
}
}
exports.getLocaleString = getLocaleString;
// Ignore namespaces for this, for everything item-shop, it's "" anyway.
async function getLocaleStrings(keys, lang_key) {
const client = await DBPool.connect();
let whereClause = [...Array(keys.length).keys()].map(v => "$" + (v + 2)).join(", ");
let params = keys.slice();
params.unshift(lang_key);
try {
let rows = await client.query("SELECT string_key, content FROM localization WHERE lang_key = $1 AND string_key IN (" + whereClause + ")", params);
return rows.rows.map(v => ({
key: v.string_key,
string: v.content,
}));
} catch (e) {
console.error(e);
} finally {
client.release();
}
}
exports.getLocaleStrings = getLocaleStrings;
async function insertLocaleString(namespace, key, lang_key, content) {
const client = await DBPool.connect();
let query = {
name: 'upsert-locale',
text: 'INSERT INTO localization (namespace, string_key, content, lang_key) VALUES ($1, $2, $3, $4)' +
' ON CONFLICT ON CONSTRAINT unique_entry DO UPDATE SET content = $3',
values: [namespace, key, content, lang_key],
};
try {
return client.query(query);
} catch (e) {
console.error(e);
} finally {
client.release();
}
}
exports.insertLocaleString = insertLocaleString;
function disconnectClient() {
DBPool.end();
}
exports.disconnectClient = disconnectClient;
process.on('exit', function() {
DBPool.end();
});