@blueshed/joined 0.0.0-stage → 0.1.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (213) hide show
  1. package/.claude/skills/joined/SKILL.md +17 -0
  2. package/.claude/skills/joined/reference.md +768 -0
  3. package/LICENSE +19 -0
  4. package/README.md +115 -2
  5. package/docs/design.md +148 -0
  6. package/docs/introduction.md +193 -0
  7. package/docs/learning.md +76 -0
  8. package/docs/stories.md +82 -0
  9. package/examples/app.css +27 -0
  10. package/examples/cafe/books.html +38 -0
  11. package/examples/cafe/books.json +13 -0
  12. package/examples/cafe/books.ts +64 -0
  13. package/examples/cafe/cafe.css +512 -0
  14. package/examples/cafe/front.html +115 -0
  15. package/examples/cafe/front.ts +7 -0
  16. package/examples/cafe/generated/books.elements.ts +52 -0
  17. package/examples/cafe/generated/books.queries.ts +11 -0
  18. package/examples/cafe/generated/books.rules.ts +42 -0
  19. package/examples/cafe/generated/kitchen.elements.ts +87 -0
  20. package/examples/cafe/generated/kitchen.queries.ts +47 -0
  21. package/examples/cafe/generated/kitchen.rules.ts +98 -0
  22. package/examples/cafe/generated/menu.elements.ts +61 -0
  23. package/examples/cafe/generated/menu.queries.ts +28 -0
  24. package/examples/cafe/generated/menu.rules.ts +65 -0
  25. package/examples/cafe/generated/order.elements.ts +87 -0
  26. package/examples/cafe/generated/order.queries.ts +47 -0
  27. package/examples/cafe/generated/order.rules.ts +95 -0
  28. package/examples/cafe/generated/staff.elements.ts +55 -0
  29. package/examples/cafe/generated/staff.queries.ts +27 -0
  30. package/examples/cafe/generated/staff.rules.ts +59 -0
  31. package/examples/cafe/generated/supplies.elements.ts +62 -0
  32. package/examples/cafe/generated/supplies.queries.ts +43 -0
  33. package/examples/cafe/generated/supplies.rules.ts +84 -0
  34. package/examples/cafe/generated/till.elements.ts +93 -0
  35. package/examples/cafe/generated/till.queries.ts +57 -0
  36. package/examples/cafe/generated/till.rules.ts +113 -0
  37. package/examples/cafe/index.html +70 -0
  38. package/examples/cafe/kitchen.css +107 -0
  39. package/examples/cafe/kitchen.html +39 -0
  40. package/examples/cafe/kitchen.ts +125 -0
  41. package/examples/cafe/menu.html +55 -0
  42. package/examples/cafe/menu.json +41 -0
  43. package/examples/cafe/menu.ts +84 -0
  44. package/examples/cafe/migrations/001-cafe.ts +11 -0
  45. package/examples/cafe/order.html +37 -0
  46. package/examples/cafe/order.ts +45 -0
  47. package/examples/cafe/rail-drag.ts +30 -0
  48. package/examples/cafe/routes.ts +32 -0
  49. package/examples/cafe/staff.json +20 -0
  50. package/examples/cafe/stories.jsonl +43 -0
  51. package/examples/cafe/supplies.html +77 -0
  52. package/examples/cafe/supplies.json +33 -0
  53. package/examples/cafe/supplies.ts +170 -0
  54. package/examples/cafe/till.json +95 -0
  55. package/examples/cafe/till.ts +122 -0
  56. package/examples/cafe/trust.ts +20 -0
  57. package/examples/cafe/views.ts +171 -0
  58. package/examples/docs.ts +28 -0
  59. package/examples/film.jpg +0 -0
  60. package/examples/hello/generated/elements.ts +45 -0
  61. package/examples/hello/generated/queries.ts +24 -0
  62. package/examples/hello/generated/rules.ts +45 -0
  63. package/examples/hello/hello.css +27 -0
  64. package/examples/hello/hello.json +1 -0
  65. package/examples/hello/index.html +32 -0
  66. package/examples/hello/migrations/001-hello.ts +6 -0
  67. package/examples/hello/page.ts +49 -0
  68. package/examples/hello/routes.ts +6 -0
  69. package/examples/hello/stories.jsonl +3 -0
  70. package/examples/index.html +146 -0
  71. package/examples/page.ts +11 -0
  72. package/examples/people/adding.ts +20 -0
  73. package/examples/people/generated/elements.ts +68 -0
  74. package/examples/people/generated/queries.ts +28 -0
  75. package/examples/people/generated/rules.ts +85 -0
  76. package/examples/people/links.ts +124 -0
  77. package/examples/people/migrations/001-people.ts +6 -0
  78. package/examples/people/migrations/002-add-people-member-and-guard-people.ts +7 -0
  79. package/examples/people/migrations/003-add-people-rank-and-guard-people.ts +7 -0
  80. package/examples/people/order.ts +61 -0
  81. package/examples/people/people.css +65 -0
  82. package/examples/people/people.json +26 -0
  83. package/examples/people/routes.ts +12 -0
  84. package/examples/people/stories.jsonl +20 -0
  85. package/examples/people/trust.ts +14 -0
  86. package/examples/people/view.ts +115 -0
  87. package/examples/routes.ts +11 -0
  88. package/examples/trip/answer-rows.ts +70 -0
  89. package/examples/trip/answers.ts +11 -0
  90. package/examples/trip/bar.ts +89 -0
  91. package/examples/trip/calendar-map.css +709 -0
  92. package/examples/trip/calendar-map.ts +249 -0
  93. package/examples/trip/drags.ts +143 -0
  94. package/examples/trip/drawer.ts +250 -0
  95. package/examples/trip/event-form.ts +199 -0
  96. package/examples/trip/generated/elements.ts +144 -0
  97. package/examples/trip/generated/queries.ts +46 -0
  98. package/examples/trip/generated/rules.ts +239 -0
  99. package/examples/trip/holding.ts +61 -0
  100. package/examples/trip/ics.ts +117 -0
  101. package/examples/trip/index.html +82 -0
  102. package/examples/trip/line-stops.ts +92 -0
  103. package/examples/trip/line-words.ts +79 -0
  104. package/examples/trip/line.ts +121 -0
  105. package/examples/trip/map.ts +234 -0
  106. package/examples/trip/mercator.ts +28 -0
  107. package/examples/trip/migrations/001-trip.ts +7 -0
  108. package/examples/trip/migrations/002-add-events-zone-and-guard-events.ts +7 -0
  109. package/examples/trip/migrations/003-guard-events-and-create-events_asked-and-more.ts +9 -0
  110. package/examples/trip/migrations/004-create-stays.ts +6 -0
  111. package/examples/trip/migrations/005-guard-events-and-create-events_arrives-and-more.ts +8 -0
  112. package/examples/trip/month.ts +87 -0
  113. package/examples/trip/other-weeks.ts +67 -0
  114. package/examples/trip/page.ts +121 -0
  115. package/examples/trip/peek.ts +71 -0
  116. package/examples/trip/people-chips.ts +74 -0
  117. package/examples/trip/place-search.ts +51 -0
  118. package/examples/trip/places.ts +34 -0
  119. package/examples/trip/routes.ts +27 -0
  120. package/examples/trip/shell.ts +39 -0
  121. package/examples/trip/stays.css +51 -0
  122. package/examples/trip/stays.ts +68 -0
  123. package/examples/trip/stories.jsonl +72 -0
  124. package/examples/trip/time.ts +46 -0
  125. package/examples/trip/told.ts +48 -0
  126. package/examples/trip/trip.json +69 -0
  127. package/examples/trip/trust.ts +51 -0
  128. package/examples/trip/week.ts +237 -0
  129. package/examples/trip/words.ts +4 -0
  130. package/examples/trip/writes.ts +91 -0
  131. package/examples/trip/zones.ts +16 -0
  132. package/examples/trips/card.ts +149 -0
  133. package/examples/trips/generated/elements.ts +60 -0
  134. package/examples/trips/generated/queries.ts +28 -0
  135. package/examples/trips/generated/rules.ts +62 -0
  136. package/examples/trips/index.html +47 -0
  137. package/examples/trips/map.ts +52 -0
  138. package/examples/trips/migrations/001-trips.ts +8 -0
  139. package/examples/trips/new-trip.ts +46 -0
  140. package/examples/trips/page.ts +9 -0
  141. package/examples/trips/routes.ts +7 -0
  142. package/examples/trips/stories.jsonl +12 -0
  143. package/examples/trips/trips.css +183 -0
  144. package/examples/trips/trips.json +7 -0
  145. package/examples/weeks/derive.ts +23 -0
  146. package/examples/weeks/generated/elements.ts +52 -0
  147. package/examples/weeks/generated/rules.ts +33 -0
  148. package/examples/weeks/routes.ts +4 -0
  149. package/examples/weeks/weeks.json +1 -0
  150. package/examples/welcome.css +299 -0
  151. package/package.json +55 -4
  152. package/src/attachment.ts +70 -0
  153. package/src/cli.ts +52 -0
  154. package/src/client.ts +98 -0
  155. package/src/compile.ts +2034 -0
  156. package/src/css.d.ts +5 -0
  157. package/src/definitions.ts +268 -0
  158. package/src/demo.css +29 -0
  159. package/src/demo.ts +42 -0
  160. package/src/draft.ts +69 -0
  161. package/src/engine.ts +288 -0
  162. package/src/file-value.ts +13 -0
  163. package/src/files.ts +187 -0
  164. package/src/follow.ts +56 -0
  165. package/src/hues.ts +3 -0
  166. package/src/icon-names.ts +77 -0
  167. package/src/icon.png +0 -0
  168. package/src/icons.ts +32 -0
  169. package/src/keep.ts +33 -0
  170. package/src/link.ts +31 -0
  171. package/src/mcp.ts +252 -0
  172. package/src/members/demo.ts +53 -0
  173. package/src/members/generated/elements.ts +68 -0
  174. package/src/members/generated/queries.ts +29 -0
  175. package/src/members/generated/rules.ts +65 -0
  176. package/src/members/group.ts +294 -0
  177. package/src/members/keys.ts +221 -0
  178. package/src/members/members.json +9 -0
  179. package/src/members/migrations/001-members.ts +6 -0
  180. package/src/members/migrations/002-guard-members.ts +11 -0
  181. package/src/members/migrations/003-add-members-act-and-guard-members.ts +7 -0
  182. package/src/members/panel.css +208 -0
  183. package/src/members/panel.ts +398 -0
  184. package/src/members/routes.ts +4 -0
  185. package/src/members/stories.jsonl +27 -0
  186. package/src/members/trust.ts +116 -0
  187. package/src/migrate.ts +597 -0
  188. package/src/migrations/001-joined.ts +13 -0
  189. package/src/migrations/002-group-keys.ts +16 -0
  190. package/src/migrations/003-change-inverse.ts +10 -0
  191. package/src/migrations/004-change-followed.ts +6 -0
  192. package/src/ops.ts +61 -0
  193. package/src/page.css +79 -0
  194. package/src/page.ts +413 -0
  195. package/src/parts.css +471 -0
  196. package/src/parts.ts +152 -0
  197. package/src/server.ts +862 -0
  198. package/src/settings.ts +94 -0
  199. package/src/starter.ts +89 -0
  200. package/src/store.ts +815 -0
  201. package/src/stories.ts +387 -0
  202. package/src/toast.ts +52 -0
  203. package/src/together.ts +167 -0
  204. package/src/tokens.css +85 -0
  205. package/src/tokens.json +457 -0
  206. package/src/upkeep.ts +86 -0
  207. package/src/visitors.ts +43 -0
  208. package/src/write.ts +567 -0
  209. package/starter/CLAUDE.md +37 -0
  210. package/starter/gitignore +3 -0
  211. package/starter/tests/hello-page.test.ts +14 -0
  212. package/starter/tests/hello.test.ts +8 -0
  213. package/starter/tsconfig.json +17 -0
