init
This commit is contained in:
commit
98131c3b78
29 changed files with 2003 additions and 0 deletions
43
src/services/db.ts
Normal file
43
src/services/db.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import { drizzle } from "drizzle-orm/sqlite-proxy";
|
||||
import { Data, Effect } from "effect";
|
||||
import { SQLocalDrizzle } from "sqlocal/drizzle";
|
||||
|
||||
class LocalSqliteError extends Data.TaggedError("LocalSqliteError")<{ cause: unknown }> {
|
||||
}
|
||||
|
||||
export class LocalSqlite extends Effect.Service<LocalSqlite>()("LocalSqlite", {
|
||||
effect: Effect.gen(function*() {
|
||||
const client = yield* Effect.try({
|
||||
catch: (error: unknown) => new LocalSqliteError({ cause: error }),
|
||||
try: () =>
|
||||
new SQLocalDrizzle({
|
||||
databasePath: "database.sqlite3",
|
||||
onInit: sql => {
|
||||
return [
|
||||
sql`PRAGMA busy_timeout = 5000;`,
|
||||
sql`PRAGMA cache_size = 1000000000;`,
|
||||
sql`PRAGMA foreign_key_check;`,
|
||||
sql`PRAGMA foreign_keys = true;`,
|
||||
sql`PRAGMA integrity_check;`,
|
||||
sql`PRAGMA journal_mode = WAL;`,
|
||||
sql`PRAGMA page_size = 1024;`,
|
||||
sql`PRAGMA synchronous = NORMAL;`,
|
||||
sql`PRAGMA foreign_key_check;`,
|
||||
sql`PRAGMA temp_story = MEMORY;`,
|
||||
sql`VACUUM;`,
|
||||
];
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const orm = 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),
|
||||
});
|
||||
|
||||
return { client, orm, query };
|
||||
}),
|
||||
}) {}
|
||||
22
src/services/read-api.ts
Normal file
22
src/services/read-api.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import { DiaryEntries } from "@/db/schemas";
|
||||
import { singleResultOrFail } from "@/libs/utils/effects";
|
||||
import { desc } from "drizzle-orm";
|
||||
import { Data, Effect } from "effect";
|
||||
|
||||
import { LocalSqlite } from "./db";
|
||||
|
||||
class ReadApiError extends Data.TaggedError("ReadApiError")<{ cause: unknown }> {}
|
||||
|
||||
export class ReadApi extends Effect.Service<ReadApi>()("ReadApi", {
|
||||
dependencies: [LocalSqlite.Default],
|
||||
effect: Effect.gen(function*() {
|
||||
const { query } = yield* LocalSqlite;
|
||||
|
||||
return {
|
||||
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." })),
|
||||
),
|
||||
};
|
||||
}),
|
||||
}) {}
|
||||
Loading…
Add table
Add a link
Reference in a new issue