@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.
- package/.claude/skills/joined/SKILL.md +17 -0
- package/.claude/skills/joined/reference.md +768 -0
- package/LICENSE +19 -0
- package/README.md +115 -2
- package/docs/design.md +148 -0
- package/docs/introduction.md +193 -0
- package/docs/learning.md +76 -0
- package/docs/stories.md +82 -0
- package/examples/app.css +27 -0
- package/examples/cafe/books.html +38 -0
- package/examples/cafe/books.json +13 -0
- package/examples/cafe/books.ts +64 -0
- package/examples/cafe/cafe.css +512 -0
- package/examples/cafe/front.html +115 -0
- package/examples/cafe/front.ts +7 -0
- package/examples/cafe/generated/books.elements.ts +52 -0
- package/examples/cafe/generated/books.queries.ts +11 -0
- package/examples/cafe/generated/books.rules.ts +42 -0
- package/examples/cafe/generated/kitchen.elements.ts +87 -0
- package/examples/cafe/generated/kitchen.queries.ts +47 -0
- package/examples/cafe/generated/kitchen.rules.ts +98 -0
- package/examples/cafe/generated/menu.elements.ts +61 -0
- package/examples/cafe/generated/menu.queries.ts +28 -0
- package/examples/cafe/generated/menu.rules.ts +65 -0
- package/examples/cafe/generated/order.elements.ts +87 -0
- package/examples/cafe/generated/order.queries.ts +47 -0
- package/examples/cafe/generated/order.rules.ts +95 -0
- package/examples/cafe/generated/staff.elements.ts +55 -0
- package/examples/cafe/generated/staff.queries.ts +27 -0
- package/examples/cafe/generated/staff.rules.ts +59 -0
- package/examples/cafe/generated/supplies.elements.ts +62 -0
- package/examples/cafe/generated/supplies.queries.ts +43 -0
- package/examples/cafe/generated/supplies.rules.ts +84 -0
- package/examples/cafe/generated/till.elements.ts +93 -0
- package/examples/cafe/generated/till.queries.ts +57 -0
- package/examples/cafe/generated/till.rules.ts +113 -0
- package/examples/cafe/index.html +70 -0
- package/examples/cafe/kitchen.css +107 -0
- package/examples/cafe/kitchen.html +39 -0
- package/examples/cafe/kitchen.ts +125 -0
- package/examples/cafe/menu.html +55 -0
- package/examples/cafe/menu.json +41 -0
- package/examples/cafe/menu.ts +84 -0
- package/examples/cafe/migrations/001-cafe.ts +11 -0
- package/examples/cafe/order.html +37 -0
- package/examples/cafe/order.ts +45 -0
- package/examples/cafe/rail-drag.ts +30 -0
- package/examples/cafe/routes.ts +32 -0
- package/examples/cafe/staff.json +20 -0
- package/examples/cafe/stories.jsonl +43 -0
- package/examples/cafe/supplies.html +77 -0
- package/examples/cafe/supplies.json +33 -0
- package/examples/cafe/supplies.ts +170 -0
- package/examples/cafe/till.json +95 -0
- package/examples/cafe/till.ts +122 -0
- package/examples/cafe/trust.ts +20 -0
- package/examples/cafe/views.ts +171 -0
- package/examples/docs.ts +28 -0
- package/examples/film.jpg +0 -0
- package/examples/hello/generated/elements.ts +45 -0
- package/examples/hello/generated/queries.ts +24 -0
- package/examples/hello/generated/rules.ts +45 -0
- package/examples/hello/hello.css +27 -0
- package/examples/hello/hello.json +1 -0
- package/examples/hello/index.html +32 -0
- package/examples/hello/migrations/001-hello.ts +6 -0
- package/examples/hello/page.ts +49 -0
- package/examples/hello/routes.ts +6 -0
- package/examples/hello/stories.jsonl +3 -0
- package/examples/index.html +146 -0
- package/examples/page.ts +11 -0
- package/examples/people/adding.ts +20 -0
- package/examples/people/generated/elements.ts +68 -0
- package/examples/people/generated/queries.ts +28 -0
- package/examples/people/generated/rules.ts +85 -0
- package/examples/people/links.ts +124 -0
- package/examples/people/migrations/001-people.ts +6 -0
- package/examples/people/migrations/002-add-people-member-and-guard-people.ts +7 -0
- package/examples/people/migrations/003-add-people-rank-and-guard-people.ts +7 -0
- package/examples/people/order.ts +61 -0
- package/examples/people/people.css +65 -0
- package/examples/people/people.json +26 -0
- package/examples/people/routes.ts +12 -0
- package/examples/people/stories.jsonl +20 -0
- package/examples/people/trust.ts +14 -0
- package/examples/people/view.ts +115 -0
- package/examples/routes.ts +11 -0
- package/examples/trip/answer-rows.ts +70 -0
- package/examples/trip/answers.ts +11 -0
- package/examples/trip/bar.ts +89 -0
- package/examples/trip/calendar-map.css +709 -0
- package/examples/trip/calendar-map.ts +249 -0
- package/examples/trip/drags.ts +143 -0
- package/examples/trip/drawer.ts +250 -0
- package/examples/trip/event-form.ts +199 -0
- package/examples/trip/generated/elements.ts +144 -0
- package/examples/trip/generated/queries.ts +46 -0
- package/examples/trip/generated/rules.ts +239 -0
- package/examples/trip/holding.ts +61 -0
- package/examples/trip/ics.ts +117 -0
- package/examples/trip/index.html +82 -0
- package/examples/trip/line-stops.ts +92 -0
- package/examples/trip/line-words.ts +79 -0
- package/examples/trip/line.ts +121 -0
- package/examples/trip/map.ts +234 -0
- package/examples/trip/mercator.ts +28 -0
- package/examples/trip/migrations/001-trip.ts +7 -0
- package/examples/trip/migrations/002-add-events-zone-and-guard-events.ts +7 -0
- package/examples/trip/migrations/003-guard-events-and-create-events_asked-and-more.ts +9 -0
- package/examples/trip/migrations/004-create-stays.ts +6 -0
- package/examples/trip/migrations/005-guard-events-and-create-events_arrives-and-more.ts +8 -0
- package/examples/trip/month.ts +87 -0
- package/examples/trip/other-weeks.ts +67 -0
- package/examples/trip/page.ts +121 -0
- package/examples/trip/peek.ts +71 -0
- package/examples/trip/people-chips.ts +74 -0
- package/examples/trip/place-search.ts +51 -0
- package/examples/trip/places.ts +34 -0
- package/examples/trip/routes.ts +27 -0
- package/examples/trip/shell.ts +39 -0
- package/examples/trip/stays.css +51 -0
- package/examples/trip/stays.ts +68 -0
- package/examples/trip/stories.jsonl +72 -0
- package/examples/trip/time.ts +46 -0
- package/examples/trip/told.ts +48 -0
- package/examples/trip/trip.json +69 -0
- package/examples/trip/trust.ts +51 -0
- package/examples/trip/week.ts +237 -0
- package/examples/trip/words.ts +4 -0
- package/examples/trip/writes.ts +91 -0
- package/examples/trip/zones.ts +16 -0
- package/examples/trips/card.ts +149 -0
- package/examples/trips/generated/elements.ts +60 -0
- package/examples/trips/generated/queries.ts +28 -0
- package/examples/trips/generated/rules.ts +62 -0
- package/examples/trips/index.html +47 -0
- package/examples/trips/map.ts +52 -0
- package/examples/trips/migrations/001-trips.ts +8 -0
- package/examples/trips/new-trip.ts +46 -0
- package/examples/trips/page.ts +9 -0
- package/examples/trips/routes.ts +7 -0
- package/examples/trips/stories.jsonl +12 -0
- package/examples/trips/trips.css +183 -0
- package/examples/trips/trips.json +7 -0
- package/examples/weeks/derive.ts +23 -0
- package/examples/weeks/generated/elements.ts +52 -0
- package/examples/weeks/generated/rules.ts +33 -0
- package/examples/weeks/routes.ts +4 -0
- package/examples/weeks/weeks.json +1 -0
- package/examples/welcome.css +299 -0
- package/package.json +55 -4
- package/src/attachment.ts +70 -0
- package/src/cli.ts +52 -0
- package/src/client.ts +98 -0
- package/src/compile.ts +2034 -0
- package/src/css.d.ts +5 -0
- package/src/definitions.ts +268 -0
- package/src/demo.css +29 -0
- package/src/demo.ts +42 -0
- package/src/draft.ts +69 -0
- package/src/engine.ts +288 -0
- package/src/file-value.ts +13 -0
- package/src/files.ts +187 -0
- package/src/follow.ts +56 -0
- package/src/hues.ts +3 -0
- package/src/icon-names.ts +77 -0
- package/src/icon.png +0 -0
- package/src/icons.ts +32 -0
- package/src/keep.ts +33 -0
- package/src/link.ts +31 -0
- package/src/mcp.ts +252 -0
- package/src/members/demo.ts +53 -0
- package/src/members/generated/elements.ts +68 -0
- package/src/members/generated/queries.ts +29 -0
- package/src/members/generated/rules.ts +65 -0
- package/src/members/group.ts +294 -0
- package/src/members/keys.ts +221 -0
- package/src/members/members.json +9 -0
- package/src/members/migrations/001-members.ts +6 -0
- package/src/members/migrations/002-guard-members.ts +11 -0
- package/src/members/migrations/003-add-members-act-and-guard-members.ts +7 -0
- package/src/members/panel.css +208 -0
- package/src/members/panel.ts +398 -0
- package/src/members/routes.ts +4 -0
- package/src/members/stories.jsonl +27 -0
- package/src/members/trust.ts +116 -0
- package/src/migrate.ts +597 -0
- package/src/migrations/001-joined.ts +13 -0
- package/src/migrations/002-group-keys.ts +16 -0
- package/src/migrations/003-change-inverse.ts +10 -0
- package/src/migrations/004-change-followed.ts +6 -0
- package/src/ops.ts +61 -0
- package/src/page.css +79 -0
- package/src/page.ts +413 -0
- package/src/parts.css +471 -0
- package/src/parts.ts +152 -0
- package/src/server.ts +862 -0
- package/src/settings.ts +94 -0
- package/src/starter.ts +89 -0
- package/src/store.ts +815 -0
- package/src/stories.ts +387 -0
- package/src/toast.ts +52 -0
- package/src/together.ts +167 -0
- package/src/tokens.css +85 -0
- package/src/tokens.json +457 -0
- package/src/upkeep.ts +86 -0
- package/src/visitors.ts +43 -0
- package/src/write.ts +567 -0
- package/starter/CLAUDE.md +37 -0
- package/starter/gitignore +3 -0
- package/starter/tests/hello-page.test.ts +14 -0
- package/starter/tests/hello.test.ts +8 -0
- package/starter/tsconfig.json +17 -0
|
@@ -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 order, 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 order
|
|
13
|
+
const docOf = (el: Element) => el.closest("[doc]")?.getAttribute("doc") ?? "order";
|
|
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 order the stories allow, in any of its collections. */
|
|
82
|
+
export type OrderOp =
|
|
83
|
+
| { op: "replace"; path: `/orders/${number}/status`; value: string };
|
|
84
|
+
|
|
85
|
+
/** One write to order 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: OrderOp[], 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
|
+
/** order, 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))),
|
|
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)))))) 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)),\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))))) 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,95 @@
|
|
|
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 = "order";
|
|
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 order 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 = ["till","manager"];
|
|
62
|
+
/** The parts of order 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 order 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/order.queries.ts). */
|
|
69
|
+
export { query, postgres, items, triggers } from "./order.queries.ts";
|
|
70
|
+
|
|
71
|
+
/** order 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:placed>cancelled": [
|
|
78
|
+
"guest"
|
|
79
|
+
]
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/** Who someone is to order (and to an item of it, `at`), beyond a member: each role whose who-path finds them. */
|
|
83
|
+
export function rolesOf(m: number, read: (doc: string) => any, doc: string, at?: any): string[] {
|
|
84
|
+
const item: any = at;
|
|
85
|
+
return [
|
|
86
|
+
(read("staff")["posts"] ?? []).some((i: any) => String(i["role"]) === "manager" && (i["members"] === m || (Array.isArray(i["members"]) && i["members"].includes(m)))) ? "manager" : "",
|
|
87
|
+
(read("staff")["posts"] ?? []).some((i: any) => String(i["role"]) === "till" && (i["members"] === m || (Array.isArray(i["members"]) && i["members"].includes(m)))) ? "till" : "",
|
|
88
|
+
(read("staff")["posts"] ?? []).some((i: any) => String(i["role"]) === "kitchen" && (i["members"] === m || (Array.isArray(i["members"]) && i["members"].includes(m)))) ? "kitchen" : "",
|
|
89
|
+
(item?.["buyer"] === m || (Array.isArray(item?.["buyer"]) && item?.["buyer"].includes(m))) ? "buyer" : "",
|
|
90
|
+
(item?.["supplier"] === m || (Array.isArray(item?.["supplier"]) && item?.["supplier"].includes(m))) ? "supplier" : "",
|
|
91
|
+
].filter(Boolean);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** What someone sees: all of order, as it has no owns. */
|
|
95
|
+
export const sees = null;
|
|
@@ -0,0 +1,55 @@
|
|
|
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 staff, 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 staff
|
|
13
|
+
const docOf = (el: Element) => el.closest("[doc]")?.getAttribute("doc") ?? "staff";
|
|
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
|
+
/** A post's fields, as the stories give them. */
|
|
18
|
+
export type JPostData = { "role": string; "members": number[] };
|
|
19
|
+
/** An add of a post, as it's sent: its fields as the stories write them. */
|
|
20
|
+
export type JPostValue = { "role": string; "members": number[] };
|
|
21
|
+
|
|
22
|
+
/** <j-post key>: role (string), members (ids in members), each an attribute. */
|
|
23
|
+
export class JPost extends HTMLElement {
|
|
24
|
+
static observedAttributes = ["role","members","may"];
|
|
25
|
+
#data?: JPostData;
|
|
26
|
+
/** Its id in the document. */
|
|
27
|
+
get key() { return Number(this.getAttribute("key")); }
|
|
28
|
+
get path() { return `/posts/${this.key}`; }
|
|
29
|
+
/** Its fields, read from its attributes once and again when one changes: frozen, since they're shared. */
|
|
30
|
+
get data(): JPostData {
|
|
31
|
+
return (this.#data ??= Object.freeze({ "role": this.getAttribute("role") ?? "", "members": Object.freeze((this.getAttribute("members") ?? "").split(" ").filter(Boolean).map(Number)) }) as JPostData);
|
|
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-posts>: the document's posts, each a <j-post> inside it. */
|
|
39
|
+
export class JPosts extends HTMLElement {
|
|
40
|
+
connectedCallback() { hold(this, docOf(this), "posts", "j-post", {"role":"string","members":"ids"}); }
|
|
41
|
+
disconnectedCallback() { letGo(this); }
|
|
42
|
+
/** Its posts, as the document has them. */
|
|
43
|
+
get items() { return [...this.children].filter((el): el is JPost => el.localName === "j-post"); }
|
|
44
|
+
/** Add posts: one write for all of them. */
|
|
45
|
+
add(...values: JPostValue[]) { return send(this, values.map((value) => ({ op: "add", path: "/posts/-", value }))); }
|
|
46
|
+
}
|
|
47
|
+
customElements.define("j-posts", JPosts);
|
|
48
|
+
|
|
49
|
+
/** An op on staff the stories allow, in any of its collections. */
|
|
50
|
+
export type StaffOp =
|
|
51
|
+
| { op: "add"; path: "/posts/-"; value: JPostValue };
|
|
52
|
+
|
|
53
|
+
/** One write to staff across its collections (a move with its legs): sent from an element of it, checked
|
|
54
|
+
* whole by its rules, and made whole or not at all. */
|
|
55
|
+
export function write(from: Element, ops: StaffOp[], why?: string) { return send(from, ops, why); }
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
"posts": "json_patch('{}', json_object('id', t.id,\n 'role', t.\"role\",\n 'members', json(NULLIF((SELECT json_group_array(p.\"members\" ORDER BY p.id) FROM \"posts_members\" p WHERE p.\"posts\" = t.id), '[]'))))"
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/** staff, 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
|
+
'posts', json((SELECT json_group_array(json_patch('{}', json_object('id', t.id,
|
|
12
|
+
'role', t."role",
|
|
13
|
+
'members', json(NULLIF((SELECT json_group_array(p."members" ORDER BY p.id) FROM "posts_members" p WHERE p."posts" = t.id), '[]')))) ORDER BY t.id)
|
|
14
|
+
FROM "posts" t))) AS doc`;
|
|
15
|
+
|
|
16
|
+
/** Its rules over values (definitions' takes, gives and totals), as triggers the store makes in its database: run in
|
|
17
|
+
* each write's transaction, and told as any change is. */
|
|
18
|
+
export const triggers: string[] = [];
|
|
19
|
+
|
|
20
|
+
/** Its items, its query and its triggers, as Postgres says them. */
|
|
21
|
+
export const postgres = {
|
|
22
|
+
"items": {
|
|
23
|
+
"posts": "json_strip_nulls(json_build_object('id', t.id,\n 'role', t.\"role\",\n 'members', (SELECT json_agg(p.\"members\" ORDER BY p.id) FROM \"posts_members\" p WHERE p.\"posts\" = t.id)))"
|
|
24
|
+
},
|
|
25
|
+
"query": "SELECT json_build_object('version', COALESCE((SELECT version FROM documents WHERE name = $doc), 0),\n 'posts', (SELECT COALESCE(json_agg(json_strip_nulls(json_build_object('id', t.id,\n 'role', t.\"role\",\n 'members', (SELECT json_agg(p.\"members\" ORDER BY p.id) FROM \"posts_members\" p WHERE p.\"posts\" = t.id))) ORDER BY t.id), '[]')\n FROM \"posts\" t)) AS doc",
|
|
26
|
+
"triggers": []
|
|
27
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
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 = "staff";
|
|
8
|
+
export const collections = {
|
|
9
|
+
"posts": {
|
|
10
|
+
"place": "j-posts",
|
|
11
|
+
"tag": "j-post",
|
|
12
|
+
"fields": {
|
|
13
|
+
"role": "string",
|
|
14
|
+
"members": "ids"
|
|
15
|
+
},
|
|
16
|
+
"needs": [
|
|
17
|
+
"role",
|
|
18
|
+
"members"
|
|
19
|
+
],
|
|
20
|
+
"links": {
|
|
21
|
+
"members": "members"
|
|
22
|
+
}
|
|
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 staff 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 staff 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/staff.queries.ts). */
|
|
33
|
+
export { query, postgres, items, triggers } from "./staff.queries.ts";
|
|
34
|
+
|
|
35
|
+
/** staff 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 posts": [
|
|
42
|
+
"manager"
|
|
43
|
+
]
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** Who someone is to staff (and to an item of it, `at`), beyond a member: each role whose who-path finds them. */
|
|
47
|
+
export function rolesOf(m: number, read: (doc: string) => any, doc: string, at?: any): string[] {
|
|
48
|
+
const item: any = at;
|
|
49
|
+
return [
|
|
50
|
+
(read("staff")["posts"] ?? []).some((i: any) => String(i["role"]) === "manager" && (i["members"] === m || (Array.isArray(i["members"]) && i["members"].includes(m)))) ? "manager" : "",
|
|
51
|
+
(read("staff")["posts"] ?? []).some((i: any) => String(i["role"]) === "till" && (i["members"] === m || (Array.isArray(i["members"]) && i["members"].includes(m)))) ? "till" : "",
|
|
52
|
+
(read("staff")["posts"] ?? []).some((i: any) => String(i["role"]) === "kitchen" && (i["members"] === m || (Array.isArray(i["members"]) && i["members"].includes(m)))) ? "kitchen" : "",
|
|
53
|
+
(item?.["buyer"] === m || (Array.isArray(item?.["buyer"]) && item?.["buyer"].includes(m))) ? "buyer" : "",
|
|
54
|
+
(item?.["supplier"] === m || (Array.isArray(item?.["supplier"]) && item?.["supplier"].includes(m))) ? "supplier" : "",
|
|
55
|
+
].filter(Boolean);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** What someone sees: all of staff, as it has no owns. */
|
|
59
|
+
export const sees = null;
|
|
@@ -0,0 +1,62 @@
|
|
|
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 supplies, 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 supplies
|
|
13
|
+
const docOf = (el: Element) => el.closest("[doc]")?.getAttribute("doc") ?? "supplies";
|
|
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
|
+
/** A supply's fields, as the stories give them. */
|
|
18
|
+
export type JSupplyData = { "title": string; "item": number; "qty": number; "price": number; "buyer": number; "supplier": number; "terms.days": number; "terms.by": string; "delivered": boolean };
|
|
19
|
+
/** An add of a supply, as it's sent: its fields as the stories write them. */
|
|
20
|
+
export type JSupplyValue = { "title": string; "item": number | `$${number}`; "qty": number; "price": number; "buyer": number | `$${number}`; "supplier": number | `$${number}`; "terms": { "days": number; "by": string }; "delivered": boolean };
|
|
21
|
+
|
|
22
|
+
/** <j-supply key>: title (string), item (an id in items), qty (integer), price (integer), buyer (an id in members), supplier (an id in members), terms.days (integer), terms.by (string), delivered (boolean), each an attribute. */
|
|
23
|
+
export class JSupply extends HTMLElement {
|
|
24
|
+
static observedAttributes = ["title","item","qty","price","buyer","supplier","terms.days","terms.by","delivered","may"];
|
|
25
|
+
#data?: Omit<JSupplyData, "buyer" | "supplier"> & Partial<Pick<JSupplyData, "buyer" | "supplier">>;
|
|
26
|
+
/** Its id in the document. */
|
|
27
|
+
get key() { return Number(this.getAttribute("key")); }
|
|
28
|
+
get path() { return `/supplies/${this.key}`; }
|
|
29
|
+
/** Its fields, read from its attributes once and again when one changes: frozen, since they're shared. */
|
|
30
|
+
get data(): Omit<JSupplyData, "buyer" | "supplier"> & Partial<Pick<JSupplyData, "buyer" | "supplier">> {
|
|
31
|
+
return (this.#data ??= Object.freeze({ "title": this.getAttribute("title") ?? "", "item": Number(this.getAttribute("item")), "qty": Number(this.getAttribute("qty")), "price": Number(this.getAttribute("price")), "buyer": this.hasAttribute("buyer") ? Number(this.getAttribute("buyer")) : undefined, "supplier": this.hasAttribute("supplier") ? Number(this.getAttribute("supplier")) : undefined, "terms.days": Number(this.getAttribute("terms.days")), "terms.by": this.getAttribute("terms.by") ?? "", "delivered": this.hasAttribute("delivered") }) as Omit<JSupplyData, "buyer" | "supplier"> & Partial<Pick<JSupplyData, "buyer" | "supplier">>);
|
|
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, terms.days, delivered): one write for all of them. */
|
|
37
|
+
replace(fields: Partial<Pick<JSupplyData, "price" | "terms.days" | "delivered">>, 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-supplies>: the document's supplies, each a <j-supply> inside it. */
|
|
43
|
+
export class JSupplies extends HTMLElement {
|
|
44
|
+
connectedCallback() { hold(this, docOf(this), "supplies", "j-supply", {"title":"string","item":"integer","qty":"integer","price":"integer","buyer":"integer","supplier":"integer","terms.days":"integer","terms.by":"string","delivered":"boolean"}); }
|
|
45
|
+
disconnectedCallback() { letGo(this); }
|
|
46
|
+
/** Its supplies, as the document has them. */
|
|
47
|
+
get items() { return [...this.children].filter((el): el is JSupply => el.localName === "j-supply"); }
|
|
48
|
+
/** Add supplies: one write for all of them. */
|
|
49
|
+
add(...values: JSupplyValue[]) { return send(this, values.map((value) => ({ op: "add", path: "/supplies/-", value }))); }
|
|
50
|
+
}
|
|
51
|
+
customElements.define("j-supplies", JSupplies);
|
|
52
|
+
|
|
53
|
+
/** An op on supplies the stories allow, in any of its collections. */
|
|
54
|
+
export type SuppliesOp =
|
|
55
|
+
| { op: "add"; path: "/supplies/-"; value: JSupplyValue }
|
|
56
|
+
| { op: "replace"; path: `/supplies/${number}/price`; value: number }
|
|
57
|
+
| { op: "replace"; path: `/supplies/${number}/terms/days`; value: number }
|
|
58
|
+
| { op: "replace"; path: `/supplies/${number}/delivered`; value: boolean };
|
|
59
|
+
|
|
60
|
+
/** One write to supplies across its collections (a move with its legs): sent from an element of it, checked
|
|
61
|
+
* whole by its rules, and made whole or not at all. */
|
|
62
|
+
export function write(from: Element, ops: SuppliesOp[], why?: string) { return send(from, ops, why); }
|
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
"supplies": "json_patch('{}', json_object('id', t.id,\n 'title', t.\"title\",\n 'item', t.\"item\",\n 'qty', t.\"qty\",\n 'price', t.\"price\",\n 'buyer', t.\"buyer\",\n 'supplier', t.\"supplier\",\n 'terms', json(NULLIF(json_patch('{}', json_object('days', t.\"terms.days\", 'by', t.\"terms.by\")), '{}')),\n 'delivered', json(CASE t.\"delivered\" WHEN 1 THEN 'true' WHEN 0 THEN 'false' END)))"
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/** supplies, whole, as one statement over its tables (SQLite): its parameters $doc, and $member, who's asking (none: all of it). */
|
|
10
|
+
export const query = `SELECT json_object('version', COALESCE((SELECT version FROM documents WHERE name = $doc), 0),
|
|
11
|
+
'supplies', json((SELECT json_group_array(json_patch('{}', json_object('id', t.id,
|
|
12
|
+
'title', t."title",
|
|
13
|
+
'item', t."item",
|
|
14
|
+
'qty', t."qty",
|
|
15
|
+
'price', t."price",
|
|
16
|
+
'buyer', t."buyer",
|
|
17
|
+
'supplier', t."supplier",
|
|
18
|
+
'terms', json(NULLIF(json_patch('{}', json_object('days', t."terms.days", 'by', t."terms.by")), '{}')),
|
|
19
|
+
'delivered', json(CASE t."delivered" WHEN 1 THEN 'true' WHEN 0 THEN 'false' END))) ORDER BY t.id)
|
|
20
|
+
FROM "supplies" t WHERE ($member IS NULL OR (t."buyer" = $member OR t."supplier" = $member))))) AS doc`;
|
|
21
|
+
|
|
22
|
+
/** Its rules over values (definitions' takes, gives and totals), as triggers the store makes in its database: run in
|
|
23
|
+
* each write's transaction, and told as any change is. */
|
|
24
|
+
export const triggers: string[] = [
|
|
25
|
+
"CREATE TEMP TRIGGER IF NOT EXISTS \"joined_gives_supplies_add\" AFTER INSERT ON main.\"supplies\" WHEN NEW.\"delivered\" = 1 BEGIN UPDATE \"items\" SET \"stock\" = \"stock\" + NEW.\"qty\" WHERE id = NEW.\"item\"; END",
|
|
26
|
+
"CREATE TEMP TRIGGER IF NOT EXISTS \"joined_gives_supplies_remove\" AFTER DELETE ON main.\"supplies\" WHEN OLD.\"delivered\" = 1 BEGIN UPDATE \"items\" SET \"stock\" = \"stock\" - OLD.\"qty\" WHERE id = OLD.\"item\"; END",
|
|
27
|
+
"CREATE TEMP TRIGGER IF NOT EXISTS \"joined_gives_supplies_replace\" AFTER UPDATE OF \"qty\", \"item\", \"delivered\" ON main.\"supplies\" BEGIN UPDATE \"items\" SET \"stock\" = \"stock\" - OLD.\"qty\" WHERE id = OLD.\"item\" AND OLD.\"delivered\" = 1; UPDATE \"items\" SET \"stock\" = \"stock\" + NEW.\"qty\" WHERE id = NEW.\"item\" AND NEW.\"delivered\" = 1; END",
|
|
28
|
+
"CREATE TEMP TRIGGER IF NOT EXISTS \"joined_gives_supplies_none\" BEFORE UPDATE OF \"stock\" ON main.\"items\" BEGIN SELECT RAISE(ABORT, 'The kitchen has ' || OLD.\"stock\" || ' of ' || OLD.\"name\" || ': not enough to take ' || (OLD.\"stock\" - NEW.\"stock\") || '.') WHERE NEW.\"stock\" < 0; END"
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
/** Its items, its query and its triggers, as Postgres says them. */
|
|
32
|
+
export const postgres = {
|
|
33
|
+
"items": {
|
|
34
|
+
"supplies": "json_strip_nulls(json_build_object('id', t.id,\n 'title', t.\"title\",\n 'item', t.\"item\",\n 'qty', t.\"qty\",\n 'price', t.\"price\",\n 'buyer', t.\"buyer\",\n 'supplier', t.\"supplier\",\n 'terms', NULLIF(jsonb_strip_nulls(jsonb_build_object('days', t.\"terms.days\", 'by', t.\"terms.by\")), '{}'),\n 'delivered', (CASE t.\"delivered\" WHEN 1 THEN true WHEN 0 THEN false END)))"
|
|
35
|
+
},
|
|
36
|
+
"query": "SELECT json_build_object('version', COALESCE((SELECT version FROM documents WHERE name = $doc), 0),\n 'supplies', (SELECT COALESCE(json_agg(json_strip_nulls(json_build_object('id', t.id,\n 'title', t.\"title\",\n 'item', t.\"item\",\n 'qty', t.\"qty\",\n 'price', t.\"price\",\n 'buyer', t.\"buyer\",\n 'supplier', t.\"supplier\",\n 'terms', NULLIF(jsonb_strip_nulls(jsonb_build_object('days', t.\"terms.days\", 'by', t.\"terms.by\")), '{}'),\n 'delivered', (CASE t.\"delivered\" WHEN 1 THEN true WHEN 0 THEN false END))) ORDER BY t.id), '[]')\n FROM \"supplies\" t WHERE ($member IS NULL OR (t.\"buyer\" = $member OR t.\"supplier\" = $member)))) AS doc",
|
|
37
|
+
"triggers": [
|
|
38
|
+
"CREATE OR REPLACE FUNCTION \"joined_gives_supplies_add_fn\"() RETURNS trigger LANGUAGE plpgsql AS $joined$ BEGIN IF NEW.\"delivered\" = 1 THEN UPDATE \"items\" SET \"stock\" = \"stock\" + NEW.\"qty\" WHERE id = NEW.\"item\"; END IF; IF TG_OP = 'DELETE' THEN RETURN OLD; END IF; RETURN NEW; END $joined$; CREATE OR REPLACE TRIGGER \"joined_gives_supplies_add\" AFTER INSERT ON \"supplies\" FOR EACH ROW EXECUTE FUNCTION \"joined_gives_supplies_add_fn\"()",
|
|
39
|
+
"CREATE OR REPLACE FUNCTION \"joined_gives_supplies_remove_fn\"() RETURNS trigger LANGUAGE plpgsql AS $joined$ BEGIN IF OLD.\"delivered\" = 1 THEN UPDATE \"items\" SET \"stock\" = \"stock\" - OLD.\"qty\" WHERE id = OLD.\"item\"; END IF; IF TG_OP = 'DELETE' THEN RETURN OLD; END IF; RETURN NEW; END $joined$; CREATE OR REPLACE TRIGGER \"joined_gives_supplies_remove\" AFTER DELETE ON \"supplies\" FOR EACH ROW EXECUTE FUNCTION \"joined_gives_supplies_remove_fn\"()",
|
|
40
|
+
"CREATE OR REPLACE FUNCTION \"joined_gives_supplies_replace_fn\"() RETURNS trigger LANGUAGE plpgsql AS $joined$ BEGIN UPDATE \"items\" SET \"stock\" = \"stock\" - OLD.\"qty\" WHERE id = OLD.\"item\" AND OLD.\"delivered\" = 1; UPDATE \"items\" SET \"stock\" = \"stock\" + NEW.\"qty\" WHERE id = NEW.\"item\" AND NEW.\"delivered\" = 1; IF TG_OP = 'DELETE' THEN RETURN OLD; END IF; RETURN NEW; END $joined$; CREATE OR REPLACE TRIGGER \"joined_gives_supplies_replace\" AFTER UPDATE OF \"qty\", \"item\", \"delivered\" ON \"supplies\" FOR EACH ROW EXECUTE FUNCTION \"joined_gives_supplies_replace_fn\"()",
|
|
41
|
+
"CREATE OR REPLACE FUNCTION \"joined_gives_supplies_none_fn\"() RETURNS trigger LANGUAGE plpgsql AS $joined$ BEGIN IF NEW.\"stock\" < 0 THEN RAISE EXCEPTION '%', 'The kitchen has ' || OLD.\"stock\" || ' of ' || OLD.\"name\" || ': not enough to take ' || (OLD.\"stock\" - NEW.\"stock\") || '.'; END IF; IF TG_OP = 'DELETE' THEN RETURN OLD; END IF; RETURN NEW; END $joined$; CREATE OR REPLACE TRIGGER \"joined_gives_supplies_none\" BEFORE UPDATE OF \"stock\" ON \"items\" FOR EACH ROW EXECUTE FUNCTION \"joined_gives_supplies_none_fn\"()"
|
|
42
|
+
]
|
|
43
|
+
};
|
|
@@ -0,0 +1,84 @@
|
|
|
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 = "supplies";
|
|
8
|
+
export const collections = {
|
|
9
|
+
"supplies": {
|
|
10
|
+
"place": "j-supplies",
|
|
11
|
+
"tag": "j-supply",
|
|
12
|
+
"fields": {
|
|
13
|
+
"title": "string",
|
|
14
|
+
"item": "integer",
|
|
15
|
+
"qty": "integer",
|
|
16
|
+
"price": "integer",
|
|
17
|
+
"buyer": "integer",
|
|
18
|
+
"supplier": "integer",
|
|
19
|
+
"terms.days": "integer",
|
|
20
|
+
"terms.by": "string",
|
|
21
|
+
"delivered": "boolean"
|
|
22
|
+
},
|
|
23
|
+
"needs": [
|
|
24
|
+
"title",
|
|
25
|
+
"item",
|
|
26
|
+
"qty",
|
|
27
|
+
"price",
|
|
28
|
+
"buyer",
|
|
29
|
+
"supplier",
|
|
30
|
+
"terms.days",
|
|
31
|
+
"terms.by",
|
|
32
|
+
"delivered"
|
|
33
|
+
],
|
|
34
|
+
"links": {
|
|
35
|
+
"item": "items",
|
|
36
|
+
"buyer": "members",
|
|
37
|
+
"supplier": "members"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
} as const;
|
|
41
|
+
/** Whether every story is anonymous, none told as a member: where every app's is, the project has no members. */
|
|
42
|
+
export const anonymous = false;
|
|
43
|
+
/** What kind of document supplies is, by the flows it uses: static, changed only with a release (a page reads it, and
|
|
44
|
+
* never subscribes); read-only, live, but no page writes it (derived); or shared, its members' to write. */
|
|
45
|
+
export const type = "shared";
|
|
46
|
+
/** The parts of supplies a page may hold alone, by its query's parameters (its definitions' window): null, none. */
|
|
47
|
+
export const window = null;
|
|
48
|
+
/** The document as one statement over its tables (generated/supplies.queries.ts). */
|
|
49
|
+
export { query, postgres, items, triggers } from "./supplies.queries.ts";
|
|
50
|
+
|
|
51
|
+
/** supplies is one document. */
|
|
52
|
+
export const kind = null;
|
|
53
|
+
|
|
54
|
+
/** Each grant, read off the stories: the roles whose stories do it (a member: anyone). An op no story
|
|
55
|
+
* does isn't one. */
|
|
56
|
+
export const grants: Record<string, string[]> = {
|
|
57
|
+
"add supplies": [
|
|
58
|
+
"buyer"
|
|
59
|
+
],
|
|
60
|
+
"replace supplies.delivered": [
|
|
61
|
+
"supplier"
|
|
62
|
+
],
|
|
63
|
+
"replace supplies.price": [
|
|
64
|
+
"supplier"
|
|
65
|
+
],
|
|
66
|
+
"replace supplies.terms.days": [
|
|
67
|
+
"supplier"
|
|
68
|
+
]
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** Who someone is to supplies (and to an item of it, `at`), beyond a member: each role whose who-path finds them. */
|
|
72
|
+
export function rolesOf(m: number, read: (doc: string) => any, doc: string, at?: any): string[] {
|
|
73
|
+
const item: any = at;
|
|
74
|
+
return [
|
|
75
|
+
(read("staff")["posts"] ?? []).some((i: any) => String(i["role"]) === "manager" && (i["members"] === m || (Array.isArray(i["members"]) && i["members"].includes(m)))) ? "manager" : "",
|
|
76
|
+
(read("staff")["posts"] ?? []).some((i: any) => String(i["role"]) === "till" && (i["members"] === m || (Array.isArray(i["members"]) && i["members"].includes(m)))) ? "till" : "",
|
|
77
|
+
(read("staff")["posts"] ?? []).some((i: any) => String(i["role"]) === "kitchen" && (i["members"] === m || (Array.isArray(i["members"]) && i["members"].includes(m)))) ? "kitchen" : "",
|
|
78
|
+
(item?.["buyer"] === m || (Array.isArray(item?.["buyer"]) && item?.["buyer"].includes(m))) ? "buyer" : "",
|
|
79
|
+
(item?.["supplier"] === m || (Array.isArray(item?.["supplier"]) && item?.["supplier"].includes(m))) ? "supplier" : "",
|
|
80
|
+
].filter(Boolean);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Each person is sent only what they see of supplies (its definitions' owns): its query, by its member. */
|
|
84
|
+
export const sees = true;
|