This commit is contained in:
JOLIMAITRE Matthieu 2024-05-29 06:09:50 +02:00
parent 8b6a86f17a
commit 3aea126342
14 changed files with 205 additions and 67 deletions

View file

@ -1,45 +1,36 @@
import { z } from "https://deno.land/x/zod@v3.23.8/mod.ts";
import {
EntryFor,
Schema,
Store,
} from "https://git.barnulf.net/mb/debilus/raw/commit/fc701bec680dd73be29c72164f47ee87fac540c7/mod.ts";
import { project_root } from "./utils.ts";
class Store<D, Schema> {
kv;
export const db = await Store.open(
new Schema({
user: {
username: "string",
password: "string",
upvoted_posts: ["many", "post"],
upvoted_comments: ["many", "comment"],
posts: ["many", "post"],
},
post: {
title: "string",
content: "string",
upvotes: "number",
author: ["one", "user"],
comments: ["many", "comment"],
},
comment: {
content: "string",
upvotes: "number",
author: ["one", "user"],
post: ["one", "post"],
},
}),
`${project_root()}/local/db.kv`,
);
private constructor(kv: Deno.Kv) {
this.kv = kv;
}
}
class Schema<T extends TablesDescriptor<T>> {
descriptor;
constructor(descriptor: T) {
this.descriptor = descriptor;
}
}
type SchemaDescriptor<T extends TablesDescriptor<T>> = {
tables: T;
};
type TablesDescriptor<Self extends TablesDescriptor<Self>> = {
[name: string]: TableDescr<Self, typeof name, { [key: string]: z.ZodType }>;
};
type TableDescr<Tables extends TablesDescriptor<Tables>, Name extends keyof Tables, S extends z.ZodRawShape> = {
structure: z.ZodObject<S>;
relations: Relations<Tables, Name, S>;
};
type Relations<Tables extends TablesDescriptor<Tables>, Name extends keyof Tables, S extends z.ZodRawShape> = {
[name in Exclude<keyof Tables, Name>]?: [keyof S, keyof z.infer<Tables[name]["structure"]>];
};
const store = new Schema({
"user": {
structure: z.object({ feur_id: z.string() }),
relations: { "feur": ["_feur_id", "id"] },
},
"feur": {
structure: z.object({ id: z.string(), content: z.string() }),
relations: {},
},
});
export type User = EntryFor<typeof db, "user">;
export type Post = EntryFor<typeof db, "post">;
export type Comment = EntryFor<typeof db, "comment">;