Compare commits

..

14 commits

Author SHA1 Message Date
64affde372 Merge pull request 'implement /hit API endpoint' (#18) from hit-endpoint into main
All checks were successful
Run node --check on everything / node-check (push) Successful in 6s
Check project using prettier / prettier (push) Successful in 26s
Make sure the server is actually able to start / boot-test (push) Successful in 39s
Reviewed-on: 9pfs/countify#18
2024-05-25 04:53:33 +00:00
7151c77d15
Handle requests with no namespace correctly
All checks were successful
Run node --check on everything / node-check (pull_request) Successful in 7s
Check project using prettier / prettier (pull_request) Successful in 20s
Make sure the server is actually able to start / boot-test (pull_request) Successful in 34s
2024-05-25 04:45:13 +00:00
6dfb8ce705
Merge branch 'main' into hit-endpoint
All checks were successful
Run node --check on everything / node-check (pull_request) Successful in 8s
Check project using prettier / prettier (pull_request) Successful in 21s
Make sure the server is actually able to start / boot-test (pull_request) Successful in 27s
2024-05-25 04:16:16 +00:00
6648294566
Merge branch 'main' of ssh://localhost:3322/9pfs/countify
All checks were successful
Run node --check on everything / node-check (push) Successful in 7s
Check project using prettier / prettier (push) Successful in 24s
Make sure the server is actually able to start / boot-test (push) Successful in 48s
2024-05-25 04:15:51 +00:00
eaaf62483c
Fix boot-test name 2024-05-25 04:15:34 +00:00
fe924df3d2
Don't let keys.namespace_id be null!
All checks were successful
Run node --check on everything / node-check (pull_request) Successful in 7s
Make sure the server is actually able to start / prettier (pull_request) Successful in 40s
Check project using prettier / prettier (pull_request) Successful in 36s
Run node --check on everything / node-check (push) Successful in 5s
Check project using prettier / prettier (push) Successful in 17s
Make sure the server is actually able to start / prettier (push) Successful in 31s
2024-05-25 03:37:56 +00:00
f767ab8483
Make sure that keys are not globally unique
All checks were successful
Run node --check on everything / node-check (pull_request) Successful in 8s
Make sure the server is actually able to start / prettier (pull_request) Successful in 25s
Check project using prettier / prettier (pull_request) Successful in 21s
Run node --check on everything / node-check (push) Successful in 7s
Check project using prettier / prettier (push) Successful in 19s
Make sure the server is actually able to start / prettier (push) Successful in 31s
2024-05-25 02:41:00 +00:00
aaed9e854d
Run prettier
All checks were successful
Run node --check on everything / node-check (pull_request) Successful in 6s
Make sure the server is actually able to start / prettier (pull_request) Successful in 26s
Check project using prettier / prettier (pull_request) Successful in 31s
2024-05-25 01:55:14 +00:00
6de8f5bcdb
Hit is supposed to make keys, following the countapi spec.
Some checks failed
Run node --check on everything / node-check (pull_request) Successful in 8s
Make sure the server is actually able to start / prettier (pull_request) Successful in 22s
Check project using prettier / prettier (pull_request) Failing after 21s
2024-05-25 01:53:16 +00:00
2c42cf9dc8
Merge branch 'main' into hit-endpoint
All checks were successful
Run node --check on everything / node-check (pull_request) Successful in 7s
Make sure the server is actually able to start / prettier (pull_request) Successful in 24s
Check project using prettier / prettier (pull_request) Successful in 32s
2024-05-25 01:20:10 +00:00
146e05751d
Merge branch 'main' into hit-endpoint
All checks were successful
Run node --check on everything / node-check (push) Successful in 6s
Run node --check on everything / node-check (pull_request) Successful in 9s
Make sure the server is actually able to start / prettier (push) Successful in 24s
Make sure the server is actually able to start / prettier (pull_request) Successful in 27s
Check project using prettier / prettier (pull_request) Successful in 24s
Check project using prettier / prettier (push) Successful in 31s
2024-05-25 01:07:05 +00:00
1906580fb4
run prettier
All checks were successful
Run node --check on everything / node-check (push) Successful in 6s
Run node --check on everything / node-check (pull_request) Successful in 8s
Make sure the server is actually able to start / prettier (push) Successful in 30s
Make sure the server is actually able to start / prettier (pull_request) Successful in 27s
Check project using prettier / prettier (pull_request) Successful in 32s
Check project using prettier / prettier (push) Successful in 38s
2024-05-25 00:58:23 +00:00
37177e391d
Fixes to the /hit endpoint
Some checks failed
Run node --check on everything / node-check (push) Successful in 9s
Run node --check on everything / node-check (pull_request) Successful in 10s
Check project using prettier / prettier (push) Failing after 25s
Check project using prettier / prettier (pull_request) Failing after 28s
Make sure the server is actually able to start / prettier (pull_request) Successful in 32s
Make sure the server is actually able to start / prettier (push) Successful in 42s
2024-05-25 00:54:52 +00:00
361ab5e8f4
Partially implement /hit
All checks were successful
Run node --check on everything / node-check (push) Successful in 8s
Make sure the server is actually able to start / prettier (push) Successful in 26s
Check project using prettier / prettier (push) Successful in 34s
Run node --check on everything / node-check (pull_request) Successful in 6s
Make sure the server is actually able to start / prettier (pull_request) Successful in 22s
Check project using prettier / prettier (pull_request) Successful in 21s
2024-05-25 00:41:38 +00:00
5 changed files with 70 additions and 3 deletions

View file

@ -8,7 +8,7 @@ on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
prettier:
boot-test:
runs-on: docker
steps:
- uses: actions/checkout@v3

View file

@ -68,6 +68,55 @@ async function get_counter_value(ns_name, key) {
}
}
async function counter_increment(ns_name, key, offset, create = false) {
var nsid = await get_namespace_id(ns_name);
return await sql.begin(async (sql) => {
if (offset == 0) return true;
if (create) {
var existence_res =
await sql`SELECT value FROM keys WHERE name = ${key} AND namespace_id = ${nsid}`;
if (existence_res.length == 0) {
if (
!(await add_counter({
namespace: ns_name,
key: key,
value: 1,
enable_reset: false,
update_lowerbound: 0,
update_upperbound: 1,
}))
)
return null;
else return 1; // We know the key value will be 1 here, just return it explicitly (Ending early to save some SQL calls)
}
}
var sql_res = await sql`UPDATE keys SET value = value + ${offset} WHERE
namespace_id = ${nsid} AND
name = ${key} AND
${offset < 0 ? sql`update_lowerbound <= ${offset}` : sql`update_upperbound >= ${offset}`}
RETURNING value`;
if (sql_res.length == 1) return parseInt(sql_res[0].value);
else return null;
});
}
app.get("/hit/:ns/:key", async function (req, res) {
var counter_res = await counter_increment(
req.params.ns,
req.params.key,
1,
true,
);
return res
.status(counter_res == null ? 403 : 200)
.json({ value: counter_res });
});
app.get("/hit/:key", async function (req, res) {
var counter_res = await counter_increment("default", req.params.key, 1, true);
return res
.status(counter_res == null ? 403 : 200)
.json({ value: counter_res });
});
app.get("/get/:ns/:key", async function (req, res) {
var e = await get_counter_value(req.params.ns, req.params.key);
return res.status(e == null ? 404 : 200).json({ value: e });

View file

@ -5,8 +5,8 @@ CREATE TABLE IF NOT EXISTS namespaces(
);
CREATE TABLE IF NOT EXISTS keys(
id SERIAL PRIMARY KEY,
namespace_id INTEGER REFERENCES namespaces (id) ON DELETE RESTRICT,
name TEXT UNIQUE NOT NULL,
namespace_id INTEGER REFERENCES namespaces (id) ON DELETE RESTRICT NOT NULL,
name TEXT NOT NULL,
-- Someone might want to store huge numbers in here, so... why not?
value BIGINT NOT NULL DEFAULT 0,
enable_reset BOOLEAN NOT NULL DEFAULT TRUE,

View file

@ -0,0 +1,10 @@
-- 0002-no-globally-unique-keys.sql
-- This makes keys with the same name be able to exist in different namespaces, while still preventing duplicates in the same namespace.
BEGIN;
-- Delete the "unique" constraint
ALTER TABLE keys DROP CONSTRAINT IF EXISTS keys_name_key;
-- Avoid duplicate constraints.
ALTER TABLE keys DROP CONSTRAINT IF EXISTS keys_name_namespace_id_key;
-- Make sure that name and namespace_id together still have to be unique
ALTER TABLE keys ADD UNIQUE (name, namespace_id);
COMMIT;

View file

@ -0,0 +1,8 @@
-- Make sure that keys.namespace_id isn't able to be null
BEGIN;
-- Remove all null keys.namespace_id values. Nuking data like this is reckless, but it's the only option.
DROP FROM keys WHERE namespace_id = NULL;
-- Delete before adding to avoid conflicts
ALTER TABLE keys ALTER COLUMN namespace_id DROP NOT NULL;
ALTER TABLE keys ALTER COLUMN namespace_id SET NOT NULL;
COMMIT;