From 0b3ea4b4061b65e4bbab068a42e657e414a2fb16 Mon Sep 17 00:00:00 2001 From: maix0 Date: Fri, 20 Jun 2025 00:13:01 +0200 Subject: [PATCH] update: started to work on the database side of things --- flake.nix | 3 ++ src/utils/.gitignore | 4 +++ src/utils/package.json | 4 ++- src/utils/scripts/embed.js | 56 ++++++++++++++++++++++++++++++++++ src/utils/scripts/embed:sql.js | 15 +++++++++ src/utils/src/database.ts | 33 +++++++++++++++----- src/utils/src/init.sql | 4 +++ 7 files changed, 111 insertions(+), 8 deletions(-) create mode 100644 src/utils/scripts/embed.js create mode 100644 src/utils/scripts/embed:sql.js create mode 100644 src/utils/src/init.sql diff --git a/flake.nix b/flake.nix index 52cfd40..88dc1d4 100644 --- a/flake.nix +++ b/flake.nix @@ -21,6 +21,9 @@ nodejs_24 pnpm typescript + + # allow building better-sqlite3 + clang ]; shellHook = '' export PODMAN_COMPOSE_WARNING_LOGS="false"; diff --git a/src/utils/.gitignore b/src/utils/.gitignore index f4cefe8..ce10241 100644 --- a/src/utils/.gitignore +++ b/src/utils/.gitignore @@ -63,3 +63,7 @@ test/types/index.js # compiled app dist + + +# generated sources +src/init.sql.ts diff --git a/src/utils/package.json b/src/utils/package.json index 824bd4b..f1b8ea3 100644 --- a/src/utils/package.json +++ b/src/utils/package.json @@ -4,7 +4,9 @@ "version": "1.0.0", "description": "utils library", "scripts": { - "build:ts": "tsc -d", + "embed": "npm run embed:sql", + "embed:sql": "node scripts/embed:sql.js", + "build:ts": "npm run embed:sql && tsc -d", "watch:ts": "tsc -d -w" }, "exports": { diff --git a/src/utils/scripts/embed.js b/src/utils/scripts/embed.js new file mode 100644 index 0000000..cdbc078 --- /dev/null +++ b/src/utils/scripts/embed.js @@ -0,0 +1,56 @@ +// ************************************************************************** // +// // +// ::: :::::::: // +// embed.js :+: :+: :+: // +// +:+ +:+ +:+ // +// By: maiboyer +#+ +:+ +#+ // +// +#+#+#+#+#+ +#+ // +// Created: 2025/06/19 23:32:59 by maiboyer #+# #+# // +// Updated: 2025/06/20 00:09:04 by maiboyer ### ########.fr // +// // +// ************************************************************************** // + +import { readFile, writeFile, stat } from "node:fs/promises"; + +/** + * escape a string to be a valid js string literal + * @param {string} input + * @returns {string} + */ +function escape(input) { + return JSON.stringify(input) + .replace('\n', '\\n') + .replace('\t', '\\t') + .replace('\r', '\\r') + .replace('\v', '\\v'); +} + +/** + * @description Embed {input} inside a default exported string at location {output} + * @param {string} input + * @param {string} output + * @returns void + */ +export default async function embed(input, output) { + const inputData = (await readFile(input)).toString('utf-8'); + const inputStat = await stat(input); + + const escapedData = escape(inputData); + + const fullFile = `\ +//! this file was generated automatically. +//! it is just a string literal that is the file ${input} +//! if you want to edit this file, DONT. edit ${input} please +//! +//! this file need to be regenerated on changes to ${input} manually. +//! the \`npm run build:ts\` might regenerate it, but do check. +//! here is the date of the last time it was generated: ${new Date(Date.now())} +//! the file ${input} that is embeded was modified on ${inputStat.mtime} +//! the file ${input} that is embeded was ${inputStat.size} bytes + + +export default ${escapedData};\ +`; + + await writeFile(output, fullFile, { flush: true, flag: "w" }) +} diff --git a/src/utils/scripts/embed:sql.js b/src/utils/scripts/embed:sql.js new file mode 100644 index 0000000..2bac90d --- /dev/null +++ b/src/utils/scripts/embed:sql.js @@ -0,0 +1,15 @@ +// ************************************************************************** // +// // +// ::: :::::::: // +// embed:sql.js :+: :+: :+: // +// +:+ +:+ +:+ // +// By: maiboyer +#+ +:+ +#+ // +// +#+#+#+#+#+ +#+ // +// Created: 2025/06/19 23:30:39 by maiboyer #+# #+# // +// Updated: 2025/06/20 00:02:19 by maiboyer ### ########.fr // +// // +// ************************************************************************** // + +import embed from "./embed.js"; + +await embed('./src/init.sql', './src/init.sql.ts') diff --git a/src/utils/src/database.ts b/src/utils/src/database.ts index 8a29415..4393fb5 100644 --- a/src/utils/src/database.ts +++ b/src/utils/src/database.ts @@ -6,7 +6,7 @@ // By: maiboyer +#+ +:+ +#+ // // +#+#+#+#+#+ +#+ // // Created: 2025/06/17 17:06:31 by maiboyer #+# #+# // -// Updated: 2025/06/17 17:27:42 by maiboyer ### ########.fr // +// Updated: 2025/06/20 00:11:43 by maiboyer ### ########.fr // // // // ************************************************************************** // @@ -14,20 +14,39 @@ import fp from 'fastify-plugin' import { FastifyInstance } from 'fastify' import sqlite from 'better-sqlite3' +import initSql from "./init.sql.js" + +export class Database { + private db: sqlite.Database; + + constructor(db_path: string) { + this.db = sqlite(db_path, {}); + this.db.pragma('journal_mode = WAL'); + this.db.exec(initSql); + } + + destroy(): void { + this.db?.close(); + } + +} + // When using .decorate you have to specify added properties for Typescript declare module 'fastify' { - export interface FastifyInstance { - database: sqlite.Database; - } + export interface FastifyInstance { + db: Database; + } } export type DatabaseOption = { - path: string; + path: string; }; export const uDatabase = fp(async function( - _fastify: FastifyInstance, - _options: DatabaseOption) { + _fastify: FastifyInstance, + _options: DatabaseOption) { + + }); export default uDatabase; diff --git a/src/utils/src/init.sql b/src/utils/src/init.sql new file mode 100644 index 0000000..9ec752f --- /dev/null +++ b/src/utils/src/init.sql @@ -0,0 +1,4 @@ +-- this file will make sure that the database is always up to date with the correct schema +-- when editing this file, make sure to always include stuff like `IF NOT EXISTS` such as to not throw error +-- NEVER DROP ANYTHING IN THIS FILE +CREATE TABLE IF NOT EXISTS users (name STRING);