update: started to work on the database side of things
This commit is contained in:
parent
eab450626a
commit
0b3ea4b406
7 changed files with 111 additions and 8 deletions
|
|
@ -21,6 +21,9 @@
|
||||||
nodejs_24
|
nodejs_24
|
||||||
pnpm
|
pnpm
|
||||||
typescript
|
typescript
|
||||||
|
|
||||||
|
# allow building better-sqlite3
|
||||||
|
clang
|
||||||
];
|
];
|
||||||
shellHook = ''
|
shellHook = ''
|
||||||
export PODMAN_COMPOSE_WARNING_LOGS="false";
|
export PODMAN_COMPOSE_WARNING_LOGS="false";
|
||||||
|
|
|
||||||
4
src/utils/.gitignore
vendored
4
src/utils/.gitignore
vendored
|
|
@ -63,3 +63,7 @@ test/types/index.js
|
||||||
|
|
||||||
# compiled app
|
# compiled app
|
||||||
dist
|
dist
|
||||||
|
|
||||||
|
|
||||||
|
# generated sources
|
||||||
|
src/init.sql.ts
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,9 @@
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "utils library",
|
"description": "utils library",
|
||||||
"scripts": {
|
"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"
|
"watch:ts": "tsc -d -w"
|
||||||
},
|
},
|
||||||
"exports": {
|
"exports": {
|
||||||
|
|
|
||||||
56
src/utils/scripts/embed.js
Normal file
56
src/utils/scripts/embed.js
Normal 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" })
|
||||||
|
}
|
||||||
15
src/utils/scripts/embed:sql.js
Normal file
15
src/utils/scripts/embed:sql.js
Normal 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')
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
// By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ //
|
// By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ //
|
||||||
// +#+#+#+#+#+ +#+ //
|
// +#+#+#+#+#+ +#+ //
|
||||||
// Created: 2025/06/17 17:06:31 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 { FastifyInstance } from 'fastify'
|
||||||
import sqlite from 'better-sqlite3'
|
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
|
// When using .decorate you have to specify added properties for Typescript
|
||||||
declare module 'fastify' {
|
declare module 'fastify' {
|
||||||
export interface FastifyInstance {
|
export interface FastifyInstance {
|
||||||
database: sqlite.Database;
|
db: Database;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type DatabaseOption = {
|
export type DatabaseOption = {
|
||||||
path: string;
|
path: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const uDatabase = fp<DatabaseOption>(async function(
|
export const uDatabase = fp<DatabaseOption>(async function(
|
||||||
_fastify: FastifyInstance,
|
_fastify: FastifyInstance,
|
||||||
_options: DatabaseOption) {
|
_options: DatabaseOption) {
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export default uDatabase;
|
export default uDatabase;
|
||||||
|
|
|
||||||
4
src/utils/src/init.sql
Normal file
4
src/utils/src/init.sql
Normal 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);
|
||||||
Loading…
Add table
Add a link
Reference in a new issue