Skip to content

Commit 098e59a

Browse files
ryanrasticlaude
andauthored
site: regenerate demo schema with Relation helpers (#96)
Ran `npm run migrate && tg generate` against the demo DDL — these files hadn't been regenerated since Relation.belongsTo/.has landed, so the playground was showing older output than `tg generate` produces today (and than the README and examples/basic already show). Two changes fall out of the regen: - relations collapse to Relation.has / Relation.belongsTo - columns lose the type-argument form: (Int8<1>).column() -> Int8.column() The `Relation` import is added by hand. `updateBlock` byte-preserves everything before @generated-start (deliberately — it protects hand-added imports like Connection in orders.ts), while buildImportLines only runs from emitNewFile. So an existing file gains Relation.* call sites without ever gaining the import, and codegen output doesn't compile until the import is added. Left as-is here; the generator needs a separate fix that merges managed symbols into the existing typegres specifier. Verified: astro check 0 errors, site vitest 17/17. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 810352c commit 098e59a

8 files changed

Lines changed: 72 additions & 72 deletions

File tree

site/src/demo/schema/customers.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { expose } from "typegres";
1+
import { Relation, expose } from "typegres";
22
import { Int8, Text } from "typegres/postgres";
33
import { db } from "../runtime";
44
import { Orders } from "./orders";
55
import { Organizations } from "./organizations";
66
export class Customers extends db.Table("customers", { live: true }) {
77
// @generated-start
8-
@expose() id = (Int8<1>).column({ nonNull: true, generated: true });
9-
@expose() organization_id = (Int8<1>).column({ nonNull: true });
10-
@expose() name = (Text<1>).column({ nonNull: true });
11-
@expose() email = (Text<1>).column({ nonNull: true });
8+
@expose() id = Int8.column({ nonNull: true, generated: true });
9+
@expose() organization_id = Int8.column({ nonNull: true });
10+
@expose() name = Text.column({ nonNull: true });
11+
@expose() email = Text.column({ nonNull: true });
1212
// relations
13-
@expose() organization() { return Organizations.scope(Customers.contextOf(this)).where(({ organizations }) => organizations.id["="](this.organization_id)).cardinality("one"); }
14-
@expose() orders() { return Orders.scope(Customers.contextOf(this)).where(({ orders }) => orders.customer_id["="](this.id)).cardinality("many"); }
13+
@expose() organization() { return Relation.belongsTo(this, Organizations, { id: this.organization_id }); }
14+
@expose() orders() { return Relation.has(this, Orders, { customer_id: this.id }); }
1515
// @generated-end
1616
}

site/src/demo/schema/inventory_positions.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Connection, sql, expose } from "typegres";
1+
import { Relation, Connection, sql, expose } from "typegres";
22
import { Int8, Text } from "typegres/postgres";
33
import { z } from "zod";
44
import { db } from "../runtime";
@@ -7,16 +7,16 @@ import { Organizations } from "./organizations";
77
import { OrderLines } from "./order_lines";
88
export class InventoryPositions extends db.Table("inventory_positions", { live: true }) {
99
// @generated-start
10-
@expose() id = (Int8<1>).column({ nonNull: true, generated: true });
11-
@expose() organization_id = (Int8<1>).column({ nonNull: true });
12-
@expose() location_id = (Int8<1>).column({ nonNull: true });
13-
@expose() sku = (Text<1>).column({ nonNull: true });
14-
@expose() on_hand = (Int8<1>).column({ nonNull: true, default: sql`0` });
15-
@expose() reserved = (Int8<1>).column({ nonNull: true, default: sql`0` });
10+
@expose() id = Int8.column({ nonNull: true, generated: true });
11+
@expose() organization_id = Int8.column({ nonNull: true });
12+
@expose() location_id = Int8.column({ nonNull: true });
13+
@expose() sku = Text.column({ nonNull: true });
14+
@expose() on_hand = Int8.column({ nonNull: true, default: sql`0` });
15+
@expose() reserved = Int8.column({ nonNull: true, default: sql`0` });
1616
// relations
17-
@expose() location() { return Locations.scope(InventoryPositions.contextOf(this)).where(({ locations }) => locations.id["="](this.location_id)).cardinality("one"); }
18-
@expose() organization() { return Organizations.scope(InventoryPositions.contextOf(this)).where(({ organizations }) => organizations.id["="](this.organization_id)).cardinality("one"); }
19-
@expose() order_lines() { return OrderLines.scope(InventoryPositions.contextOf(this)).where(({ order_lines }) => order_lines.inventory_position_id["="](this.id)).cardinality("many"); }
17+
@expose() location() { return Relation.belongsTo(this, Locations, { id: this.location_id }); }
18+
@expose() organization() { return Relation.belongsTo(this, Organizations, { id: this.organization_id }); }
19+
@expose() order_lines() { return Relation.has(this, OrderLines, { inventory_position_id: this.id }); }
2020
// @generated-end
2121

2222
// inventory_control-only: adjust on_hand by a signed delta.

site/src/demo/schema/locations.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { expose } from "typegres";
1+
import { Relation, expose } from "typegres";
22
import { Int8, Text } from "typegres/postgres";
33
import { db } from "../runtime";
44
import { InventoryPositions } from "./inventory_positions";
55
import { Organizations } from "./organizations";
66

77
export class Locations extends db.Table("locations", { live: true }) {
88
// @generated-start
9-
@expose() id = (Int8<1>).column({ nonNull: true, generated: true });
10-
@expose() organization_id = (Int8<1>).column({ nonNull: true });
11-
@expose() code = (Text<1>).column({ nonNull: true });
12-
@expose() name = (Text<1>).column({ nonNull: true });
9+
@expose() id = Int8.column({ nonNull: true, generated: true });
10+
@expose() organization_id = Int8.column({ nonNull: true });
11+
@expose() code = Text.column({ nonNull: true });
12+
@expose() name = Text.column({ nonNull: true });
1313
// relations
14-
@expose() organization() { return Organizations.scope(Locations.contextOf(this)).where(({ organizations }) => organizations.id["="](this.organization_id)).cardinality("one"); }
15-
@expose() inventory_positions() { return InventoryPositions.scope(Locations.contextOf(this)).where(({ inventory_positions }) => inventory_positions.location_id["="](this.id)).cardinality("many"); }
14+
@expose() organization() { return Relation.belongsTo(this, Organizations, { id: this.organization_id }); }
15+
@expose() inventory_positions() { return Relation.has(this, InventoryPositions, { location_id: this.id }); }
1616
// @generated-end
1717
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { expose } from "typegres";
1+
import { Relation, expose } from "typegres";
22
import { Int8, Text } from "typegres/postgres";
33
import { db } from "../runtime";
44
import { InventoryPositions } from "./inventory_positions";
55
import { Orders } from "./orders";
66

77
export class OrderLines extends db.Table("order_lines", { live: true }) {
88
// @generated-start
9-
@expose() id = (Int8<1>).column({ nonNull: true, generated: true });
10-
@expose() order_id = (Int8<1>).column({ nonNull: true });
11-
@expose() sku = (Text<1>).column({ nonNull: true });
12-
@expose() quantity = (Int8<1>).column({ nonNull: true });
13-
@expose() inventory_position_id = (Int8<0 | 1>).column();
9+
@expose() id = Int8.column({ nonNull: true, generated: true });
10+
@expose() order_id = Int8.column({ nonNull: true });
11+
@expose() sku = Text.column({ nonNull: true });
12+
@expose() quantity = Int8.column({ nonNull: true });
13+
@expose() inventory_position_id = Int8.column();
1414
// relations
15-
@expose() inventory_position() { return InventoryPositions.scope(OrderLines.contextOf(this)).where(({ inventory_positions }) => inventory_positions.id["="](this.inventory_position_id)).cardinality("maybe"); }
16-
@expose() order() { return Orders.scope(OrderLines.contextOf(this)).where(({ orders }) => orders.id["="](this.order_id)).cardinality("one"); }
15+
@expose() inventory_position() { return Relation.belongsTo(this, InventoryPositions, { id: this.inventory_position_id }, { card: "maybe" }); }
16+
@expose() order() { return Relation.belongsTo(this, Orders, { id: this.order_id }); }
1717
// @generated-end
1818
}

site/src/demo/schema/orders.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Connection, sql, expose } from "typegres";
1+
import { Relation, Connection, sql, expose } from "typegres";
22
import { Int8, Text, Timestamptz } from "typegres/postgres";
33
import { z } from "zod";
44
import { db } from "../runtime";
@@ -9,18 +9,18 @@ import { Shipments } from "./shipments";
99

1010
export class Orders extends db.Table("orders", { live: true }) {
1111
// @generated-start
12-
@expose() id = (Int8<1>).column({ nonNull: true, generated: true });
13-
@expose() organization_id = (Int8<1>).column({ nonNull: true });
14-
@expose() customer_id = (Int8<1>).column({ nonNull: true });
15-
@expose() status = (Text<1>).column({ nonNull: true, default: sql`'draft'::text` });
16-
@expose() priority = (Int8<1>).column({ nonNull: true, default: sql`0` });
17-
@expose() ship_by = (Timestamptz<0 | 1>).column();
18-
@expose() created_at = (Timestamptz<1>).column({ nonNull: true, default: sql`now()` });
12+
@expose() id = Int8.column({ nonNull: true, generated: true });
13+
@expose() organization_id = Int8.column({ nonNull: true });
14+
@expose() customer_id = Int8.column({ nonNull: true });
15+
@expose() status = Text.column({ nonNull: true, default: sql`'draft'::text` });
16+
@expose() priority = Int8.column({ nonNull: true, default: sql`0` });
17+
@expose() ship_by = Timestamptz.column();
18+
@expose() created_at = Timestamptz.column({ nonNull: true, default: sql`now()` });
1919
// relations
20-
@expose() customer() { return Customers.scope(Orders.contextOf(this)).where(({ customers }) => customers.id["="](this.customer_id)).cardinality("one"); }
21-
@expose() organization() { return Organizations.scope(Orders.contextOf(this)).where(({ organizations }) => organizations.id["="](this.organization_id)).cardinality("one"); }
22-
@expose() order_lines() { return OrderLines.scope(Orders.contextOf(this)).where(({ order_lines }) => order_lines.order_id["="](this.id)).cardinality("many"); }
23-
@expose() shipments() { return Shipments.scope(Orders.contextOf(this)).where(({ shipments }) => shipments.order_id["="](this.id)).cardinality("many"); }
20+
@expose() customer() { return Relation.belongsTo(this, Customers, { id: this.customer_id }); }
21+
@expose() organization() { return Relation.belongsTo(this, Organizations, { id: this.organization_id }); }
22+
@expose() order_lines() { return Relation.has(this, OrderLines, { order_id: this.id }); }
23+
@expose() shipments() { return Relation.has(this, Shipments, { order_id: this.id }); }
2424
// @generated-end
2525

2626
// ops_lead-only: advance this order one step along the lifecycle
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expose } from "typegres";
1+
import { Relation, expose } from "typegres";
22
import { Int8, Text } from "typegres/postgres";
33
import { db } from "../runtime";
44
import { Customers } from "./customers";
@@ -9,15 +9,15 @@ import { Orders } from "./orders";
99
import { Shipments } from "./shipments";
1010
export class Organizations extends db.Table("organizations", { live: true }) {
1111
// @generated-start
12-
@expose() id = (Int8<1>).column({ nonNull: true, generated: true });
13-
@expose() name = (Text<1>).column({ nonNull: true });
14-
@expose() slug = (Text<1>).column({ nonNull: true });
12+
@expose() id = Int8.column({ nonNull: true, generated: true });
13+
@expose() name = Text.column({ nonNull: true });
14+
@expose() slug = Text.column({ nonNull: true });
1515
// relations
16-
@expose() customers() { return Customers.scope(Organizations.contextOf(this)).where(({ customers }) => customers.organization_id["="](this.id)).cardinality("many"); }
17-
@expose() inventory_positions() { return InventoryPositions.scope(Organizations.contextOf(this)).where(({ inventory_positions }) => inventory_positions.organization_id["="](this.id)).cardinality("many"); }
18-
@expose() locations() { return Locations.scope(Organizations.contextOf(this)).where(({ locations }) => locations.organization_id["="](this.id)).cardinality("many"); }
19-
@expose() orders() { return Orders.scope(Organizations.contextOf(this)).where(({ orders }) => orders.organization_id["="](this.id)).cardinality("many"); }
20-
@expose() shipments() { return Shipments.scope(Organizations.contextOf(this)).where(({ shipments }) => shipments.organization_id["="](this.id)).cardinality("many"); }
21-
@expose() users() { return Users.scope(Organizations.contextOf(this)).where(({ users }) => users.organization_id["="](this.id)).cardinality("many"); }
16+
@expose() customers() { return Relation.has(this, Customers, { organization_id: this.id }); }
17+
@expose() inventory_positions() { return Relation.has(this, InventoryPositions, { organization_id: this.id }); }
18+
@expose() locations() { return Relation.has(this, Locations, { organization_id: this.id }); }
19+
@expose() orders() { return Relation.has(this, Orders, { organization_id: this.id }); }
20+
@expose() shipments() { return Relation.has(this, Shipments, { organization_id: this.id }); }
21+
@expose() users() { return Relation.has(this, Users, { organization_id: this.id }); }
2222
// @generated-end
2323
}

site/src/demo/schema/shipments.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { sql, expose } from "typegres";
1+
import { Relation, sql, expose } from "typegres";
22
import { Int8, Text, Timestamptz } from "typegres/postgres";
33
import { db } from "../runtime";
44
import { Orders } from "./orders";
55
import { Organizations } from "./organizations";
66
export class Shipments extends db.Table("shipments", { live: true }) {
77
// @generated-start
8-
@expose() id = (Int8<1>).column({ nonNull: true, generated: true });
9-
@expose() organization_id = (Int8<1>).column({ nonNull: true });
10-
@expose() order_id = (Int8<1>).column({ nonNull: true });
11-
@expose() carrier = (Text<1>).column({ nonNull: true });
12-
@expose() cutoff_at = (Timestamptz<1>).column({ nonNull: true });
13-
@expose() shipped_at = (Timestamptz<0 | 1>).column();
14-
@expose() status = (Text<1>).column({ nonNull: true, default: sql`'pending'::text` });
8+
@expose() id = Int8.column({ nonNull: true, generated: true });
9+
@expose() organization_id = Int8.column({ nonNull: true });
10+
@expose() order_id = Int8.column({ nonNull: true });
11+
@expose() carrier = Text.column({ nonNull: true });
12+
@expose() cutoff_at = Timestamptz.column({ nonNull: true });
13+
@expose() shipped_at = Timestamptz.column();
14+
@expose() status = Text.column({ nonNull: true, default: sql`'pending'::text` });
1515
// relations
16-
@expose() order() { return Orders.scope(Shipments.contextOf(this)).where(({ orders }) => orders.id["="](this.order_id)).cardinality("one"); }
17-
@expose() organization() { return Organizations.scope(Shipments.contextOf(this)).where(({ organizations }) => organizations.id["="](this.organization_id)).cardinality("one"); }
16+
@expose() order() { return Relation.belongsTo(this, Orders, { id: this.order_id }); }
17+
@expose() organization() { return Relation.belongsTo(this, Organizations, { id: this.organization_id }); }
1818
// @generated-end
1919
}

site/src/demo/schema/users.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { expose } from "typegres";
1+
import { Relation, expose } from "typegres";
22
import { Int8, Text } from "typegres/postgres";
33
import { db } from "../runtime";
44
import { Organizations } from "./organizations";
55
export class Users extends db.Table("users", { live: true }) {
66
// @generated-start
7-
@expose() id = (Int8<1>).column({ nonNull: true, generated: true });
8-
@expose() organization_id = (Int8<1>).column({ nonNull: true });
9-
@expose() name = (Text<1>).column({ nonNull: true });
10-
@expose() email = (Text<1>).column({ nonNull: true });
11-
@expose() role = (Text<1>).column({ nonNull: true });
12-
@expose() token = (Text<1>).column({ nonNull: true });
7+
@expose() id = Int8.column({ nonNull: true, generated: true });
8+
@expose() organization_id = Int8.column({ nonNull: true });
9+
@expose() name = Text.column({ nonNull: true });
10+
@expose() email = Text.column({ nonNull: true });
11+
@expose() role = Text.column({ nonNull: true });
12+
@expose() token = Text.column({ nonNull: true });
1313
// relations
14-
@expose() organization() { return Organizations.scope(Users.contextOf(this)).where(({ organizations }) => organizations.id["="](this.organization_id)).cardinality("one"); }
14+
@expose() organization() { return Relation.belongsTo(this, Organizations, { id: this.organization_id }); }
1515
// @generated-end
1616
}

0 commit comments

Comments
 (0)