@biltme/backend 0.6.0 → 0.7.0

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/README.md CHANGED
@@ -70,14 +70,36 @@ every `bilt_apply_sql`, and the agent drops it into the project. Because it is a
70
70
  module augmentation rather than `createClient<Database>`, there is no generic to
71
71
  thread through and nothing to keep in sync at the call site.
72
72
 
73
- What bilt-cloud emits differs from the snippet above in one way that matters:
74
- its `Views` and `Functions` are open index signatures rather than closed. So a
75
- column that does not exist on a known table fails to compile, while a view, an
76
- `rpc()` call, or a table created since the last render falls through to `any`
77
- instead of erroring. A stale file never blocks a build.
73
+ Every key bilt-cloud emits is closed. A table, view or column that is not in
74
+ the file fails to compile, and so does an `rpc()` call to a function the file
75
+ does not list or with an argument it does not have. A stale file therefore
76
+ blocks a build until the next schema change regenerates it.
78
77
 
79
78
  Leave `Database` unaugmented and rows come back `any`, which is what 0.5.x did.
80
79
 
80
+ ### Naming a row
81
+
82
+ Four helpers resolve over that same augmented `Database`, so a row can cross a
83
+ component boundary without being restated:
84
+
85
+ ```ts
86
+ import type { Tables, TablesInsert, TablesUpdate, Enums } from '@biltme/backend'
87
+
88
+ function MessageCard({ message }: { message: Tables<'messages'> }) { … }
89
+ function draft(): TablesInsert<'messages'> { … }
90
+ function patch(): TablesUpdate<'messages'> { … }
91
+ function badge(status: Enums<'order_status'>) { … }
92
+ ```
93
+
94
+ `Tables` spans tables and views and gives the same type `select()` returns per
95
+ element. `TablesInsert` and `TablesUpdate` take tables only, as in supabase-js,
96
+ so a writable view has a row name but no payload name. `Enums` resolves to the
97
+ union bilt-cloud renders under `Database['public']['Enums']`, so a `switch` over
98
+ it is checked for exhaustiveness.
99
+
100
+ Before the first schema change there are no names, and passing one is a compile
101
+ error rather than `any`.
102
+
81
103
  ## Calling functions
82
104
 
83
105
  ```ts
package/dist/index.d.ts CHANGED
@@ -19,6 +19,68 @@ export interface Database {
19
19
  }
20
20
  /** {@link Database} once augmented, `any` while empty — which is the untyped behaviour of 0.5.x. */
21
21
  type BiltDatabase = [keyof Database] extends [never] ? any : Database;
22
+ type PublicEnums = Database extends {
23
+ public: {
24
+ Enums: infer E;
25
+ };
26
+ } ? E : {
27
+ [_ in never]: never;
28
+ };
29
+ type PublicTables = Database extends {
30
+ public: {
31
+ Tables: infer T;
32
+ };
33
+ } ? T : {
34
+ [_ in never]: never;
35
+ };
36
+ type PublicViews = Database extends {
37
+ public: {
38
+ Views: infer V;
39
+ };
40
+ } ? V : {
41
+ [_ in never]: never;
42
+ };
43
+ type PublicRelations = PublicTables & PublicViews;
44
+ /**
45
+ * The labels of a Postgres enum in the project's `public` schema, by name.
46
+ *
47
+ * ```ts
48
+ * import type { Enums } from '@biltme/backend'
49
+ * function badge(status: Enums<'order_status'>) { … }
50
+ * ```
51
+ *
52
+ * Resolves to the union bilt-cloud renders under `Database['public']['Enums']`,
53
+ * so a `switch` over it is checked for exhaustiveness. Before the first schema
54
+ * change there are no names, and any argument is a compile error.
55
+ */
56
+ export type Enums<Name extends keyof PublicEnums> = PublicEnums[Name];
57
+ /**
58
+ * A row of a table or view in the project's `public` schema, by name.
59
+ *
60
+ * ```ts
61
+ * import type { Tables } from '@biltme/backend'
62
+ * function MessageCard({ message }: { message: Tables<'messages'> }) { … }
63
+ * ```
64
+ *
65
+ * The same type `bilt.from(name).select()` returns per element, so a row can
66
+ * cross a component boundary without being restated. Before the first schema
67
+ * change there are no names, and any argument is a compile error.
68
+ */
69
+ export type Tables<Name extends keyof PublicRelations> = PublicRelations[Name] extends {
70
+ Row: infer R;
71
+ } ? R : never;
72
+ /**
73
+ * The payload `bilt.from(name).insert()` accepts, for a table in the `public`
74
+ * schema. Keyed on tables alone, as in supabase-js: a writable view has a
75
+ * {@link Tables} row but no name for its insert payload.
76
+ */
77
+ export type TablesInsert<Name extends keyof PublicTables> = PublicTables[Name] extends {
78
+ Insert: infer I;
79
+ } ? I : never;
80
+ /** The payload `bilt.from(name).update()` accepts. Tables only, like {@link TablesInsert}. */
81
+ export type TablesUpdate<Name extends keyof PublicTables> = PublicTables[Name] extends {
82
+ Update: infer U;
83
+ } ? U : never;
22
84
  /**
23
85
  * Creates a Bilt client. supabase-js's `createClient` with two changes: called
24
86
  * with no type arguments, the schema is {@link BiltDatabase} instead of `any`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@biltme/backend",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Bilt backend SDK — thin shim over @supabase/supabase-js for bilt-cloud projects",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",