57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
import { createPentagon } from "https://deno.land/x/pentagon@v0.1.5/mod.ts";
|
|
import { project_root_dir } from "../utils.ts";
|
|
import { z } from "https://deno.land/x/zod@v3.21.4/mod.ts";
|
|
|
|
const kv = await Deno.openKv(project_root_dir() + "local/kv");
|
|
|
|
export type User = z.infer<typeof user_model>;
|
|
export const user_model = z.object({
|
|
id: z.string().uuid().describe("primary"),
|
|
name: z.string(),
|
|
email: z.string(),
|
|
password: z.string(),
|
|
pfp_url: z.string(),
|
|
like_set: z.set(z.string().uuid()),
|
|
});
|
|
|
|
export type Post = z.infer<typeof post_model>;
|
|
export const post_model = z.object({
|
|
id: z.string().uuid().describe("primary"),
|
|
title: z.string(),
|
|
content: z.string(),
|
|
date: z.number(),
|
|
like_count: z.number(),
|
|
author_id: z.string().uuid(),
|
|
});
|
|
|
|
export type Comment = z.infer<typeof comment_model>;
|
|
export const comment_model = z.object({
|
|
id: z.string().uuid().describe("primary"),
|
|
date: z.number(),
|
|
post_id: z.string().uuid(),
|
|
author_id: z.string().uuid(),
|
|
});
|
|
|
|
export const db = createPentagon(kv, {
|
|
users: {
|
|
schema: user_model,
|
|
relations: {
|
|
posts: ["posts", [post_model], "id", "author_id"],
|
|
comments: ["comments", [comment_model], "id", "author_id"],
|
|
},
|
|
},
|
|
posts: {
|
|
schema: post_model,
|
|
relations: {
|
|
author: ["users", user_model, "author_id", "id"],
|
|
comments: ["comments", [comment_model], "id", "post_id"],
|
|
},
|
|
},
|
|
comments: {
|
|
schema: comment_model,
|
|
relations: {
|
|
author: ["users", user_model, "author_id", "id"],
|
|
post: ["post", post_model, "post_id", "id"],
|
|
},
|
|
},
|
|
});
|