diff --git a/site/src/demo/schema/customers.ts b/site/src/demo/schema/customers.ts index f502bb06..60e88dfd 100644 --- a/site/src/demo/schema/customers.ts +++ b/site/src/demo/schema/customers.ts @@ -1,16 +1,16 @@ -import { expose } from "typegres"; +import { Relation, expose } from "typegres"; import { Int8, Text } from "typegres/postgres"; import { db } from "../runtime"; import { Orders } from "./orders"; import { Organizations } from "./organizations"; export class Customers extends db.Table("customers", { live: true }) { // @generated-start - @expose() id = (Int8<1>).column({ nonNull: true, generated: true }); - @expose() organization_id = (Int8<1>).column({ nonNull: true }); - @expose() name = (Text<1>).column({ nonNull: true }); - @expose() email = (Text<1>).column({ nonNull: true }); + @expose() id = Int8.column({ nonNull: true, generated: true }); + @expose() organization_id = Int8.column({ nonNull: true }); + @expose() name = Text.column({ nonNull: true }); + @expose() email = Text.column({ nonNull: true }); // relations - @expose() organization() { return Organizations.scope(Customers.contextOf(this)).where(({ organizations }) => organizations.id["="](this.organization_id)).cardinality("one"); } - @expose() orders() { return Orders.scope(Customers.contextOf(this)).where(({ orders }) => orders.customer_id["="](this.id)).cardinality("many"); } + @expose() organization() { return Relation.belongsTo(this, Organizations, { id: this.organization_id }); } + @expose() orders() { return Relation.has(this, Orders, { customer_id: this.id }); } // @generated-end } diff --git a/site/src/demo/schema/inventory_positions.ts b/site/src/demo/schema/inventory_positions.ts index 12566ed2..71d3936e 100644 --- a/site/src/demo/schema/inventory_positions.ts +++ b/site/src/demo/schema/inventory_positions.ts @@ -1,4 +1,4 @@ -import { Connection, sql, expose } from "typegres"; +import { Relation, Connection, sql, expose } from "typegres"; import { Int8, Text } from "typegres/postgres"; import { z } from "zod"; import { db } from "../runtime"; @@ -7,16 +7,16 @@ import { Organizations } from "./organizations"; import { OrderLines } from "./order_lines"; export class InventoryPositions extends db.Table("inventory_positions", { live: true }) { // @generated-start - @expose() id = (Int8<1>).column({ nonNull: true, generated: true }); - @expose() organization_id = (Int8<1>).column({ nonNull: true }); - @expose() location_id = (Int8<1>).column({ nonNull: true }); - @expose() sku = (Text<1>).column({ nonNull: true }); - @expose() on_hand = (Int8<1>).column({ nonNull: true, default: sql`0` }); - @expose() reserved = (Int8<1>).column({ nonNull: true, default: sql`0` }); + @expose() id = Int8.column({ nonNull: true, generated: true }); + @expose() organization_id = Int8.column({ nonNull: true }); + @expose() location_id = Int8.column({ nonNull: true }); + @expose() sku = Text.column({ nonNull: true }); + @expose() on_hand = Int8.column({ nonNull: true, default: sql`0` }); + @expose() reserved = Int8.column({ nonNull: true, default: sql`0` }); // relations - @expose() location() { return Locations.scope(InventoryPositions.contextOf(this)).where(({ locations }) => locations.id["="](this.location_id)).cardinality("one"); } - @expose() organization() { return Organizations.scope(InventoryPositions.contextOf(this)).where(({ organizations }) => organizations.id["="](this.organization_id)).cardinality("one"); } - @expose() order_lines() { return OrderLines.scope(InventoryPositions.contextOf(this)).where(({ order_lines }) => order_lines.inventory_position_id["="](this.id)).cardinality("many"); } + @expose() location() { return Relation.belongsTo(this, Locations, { id: this.location_id }); } + @expose() organization() { return Relation.belongsTo(this, Organizations, { id: this.organization_id }); } + @expose() order_lines() { return Relation.has(this, OrderLines, { inventory_position_id: this.id }); } // @generated-end // inventory_control-only: adjust on_hand by a signed delta. diff --git a/site/src/demo/schema/locations.ts b/site/src/demo/schema/locations.ts index 5a93911d..3878fd49 100644 --- a/site/src/demo/schema/locations.ts +++ b/site/src/demo/schema/locations.ts @@ -1,4 +1,4 @@ -import { expose } from "typegres"; +import { Relation, expose } from "typegres"; import { Int8, Text } from "typegres/postgres"; import { db } from "../runtime"; import { InventoryPositions } from "./inventory_positions"; @@ -6,12 +6,12 @@ import { Organizations } from "./organizations"; export class Locations extends db.Table("locations", { live: true }) { // @generated-start - @expose() id = (Int8<1>).column({ nonNull: true, generated: true }); - @expose() organization_id = (Int8<1>).column({ nonNull: true }); - @expose() code = (Text<1>).column({ nonNull: true }); - @expose() name = (Text<1>).column({ nonNull: true }); + @expose() id = Int8.column({ nonNull: true, generated: true }); + @expose() organization_id = Int8.column({ nonNull: true }); + @expose() code = Text.column({ nonNull: true }); + @expose() name = Text.column({ nonNull: true }); // relations - @expose() organization() { return Organizations.scope(Locations.contextOf(this)).where(({ organizations }) => organizations.id["="](this.organization_id)).cardinality("one"); } - @expose() inventory_positions() { return InventoryPositions.scope(Locations.contextOf(this)).where(({ inventory_positions }) => inventory_positions.location_id["="](this.id)).cardinality("many"); } + @expose() organization() { return Relation.belongsTo(this, Organizations, { id: this.organization_id }); } + @expose() inventory_positions() { return Relation.has(this, InventoryPositions, { location_id: this.id }); } // @generated-end } diff --git a/site/src/demo/schema/order_lines.ts b/site/src/demo/schema/order_lines.ts index dbf8f268..4f60272d 100644 --- a/site/src/demo/schema/order_lines.ts +++ b/site/src/demo/schema/order_lines.ts @@ -1,4 +1,4 @@ -import { expose } from "typegres"; +import { Relation, expose } from "typegres"; import { Int8, Text } from "typegres/postgres"; import { db } from "../runtime"; import { InventoryPositions } from "./inventory_positions"; @@ -6,13 +6,13 @@ import { Orders } from "./orders"; export class OrderLines extends db.Table("order_lines", { live: true }) { // @generated-start - @expose() id = (Int8<1>).column({ nonNull: true, generated: true }); - @expose() order_id = (Int8<1>).column({ nonNull: true }); - @expose() sku = (Text<1>).column({ nonNull: true }); - @expose() quantity = (Int8<1>).column({ nonNull: true }); - @expose() inventory_position_id = (Int8<0 | 1>).column(); + @expose() id = Int8.column({ nonNull: true, generated: true }); + @expose() order_id = Int8.column({ nonNull: true }); + @expose() sku = Text.column({ nonNull: true }); + @expose() quantity = Int8.column({ nonNull: true }); + @expose() inventory_position_id = Int8.column(); // relations - @expose() inventory_position() { return InventoryPositions.scope(OrderLines.contextOf(this)).where(({ inventory_positions }) => inventory_positions.id["="](this.inventory_position_id)).cardinality("maybe"); } - @expose() order() { return Orders.scope(OrderLines.contextOf(this)).where(({ orders }) => orders.id["="](this.order_id)).cardinality("one"); } + @expose() inventory_position() { return Relation.belongsTo(this, InventoryPositions, { id: this.inventory_position_id }, { card: "maybe" }); } + @expose() order() { return Relation.belongsTo(this, Orders, { id: this.order_id }); } // @generated-end } diff --git a/site/src/demo/schema/orders.ts b/site/src/demo/schema/orders.ts index 01e507bf..fa9953e4 100644 --- a/site/src/demo/schema/orders.ts +++ b/site/src/demo/schema/orders.ts @@ -1,4 +1,4 @@ -import { Connection, sql, expose } from "typegres"; +import { Relation, Connection, sql, expose } from "typegres"; import { Int8, Text, Timestamptz } from "typegres/postgres"; import { z } from "zod"; import { db } from "../runtime"; @@ -9,18 +9,18 @@ import { Shipments } from "./shipments"; export class Orders extends db.Table("orders", { live: true }) { // @generated-start - @expose() id = (Int8<1>).column({ nonNull: true, generated: true }); - @expose() organization_id = (Int8<1>).column({ nonNull: true }); - @expose() customer_id = (Int8<1>).column({ nonNull: true }); - @expose() status = (Text<1>).column({ nonNull: true, default: sql`'draft'::text` }); - @expose() priority = (Int8<1>).column({ nonNull: true, default: sql`0` }); - @expose() ship_by = (Timestamptz<0 | 1>).column(); - @expose() created_at = (Timestamptz<1>).column({ nonNull: true, default: sql`now()` }); + @expose() id = Int8.column({ nonNull: true, generated: true }); + @expose() organization_id = Int8.column({ nonNull: true }); + @expose() customer_id = Int8.column({ nonNull: true }); + @expose() status = Text.column({ nonNull: true, default: sql`'draft'::text` }); + @expose() priority = Int8.column({ nonNull: true, default: sql`0` }); + @expose() ship_by = Timestamptz.column(); + @expose() created_at = Timestamptz.column({ nonNull: true, default: sql`now()` }); // relations - @expose() customer() { return Customers.scope(Orders.contextOf(this)).where(({ customers }) => customers.id["="](this.customer_id)).cardinality("one"); } - @expose() organization() { return Organizations.scope(Orders.contextOf(this)).where(({ organizations }) => organizations.id["="](this.organization_id)).cardinality("one"); } - @expose() order_lines() { return OrderLines.scope(Orders.contextOf(this)).where(({ order_lines }) => order_lines.order_id["="](this.id)).cardinality("many"); } - @expose() shipments() { return Shipments.scope(Orders.contextOf(this)).where(({ shipments }) => shipments.order_id["="](this.id)).cardinality("many"); } + @expose() customer() { return Relation.belongsTo(this, Customers, { id: this.customer_id }); } + @expose() organization() { return Relation.belongsTo(this, Organizations, { id: this.organization_id }); } + @expose() order_lines() { return Relation.has(this, OrderLines, { order_id: this.id }); } + @expose() shipments() { return Relation.has(this, Shipments, { order_id: this.id }); } // @generated-end // ops_lead-only: advance this order one step along the lifecycle diff --git a/site/src/demo/schema/organizations.ts b/site/src/demo/schema/organizations.ts index 23a2856e..edfe3c1b 100644 --- a/site/src/demo/schema/organizations.ts +++ b/site/src/demo/schema/organizations.ts @@ -1,4 +1,4 @@ -import { expose } from "typegres"; +import { Relation, expose } from "typegres"; import { Int8, Text } from "typegres/postgres"; import { db } from "../runtime"; import { Customers } from "./customers"; @@ -9,15 +9,15 @@ import { Orders } from "./orders"; import { Shipments } from "./shipments"; export class Organizations extends db.Table("organizations", { live: true }) { // @generated-start - @expose() id = (Int8<1>).column({ nonNull: true, generated: true }); - @expose() name = (Text<1>).column({ nonNull: true }); - @expose() slug = (Text<1>).column({ nonNull: true }); + @expose() id = Int8.column({ nonNull: true, generated: true }); + @expose() name = Text.column({ nonNull: true }); + @expose() slug = Text.column({ nonNull: true }); // relations - @expose() customers() { return Customers.scope(Organizations.contextOf(this)).where(({ customers }) => customers.organization_id["="](this.id)).cardinality("many"); } - @expose() inventory_positions() { return InventoryPositions.scope(Organizations.contextOf(this)).where(({ inventory_positions }) => inventory_positions.organization_id["="](this.id)).cardinality("many"); } - @expose() locations() { return Locations.scope(Organizations.contextOf(this)).where(({ locations }) => locations.organization_id["="](this.id)).cardinality("many"); } - @expose() orders() { return Orders.scope(Organizations.contextOf(this)).where(({ orders }) => orders.organization_id["="](this.id)).cardinality("many"); } - @expose() shipments() { return Shipments.scope(Organizations.contextOf(this)).where(({ shipments }) => shipments.organization_id["="](this.id)).cardinality("many"); } - @expose() users() { return Users.scope(Organizations.contextOf(this)).where(({ users }) => users.organization_id["="](this.id)).cardinality("many"); } + @expose() customers() { return Relation.has(this, Customers, { organization_id: this.id }); } + @expose() inventory_positions() { return Relation.has(this, InventoryPositions, { organization_id: this.id }); } + @expose() locations() { return Relation.has(this, Locations, { organization_id: this.id }); } + @expose() orders() { return Relation.has(this, Orders, { organization_id: this.id }); } + @expose() shipments() { return Relation.has(this, Shipments, { organization_id: this.id }); } + @expose() users() { return Relation.has(this, Users, { organization_id: this.id }); } // @generated-end } diff --git a/site/src/demo/schema/shipments.ts b/site/src/demo/schema/shipments.ts index 0ae2af4d..c26deb4d 100644 --- a/site/src/demo/schema/shipments.ts +++ b/site/src/demo/schema/shipments.ts @@ -1,19 +1,19 @@ -import { sql, expose } from "typegres"; +import { Relation, sql, expose } from "typegres"; import { Int8, Text, Timestamptz } from "typegres/postgres"; import { db } from "../runtime"; import { Orders } from "./orders"; import { Organizations } from "./organizations"; export class Shipments extends db.Table("shipments", { live: true }) { // @generated-start - @expose() id = (Int8<1>).column({ nonNull: true, generated: true }); - @expose() organization_id = (Int8<1>).column({ nonNull: true }); - @expose() order_id = (Int8<1>).column({ nonNull: true }); - @expose() carrier = (Text<1>).column({ nonNull: true }); - @expose() cutoff_at = (Timestamptz<1>).column({ nonNull: true }); - @expose() shipped_at = (Timestamptz<0 | 1>).column(); - @expose() status = (Text<1>).column({ nonNull: true, default: sql`'pending'::text` }); + @expose() id = Int8.column({ nonNull: true, generated: true }); + @expose() organization_id = Int8.column({ nonNull: true }); + @expose() order_id = Int8.column({ nonNull: true }); + @expose() carrier = Text.column({ nonNull: true }); + @expose() cutoff_at = Timestamptz.column({ nonNull: true }); + @expose() shipped_at = Timestamptz.column(); + @expose() status = Text.column({ nonNull: true, default: sql`'pending'::text` }); // relations - @expose() order() { return Orders.scope(Shipments.contextOf(this)).where(({ orders }) => orders.id["="](this.order_id)).cardinality("one"); } - @expose() organization() { return Organizations.scope(Shipments.contextOf(this)).where(({ organizations }) => organizations.id["="](this.organization_id)).cardinality("one"); } + @expose() order() { return Relation.belongsTo(this, Orders, { id: this.order_id }); } + @expose() organization() { return Relation.belongsTo(this, Organizations, { id: this.organization_id }); } // @generated-end } diff --git a/site/src/demo/schema/users.ts b/site/src/demo/schema/users.ts index 7c9c1321..e60bd9b9 100644 --- a/site/src/demo/schema/users.ts +++ b/site/src/demo/schema/users.ts @@ -1,16 +1,16 @@ -import { expose } from "typegres"; +import { Relation, expose } from "typegres"; import { Int8, Text } from "typegres/postgres"; import { db } from "../runtime"; import { Organizations } from "./organizations"; export class Users extends db.Table("users", { live: true }) { // @generated-start - @expose() id = (Int8<1>).column({ nonNull: true, generated: true }); - @expose() organization_id = (Int8<1>).column({ nonNull: true }); - @expose() name = (Text<1>).column({ nonNull: true }); - @expose() email = (Text<1>).column({ nonNull: true }); - @expose() role = (Text<1>).column({ nonNull: true }); - @expose() token = (Text<1>).column({ nonNull: true }); + @expose() id = Int8.column({ nonNull: true, generated: true }); + @expose() organization_id = Int8.column({ nonNull: true }); + @expose() name = Text.column({ nonNull: true }); + @expose() email = Text.column({ nonNull: true }); + @expose() role = Text.column({ nonNull: true }); + @expose() token = Text.column({ nonNull: true }); // relations - @expose() organization() { return Organizations.scope(Users.contextOf(this)).where(({ organizations }) => organizations.id["="](this.organization_id)).cardinality("one"); } + @expose() organization() { return Relation.belongsTo(this, Organizations, { id: this.organization_id }); } // @generated-end }