Compare commits
3 commits
57a6494261
...
499a1eb440
Author | SHA1 | Date | |
---|---|---|---|
499a1eb440 | |||
0b67b3e3e8 | |||
b9f41165e0 |
1 changed files with 14 additions and 10 deletions
24
index.js
24
index.js
|
@ -4,6 +4,7 @@ import { unlink } from "fs/promises";
|
|||
import { randomUUID } from "crypto";
|
||||
const app = express();
|
||||
const valid_regex = /^[A-Za-z0-9_\-\.]{3,64}$/;
|
||||
var namespace_id_cache = {};
|
||||
var stats = {
|
||||
keys_created: 0,
|
||||
keys_updated: 0,
|
||||
|
@ -18,6 +19,7 @@ app.use((req, res, next) => {
|
|||
async function get_namespace_id(name, create = true) {
|
||||
return await sql.begin(async (sql) => {
|
||||
if (!valid_regex.test(name)) return null; // For sanity.
|
||||
if (namespace_id_cache[name] != undefined) return namespace_id_cache[name];
|
||||
if (create) {
|
||||
var potential_res = await sql`
|
||||
INSERT INTO namespaces(name) VALUES (${name}) ON CONFLICT DO NOTHING RETURNING id;
|
||||
|
@ -26,13 +28,15 @@ async function get_namespace_id(name, create = true) {
|
|||
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;
|
||||
namespace_id_cache[name] = e[0].id;
|
||||
return e[0].id;
|
||||
} else {
|
||||
namespace_id_cache[name] = potential_res[0].id;
|
||||
return potential_res[0].id;
|
||||
}
|
||||
});
|
||||
}
|
||||
async function add_counter(opts) {
|
||||
async function counter_create(opts) {
|
||||
const {
|
||||
key,
|
||||
value,
|
||||
|
@ -68,7 +72,7 @@ async function add_counter(opts) {
|
|||
}
|
||||
}
|
||||
|
||||
async function get_counter_value(ns_name, key, return_id = false) {
|
||||
async function counter_get(ns_name, key, return_id = false) {
|
||||
const nsid = await get_namespace_id(ns_name, false);
|
||||
if (nsid == null) {
|
||||
if (!return_id) return null;
|
||||
|
@ -98,7 +102,7 @@ async function counter_increment(ns_name, key, offset, create = false) {
|
|||
await sql`SELECT value FROM keys WHERE name = ${key} AND namespace_id = ${nsid}`;
|
||||
if (existence_res.length == 0) {
|
||||
if (
|
||||
!(await add_counter({
|
||||
!(await counter_create({
|
||||
namespace: ns_name,
|
||||
key: key,
|
||||
value: 1,
|
||||
|
@ -137,7 +141,7 @@ async function counter_update(ns_name, key, amount) {
|
|||
}
|
||||
|
||||
async function counter_set(ns_name, key, value) {
|
||||
var { old_val: value, nsid } = await get_counter_value(ns_name, key, true);
|
||||
var { old_val: value, nsid } = await counter_get(ns_name, key, true);
|
||||
if (old_val == null) return { old_value: null, value: null, failed: true };
|
||||
return await sql.begin(async (sql) => {
|
||||
var changeable = (
|
||||
|
@ -208,7 +212,7 @@ app.get("/hit/:key", async function (req, res) {
|
|||
.json({ value: counter_res });
|
||||
});
|
||||
app.get("/update/:ns/:key", async function (req, res) {
|
||||
var old_val = await get_counter_value(req.params.ns, req.params.key);
|
||||
var old_val = await counter_get(req.params.ns, req.params.key);
|
||||
var counter_res = await counter_increment(
|
||||
req.params.ns,
|
||||
req.params.key,
|
||||
|
@ -221,7 +225,7 @@ app.get("/update/:ns/:key", async function (req, res) {
|
|||
.json({ value: counter_res });
|
||||
});
|
||||
app.get("/update/:key", async function (req, res) {
|
||||
var old_val = await get_counter_value("default", req.params.key);
|
||||
var old_val = await counter_get("default", req.params.key);
|
||||
var counter_res = await counter_increment(
|
||||
"default",
|
||||
req.params.key,
|
||||
|
@ -274,11 +278,11 @@ app.get("/info/:key", async function (req, res) {
|
|||
return res.status(e.value == null ? 404 : 200).json(e);
|
||||
});
|
||||
app.get("/get/:ns/:key", async function (req, res) {
|
||||
var e = await get_counter_value(req.params.ns, req.params.key);
|
||||
var e = await counter_get(req.params.ns, req.params.key);
|
||||
return res.status(e == null ? 404 : 200).json({ value: e });
|
||||
});
|
||||
app.get("/get/:key", async function (req, res) {
|
||||
var e = await get_counter_value("default", req.params.key);
|
||||
var e = await counter_get("default", req.params.key);
|
||||
return res.status(e == null ? 404 : 200).json({ value: e });
|
||||
});
|
||||
app.get("/create", async function (req, res) {
|
||||
|
@ -292,8 +296,8 @@ app.get("/create", async function (req, res) {
|
|||
if (!sane_query?.update_lowerbound) sane_query.update_lowerbound = "-1";
|
||||
if (!sane_query?.update_upperbound) sane_query.update_upperbound = "1";
|
||||
if (!sane_query?.value) sane_query.value = "0";
|
||||
const add_counter_res = await add_counter(sane_query);
|
||||
if (add_counter_res == true) {
|
||||
const counter_create_res = await counter_create(sane_query);
|
||||
if (counter_create_res == true) {
|
||||
return res.json({
|
||||
namespace: sane_query.namespace,
|
||||
key: sane_query.key,
|
||||
|
|
Loading…
Reference in a new issue