Compare commits

...

2 commits

Author SHA1 Message Date
fda599c32a
Un-wrap this sql call
All checks were successful
/ node-check (pull_request) Successful in 3s
/ tests (pull_request) Successful in 19s
/ boot (pull_request) Successful in 13s
/ prettier (pull_request) Successful in 20s
/ node-check (push) Successful in 3s
/ tests (push) Successful in 18s
/ prettier (push) Successful in 20s
/ boot (push) Successful in 30s
2024-05-26 15:07:01 +00:00
51c684a416
Revert "Un-wrap this sql call"
This reverts commit 8314e7fe6c.
2024-05-26 15:06:07 +00:00

View file

@ -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) {