@biltme/backend 0.5.2 → 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
@@ -39,6 +39,67 @@ const { data, error } = await bilt.auth.signInWithPassword({ email, password })
39
39
  const { data: rows } = await bilt.from('notes').select('*')
40
40
  ```
41
41
 
42
+ ## Typed queries
43
+
44
+ `createClient` reads the schema off an empty `Database` interface this package
45
+ exports. Augment it and the columns of every table in the file are typed:
46
+
47
+ ```ts
48
+ declare module '@biltme/backend' {
49
+ interface Database {
50
+ public: {
51
+ Tables: {
52
+ notes: {
53
+ Row: { id: string; title: string; tags: string[] | null }
54
+ Insert: { id?: string; title: string; tags?: string[] | null }
55
+ Update: { id?: string; title?: string; tags?: string[] | null }
56
+ Relationships: []
57
+ }
58
+ }
59
+ Views: Record<never, never>
60
+ Functions: Record<never, never>
61
+ Enums: Record<never, never>
62
+ CompositeTypes: Record<never, never>
63
+ }
64
+ }
65
+ }
66
+ ```
67
+
68
+ You do not write that by hand. bilt-cloud regenerates it from the live schema on
69
+ every `bilt_apply_sql`, and the agent drops it into the project. Because it is a
70
+ module augmentation rather than `createClient<Database>`, there is no generic to
71
+ thread through and nothing to keep in sync at the call site.
72
+
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.
77
+
78
+ Leave `Database` unaugmented and rows come back `any`, which is what 0.5.x did.
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
+
42
103
  ## Calling functions
43
104
 
44
105
  ```ts
@@ -124,7 +185,9 @@ const bilt = createClient(url, key, {
124
185
 
125
186
  ## What this package adds vs `@supabase/supabase-js`
126
187
 
127
- - `createClient` is re-exported verbatim — same positional signature.
188
+ - `createClient` *is* supabase-js's `createClient`, re-typed so that a call with no type
189
+ arguments defaults its schema to the augmentable `Database` interface above instead of `any`.
190
+ - `Database`, the empty interface a project augments to type its schema.
128
191
  - `asyncStorage(impl)` factory wrapping `@react-native-async-storage/async-storage` to the supabase-js storage shape.
129
192
  - `webStorage()` and `memoryStorage()` for completeness.
130
193
  - `FunctionsHttpError`, `FunctionsRelayError`, `FunctionsFetchError`, `FunctionRegion` re-exports for clean `catch` and option typing.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,98 @@
1
- export { createClient } from '@supabase/supabase-js';
1
+ import { createClient as createSupabaseClient } from '@supabase/supabase-js';
2
+ /**
3
+ * Schema of the project's database, in postgrest-js's `Database` shape.
4
+ *
5
+ * Empty here; projects fill it by augmenting this module:
6
+ *
7
+ * ```ts
8
+ * declare module '@biltme/backend' {
9
+ * interface Database {
10
+ * public: { Tables: { … }; Views: { … }; Functions: { … } }
11
+ * }
12
+ * }
13
+ * ```
14
+ *
15
+ * bilt-cloud writes that file after every schema change, so there is nothing to
16
+ * pass to {@link createClient}.
17
+ */
18
+ export interface Database {
19
+ }
20
+ /** {@link Database} once augmented, `any` while empty — which is the untyped behaviour of 0.5.x. */
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;
84
+ /**
85
+ * Creates a Bilt client. supabase-js's `createClient` with two changes: called
86
+ * with no type arguments, the schema is {@link BiltDatabase} instead of `any`,
87
+ * and a missing or malformed URL or key names Bilt Cloud rather than Supabase.
88
+ *
89
+ * The second member of the intersection is supabase-js's own signature. An
90
+ * intersection of function types is an overload set, so a bare call takes the
91
+ * first member and any call with explicit type arguments falls through to the
92
+ * second. Restating the parameter list here instead would have to track
93
+ * supabase-js's defaults by hand across a caret range.
94
+ */
95
+ export declare const createClient: typeof createSupabaseClient<BiltDatabase> & typeof createSupabaseClient;
2
96
  export { webStorage, memoryStorage, asyncStorage } from './storage';
3
97
  export type { StorageAdapter } from './storage';
4
98
  export { FunctionsHttpError, FunctionsRelayError, FunctionsFetchError, FunctionRegion } from '@supabase/supabase-js';
package/dist/index.js CHANGED
@@ -11795,6 +11795,7 @@ function shouldShowDeprecationWarning() {
11795
11795
  }
11796
11796
  if (shouldShowDeprecationWarning())
11797
11797
  console.warn("⚠️ Node.js 20 and below are deprecated and will no longer be supported in future versions of @supabase/supabase-js. Please upgrade to Node.js 22 or later. For more information, visit: https://github.com/orgs/supabase/discussions/45715");
11798
+
11798
11799
  // src/storage/memory.ts
11799
11800
  function memoryStorage() {
11800
11801
  const store = new Map;
@@ -11858,6 +11859,28 @@ function asyncStorage(impl) {
11858
11859
  removeItem: (key) => impl.removeItem(key)
11859
11860
  };
11860
11861
  }
11862
+ // src/index.ts
11863
+ function assertCredentials(url, key) {
11864
+ const trimmed = url?.trim();
11865
+ if (!trimmed) {
11866
+ throw new Error("Bilt Cloud URL is required. Pass process.env.EXPO_PUBLIC_BILT_URL, which is injected into the app at runtime.");
11867
+ }
11868
+ if (!/^https?:\/\//i.test(trimmed)) {
11869
+ throw new Error(`Invalid Bilt Cloud URL: must be an http(s) URL, like https://<project>.cloud.bilt.me. Got: ${url}`);
11870
+ }
11871
+ try {
11872
+ new URL(trimmed);
11873
+ } catch {
11874
+ throw new Error(`Invalid Bilt Cloud URL: ${url} is malformed.`);
11875
+ }
11876
+ if (!key) {
11877
+ throw new Error("Bilt Cloud anon key is required. Pass process.env.EXPO_PUBLIC_BILT_ANON_KEY, which is injected into the app at runtime.");
11878
+ }
11879
+ }
11880
+ var createClient2 = (url, key, options) => {
11881
+ assertCredentials(url, key);
11882
+ return createClient(url, key, options);
11883
+ };
11861
11884
  var export_FunctionsRelayError = import_functions_js.FunctionsRelayError;
11862
11885
  var export_FunctionsHttpError = import_functions_js.FunctionsHttpError;
11863
11886
  var export_FunctionsFetchError = import_functions_js.FunctionsFetchError;
@@ -11866,7 +11889,7 @@ var export_FunctionRegion = import_functions_js.FunctionRegion;
11866
11889
  export {
11867
11890
  webStorage,
11868
11891
  memoryStorage,
11869
- createClient,
11892
+ createClient2 as createClient,
11870
11893
  asyncStorage,
11871
11894
  StorageApiError,
11872
11895
  export_FunctionsRelayError as FunctionsRelayError,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@biltme/backend",
3
- "version": "0.5.2",
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",