move DB initialization
This commit is contained in:
@@ -5,9 +5,7 @@ import langCard from "./api/top-langs.js";
|
||||
import wakatimeCard from "./api/wakatime.js";
|
||||
import gistCard from "./api/gist.js";
|
||||
import express from "express";
|
||||
import ensureDbSchema from "./src/common/utils.js";
|
||||
|
||||
ensureDbSchema();
|
||||
const app = express();
|
||||
app.listen(process.env.port || 9000);
|
||||
|
||||
|
||||
+5
-16
@@ -257,22 +257,6 @@ const request = (data, headers) => {
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates the database table "requests" if it does not exist.
|
||||
*/
|
||||
export async function ensureDbSchema() {
|
||||
if (!pool) {
|
||||
return;
|
||||
}
|
||||
const createTableSQL = `
|
||||
CREATE TABLE IF NOT EXISTS requests (
|
||||
request TEXT PRIMARY KEY,
|
||||
requested_at TIMESTAMP NOT NULL DEFAULT now()
|
||||
);
|
||||
`;
|
||||
await pool.query(createTableSQL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores or updates the request in the database.
|
||||
*/
|
||||
@@ -281,6 +265,11 @@ export async function storeRequest(req) {
|
||||
return;
|
||||
}
|
||||
const query = `
|
||||
CREATE TABLE IF NOT EXISTS requests (
|
||||
request TEXT PRIMARY KEY,
|
||||
requested_at TIMESTAMP NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
INSERT INTO requests (request, requested_at)
|
||||
VALUES ($1, NOW())
|
||||
ON CONFLICT (request)
|
||||
|
||||
Reference in New Issue
Block a user