refactor: restructure frontend and backend by domain services (#26)
CD — Deployment / deploy (push) Successful in 34s
CD — Deployment / deploy (push) Successful in 34s
Split the monolithic backend (lib/, routes/, access.ts, storage.ts, media.ts) into a layered structure inspired by JeanXXIII-CRM: - config/: appConfig, db - middleware/: auth (session, roles, cookies) - models/: shared types - routes/: *Routes.ts (thin HTTP handlers) - services/: *Service.ts (business logic, one per domain) - utils/: errors, http, migrate, ratelimit, validators, service bootstrap Frontend likewise: services/*Service.ts + apiClient, contexts/, types/, utils/format. Route files now carry the "Routes" suffix; service files are named authService.ts, ratelimit.ts, etc. and live in their own files, frontend and backend separated. Updated docs and READMEs to reflect the new layout. Typecheck and builds pass. --------- Co-authored-by: xEsiah <[email protected]> Reviewed-on: #26 Reviewed-by: Esiah <[email protected]> Co-authored-by: Gitea Actions <-[email protected]>
This commit was merged in pull request #26.
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { readdir, readFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import type { Db } from "../config/db.js";
|
||||
|
||||
export interface MigrationFile {
|
||||
version: string;
|
||||
file: string;
|
||||
sql: string;
|
||||
checksum: string;
|
||||
}
|
||||
|
||||
export interface MigrationResult {
|
||||
applied: string[];
|
||||
skipped: string[];
|
||||
drifted: { version: string; recorded: string; current: string }[];
|
||||
}
|
||||
|
||||
const FILE_PATTERN = /^(\d{3,4}_[a-z0-9_]+)\.sql$/;
|
||||
|
||||
export const listMigrations = async (dir: string): Promise<MigrationFile[]> => {
|
||||
const entries = await readdir(dir);
|
||||
const files = entries.filter((entry) => FILE_PATTERN.test(entry)).sort();
|
||||
|
||||
return Promise.all(
|
||||
files.map(async (file) => {
|
||||
const sql = await readFile(path.join(dir, file), "utf8");
|
||||
return {
|
||||
version: FILE_PATTERN.exec(file)?.[1] ?? file,
|
||||
file,
|
||||
sql,
|
||||
checksum: createHash("sha256").update(sql).digest("hex"),
|
||||
};
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
const ensureLedger = async (db: Db): Promise<void> => {
|
||||
await db.raw(`
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version VARCHAR(64) NOT NULL,
|
||||
applied_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (version)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
||||
`);
|
||||
await db.raw(
|
||||
"ALTER TABLE schema_migrations ADD COLUMN IF NOT EXISTS checksum CHAR(64) NULL AFTER applied_at",
|
||||
);
|
||||
};
|
||||
|
||||
export const runMigrations = async (
|
||||
db: Db,
|
||||
dir: string,
|
||||
options: { dryRun?: boolean } = {},
|
||||
): Promise<MigrationResult> => {
|
||||
const files = await listMigrations(dir);
|
||||
await ensureLedger(db);
|
||||
|
||||
const applied = new Map<string, { checksum: string | null }>();
|
||||
const rows = await db.query<{ version: string; checksum: string | null }>(
|
||||
"SELECT version, checksum FROM schema_migrations",
|
||||
);
|
||||
for (const row of rows) applied.set(row.version, { checksum: row.checksum });
|
||||
|
||||
const result: MigrationResult = { applied: [], skipped: [], drifted: [] };
|
||||
|
||||
for (const migration of files) {
|
||||
const record = applied.get(migration.version);
|
||||
|
||||
if (record) {
|
||||
result.skipped.push(migration.version);
|
||||
if (record.checksum && record.checksum !== migration.checksum) {
|
||||
result.drifted.push({
|
||||
version: migration.version,
|
||||
recorded: record.checksum,
|
||||
current: migration.checksum,
|
||||
});
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (options.dryRun) {
|
||||
result.applied.push(migration.version);
|
||||
continue;
|
||||
}
|
||||
|
||||
const statements = migration.sql
|
||||
.split(/;\s*(?:\r?\n|$)/)
|
||||
.map((statement) =>
|
||||
statement
|
||||
.split(/\r?\n/)
|
||||
.filter((line) => !line.trim().startsWith("--"))
|
||||
.join("\n")
|
||||
.trim(),
|
||||
)
|
||||
.filter((statement) => statement.length > 0);
|
||||
|
||||
for (const statement of statements) await db.raw(statement);
|
||||
|
||||
await db.exec("INSERT INTO schema_migrations (version, checksum) VALUES (?, ?)", [
|
||||
migration.version,
|
||||
migration.checksum,
|
||||
]);
|
||||
result.applied.push(migration.version);
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
export const assertNoDrift = (result: MigrationResult): void => {
|
||||
if (result.drifted.length === 0) return;
|
||||
const lines = result.drifted.map(
|
||||
(d) => ` ${d.version} : appliquee ${d.recorded.slice(0, 12)}, fichier ${d.current.slice(0, 12)}`,
|
||||
);
|
||||
throw new Error(
|
||||
`Migrations deja appliquees modifiees depuis leur execution (${result.drifted.length}) :\n` +
|
||||
`${lines.join("\n")}\n` +
|
||||
"Une migration appliquee ne se modifie pas : creez un nouveau fichier.",
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user