-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickstart.test.ts
More file actions
71 lines (61 loc) · 2.69 KB
/
Copy pathquickstart.test.ts
File metadata and controls
71 lines (61 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* The README "Quickstart" snippet, executed in CI so it can't rot.
*
* The code between the `#region quickstart` / `#endregion` markers is the single
* source of truth for the README block: `scripts/quickstart-snippet.mjs`
* extracts it and `--check` (run in `verify` + CI) fails if the committed README
* has drifted. Keep that region as clean, copy-pasteable user code (imports,
* schema, wiring, one write, one read). Test scaffolding stays OUTSIDE the region.
*
* Memory + core only: an adapter test that imported a sibling adapter would
* violate the `adapters-only-core` dependency-cruiser boundary (it applies to
* `test/` too), so the quickstart deliberately uses the in-memory adapter, which
* needs no server and is the "first query in under 10 minutes" path.
*/
// #region quickstart
import { createMemoryBackend } from "@baas/adapter-memory";
import type { DocumentId, StoreSchema } from "@baas/core";
// 1. Describe your backend surface: named reads (`queries`) and writes
// (`mutations`), each with its arg and result types. This is the contract;
// every adapter implements the same named operations.
interface Todo {
_id: DocumentId;
title: string;
}
interface TodoSchema extends StoreSchema {
queries: {
listTodos: { args: Record<string, never>; result: Todo[] };
};
mutations: {
addTodo: { args: { title: string }; result: DocumentId };
};
}
// 2. Wire the schema to a backend. The in-memory adapter needs no server, so it
// runs anywhere (tests, a REPL, a demo) with the same contract as Supabase
// or Convex. Each operation is a plain function over a tiny document context.
const backend = createMemoryBackend<TodoSchema>({
queries: {
listTodos: (ctx) => ctx.all<Todo>("todos"),
},
mutations: {
addTodo: (ctx, { title }) => ctx.insert("todos", { title }),
},
});
// 3. First write, then first query. Every call returns a `Result`, either
// `{ ok: true, data }` or `{ ok: false, error }`, so errors are values
// rather than thrown exceptions, uniformly across every backend.
async function quickstart() {
const added = await backend.store.mutate("addTodo", { title: "Ship baasdk" });
if (!added.ok) throw new Error(added.error.message);
const result = await backend.store.run("listTodos", {});
if (!result.ok) throw new Error(result.error.message);
return result.data; // [{ _id: "todos:1", title: "Ship baasdk" }]
}
// #endregion
import { expect, test } from "vitest";
test("quickstart: first write + first query returns the inserted todo", async () => {
const todos = await quickstart();
expect(todos).toHaveLength(1);
expect(todos[0]).toMatchObject({ title: "Ship baasdk" });
expect(todos[0]?._id).toBeTypeOf("string");
});