This commit is contained in:
JOLIMAITRE Matthieu 2024-05-24 13:04:36 +02:00
parent 2737aadc7f
commit 8b6a86f17a
8 changed files with 95 additions and 10 deletions

45
lib/storage.ts Normal file
View file

@ -0,0 +1,45 @@
import { z } from "https://deno.land/x/zod@v3.23.8/mod.ts";
class Store<D, Schema> {
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: {},
},
});