-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory.mjs
85 lines (75 loc) · 3.13 KB
/
memory.mjs
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
import {
MYSQL, POSTGRESQL, cleanSql, deleteAll, deleteById, getProvider, query,
queryAll, queryById, upsert,
} from './dbio.mjs';
import { ensureArray, parseJson, trim } from './utilitas.mjs';
const [table, defaultKey] = ['utilitas_memory', { key: 'key' }];
const [pack, unpack] = [val => JSON.stringify(val), val => parseJson(val, val)];
const initSql = {
[MYSQL]: [[
cleanSql(`CREATE TABLE IF NOT EXISTS ?? (
\`key\` VARCHAR(255) NOT NULL,
\`value\` TEXT,
\`created_at\` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
\`updated_at\` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (\`key\`),
INDEX value (\`value\`(768)),
INDEX created_at (\`created_at\`),
INDEX updated_at (\`updated_at\`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`), [table],
]],
[POSTGRESQL]: [[
cleanSql(`CREATE TABLE IF NOT EXISTS ${table} (
key VARCHAR(255) NOT NULL,
value TEXT,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (key)
)`)
// Error: index row size 2776 exceeds btree version 4 maximum 2704 for index "value_index"
// @todo: Fix this issue, by @Leask.
], /* [`CREATE INDEX IF NOT EXISTS value_index ON ${table} (value)`], */[
`CREATE INDEX IF NOT EXISTS ${table}_created_at_index ON ${table} (created_at)`,
], [
`CREATE INDEX IF NOT EXISTS ${table}_updated_at_index ON ${table} (updated_at)`,
]],
};
const init = async () => {
const provider = await getProvider();
const _result = [];
for (let act of initSql[provider]) { _result.push(await query(...act)); }
return { del, get, set, _result };
};
const assertKey = (key, options) => {
key = trim(key, { case: 'UP' });
assert(options?.ignoreError || key, 'Invalid memory key.', 400);
return key;
};
const handleResult = (resp, options) => {
if (!options?.skipEcho) {
ensureArray(resp).map(x => { x.value = unpack(x.value); });
let result = {};
if (Array.isArray(resp)) {
for (let i in resp) { result[resp[i].key] = resp[i].value; }
} else { result = resp?.value; }
resp = result;
}
return resp;
};
const set = async (key, value, options) => {
options = { skipEcho: true, ...options || {}, ...defaultKey };
return handleResult(await upsert(table, {
key: assertKey(key), value: pack(value), updated_at: new Date()
}, options), options);
};
const get = async (key, options) => {
options = { ...options || {}, ...defaultKey };
return handleResult(await ((key = assertKey(key, { ignoreError: true }))
? queryById(table, key, options) : queryAll(table, options)), options);
};
const del = async (key, options) => {
options = { ...options || {}, ...defaultKey };
return await ((key = assertKey(key, { ignoreError: options?.force }))
? deleteById(table, key, options) : deleteAll(table, options));
};
export { del, get, init, set };