feat: refactor account
This commit is contained in:
@@ -1,6 +0,0 @@
|
||||
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);
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
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>;
|
||||
@@ -1,10 +1,10 @@
|
||||
import type { AccountDocument } from "@spec/modules/account/account.ts";
|
||||
|
||||
import { config } from "~config";
|
||||
import { getDatabaseAccessor } from "~libraries/database/accessor.ts";
|
||||
|
||||
import { AccountInsert } from "./account/schema.ts";
|
||||
|
||||
export const db = getDatabaseAccessor<{
|
||||
accounts: AccountInsert;
|
||||
accounts: AccountDocument;
|
||||
}>(`${config.name}:read-store`);
|
||||
|
||||
export function takeOne<TDocument>(documents: TDocument[]): TDocument | undefined {
|
||||
|
||||
35
api/libraries/read-store/methods.ts
Normal file
35
api/libraries/read-store/methods.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { type Account, parseAccount } from "@spec/modules/account/account.ts";
|
||||
|
||||
import { db, takeOne } from "./database.ts";
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Accounts
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* Retrieve a single account by its primary identifier.
|
||||
*
|
||||
* @param id - Account identifier.
|
||||
*/
|
||||
export async function getAccountById(id: string): Promise<Account | undefined> {
|
||||
return db
|
||||
.collection("accounts")
|
||||
.aggregate([
|
||||
{
|
||||
$match: { id },
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "roles",
|
||||
localField: "roles",
|
||||
foreignField: "id",
|
||||
as: "roles",
|
||||
},
|
||||
},
|
||||
])
|
||||
.toArray()
|
||||
.then(parseAccount)
|
||||
.then(takeOne);
|
||||
}
|
||||
@@ -1,3 +1,2 @@
|
||||
export * from "./account/methods.ts";
|
||||
export * from "./account/schema.ts";
|
||||
export * from "./database.ts";
|
||||
export * from "./methods.ts";
|
||||
|
||||
Reference in New Issue
Block a user