From 90d578e662789bb989aac1d8c65ebfdada594544 Mon Sep 17 00:00:00 2001 From: Ean Milligan Date: Tue, 7 Apr 2026 03:47:00 -0400 Subject: [PATCH] db init stuff --- .vscode/settings.json | 3 +++ config.example.ts | 1 - db/client.ts | 14 +++++++++++++ db/initialize.ts | 46 +++++++++++++++++++++++++++++++++++++++++++ mod.ts | 16 ++++++++++++++- 5 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 db/client.ts create mode 100644 db/initialize.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index a0afb19..08bdc99 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,8 @@ { "cSpell.words": [ + "Inno", + "longtext", + "tinyint", "xivplan" ] } \ No newline at end of file diff --git a/config.example.ts b/config.example.ts index 5b9dd32..4d857f0 100644 --- a/config.example.ts +++ b/config.example.ts @@ -6,7 +6,6 @@ export const config = { }, db: { host: 'localhost', - localhost: 'localhost', port: 3306, username: '', password: '', diff --git a/db/client.ts b/db/client.ts new file mode 100644 index 0000000..9088763 --- /dev/null +++ b/db/client.ts @@ -0,0 +1,14 @@ +import { Client } from '@mysql'; + +import config from '~config'; + +const dbClient = await new Client().connect({ + hostname: config.db.host, + port: config.db.port, + db: config.db.name, + username: config.db.username, + password: config.db.password, + charset: 'utf8mb4', +}); + +export default dbClient; diff --git a/db/initialize.ts b/db/initialize.ts new file mode 100644 index 0000000..70488cf --- /dev/null +++ b/db/initialize.ts @@ -0,0 +1,46 @@ +import config from '~config'; + +import dbClient from './client.ts'; + +console.log('Attempting to create DB'); +await dbClient.execute(`CREATE SCHEMA IF NOT EXISTS ${config.db.name};`); +await dbClient.execute(`USE ${config.db.name}`); +console.log('DB created'); + +console.log('Attempt to drop all tables'); +await dbClient.execute(`DROP TABLE IF EXISTS plans;`); +await dbClient.execute(`DROP TABLE IF EXISTS users;`); +console.log('Tables dropped'); + +console.log('Attempting to create table users'); +await dbClient.execute(` + CREATE TABLE users ( + id varchar(20) NOT NULL, + name varchar(20) NOT NULL, + pin varchar(16) NOT NULL, + email varchar(255) NULL, + deleteCode varchar(20) NULL, + PRIMARY KEY (id), + UNIQUE KEY users_id_UNIQUE (id) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +`); +console.log('Table created'); + +console.log('Attempting to create table plans'); +await dbClient.execute(` + CREATE TABLE plans ( + id varchar(20) NOT NULL, + ownerId varchar(20) NOT NULL, + name varchar(200) NOT NULL, + folder varchar(200) NOT NULL, + data longtext NOT NULL, + deleted tinyint(1) NOT NULL DEFAULT 0, + PRIMARY KEY (id), + CONSTRAINT plans_ownerId_FK FOREIGN KEY (ownerId) REFERENCES users (id) ON DELETE RESTRICT ON UPDATE RESTRICT, + UNIQUE KEY plans_id_UNIQUE (id) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +`); +console.log('Table created'); + +await dbClient.close(); +console.log('Done!'); diff --git a/mod.ts b/mod.ts index 5ac11d6..722390e 100644 --- a/mod.ts +++ b/mod.ts @@ -1,13 +1,27 @@ +import { customAlphabet } from '@nanoid'; import { STATUS_CODE, STATUS_TEXT, StatusCode } from '@std/http'; import config from '~config'; +// Using custom alphabet to exclude - and _ +const alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; +const nanoid = customAlphabet(alphabet, 20); + +const genericResponse = (status: StatusCode, customText = '') => + new Response(customText || STATUS_TEXT[status], { status: status, statusText: STATUS_TEXT[status] }); + Deno.serve({ port: config.api.port }, async (req) => { + const [urlPath, tempQ] = req.url.split('?'); + const path = (urlPath.split('api/')[1] ?? '').toLowerCase().trim(); + console.log(urlPath, path); + if (req.method === 'GET') { // handle all gets + } else if (path === '/auth') { + return new Response(nanoid()); } else { // handle auth then all other shiz } - return new Response(STATUS_TEXT[STATUS_CODE.NotImplemented]); + return genericResponse(STATUS_CODE.NotImplemented); });