feat: initial boilerplate
This commit is contained in:
11
api/libraries/read-store/.tasks/bootstrap.ts
Normal file
11
api/libraries/read-store/.tasks/bootstrap.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { idIndex } from "~libraries/database/id.ts";
|
||||
import { register } from "~libraries/database/registrar.ts";
|
||||
|
||||
import { db } from "../database.ts";
|
||||
|
||||
await register(db.db, [
|
||||
{
|
||||
name: "accounts",
|
||||
indexes: [idIndex],
|
||||
},
|
||||
]);
|
||||
6
api/libraries/read-store/account/methods.ts
Normal file
6
api/libraries/read-store/account/methods.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { db, takeOne } from "../database.ts";
|
||||
import { type AccountSchema, fromAccountDriver } from "./schema.ts";
|
||||
|
||||
export async function getAccountById(id: string): Promise<AccountSchema | undefined> {
|
||||
return db.collection("accounts").find({ id }).toArray().then(fromAccountDriver).then(takeOne);
|
||||
}
|
||||
36
api/libraries/read-store/account/schema.ts
Normal file
36
api/libraries/read-store/account/schema.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { z } from "zod";
|
||||
|
||||
const account = z.object({
|
||||
id: z.uuid(),
|
||||
name: z.object({
|
||||
given: z.string(),
|
||||
family: z.string(),
|
||||
}),
|
||||
email: z.email(),
|
||||
});
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Parsers
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
const select = account;
|
||||
const insert = account;
|
||||
|
||||
export function toAccountDriver(documents: unknown): AccountInsert {
|
||||
return insert.parse(documents);
|
||||
}
|
||||
|
||||
export function fromAccountDriver(documents: unknown[]): AccountSchema[] {
|
||||
return documents.map((document) => select.parse(document));
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Types
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
export type AccountSchema = z.infer<typeof select>;
|
||||
export type AccountInsert = z.infer<typeof insert>;
|
||||
12
api/libraries/read-store/database.ts
Normal file
12
api/libraries/read-store/database.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { config } from "~config";
|
||||
import { getDatabaseAccessor } from "~libraries/database/accessor.ts";
|
||||
|
||||
import { AccountInsert } from "./account/schema.ts";
|
||||
|
||||
export const db = getDatabaseAccessor<{
|
||||
accounts: AccountInsert;
|
||||
}>(`${config.name}:read-store`);
|
||||
|
||||
export function takeOne<TDocument>(documents: TDocument[]): TDocument | undefined {
|
||||
return documents[0];
|
||||
}
|
||||
3
api/libraries/read-store/mod.ts
Normal file
3
api/libraries/read-store/mod.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from "./account/methods.ts";
|
||||
export * from "./account/schema.ts";
|
||||
export * from "./database.ts";
|
||||
Reference in New Issue
Block a user