update: started to work on the database side of things

This commit is contained in:
maix0 2025-06-20 00:13:01 +02:00 committed by Maieul BOYER
parent eab450626a
commit 0b3ea4b406
7 changed files with 111 additions and 8 deletions

View file

@ -21,6 +21,9 @@
nodejs_24
pnpm
typescript
# allow building better-sqlite3
clang
];
shellHook = ''
export PODMAN_COMPOSE_WARNING_LOGS="false";

View file

@ -63,3 +63,7 @@ test/types/index.js
# compiled app
dist
# generated sources
src/init.sql.ts

View file

@ -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": {

View file

@ -0,0 +1,56 @@
// ************************************************************************** //
// //
// ::: :::::::: //
// embed.js :+: :+: :+: //
// +:+ +:+ +:+ //
// By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ //
// +#+#+#+#+#+ +#+ //
// 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" })
}

View file

@ -0,0 +1,15 @@
// ************************************************************************** //
// //
// ::: :::::::: //
// embed:sql.js :+: :+: :+: //
// +:+ +:+ +:+ //
// By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ //
// +#+#+#+#+#+ +#+ //
// 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')

View file

@ -6,7 +6,7 @@
// By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ //
// +#+#+#+#+#+ +#+ //
// 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<DatabaseOption>(async function(
_fastify: FastifyInstance,
_options: DatabaseOption) {
_fastify: FastifyInstance,
_options: DatabaseOption) {
});
export default uDatabase;

4
src/utils/src/init.sql Normal file
View file

@ -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);