2025-02-21
This commit is contained in:
parent
98131c3b78
commit
5d5918f0d7
69 changed files with 1481 additions and 305 deletions
|
|
@ -1,9 +1,8 @@
|
|||
import { drizzle } from "drizzle-orm/sqlite-proxy";
|
||||
import { drizzle, type SqliteRemoteDatabase } from "drizzle-orm/sqlite-proxy";
|
||||
import { Data, Effect } from "effect";
|
||||
import { SQLocalDrizzle } from "sqlocal/drizzle";
|
||||
|
||||
class LocalSqliteError extends Data.TaggedError("LocalSqliteError")<{ cause: unknown }> {
|
||||
}
|
||||
class LocalSqliteError extends Data.TaggedError("LocalSqliteError")<{ cause: unknown }> {}
|
||||
|
||||
export class LocalSqlite extends Effect.Service<LocalSqlite>()("LocalSqlite", {
|
||||
effect: Effect.gen(function*() {
|
||||
|
|
@ -30,13 +29,13 @@ export class LocalSqlite extends Effect.Service<LocalSqlite>()("LocalSqlite", {
|
|||
}),
|
||||
});
|
||||
|
||||
const orm = drizzle(client.driver, client.batchDriver);
|
||||
const orm: SqliteRemoteDatabase = drizzle(client.driver, client.batchDriver);
|
||||
|
||||
const query = <R>(execute: (_: typeof orm) => Promise<R>) =>
|
||||
Effect.tryPromise({
|
||||
catch: (error: unknown) => new LocalSqliteError({ cause: error }),
|
||||
try: () => execute(orm),
|
||||
});
|
||||
Effect.tryPromise({ catch: (error: unknown) => new LocalSqliteError({ cause: error }), try: () => execute(orm) });
|
||||
|
||||
yield* Effect.logDebug("--- DB ---");
|
||||
yield* Effect.tryPromise(() => client.deleteDatabaseFile());
|
||||
|
||||
return { client, orm, query };
|
||||
}),
|
||||
|
|
|
|||
14
src/services/logger.ts
Normal file
14
src/services/logger.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import type { LogLevel } from "effect/LogLevel";
|
||||
|
||||
import { Config, ConfigProvider, Effect, Layer, Logger, pipe } from "effect";
|
||||
|
||||
const EnvConfigProvider = Layer.setConfigProvider(ConfigProvider.fromMap(new Map([["LOG_LEVEL", "DEBUG"]])));
|
||||
|
||||
const LogLevelLive = pipe(
|
||||
Config.logLevel("LOG_LEVEL"),
|
||||
Effect.andThen((level: LogLevel) => Logger.minimumLogLevel(level)),
|
||||
Layer.unwrapEffect, // Convertis l'Effect en Layer
|
||||
Layer.provide(EnvConfigProvider),
|
||||
);
|
||||
|
||||
export const PrettyLogger = Layer.mergeAll(Logger.pretty, LogLevelLive);
|
||||
20
src/services/migrations.ts
Normal file
20
src/services/migrations.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import type { SQLocalDrizzle } from "sqlocal/drizzle";
|
||||
|
||||
import v0000 from "@/db/drizzle/0000_perfect_justice.sql?raw";
|
||||
import { Data, Effect } from "effect";
|
||||
|
||||
import { LocalSqlite } from "./db";
|
||||
|
||||
class MigrationsError extends Data.TaggedError("MigrationsError")<{ cause: unknown }> {}
|
||||
|
||||
const executeRawSql = (client: SQLocalDrizzle) => (sql: string) =>
|
||||
Effect.tryPromise({ catch: (error: unknown) => new MigrationsError({ cause: error }), try: () => client.sql(sql) });
|
||||
|
||||
export class Migrations extends Effect.Service<Migrations>()("Migrations", {
|
||||
dependencies: [LocalSqlite.Default],
|
||||
effect: Effect.gen(function*() {
|
||||
const db = yield* LocalSqlite;
|
||||
yield* Effect.logDebug("--- MIGRATIONS ---");
|
||||
return [yield* executeRawSql(db.client)(v0000)] as const;
|
||||
}),
|
||||
}) {}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { DiaryEntries } from "@/db/schemas";
|
||||
import { DiaryEntries, Users } from "@/db/schemas";
|
||||
import { singleResultOrFail } from "@/libs/utils/effects";
|
||||
import { desc } from "drizzle-orm";
|
||||
import { Data, Effect } from "effect";
|
||||
|
|
@ -12,11 +12,15 @@ export class ReadApi extends Effect.Service<ReadApi>()("ReadApi", {
|
|||
effect: Effect.gen(function*() {
|
||||
const { query } = yield* LocalSqlite;
|
||||
|
||||
yield* Effect.logDebug("--- READ-API ---");
|
||||
|
||||
return {
|
||||
getAllEntries: () => query(_ => _.select().from(DiaryEntries)),
|
||||
getLastAddedEntry: () =>
|
||||
query(_ => _.select().from(DiaryEntries).limit(1).orderBy(desc(DiaryEntries.dateCreated))).pipe(
|
||||
singleResultOrFail(() => new ReadApiError({ cause: "Aucune entrée n'a encore été ajoutée." })),
|
||||
),
|
||||
getUsers: () => query(_ => _.select().from(Users)),
|
||||
};
|
||||
}),
|
||||
}) {}
|
||||
|
|
|
|||
15
src/services/runtime-client.ts
Normal file
15
src/services/runtime-client.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import { Layer, ManagedRuntime } from "effect";
|
||||
|
||||
import { LocalSqlite } from "./db";
|
||||
import { PrettyLogger } from "./logger";
|
||||
import { Migrations } from "./migrations";
|
||||
import { ReadApi } from "./read-api";
|
||||
|
||||
const MainLayer = Layer.mergeAll(
|
||||
// WriteApi.Default,
|
||||
LocalSqlite.Default,
|
||||
Migrations.Default,
|
||||
ReadApi.Default,
|
||||
).pipe(Layer.provide(PrettyLogger));
|
||||
|
||||
export const RuntimeClient = ManagedRuntime.make(MainLayer);
|
||||
Loading…
Add table
Add a link
Reference in a new issue