forked from mb/debilus
init.
This commit is contained in:
commit
fc701bec68
9 changed files with 366 additions and 0 deletions
62
examples/demo.ts
Executable file
62
examples/demo.ts
Executable 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();
|
Loading…
Add table
Add a link
Reference in a new issue