This commit is contained in:
JOLIMAITRE Matthieu 2024-05-29 03:57:05 +02:00
commit fc701bec68
9 changed files with 366 additions and 0 deletions

62
examples/demo.ts Executable file
View file

@ -0,0 +1,62 @@
#!/usr/bin/env -S deno run -A --unstable-kv
import { EntryFor, Schema, Store } from "../mod.ts";
async function main() {
// define nodes and open store.
const nodes = new Schema({
user: {
"name": "string",
"posts": ["many", "post"],
},
post: {
"content": "string",
"upvotes": "number",
"author": ["one", "user"],
},
});
const store = await Store.open(nodes, "/tmp/feur.kv");
type User = EntryFor<typeof store, "user">;
{
// insert data.
const bob = await store.insert("user", {
name: "bob",
posts: store.empty_collection("post"),
});
const post_a = await store.insert("post", {
content: "Lorem",
upvotes: 5,
author: bob,
});
await (await bob.get("posts")).add(post_a);
const post_b = await store.insert("post", {
content: "Ipsum",
upvotes: 3,
author: bob,
});
await (await bob.get("posts")).add(post_b);
}
{
// prints
let bob = null as User | null;
for await (const user of store.all("user")) if (await user.get("name") === "bob") bob = user;
if (bob === null) throw new Error("Bob not found");
console.log({ name: await bob.get("name") });
const posts = await bob.get("posts");
for await (const post of posts) {
console.log({
author: await (await post.get("author")).get("name"),
content: await post.get("content"),
upvotes: await post.get("upvotes"),
});
}
}
}
if (import.meta.main) await main();