Compare commits
2 commits
8314e7fe6c
...
fda599c32a
Author | SHA1 | Date | |
---|---|---|---|
fda599c32a | |||
51c684a416 |
1 changed files with 19 additions and 18 deletions
37
index.js
37
index.js
|
@ -5,18 +5,21 @@ import { randomUUID } from "crypto";
|
|||
const app = express();
|
||||
const valid_regex = /^[A-Za-z0-9_\-\.]{3,64}$/;
|
||||
async function get_namespace_id(name, create = true) {
|
||||
if (!valid_regex.test(name)) return null; // For sanity.
|
||||
if (create) {
|
||||
var potential_res =
|
||||
await sql`INSERT INTO namespaces(name) VALUES (${name}) ON CONFLICT DO NOTHING RETURNING id;`;
|
||||
}
|
||||
if (!create || potential_res?.length == 0) {
|
||||
var e = await sql`SELECT id FROM namespaces WHERE name = ${name} LIMIT 1`;
|
||||
if (e.length == 0) return null;
|
||||
return e[0].id;
|
||||
} else {
|
||||
return potential_res[0].id;
|
||||
}
|
||||
return await sql.begin(async (sql) => {
|
||||
if (!valid_regex.test(name)) return null; // For sanity.
|
||||
if (create) {
|
||||
var potential_res = await sql`
|
||||
INSERT INTO namespaces(name) VALUES (${name}) ON CONFLICT DO NOTHING RETURNING id;
|
||||
`;
|
||||
}
|
||||
if (!create || potential_res?.length == 0) {
|
||||
var e = await sql`SELECT id FROM namespaces WHERE name = ${name} LIMIT 1`;
|
||||
if (e.length == 0) return null;
|
||||
return e[0].id;
|
||||
} else {
|
||||
return potential_res[0].id;
|
||||
}
|
||||
});
|
||||
}
|
||||
async function add_counter(opts) {
|
||||
const {
|
||||
|
@ -102,12 +105,10 @@ async function counter_update(ns_name, key, amount) {
|
|||
var attempt = await counter_increment(ns_name, key, amount, false);
|
||||
if (attempt) return attempt;
|
||||
var nsid = await get_namespace_id(ns_name);
|
||||
return await sql.begin(async (sql) => {
|
||||
var res =
|
||||
await sql`SELECT value FROM keys WHERE name = ${key} AND namespace_id = ${nsid}`;
|
||||
if (res.length == 0) return null;
|
||||
else return parseInt(res[0].value);
|
||||
});
|
||||
var res =
|
||||
await sql`SELECT value FROM keys WHERE name = ${key} AND namespace_id = ${nsid}`;
|
||||
if (res.length == 0) return null;
|
||||
else return parseInt(res[0].value);
|
||||
}
|
||||
|
||||
async function counter_set(ns_name, key, value) {
|
||||
|
|
Loading…
Reference in a new issue