@@ -0,0 +1,115 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
6
+ <title>joined: the cafe</title>
7
+ <link rel="stylesheet" href="joined/tokens.css" />
8
+ <link rel="stylesheet" href="joined/page.css" />
9
+ <link rel="stylesheet" href="../app.css" />
10
+ <link rel="stylesheet" href="./cafe.css" />
11
+ </head>
12
+ <body data-flavour="bondi">
13
+ <a class="brand" href="/"><img src="joined/icon.png" alt="" width="28" height="28" />joined</a>
14
+ <header class="band">
15
+ <h1>The cafe</h1>
16
+ <p class="lead">
17
+ One store, and a page for each person who works in the cafe. An order placed at the till is in the kitchen, and
18
+ on its customer's phone, the moment it's placed.
19
+ </p>
20
+ <nav class="bar">
21
+ <members-panel></members-panel>
22
+ <a href="/cafe" aria-current="page">The cafe</a> <a href="/cafe/till" data-holds="till">Till</a>
23
+ <a href="/cafe/menu" data-holds="menu">Menu</a> <a href="/cafe/kitchen" data-holds="kitchen">Kitchen</a>
24
+ <a href="/cafe/supplies" data-holds="supplies">Supplies</a> <a href="/cafe/books" data-holds="books">Books</a>
25
+ </nav>
26
+ </header>
27
+ <!-- words alone, no document: not a <main>, which a page signed out hides, so a stranger reads it too -->
28
+ <article class="cafe front">
29
+ <section class="card try" aria-label="Try it">
30
+ <h2>Try it</h2>
31
+ <p>
32
+ Open the till and the kitchen side by side, or the kitchen on your phone. Place an order at the till: its
33
+ ticket is in the kitchen at once, and what's left of each item falls on every till. Each page is its people's:
34
+ Peter, the manager, has both; running the demo on this machine, be Nora (the till) or Dev (the kitchen) from
35
+ the home screen, and Peter again. Not signed in, a page says how.
36
+ </p>
37
+ <p class="go">
38
+ <a class="act ink" href="/cafe/till" target="till" data-icon="on">Open the till</a>
39
+ <a class="act" href="/cafe/kitchen" target="kitchen" data-icon="on">Open the kitchen</a>
40
+ </p>
41
+ </section>
42
+ <section class="card try" aria-label="Try a delivery">
43
+ <h2>Try a delivery</h2>
44
+ <p>
45
+ The cafe buys its croissants from Nick, the roaster: one supply, shared by the two of them and no one else.
46
+ Open the cafe's supplies and the till. Then be Nick beside them: running the demo on this machine, open
47
+ 127.0.0.1:3380 in a second window (it keeps a sign-in of its own) and choose Nick on its home screen;
48
+ elsewhere, open his link in a private window. As Nick, set the price, then say Delivered: the cafe's supply
49
+ says so at once, the till has twenty more croissants, and both books have it, bought in the cafe's and sold in
50
+ Nick's.
51
+ </p>
52
+ <p class="go">
53
+ <a class="act ink" href="/cafe/supplies" target="supplies" data-icon="on">Open supplies</a>
54
+ <a class="act" href="/cafe/till" target="till" data-icon="on">Open the till</a>
55
+ <a class="act" href="/cafe/books" target="books" data-icon="on">Open the books</a>
56
+ </p>
57
+ </section>
58
+ <section class="pages" aria-label="Whose page is whose">
59
+ <a class="page" href="/cafe/till">
60
+ <b>The till</b>
61
+ <span>Whoever's at the till: places an order, cancels one not yet started, hands over what's ready.</span>
62
+ <small>The till's document: the open orders, and the last eight finished.</small>
63
+ </a>
64
+ <a class="page" href="/cafe/kitchen">
65
+ <b>The kitchen</b>
66
+ <span>The cooks: a ticket for each order, started, then ready, by a button or a drag.</span>
67
+ <small>The kitchen's document: the orders placed, being made or ready.</small>
68
+ </a>
69
+ <a class="page" href="/cafe/order/q3Zt9xKcW1aB">
70
+ <b>A customer's order</b>
71
+ <span
72
+ >Its customer, by the link the till gives them, signed in to nothing: where it is, and what's in it.</span
73
+ >
74
+ <small>One order, by its code: Ann's, here.</small>
75
+ </a>
76
+ <a class="page" href="/cafe/menu">
77
+ <b>The menu</b>
78
+ <span>The manager: each item's name and price, changed in a drawer.</span>
79
+ <small>The menu's document: its items, and what's left of each.</small>
80
+ </a>
81
+ <a class="page" href="/cafe/supplies">
82
+ <b>Supplies</b>
83
+ <span>A buyer and a supplier, two businesses: each changes only their own part of what they share.</span>
84
+ <small>The supplies each member buys or sells: delivered, the till has more.</small>
85
+ </a>
86
+ <a class="page" href="/cafe/books">
87
+ <b>Books</b>
88
+ <span>Each member: what they've bought and sold. No one writes it.</span>
89
+ <small>Worked out from the supplies as each is delivered, each member's own.</small>
90
+ </a>
91
+ </section>
92
+ <section class="card how" aria-label="What it shows">
93
+ <h2>What it shows</h2>
94
+ <ul>
95
+ <li>Several documents over one store, each holding only the rows its people need.</li>
96
+ <li>
97
+ Roles from data: the staff document's posts say who's manager, till or kitchen, and each page offers only
98
+ their moves. The staff document has no page of its own: it's there for the rules, changed by a write (an
99
+ agent's, say), not a screen.
100
+ </li>
101
+ <li>
102
+ The store keeps the rules: stock taken as an order is placed, a price kept as it's sold, an order's total, a
103
+ status that moves only as its stories do.
104
+ </li>
105
+ <li>
106
+ Between businesses: a supply shared by its buyer and supplier, each changing only their own part; delivered,
107
+ it changes another document (the menu's stock); and books no one writes, worked out from it for each side.
108
+ </li>
109
+ <li>Three parts to copy: the kitchen's display, the menu's drawer, and the rail's drag.</li>
110
+ </ul>
111
+ </section>
112
+ </article>
113
+ <script type="module" src="./front.ts"></script>
114
+ </body>
115
+ </html>
@@ -0,0 +1,7 @@
1
+ // The cafe's front page: what the cafe is, whose page is whose, and how to try it; the group's panel, and its icons.
2
+
3
+ import "joined/panel";
4
+ import "joined/toast";
5
+ import { iconify } from "joined/icons";
6
+
7
+ iconify();
@@ -0,0 +1,52 @@
1
+ // Written by src/compile.ts from examples/cafe/stories.jsonl: change the stories, not this.
2
+
3
+ // Each collection is an element, defined here: on a page, it holds the document, and its items
4
+ // are put inside it. Each item is an element, defined by the page: as it is, or by a view that
5
+ // extends it. A field is an attribute (a list of ids as IDREFS: "1 3"), and an item's key the
6
+ // document's id for it. How an item looks is the app's: its view, or a display holding the
7
+ // collection draws it. Ops go up as an "ops" event, for the page to send to books, with why they
8
+ // were made if it's said; only the ops the stories allow can be sent. Each returns its write's answer.
9
+
10
+ import { hold, letGo, type Answer } from "joined/page";
11
+
12
+ // the document an element is of: its collection's doc attribute (an instance of a kind, shelf:1), or books
13
+ const docOf = (el: Element) => el.closest("[doc]")?.getAttribute("doc") ?? "books";
14
+ // an element's ops go up as an "ops" event; what it returns is the write's answer, done or refused
15
+ const send = (from: Element, ops: object[], why?: string) => new Promise<Answer>((answer) => from.dispatchEvent(new CustomEvent("ops", { bubbles: true, composed: true, detail: { doc: docOf(from), ops, why, answer } })));
16
+
17
+ /** An entry's fields, as the stories give them. */
18
+ export type JEntryData = { "title": string; "buyer": number; "supplier": number; "side": string; "total": number };
19
+ /** An add of an entry, as it's sent: its fields as the stories write them. */
20
+ export type JEntryValue = { "title": string; "buyer": number; "supplier": number; "side": string; "total": number };
21
+
22
+ /** <j-entry key>: title (string), buyer (integer), supplier (integer), side (string), total (decimal), each an attribute. */
23
+ export class JEntry extends HTMLElement {
24
+ static observedAttributes = ["title","buyer","supplier","side","total","may"];
25
+ #data?: JEntryData;
26
+ /** Its id in the document. */
27
+ get key() { return Number(this.getAttribute("key")); }
28
+ get path() { return `/entries/${this.key}`; }
29
+ /** Its fields, read from its attributes once and again when one changes: frozen, since they're shared. */
30
+ get data(): JEntryData {
31
+ return (this.#data ??= Object.freeze({ "title": this.getAttribute("title") ?? "", "buyer": Number(this.getAttribute("buyer")), "supplier": Number(this.getAttribute("supplier")), "side": this.getAttribute("side") ?? "", "total": Number(this.getAttribute("total")) }) as JEntryData);
32
+ }
33
+ /** How a view shows it, drawn again whenever an attribute changes (what its person may do, its may, too). */
34
+ draw?(): void;
35
+ attributeChangedCallback() { this.#data = undefined; this.draw?.(); }
36
+ }
37
+
38
+ /** <j-entries>: the document's entries, each a <j-entry> inside it. */
39
+ export class JEntries extends HTMLElement {
40
+ connectedCallback() { hold(this, docOf(this), "entries", "j-entry", {"title":"string","buyer":"integer","supplier":"integer","side":"string","total":"decimal"}); }
41
+ disconnectedCallback() { letGo(this); }
42
+ /** Its entries, as the document has them. */
43
+ get items() { return [...this.children].filter((el): el is JEntry => el.localName === "j-entry"); }
44
+ }
45
+ customElements.define("j-entries", JEntries);
46
+
47
+ /** An op on books the stories allow, in any of its collections. */
48
+ export type BooksOp = never;
49
+
50
+ /** One write to books across its collections (a move with its legs): sent from an element of it, checked
51
+ * whole by its rules, and made whole or not at all. */
52
+ export function write(from: Element, ops: BooksOp[], why?: string) { return send(from, ops, why); }
@@ -0,0 +1,11 @@
1
+ // Written by src/compile.ts from examples/cafe/stories.jsonl: change the stories, not this.
2
+
3
+ /** books, as a member has it, one statement over the app's tables (SQLite): its parameters $doc and $member. */
4
+ export const query = `SELECT json_object('version', COALESCE((SELECT version FROM documents WHERE name = $doc), 0),
5
+ 'entries', json((SELECT json_group_array(json_object('id', o.id, 'title', o."title", 'buyer', o."buyer", 'supplier', o."supplier", 'side', CASE WHEN o."buyer" = $member THEN 'bought' WHEN o."supplier" = $member THEN 'sold' END, 'total', o."qty" * o."price") ORDER BY o.id)
6
+ FROM "supplies" o WHERE o."delivered" = 1 AND (o."buyer" = $member OR o."supplier" = $member)))) AS doc`;
7
+
8
+ /** Its query, as Postgres says it. */
9
+ export const postgres = {
10
+ "query": "SELECT json_build_object('version', COALESCE((SELECT version FROM documents WHERE name = $doc), 0),\n 'entries', (SELECT COALESCE(json_agg(json_build_object('id', o.id, 'title', o.\"title\", 'buyer', o.\"buyer\", 'supplier', o.\"supplier\", 'side', CASE WHEN o.\"buyer\" = $member THEN 'bought' WHEN o.\"supplier\" = $member THEN 'sold' END, 'total', o.\"qty\" * o.\"price\") ORDER BY o.id), '[]')\n FROM \"supplies\" o WHERE o.\"delivered\" = 1 AND (o.\"buyer\" = $member OR o.\"supplier\" = $member))) AS doc"
11
+ };
@@ -0,0 +1,42 @@
1
+ // Written by src/compile.ts from examples/cafe/stories.jsonl: change the stories, not this.
2
+
3
+ export type Op = { op: "add" | "replace" | "remove"; path: string; value?: any };
4
+
5
+ /** The document, and each collection: its element, the element its items are, their fields, those an add needs, and
6
+ * what each link is to. */
7
+ export const doc = "books";
8
+ export const collections = {
9
+ "entries": {
10
+ "place": "j-entries",
11
+ "tag": "j-entry",
12
+ "fields": {
13
+ "title": "string",
14
+ "buyer": "integer",
15
+ "supplier": "integer",
16
+ "side": "string",
17
+ "total": "decimal"
18
+ },
19
+ "needs": [
20
+ "title",
21
+ "buyer",
22
+ "supplier",
23
+ "side",
24
+ "total"
25
+ ],
26
+ "links": {}
27
+ }
28
+ } as const;
29
+ /** Whether every story is anonymous, none told as a member: where every app's is, the project has no members. */
30
+ export const anonymous = false;
31
+ /** What kind of document books is, by the flows it uses: static, changed only with a release (a page reads it, and
32
+ * never subscribes); read-only, live, but no page writes it (derived); or shared, its members' to write. */
33
+ export const type = "read-only";
34
+ /** The tables its query reads: a change to any of them moves its version, and is told to each page holding it, as
35
+ * its member has it. */
36
+ export const reads = ["supplies"];
37
+ /** It's read as each member has it: a parameter of its query. */
38
+ export const member = true;
39
+ /** The parts of books a page may hold alone, by its query's parameters (its definitions' window): null, none. */
40
+ export const window = null;
41
+ /** The document as one statement over its tables (generated/books.queries.ts). */
42
+ export { query, postgres } from "./books.queries.ts";
@@ -0,0 +1,87 @@
1
+ // Written by src/compile.ts from examples/cafe/stories.jsonl: change the stories, not this.
2
+
3
+ // Each collection is an element, defined here: on a page, it holds the document, and its items
4
+ // are put inside it. Each item is an element, defined by the page: as it is, or by a view that
5
+ // extends it. A field is an attribute (a list of ids as IDREFS: "1 3"), and an item's key the
6
+ // document's id for it. How an item looks is the app's: its view, or a display holding the
7
+ // collection draws it. Ops go up as an "ops" event, for the page to send to kitchen, with why they
8
+ // were made if it's said; only the ops the stories allow can be sent. Each returns its write's answer.
9
+
10
+ import { hold, letGo, type Answer } from "joined/page";
11
+
12
+ // the document an element is of: its collection's doc attribute (an instance of a kind, shelf:1), or kitchen
13
+ const docOf = (el: Element) => el.closest("[doc]")?.getAttribute("doc") ?? "kitchen";
14
+ // an element's ops go up as an "ops" event; what it returns is the write's answer, done or refused
15
+ const send = (from: Element, ops: object[], why?: string) => new Promise<Answer>((answer) => from.dispatchEvent(new CustomEvent("ops", { bubbles: true, composed: true, detail: { doc: docOf(from), ops, why, answer } })));
16
+
17
+ /** An order's fields, as the stories give them. */
18
+ export type JOrderData = { "customer": string; "status"?: string; "placed"?: string; "code"?: string; "total"?: number };
19
+ /** An add of an order, as it's sent: its fields as the stories write them. */
20
+ export type JOrderValue = { "customer": string; "status"?: string; "placed"?: string; "code"?: string; "total"?: number };
21
+
22
+ /** <j-order key>: customer (string), status (string), placed (instant), code (string), total (integer), each an attribute. */
23
+ export class JOrder extends HTMLElement {
24
+ static observedAttributes = ["customer","status","placed","code","total","may"];
25
+ #data?: JOrderData;
26
+ /** Its id in the document. */
27
+ get key() { return Number(this.getAttribute("key")); }
28
+ get path() { return `/orders/${this.key}`; }
29
+ /** Its fields, read from its attributes once and again when one changes: frozen, since they're shared. */
30
+ get data(): JOrderData {
31
+ return (this.#data ??= Object.freeze({ "customer": this.getAttribute("customer") ?? "", "status": this.getAttribute("status") ?? undefined, "placed": this.getAttribute("placed") ?? undefined, "code": this.getAttribute("code") ?? undefined, "total": this.hasAttribute("total") ? Number(this.getAttribute("total")) : undefined }) as JOrderData);
32
+ }
33
+ /** How a view shows it, drawn again whenever an attribute changes (what its person may do, its may, too). */
34
+ draw?(): void;
35
+ attributeChangedCallback() { this.#data = undefined; this.draw?.(); }
36
+ /** Replace some of its fields (status): one write for all of them. */
37
+ replace(fields: Partial<Pick<JOrderData, "status">>, why?: string) {
38
+ return send(this, Object.entries(fields).filter(([, value]) => value !== undefined).map(([field, value]) => ({ op: "replace", path: `${this.path}/${field.replace(".", "/")}`, value })), why);
39
+ }
40
+ }
41
+
42
+ /** <j-orders>: the document's orders, each a <j-order> inside it. */
43
+ export class JOrders extends HTMLElement {
44
+ connectedCallback() { hold(this, docOf(this), "orders", "j-order", {"customer":"string","status":"string","placed":"instant","code":"string","total":"integer"}); }
45
+ disconnectedCallback() { letGo(this); }
46
+ /** Its orders, as the document has them. */
47
+ get items() { return [...this.children].filter((el): el is JOrder => el.localName === "j-order"); }
48
+ }
49
+ customElements.define("j-orders", JOrders);
50
+
51
+ /** A line's fields, as the stories give them. */
52
+ export type JLineData = { "order": number; "item": number; "qty": number; "name"?: string; "price"?: number };
53
+ /** An add of a line, as it's sent: its fields as the stories write them. */
54
+ export type JLineValue = { "order": number | `$${number}`; "item": number | `$${number}`; "qty": number; "name"?: string; "price"?: number };
55
+
56
+ /** <j-line key>: order (an id in orders), item (an id in items), qty (integer), name (string), price (integer), each an attribute. */
57
+ export class JLine extends HTMLElement {
58
+ static observedAttributes = ["order","item","qty","name","price","may"];
59
+ #data?: JLineData;
60
+ /** Its id in the document. */
61
+ get key() { return Number(this.getAttribute("key")); }
62
+ get path() { return `/lines/${this.key}`; }
63
+ /** Its fields, read from its attributes once and again when one changes: frozen, since they're shared. */
64
+ get data(): JLineData {
65
+ return (this.#data ??= Object.freeze({ "order": Number(this.getAttribute("order")), "item": Number(this.getAttribute("item")), "qty": Number(this.getAttribute("qty")), "name": this.getAttribute("name") ?? undefined, "price": this.hasAttribute("price") ? Number(this.getAttribute("price")) : undefined }) as JLineData);
66
+ }
67
+ /** How a view shows it, drawn again whenever an attribute changes (what its person may do, its may, too). */
68
+ draw?(): void;
69
+ attributeChangedCallback() { this.#data = undefined; this.draw?.(); }
70
+ }
71
+
72
+ /** <j-lines>: the document's lines, each a <j-line> inside it. */
73
+ export class JLines extends HTMLElement {
74
+ connectedCallback() { hold(this, docOf(this), "lines", "j-line", {"order":"integer","item":"integer","qty":"integer","name":"string","price":"integer"}); }
75
+ disconnectedCallback() { letGo(this); }
76
+ /** Its lines, as the document has them. */
77
+ get items() { return [...this.children].filter((el): el is JLine => el.localName === "j-line"); }
78
+ }
79
+ customElements.define("j-lines", JLines);
80
+
81
+ /** An op on kitchen the stories allow, in any of its collections. */
82
+ export type KitchenOp =
83
+ | { op: "replace"; path: `/orders/${number}/status`; value: string };
84
+
85
+ /** One write to kitchen across its collections (a move with its legs): sent from an element of it, checked
86
+ * whole by its rules, and made whole or not at all. */
87
+ export function write(from: Element, ops: KitchenOp[], why?: string) { return send(from, ops, why); }
@@ -0,0 +1,47 @@
1
+ // Written by src/compile.ts from examples/cafe/stories.jsonl: change the stories, not this.
2
+
3
+ /** Each collection's item, as one row of its table (`t`) is it: what the query gathers, and what the store keeps of a
4
+ * row a write changes, before and after, to tell each document holding it. */
5
+ export const items: Record<string, string> = {
6
+ "orders": "json_patch('{}', json_object('id', t.id,\n 'customer', t.\"customer\",\n 'status', t.\"status\",\n 'placed', t.\"placed\",\n 'code', t.\"code\",\n 'total', t.\"total\"))",
7
+ "lines": "json_patch('{}', json_object('id', t.id,\n 'order', t.\"order\",\n 'item', t.\"item\",\n 'qty', t.\"qty\",\n 'name', t.\"name\",\n 'price', t.\"price\"))"
8
+ };
9
+
10
+ /** kitchen, whole, as one statement over its tables (SQLite): its parameters $doc, and its window's, $code. */
11
+ export const query = `SELECT json_object('version', COALESCE((SELECT version FROM documents WHERE name = $doc), 0),
12
+ 'orders', json((SELECT json_group_array(json_patch('{}', json_object('id', t.id,
13
+ 'customer', t."customer",
14
+ 'status', t."status",
15
+ 'placed', t."placed",
16
+ 'code', t."code",
17
+ 'total', t."total")) ORDER BY t.id)
18
+ FROM "orders" t WHERE ($code IS NULL OR t."code" = $code) AND ((t."status" IN ('placed', 'being made', 'ready'))))),
19
+ 'lines', json((SELECT json_group_array(json_patch('{}', json_object('id', t.id,
20
+ 'order', t."order",
21
+ 'item', t."item",
22
+ 'qty', t."qty",
23
+ 'name', t."name",
24
+ 'price', t."price")) ORDER BY t.id)
25
+ FROM "lines" t WHERE (t."order" IN (SELECT p.id FROM "orders" p WHERE ($code IS NULL OR p."code" = $code) AND ((p."status" IN ('placed', 'being made', 'ready')))))))) AS doc`;
26
+
27
+ /** Its rules over values (definitions' takes, gives and totals), as triggers the store makes in its database: run in
28
+ * each write's transaction, and told as any change is. */
29
+ export const triggers: string[] = [
30
+ "CREATE TEMP TRIGGER IF NOT EXISTS \"joined_totals_orders_total_add\" AFTER INSERT ON main.\"lines\" BEGIN UPDATE \"orders\" SET \"total\" = (SELECT COALESCE(SUM(i.\"qty\" * i.\"price\"), 0) FROM \"lines\" i WHERE i.\"order\" = NEW.\"order\") WHERE id = NEW.\"order\"; END",
31
+ "CREATE TEMP TRIGGER IF NOT EXISTS \"joined_totals_orders_total_remove\" AFTER DELETE ON main.\"lines\" BEGIN UPDATE \"orders\" SET \"total\" = (SELECT COALESCE(SUM(i.\"qty\" * i.\"price\"), 0) FROM \"lines\" i WHERE i.\"order\" = OLD.\"order\") WHERE id = OLD.\"order\"; END",
32
+ "CREATE TEMP TRIGGER IF NOT EXISTS \"joined_totals_orders_total_replace\" AFTER UPDATE ON main.\"lines\" BEGIN UPDATE \"orders\" SET \"total\" = (SELECT COALESCE(SUM(i.\"qty\" * i.\"price\"), 0) FROM \"lines\" i WHERE i.\"order\" = OLD.\"order\") WHERE id = OLD.\"order\"; UPDATE \"orders\" SET \"total\" = (SELECT COALESCE(SUM(i.\"qty\" * i.\"price\"), 0) FROM \"lines\" i WHERE i.\"order\" = NEW.\"order\") WHERE id = NEW.\"order\"; END"
33
+ ];
34
+
35
+ /** Its items, its query and its triggers, as Postgres says them. */
36
+ export const postgres = {
37
+ "items": {
38
+ "orders": "json_strip_nulls(json_build_object('id', t.id,\n 'customer', t.\"customer\",\n 'status', t.\"status\",\n 'placed', t.\"placed\",\n 'code', t.\"code\",\n 'total', t.\"total\"))",
39
+ "lines": "json_strip_nulls(json_build_object('id', t.id,\n 'order', t.\"order\",\n 'item', t.\"item\",\n 'qty', t.\"qty\",\n 'name', t.\"name\",\n 'price', t.\"price\"))"
40
+ },
41
+ "query": "SELECT json_build_object('version', COALESCE((SELECT version FROM documents WHERE name = $doc), 0),\n 'orders', (SELECT COALESCE(json_agg(json_strip_nulls(json_build_object('id', t.id,\n 'customer', t.\"customer\",\n 'status', t.\"status\",\n 'placed', t.\"placed\",\n 'code', t.\"code\",\n 'total', t.\"total\")) ORDER BY t.id), '[]')\n FROM \"orders\" t WHERE ($code IS NULL OR t.\"code\" = $code) AND ((t.\"status\" IN ('placed', 'being made', 'ready')))),\n 'lines', (SELECT COALESCE(json_agg(json_strip_nulls(json_build_object('id', t.id,\n 'order', t.\"order\",\n 'item', t.\"item\",\n 'qty', t.\"qty\",\n 'name', t.\"name\",\n 'price', t.\"price\")) ORDER BY t.id), '[]')\n FROM \"lines\" t WHERE (t.\"order\" IN (SELECT p.id FROM \"orders\" p WHERE ($code IS NULL OR p.\"code\" = $code) AND ((p.\"status\" IN ('placed', 'being made', 'ready'))))))) AS doc",
42
+ "triggers": [
43
+ "CREATE OR REPLACE FUNCTION \"joined_totals_orders_total_add_fn\"() RETURNS trigger LANGUAGE plpgsql AS $joined$ BEGIN UPDATE \"orders\" SET \"total\" = (SELECT COALESCE(SUM(i.\"qty\" * i.\"price\"), 0) FROM \"lines\" i WHERE i.\"order\" = NEW.\"order\") WHERE id = NEW.\"order\"; IF TG_OP = 'DELETE' THEN RETURN OLD; END IF; RETURN NEW; END $joined$; CREATE OR REPLACE TRIGGER \"joined_totals_orders_total_add\" AFTER INSERT ON \"lines\" FOR EACH ROW EXECUTE FUNCTION \"joined_totals_orders_total_add_fn\"()",
44
+ "CREATE OR REPLACE FUNCTION \"joined_totals_orders_total_remove_fn\"() RETURNS trigger LANGUAGE plpgsql AS $joined$ BEGIN UPDATE \"orders\" SET \"total\" = (SELECT COALESCE(SUM(i.\"qty\" * i.\"price\"), 0) FROM \"lines\" i WHERE i.\"order\" = OLD.\"order\") WHERE id = OLD.\"order\"; IF TG_OP = 'DELETE' THEN RETURN OLD; END IF; RETURN NEW; END $joined$; CREATE OR REPLACE TRIGGER \"joined_totals_orders_total_remove\" AFTER DELETE ON \"lines\" FOR EACH ROW EXECUTE FUNCTION \"joined_totals_orders_total_remove_fn\"()",
45
+ "CREATE OR REPLACE FUNCTION \"joined_totals_orders_total_replace_fn\"() RETURNS trigger LANGUAGE plpgsql AS $joined$ BEGIN UPDATE \"orders\" SET \"total\" = (SELECT COALESCE(SUM(i.\"qty\" * i.\"price\"), 0) FROM \"lines\" i WHERE i.\"order\" = OLD.\"order\") WHERE id = OLD.\"order\"; UPDATE \"orders\" SET \"total\" = (SELECT COALESCE(SUM(i.\"qty\" * i.\"price\"), 0) FROM \"lines\" i WHERE i.\"order\" = NEW.\"order\") WHERE id = NEW.\"order\"; IF TG_OP = 'DELETE' THEN RETURN OLD; END IF; RETURN NEW; END $joined$; CREATE OR REPLACE TRIGGER \"joined_totals_orders_total_replace\" AFTER UPDATE ON \"lines\" FOR EACH ROW EXECUTE FUNCTION \"joined_totals_orders_total_replace_fn\"()"
46
+ ]
47
+ };
@@ -0,0 +1,98 @@
1
+ // Written by src/compile.ts from examples/cafe/stories.jsonl: change the stories, not this.
2
+
3
+ export type Op = { op: "add" | "replace" | "remove"; path: string; value?: any };
4
+
5
+ /** The document, and each collection: its element, the element its items are, their fields, those an add needs, and
6
+ * what each link is to. */
7
+ export const doc = "kitchen";
8
+ export const collections = {
9
+ "orders": {
10
+ "place": "j-orders",
11
+ "tag": "j-order",
12
+ "fields": {
13
+ "customer": "string",
14
+ "status": "string",
15
+ "placed": "instant",
16
+ "code": "string",
17
+ "total": "integer"
18
+ },
19
+ "needs": [
20
+ "customer"
21
+ ],
22
+ "links": {},
23
+ "fills": {
24
+ "status": "placed",
25
+ "placed": "now",
26
+ "code": "random",
27
+ "total": 0
28
+ }
29
+ },
30
+ "lines": {
31
+ "place": "j-lines",
32
+ "tag": "j-line",
33
+ "fields": {
34
+ "order": "integer",
35
+ "item": "integer",
36
+ "qty": "integer",
37
+ "name": "string",
38
+ "price": "integer"
39
+ },
40
+ "needs": [
41
+ "order",
42
+ "item",
43
+ "qty"
44
+ ],
45
+ "links": {
46
+ "order": "orders",
47
+ "item": "items"
48
+ }
49
+ }
50
+ } as const;
51
+ /** Whether every story is anonymous, none told as a member: where every app's is, the project has no members. */
52
+ export const anonymous = false;
53
+ /** What kind of document kitchen is, by the flows it uses: static, changed only with a release (a page reads it, and
54
+ * never subscribes); read-only, live, but no page writes it (derived); or shared, its members' to write. */
55
+ export const type = "shared";
56
+ /** It holds only some of its tables' rows (its definitions' where): a change to any of them moves its version,
57
+ * and each page holding it is told what changed of it. */
58
+ export const view = true;
59
+ export const reads = ["lines","orders"];
60
+ /** Who may hold it at all (its where's holds), each a role; null, anyone. */
61
+ export const mayHold = ["kitchen","manager"];
62
+ /** The parts of kitchen a page may hold alone, by its query's parameters (its definitions' window): null, none. */
63
+ export const window = {"orders":{"code":"code"}};
64
+ /** Each field whose value moves only as the stories move it, each step by its roles (its definitions' moves). */
65
+ export const moves = {"orders":"status"};
66
+ /** The field holding an item's own code: a page holding kitchen by it is the item's guest (its definitions' guests). */
67
+ export const guests = {"orders":"code"};
68
+ /** The document as one statement over its tables (generated/kitchen.queries.ts). */
69
+ export { query, postgres, items, triggers } from "./kitchen.queries.ts";
70
+
71
+ /** kitchen is one document. */
72
+ export const kind = null;
73
+
74
+ /** Each grant, read off the stories: the roles whose stories do it (a member: anyone). An op no story
75
+ * does isn't one. */
76
+ export const grants: Record<string, string[]> = {
77
+ "replace orders.status:being made>ready": [
78
+ "kitchen"
79
+ ],
80
+ "replace orders.status:placed>being made": [
81
+ "kitchen"
82
+ ]
83
+ };
84
+
85
+ /** Who someone is to kitchen (and to an item of it, `at`), beyond a member: each role whose who-path finds them. */
86
+ export function rolesOf(m: number, read: (doc: string) => any, doc: string, at?: any): string[] {
87
+ const item: any = at;
88
+ return [
89
+ (read("staff")["posts"] ?? []).some((i: any) => String(i["role"]) === "manager" && (i["members"] === m || (Array.isArray(i["members"]) && i["members"].includes(m)))) ? "manager" : "",
90
+ (read("staff")["posts"] ?? []).some((i: any) => String(i["role"]) === "till" && (i["members"] === m || (Array.isArray(i["members"]) && i["members"].includes(m)))) ? "till" : "",
91
+ (read("staff")["posts"] ?? []).some((i: any) => String(i["role"]) === "kitchen" && (i["members"] === m || (Array.isArray(i["members"]) && i["members"].includes(m)))) ? "kitchen" : "",
92
+ (item?.["buyer"] === m || (Array.isArray(item?.["buyer"]) && item?.["buyer"].includes(m))) ? "buyer" : "",
93
+ (item?.["supplier"] === m || (Array.isArray(item?.["supplier"]) && item?.["supplier"].includes(m))) ? "supplier" : "",
94
+ ].filter(Boolean);
95
+ }
96
+
97
+ /** What someone sees: all of kitchen, as it has no owns. */
98
+ export const sees = null;
@@ -0,0 +1,61 @@
1
+ // Written by src/compile.ts from examples/cafe/stories.jsonl: change the stories, not this.
2
+
3
+ // Each collection is an element, defined here: on a page, it holds the document, and its items
4
+ // are put inside it. Each item is an element, defined by the page: as it is, or by a view that
5
+ // extends it. A field is an attribute (a list of ids as IDREFS: "1 3"), and an item's key the
6
+ // document's id for it. How an item looks is the app's: its view, or a display holding the
7
+ // collection draws it. Ops go up as an "ops" event, for the page to send to menu, with why they
8
+ // were made if it's said; only the ops the stories allow can be sent. Each returns its write's answer.
9
+
10
+ import { hold, letGo, type Answer } from "joined/page";
11
+
12
+ // the document an element is of: its collection's doc attribute (an instance of a kind, shelf:1), or menu
13
+ const docOf = (el: Element) => el.closest("[doc]")?.getAttribute("doc") ?? "menu";
14
+ // an element's ops go up as an "ops" event; what it returns is the write's answer, done or refused
15
+ const send = (from: Element, ops: object[], why?: string) => new Promise<Answer>((answer) => from.dispatchEvent(new CustomEvent("ops", { bubbles: true, composed: true, detail: { doc: docOf(from), ops, why, answer } })));
16
+
17
+ /** An item's fields, as the stories give them. */
18
+ export type JItemData = { "name": string; "price": number; "stock": number };
19
+ /** An add of an item, as it's sent: its fields as the stories write them. */
20
+ export type JItemValue = { "name": string; "price": number; "stock": number };
21
+
22
+ /** <j-item key>: name (string), price (integer), stock (integer), each an attribute. */
23
+ export class JItem extends HTMLElement {
24
+ static observedAttributes = ["name","price","stock","may"];
25
+ #data?: JItemData;
26
+ /** Its id in the document. */
27
+ get key() { return Number(this.getAttribute("key")); }
28
+ get path() { return `/items/${this.key}`; }
29
+ /** Its fields, read from its attributes once and again when one changes: frozen, since they're shared. */
30
+ get data(): JItemData {
31
+ return (this.#data ??= Object.freeze({ "name": this.getAttribute("name") ?? "", "price": Number(this.getAttribute("price")), "stock": Number(this.getAttribute("stock")) }) as JItemData);
32
+ }
33
+ /** How a view shows it, drawn again whenever an attribute changes (what its person may do, its may, too). */
34
+ draw?(): void;
35
+ attributeChangedCallback() { this.#data = undefined; this.draw?.(); }
36
+ /** Replace some of its fields (price, name): one write for all of them. */
37
+ replace(fields: Partial<Pick<JItemData, "price" | "name">>, why?: string) {
38
+ return send(this, Object.entries(fields).filter(([, value]) => value !== undefined).map(([field, value]) => ({ op: "replace", path: `${this.path}/${field.replace(".", "/")}`, value })), why);
39
+ }
40
+ }
41
+
42
+ /** <j-items>: the document's items, each a <j-item> inside it. */
43
+ export class JItems extends HTMLElement {
44
+ connectedCallback() { hold(this, docOf(this), "items", "j-item", {"name":"string","price":"integer","stock":"integer"}); }
45
+ disconnectedCallback() { letGo(this); }
46
+ /** Its items, as the document has them. */
47
+ get items() { return [...this.children].filter((el): el is JItem => el.localName === "j-item"); }
48
+ /** Add items: one write for all of them. */
49
+ add(...values: JItemValue[]) { return send(this, values.map((value) => ({ op: "add", path: "/items/-", value }))); }
50
+ }
51
+ customElements.define("j-items", JItems);
52
+
53
+ /** An op on menu the stories allow, in any of its collections. */
54
+ export type MenuOp =
55
+ | { op: "add"; path: "/items/-"; value: JItemValue }
56
+ | { op: "replace"; path: `/items/${number}/price`; value: number }
57
+ | { op: "replace"; path: `/items/${number}/name`; value: string };
58
+
59
+ /** One write to menu across its collections (a move with its legs): sent from an element of it, checked
60
+ * whole by its rules, and made whole or not at all. */
61
+ export function write(from: Element, ops: MenuOp[], why?: string) { return send(from, ops, why); }
@@ -0,0 +1,28 @@
1
+ // Written by src/compile.ts from examples/cafe/stories.jsonl: change the stories, not this.
2
+
3
+ /** Each collection's item, as one row of its table (`t`) is it: what the query gathers, and what the store keeps of a
4
+ * row a write changes, before and after, to tell each document holding it. */
5
+ export const items: Record<string, string> = {
6
+ "items": "json_patch('{}', json_object('id', t.id,\n 'name', t.\"name\",\n 'price', t.\"price\",\n 'stock', t.\"stock\"))"
7
+ };
8
+
9
+ /** menu, whole, as one statement over its tables (SQLite): its parameters $doc. */
10
+ export const query = `SELECT json_object('version', COALESCE((SELECT version FROM documents WHERE name = $doc), 0),
11
+ 'items', json((SELECT json_group_array(json_patch('{}', json_object('id', t.id,
12
+ 'name', t."name",
13
+ 'price', t."price",
14
+ 'stock', t."stock")) ORDER BY t.id)
15
+ FROM "items" t))) AS doc`;
16
+
17
+ /** Its rules over values (definitions' takes, gives and totals), as triggers the store makes in its database: run in
18
+ * each write's transaction, and told as any change is. */
19
+ export const triggers: string[] = [];
20
+
21
+ /** Its items, its query and its triggers, as Postgres says them. */
22
+ export const postgres = {
23
+ "items": {
24
+ "items": "json_strip_nulls(json_build_object('id', t.id,\n 'name', t.\"name\",\n 'price', t.\"price\",\n 'stock', t.\"stock\"))"
25
+ },
26
+ "query": "SELECT json_build_object('version', COALESCE((SELECT version FROM documents WHERE name = $doc), 0),\n 'items', (SELECT COALESCE(json_agg(json_strip_nulls(json_build_object('id', t.id,\n 'name', t.\"name\",\n 'price', t.\"price\",\n 'stock', t.\"stock\")) ORDER BY t.id), '[]')\n FROM \"items\" t)) AS doc",
27
+ "triggers": []
28
+ };
@@ -0,0 +1,65 @@
1
+ // Written by src/compile.ts from examples/cafe/stories.jsonl: change the stories, not this.
2
+
3
+ export type Op = { op: "add" | "replace" | "remove"; path: string; value?: any };
4
+
5
+ /** The document, and each collection: its element, the element its items are, their fields, those an add needs, and
6
+ * what each link is to. */
7
+ export const doc = "menu";
8
+ export const collections = {
9
+ "items": {
10
+ "place": "j-items",
11
+ "tag": "j-item",
12
+ "fields": {
13
+ "name": "string",
14
+ "price": "integer",
15
+ "stock": "integer"
16
+ },
17
+ "needs": [
18
+ "name",
19
+ "price",
20
+ "stock"
21
+ ],
22
+ "links": {}
23
+ }
24
+ } as const;
25
+ /** Whether every story is anonymous, none told as a member: where every app's is, the project has no members. */
26
+ export const anonymous = false;
27
+ /** What kind of document menu is, by the flows it uses: static, changed only with a release (a page reads it, and
28
+ * never subscribes); read-only, live, but no page writes it (derived); or shared, its members' to write. */
29
+ export const type = "shared";
30
+ /** The parts of menu a page may hold alone, by its query's parameters (its definitions' window): null, none. */
31
+ export const window = null;
32
+ /** The document as one statement over its tables (generated/menu.queries.ts). */
33
+ export { query, postgres, items, triggers } from "./menu.queries.ts";
34
+
35
+ /** menu is one document. */
36
+ export const kind = null;
37
+
38
+ /** Each grant, read off the stories: the roles whose stories do it (a member: anyone). An op no story
39
+ * does isn't one. */
40
+ export const grants: Record<string, string[]> = {
41
+ "add items": [
42
+ "manager"
43
+ ],
44
+ "replace items.name": [
45
+ "manager"
46
+ ],
47
+ "replace items.price": [
48
+ "manager"
49
+ ]
50
+ };
51
+
52
+ /** Who someone is to menu (and to an item of it, `at`), beyond a member: each role whose who-path finds them. */
53
+ export function rolesOf(m: number, read: (doc: string) => any, doc: string, at?: any): string[] {
54
+ const item: any = at;
55
+ return [
56
+ (read("staff")["posts"] ?? []).some((i: any) => String(i["role"]) === "manager" && (i["members"] === m || (Array.isArray(i["members"]) && i["members"].includes(m)))) ? "manager" : "",
57
+ (read("staff")["posts"] ?? []).some((i: any) => String(i["role"]) === "till" && (i["members"] === m || (Array.isArray(i["members"]) && i["members"].includes(m)))) ? "till" : "",
58
+ (read("staff")["posts"] ?? []).some((i: any) => String(i["role"]) === "kitchen" && (i["members"] === m || (Array.isArray(i["members"]) && i["members"].includes(m)))) ? "kitchen" : "",
59
+ (item?.["buyer"] === m || (Array.isArray(item?.["buyer"]) && item?.["buyer"].includes(m))) ? "buyer" : "",
60
+ (item?.["supplier"] === m || (Array.isArray(item?.["supplier"]) && item?.["supplier"].includes(m))) ? "supplier" : "",
61
+ ].filter(Boolean);
62
+ }
63
+
64
+ /** What someone sees: all of menu, as it has no owns. */
65
+ export const sees = null;