Rename methods for consistency
All checks were successful
/ prettier (pull_request) Successful in 16s
/ node-check (pull_request) Successful in 3s
/ tests (pull_request) Successful in 25s
/ boot (pull_request) Successful in 9s
/ node-check (push) Successful in 3s
/ prettier (push) Successful in 15s
/ boot (push) Successful in 14s
/ tests (push) Successful in 24s
All checks were successful
/ prettier (pull_request) Successful in 16s
/ node-check (pull_request) Successful in 3s
/ tests (pull_request) Successful in 25s
/ boot (pull_request) Successful in 9s
/ node-check (push) Successful in 3s
/ prettier (push) Successful in 15s
/ boot (push) Successful in 14s
/ tests (push) Successful in 24s
This commit is contained in:
parent
57a6494261
commit
b9f41165e0
1 changed files with 10 additions and 10 deletions
20
index.js
20
index.js
|
@ -32,7 +32,7 @@ async function get_namespace_id(name, create = true) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
async function add_counter(opts) {
|
async function counter_create(opts) {
|
||||||
const {
|
const {
|
||||||
key,
|
key,
|
||||||
value,
|
value,
|
||||||
|
@ -68,7 +68,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);
|
const nsid = await get_namespace_id(ns_name, false);
|
||||||
if (nsid == null) {
|
if (nsid == null) {
|
||||||
if (!return_id) return null;
|
if (!return_id) return null;
|
||||||
|
@ -98,7 +98,7 @@ async function counter_increment(ns_name, key, offset, create = false) {
|
||||||
await sql`SELECT value FROM keys WHERE name = ${key} AND namespace_id = ${nsid}`;
|
await sql`SELECT value FROM keys WHERE name = ${key} AND namespace_id = ${nsid}`;
|
||||||
if (existence_res.length == 0) {
|
if (existence_res.length == 0) {
|
||||||
if (
|
if (
|
||||||
!(await add_counter({
|
!(await counter_create({
|
||||||
namespace: ns_name,
|
namespace: ns_name,
|
||||||
key: key,
|
key: key,
|
||||||
value: 1,
|
value: 1,
|
||||||
|
@ -137,7 +137,7 @@ async function counter_update(ns_name, key, amount) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function counter_set(ns_name, key, value) {
|
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 };
|
if (old_val == null) return { old_value: null, value: null, failed: true };
|
||||||
return await sql.begin(async (sql) => {
|
return await sql.begin(async (sql) => {
|
||||||
var changeable = (
|
var changeable = (
|
||||||
|
@ -208,7 +208,7 @@ app.get("/hit/:key", async function (req, res) {
|
||||||
.json({ value: counter_res });
|
.json({ value: counter_res });
|
||||||
});
|
});
|
||||||
app.get("/update/:ns/:key", async function (req, 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(
|
var counter_res = await counter_increment(
|
||||||
req.params.ns,
|
req.params.ns,
|
||||||
req.params.key,
|
req.params.key,
|
||||||
|
@ -221,7 +221,7 @@ app.get("/update/:ns/:key", async function (req, res) {
|
||||||
.json({ value: counter_res });
|
.json({ value: counter_res });
|
||||||
});
|
});
|
||||||
app.get("/update/:key", async function (req, 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(
|
var counter_res = await counter_increment(
|
||||||
"default",
|
"default",
|
||||||
req.params.key,
|
req.params.key,
|
||||||
|
@ -274,11 +274,11 @@ app.get("/info/:key", async function (req, res) {
|
||||||
return res.status(e.value == null ? 404 : 200).json(e);
|
return res.status(e.value == null ? 404 : 200).json(e);
|
||||||
});
|
});
|
||||||
app.get("/get/:ns/:key", async function (req, res) {
|
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 });
|
return res.status(e == null ? 404 : 200).json({ value: e });
|
||||||
});
|
});
|
||||||
app.get("/get/:key", async function (req, res) {
|
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 });
|
return res.status(e == null ? 404 : 200).json({ value: e });
|
||||||
});
|
});
|
||||||
app.get("/create", async function (req, res) {
|
app.get("/create", async function (req, res) {
|
||||||
|
@ -292,8 +292,8 @@ app.get("/create", async function (req, res) {
|
||||||
if (!sane_query?.update_lowerbound) sane_query.update_lowerbound = "-1";
|
if (!sane_query?.update_lowerbound) sane_query.update_lowerbound = "-1";
|
||||||
if (!sane_query?.update_upperbound) sane_query.update_upperbound = "1";
|
if (!sane_query?.update_upperbound) sane_query.update_upperbound = "1";
|
||||||
if (!sane_query?.value) sane_query.value = "0";
|
if (!sane_query?.value) sane_query.value = "0";
|
||||||
const add_counter_res = await add_counter(sane_query);
|
const counter_create_res = await counter_create(sane_query);
|
||||||
if (add_counter_res == true) {
|
if (counter_create_res == true) {
|
||||||
return res.json({
|
return res.json({
|
||||||
namespace: sane_query.namespace,
|
namespace: sane_query.namespace,
|
||||||
key: sane_query.key,
|
key: sane_query.key,
|
||||||
|
|
Loading…
Reference in a new issue