@supabase/postgrest-typegen 0.0.1

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.
Files changed (78) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +133 -0
  3. package/dist/generation/constants.d.ts +9 -0
  4. package/dist/generation/constants.js +10 -0
  5. package/dist/generation/go.d.ts +6 -0
  6. package/dist/generation/go.js +233 -0
  7. package/dist/generation/index.d.ts +5 -0
  8. package/dist/generation/index.js +5 -0
  9. package/dist/generation/python.d.ts +6 -0
  10. package/dist/generation/python.js +339 -0
  11. package/dist/generation/swift.d.ts +11 -0
  12. package/dist/generation/swift.js +309 -0
  13. package/dist/generation/typescript.d.ts +32 -0
  14. package/dist/generation/typescript.js +796 -0
  15. package/dist/index.d.ts +4 -0
  16. package/dist/index.js +4 -0
  17. package/dist/introspection/index.d.ts +27 -0
  18. package/dist/introspection/index.js +70 -0
  19. package/dist/introspection/normalize.d.ts +31 -0
  20. package/dist/introspection/normalize.js +65 -0
  21. package/dist/introspection/relationships.d.ts +47 -0
  22. package/dist/introspection/relationships.js +85 -0
  23. package/dist/introspection/sql/columns.sql.d.ts +7 -0
  24. package/dist/introspection/sql/columns.sql.js +122 -0
  25. package/dist/introspection/sql/common.d.ts +16 -0
  26. package/dist/introspection/sql/common.js +1 -0
  27. package/dist/introspection/sql/foreign_tables.sql.d.ts +6 -0
  28. package/dist/introspection/sql/foreign_tables.sql.js +18 -0
  29. package/dist/introspection/sql/functions.sql.d.ts +5 -0
  30. package/dist/introspection/sql/functions.sql.js +148 -0
  31. package/dist/introspection/sql/helpers.d.ts +12 -0
  32. package/dist/introspection/sql/helpers.js +28 -0
  33. package/dist/introspection/sql/materialized_views.sql.d.ts +4 -0
  34. package/dist/introspection/sql/materialized_views.sql.js +19 -0
  35. package/dist/introspection/sql/primary_keys.sql.d.ts +4 -0
  36. package/dist/introspection/sql/primary_keys.sql.js +32 -0
  37. package/dist/introspection/sql/schemas.sql.d.ts +6 -0
  38. package/dist/introspection/sql/schemas.sql.js +24 -0
  39. package/dist/introspection/sql/table.sql.d.ts +4 -0
  40. package/dist/introspection/sql/table.sql.js +41 -0
  41. package/dist/introspection/sql/table_relationships.sql.d.ts +2 -0
  42. package/dist/introspection/sql/table_relationships.sql.js +49 -0
  43. package/dist/introspection/sql/types.sql.d.ts +5 -0
  44. package/dist/introspection/sql/types.sql.js +65 -0
  45. package/dist/introspection/sql/views.sql.d.ts +4 -0
  46. package/dist/introspection/sql/views.sql.js +20 -0
  47. package/dist/introspection/sql/views_key_dependencies.sql.d.ts +2 -0
  48. package/dist/introspection/sql/views_key_dependencies.sql.js +195 -0
  49. package/dist/sort.d.ts +35 -0
  50. package/dist/sort.js +48 -0
  51. package/dist/types.d.ts +372 -0
  52. package/dist/types.js +207 -0
  53. package/package.json +87 -0
  54. package/src/generation/constants.ts +11 -0
  55. package/src/generation/go.ts +356 -0
  56. package/src/generation/index.ts +13 -0
  57. package/src/generation/python.ts +461 -0
  58. package/src/generation/swift.ts +481 -0
  59. package/src/generation/typescript.ts +1144 -0
  60. package/src/index.ts +4 -0
  61. package/src/introspection/index.ts +124 -0
  62. package/src/introspection/normalize.ts +71 -0
  63. package/src/introspection/relationships.ts +173 -0
  64. package/src/introspection/sql/columns.sql.ts +131 -0
  65. package/src/introspection/sql/common.ts +18 -0
  66. package/src/introspection/sql/foreign_tables.sql.ts +26 -0
  67. package/src/introspection/sql/functions.sql.ts +159 -0
  68. package/src/introspection/sql/helpers.ts +33 -0
  69. package/src/introspection/sql/materialized_views.sql.ts +25 -0
  70. package/src/introspection/sql/primary_keys.sql.ts +38 -0
  71. package/src/introspection/sql/schemas.sql.ts +32 -0
  72. package/src/introspection/sql/table.sql.ts +47 -0
  73. package/src/introspection/sql/table_relationships.sql.ts +53 -0
  74. package/src/introspection/sql/types.sql.ts +74 -0
  75. package/src/introspection/sql/views.sql.ts +26 -0
  76. package/src/introspection/sql/views_key_dependencies.sql.ts +199 -0
  77. package/src/sort.ts +102 -0
  78. package/src/types.ts +237 -0
@@ -0,0 +1,372 @@
1
+ declare const postgresColumnSchema: import("arktype/internal/variants/object.ts").ObjectType<{
2
+ table_id: number;
3
+ schema: string;
4
+ table: string;
5
+ id: string;
6
+ ordinal_position: number;
7
+ name: string;
8
+ default_value: unknown;
9
+ data_type: string;
10
+ format: string;
11
+ type_schema: string;
12
+ is_identity: boolean;
13
+ identity_generation: "ALWAYS" | "BY DEFAULT" | null;
14
+ is_generated: boolean;
15
+ is_nullable: boolean;
16
+ is_updatable: boolean;
17
+ is_unique: boolean;
18
+ enums: string[];
19
+ check: string | null;
20
+ comment: string | null;
21
+ }, {}>;
22
+ export type PostgresColumn = typeof postgresColumnSchema.infer;
23
+ declare const postgresForeignTableSchema: import("arktype/internal/variants/object.ts").ObjectType<{
24
+ id: number;
25
+ schema: string;
26
+ name: string;
27
+ comment: string | null;
28
+ columns?: {
29
+ table_id: number;
30
+ schema: string;
31
+ table: string;
32
+ id: string;
33
+ ordinal_position: number;
34
+ name: string;
35
+ default_value: unknown;
36
+ data_type: string;
37
+ format: string;
38
+ type_schema: string;
39
+ is_identity: boolean;
40
+ identity_generation: "ALWAYS" | "BY DEFAULT" | null;
41
+ is_generated: boolean;
42
+ is_nullable: boolean;
43
+ is_updatable: boolean;
44
+ is_unique: boolean;
45
+ enums: string[];
46
+ check: string | null;
47
+ comment: string | null;
48
+ }[] | undefined;
49
+ }, {}>;
50
+ export type PostgresForeignTable = typeof postgresForeignTableSchema.infer;
51
+ declare const postgresFunctionSchema: import("arktype/internal/variants/object.ts").ObjectType<{
52
+ id: number;
53
+ schema: string;
54
+ name: string;
55
+ language: string;
56
+ definition: string;
57
+ complete_statement: string;
58
+ args: {
59
+ mode: "in" | "inout" | "out" | "table" | "variadic";
60
+ name: string;
61
+ type_id: number;
62
+ has_default: boolean | null;
63
+ }[];
64
+ argument_types: string;
65
+ identity_argument_types: string;
66
+ return_type_id: number;
67
+ return_type: string;
68
+ return_type_relation_id: number | null;
69
+ is_set_returning_function: boolean;
70
+ prorows: number | null;
71
+ behavior: "IMMUTABLE" | "STABLE" | "VOLATILE";
72
+ security_definer: boolean;
73
+ config_params: {
74
+ [x: string]: string;
75
+ } | null;
76
+ }, {}>;
77
+ export type PostgresFunction = typeof postgresFunctionSchema.infer;
78
+ declare const postgresMaterializedViewSchema: import("arktype/internal/variants/object.ts").ObjectType<{
79
+ id: number;
80
+ schema: string;
81
+ name: string;
82
+ is_populated: boolean;
83
+ comment: string | null;
84
+ columns?: {
85
+ table_id: number;
86
+ schema: string;
87
+ table: string;
88
+ id: string;
89
+ ordinal_position: number;
90
+ name: string;
91
+ default_value: unknown;
92
+ data_type: string;
93
+ format: string;
94
+ type_schema: string;
95
+ is_identity: boolean;
96
+ identity_generation: "ALWAYS" | "BY DEFAULT" | null;
97
+ is_generated: boolean;
98
+ is_nullable: boolean;
99
+ is_updatable: boolean;
100
+ is_unique: boolean;
101
+ enums: string[];
102
+ check: string | null;
103
+ comment: string | null;
104
+ }[] | undefined;
105
+ }, {}>;
106
+ export type PostgresMaterializedView = typeof postgresMaterializedViewSchema.infer;
107
+ declare const postgresPrimaryKeySchema: import("arktype/internal/variants/object.ts").ObjectType<{
108
+ schema: string;
109
+ table_name: string;
110
+ name: string;
111
+ table_id: number;
112
+ }, {}>;
113
+ export type PostgresPrimaryKey = typeof postgresPrimaryKeySchema.infer;
114
+ declare const postgresRelationshipSchema: import("arktype/internal/variants/object.ts").ObjectType<{
115
+ foreign_key_name: string;
116
+ schema: string;
117
+ relation: string;
118
+ columns: string[];
119
+ is_one_to_one: boolean;
120
+ referenced_schema: string;
121
+ referenced_relation: string;
122
+ referenced_columns: string[];
123
+ }, {}>;
124
+ export type PostgresRelationship = typeof postgresRelationshipSchema.infer;
125
+ declare const postgresSchemaSchema: import("arktype/internal/variants/object.ts").ObjectType<{
126
+ id: number;
127
+ name: string;
128
+ owner: string;
129
+ }, {}>;
130
+ export type PostgresSchema = typeof postgresSchemaSchema.infer;
131
+ declare const postgresTableSchema: import("arktype/internal/variants/object.ts").ObjectType<{
132
+ id: number;
133
+ schema: string;
134
+ name: string;
135
+ rls_enabled: boolean;
136
+ rls_forced: boolean;
137
+ replica_identity: "DEFAULT" | "FULL" | "INDEX" | "NOTHING";
138
+ bytes: number;
139
+ size: string;
140
+ live_rows_estimate: number;
141
+ dead_rows_estimate: number;
142
+ comment: string | null;
143
+ columns?: {
144
+ table_id: number;
145
+ schema: string;
146
+ table: string;
147
+ id: string;
148
+ ordinal_position: number;
149
+ name: string;
150
+ default_value: unknown;
151
+ data_type: string;
152
+ format: string;
153
+ type_schema: string;
154
+ is_identity: boolean;
155
+ identity_generation: "ALWAYS" | "BY DEFAULT" | null;
156
+ is_generated: boolean;
157
+ is_nullable: boolean;
158
+ is_updatable: boolean;
159
+ is_unique: boolean;
160
+ enums: string[];
161
+ check: string | null;
162
+ comment: string | null;
163
+ }[] | undefined;
164
+ }, {}>;
165
+ export type PostgresTable = typeof postgresTableSchema.infer;
166
+ declare const postgresTypeSchema: import("arktype/internal/variants/object.ts").ObjectType<{
167
+ id: number;
168
+ name: string;
169
+ schema: string;
170
+ format: string;
171
+ enums: string[];
172
+ attributes: {
173
+ name: string;
174
+ type_id: number;
175
+ }[];
176
+ comment: string | null;
177
+ type_relation_id: number | null;
178
+ }, {}>;
179
+ export type PostgresType = typeof postgresTypeSchema.infer;
180
+ declare const postgresViewSchema: import("arktype/internal/variants/object.ts").ObjectType<{
181
+ id: number;
182
+ schema: string;
183
+ name: string;
184
+ is_updatable: boolean;
185
+ comment: string | null;
186
+ columns?: {
187
+ table_id: number;
188
+ schema: string;
189
+ table: string;
190
+ id: string;
191
+ ordinal_position: number;
192
+ name: string;
193
+ default_value: unknown;
194
+ data_type: string;
195
+ format: string;
196
+ type_schema: string;
197
+ is_identity: boolean;
198
+ identity_generation: "ALWAYS" | "BY DEFAULT" | null;
199
+ is_generated: boolean;
200
+ is_nullable: boolean;
201
+ is_updatable: boolean;
202
+ is_unique: boolean;
203
+ enums: string[];
204
+ check: string | null;
205
+ comment: string | null;
206
+ }[] | undefined;
207
+ }, {}>;
208
+ export type PostgresView = typeof postgresViewSchema.infer;
209
+ /**
210
+ * Bumped whenever `GeneratorMetadata`'s shape changes in a way a consumer
211
+ * should branch on. Out-of-process consumers (a generator in another
212
+ * language's SDK repo, fed JSON over a process boundary) only see the
213
+ * serialized document, so a shape change is otherwise undetectable until
214
+ * something breaks at read time.
215
+ */
216
+ export declare const GENERATOR_METADATA_VERSION = 1;
217
+ /**
218
+ * The complete set of introspected metadata required to generate types.
219
+ * Produced by `introspect()` (the default SQL-based producer) or any other
220
+ * source able to satisfy this shape. The table-like collections drop the
221
+ * optional `columns` field (mirroring `Omit<…, "columns">`); columns are
222
+ * carried separately on `columns`.
223
+ */
224
+ export declare const generatorMetadataSchema: import("arktype/internal/variants/object.ts").ObjectType<{
225
+ version: 1;
226
+ schemas: {
227
+ id: number;
228
+ name: string;
229
+ owner: string;
230
+ }[];
231
+ tables: {
232
+ bytes: number;
233
+ comment: string | null;
234
+ dead_rows_estimate: number;
235
+ id: number;
236
+ live_rows_estimate: number;
237
+ name: string;
238
+ replica_identity: "DEFAULT" | "FULL" | "INDEX" | "NOTHING";
239
+ rls_enabled: boolean;
240
+ rls_forced: boolean;
241
+ schema: string;
242
+ size: string;
243
+ }[];
244
+ foreignTables: {
245
+ comment: string | null;
246
+ id: number;
247
+ name: string;
248
+ schema: string;
249
+ }[];
250
+ views: {
251
+ comment: string | null;
252
+ id: number;
253
+ is_updatable: boolean;
254
+ name: string;
255
+ schema: string;
256
+ }[];
257
+ materializedViews: {
258
+ comment: string | null;
259
+ id: number;
260
+ is_populated: boolean;
261
+ name: string;
262
+ schema: string;
263
+ }[];
264
+ columns: {
265
+ table_id: number;
266
+ schema: string;
267
+ table: string;
268
+ id: string;
269
+ ordinal_position: number;
270
+ name: string;
271
+ default_value: unknown;
272
+ data_type: string;
273
+ format: string;
274
+ type_schema: string;
275
+ is_identity: boolean;
276
+ identity_generation: "ALWAYS" | "BY DEFAULT" | null;
277
+ is_generated: boolean;
278
+ is_nullable: boolean;
279
+ is_updatable: boolean;
280
+ is_unique: boolean;
281
+ enums: string[];
282
+ check: string | null;
283
+ comment: string | null;
284
+ }[];
285
+ primaryKeys: {
286
+ schema: string;
287
+ table_name: string;
288
+ name: string;
289
+ table_id: number;
290
+ }[];
291
+ relationships: {
292
+ foreign_key_name: string;
293
+ schema: string;
294
+ relation: string;
295
+ columns: string[];
296
+ is_one_to_one: boolean;
297
+ referenced_schema: string;
298
+ referenced_relation: string;
299
+ referenced_columns: string[];
300
+ }[];
301
+ functions: {
302
+ id: number;
303
+ schema: string;
304
+ name: string;
305
+ language: string;
306
+ definition: string;
307
+ complete_statement: string;
308
+ args: {
309
+ mode: "in" | "inout" | "out" | "table" | "variadic";
310
+ name: string;
311
+ type_id: number;
312
+ has_default: boolean | null;
313
+ }[];
314
+ argument_types: string;
315
+ identity_argument_types: string;
316
+ return_type_id: number;
317
+ return_type: string;
318
+ return_type_relation_id: number | null;
319
+ is_set_returning_function: boolean;
320
+ prorows: number | null;
321
+ behavior: "IMMUTABLE" | "STABLE" | "VOLATILE";
322
+ security_definer: boolean;
323
+ config_params: {
324
+ [x: string]: string;
325
+ } | null;
326
+ }[];
327
+ types: {
328
+ id: number;
329
+ name: string;
330
+ schema: string;
331
+ format: string;
332
+ enums: string[];
333
+ attributes: {
334
+ name: string;
335
+ type_id: number;
336
+ }[];
337
+ comment: string | null;
338
+ type_relation_id: number | null;
339
+ }[];
340
+ }, {}>;
341
+ export type GeneratorMetadata = typeof generatorMetadataSchema.infer;
342
+ /**
343
+ * JSON Schema for {@link generatorMetadataSchema}, published so out-of-process
344
+ * consumers (a generator living in its own SDK repo, not this package) can
345
+ * validate or generate types against the contract without depending on
346
+ * ArkType or this package's TypeScript types.
347
+ */
348
+ export declare const generatorMetadataJsonSchema: import("arktype").JsonSchema.NonBooleanBranch;
349
+ /**
350
+ * Serialize a {@link GeneratorMetadata} value to the JSON document that
351
+ * crosses the process boundary to an out-of-process generator (e.g. `dart
352
+ * run supabase_typegen`). `GeneratorMetadata` is plain, JSON-safe data, so
353
+ * this is a thin, explicit boundary rather than a transform, kept as a named
354
+ * export so every caller serializes the contract the same way.
355
+ */
356
+ export declare function serializeGeneratorMetadata(metadata: GeneratorMetadata): string;
357
+ /**
358
+ * Validate an unknown value against the {@link generatorMetadataSchema} and
359
+ * return it typed as {@link GeneratorMetadata}. Throws a `TypeError` with
360
+ * ArkType's human-readable summary if validation fails.
361
+ *
362
+ * Offered as an opt-in helper so any integrator producing `GeneratorMetadata`
363
+ * through an injected adapter can verify the result at runtime instead of
364
+ * casting. `introspect()` does not call this — wrap its result yourself when
365
+ * you want the guarantee:
366
+ *
367
+ * ```ts
368
+ * const metadata = parseGeneratorMetadata(await introspect(db));
369
+ * ```
370
+ */
371
+ export declare function parseGeneratorMetadata(data: unknown): GeneratorMetadata;
372
+ export {};
package/dist/types.js ADDED
@@ -0,0 +1,207 @@
1
+ /**
2
+ * Public metadata contract for type generation.
3
+ *
4
+ * ArkType is the single source of truth: each `Postgres*` schema below is a
5
+ * runtime validator, and the corresponding exported type is derived from it via
6
+ * `.infer`. The shapes are kept identical to postgres-meta's `src/lib/types.ts`
7
+ * so that postgres-meta can consume this package as a drop-in replacement for
8
+ * its embedded templates (a compile-time equivalence test pins this).
9
+ *
10
+ * `GeneratorMetadata` is the pluggable contract: the SQL introspector is the
11
+ * default producer, but any source able to produce this shape can feed the
12
+ * language generators. Because the producer may be an injected adapter,
13
+ * `parseGeneratorMetadata` is offered so integrators can validate a result at
14
+ * runtime instead of blindly casting it. Validation is deliberately *not* baked
15
+ * into `introspect()` — it is an opt-in helper.
16
+ */
17
+ import { type } from "arktype";
18
+ const postgresColumnSchema = type({
19
+ table_id: "number",
20
+ schema: "string",
21
+ table: "string",
22
+ /** `<table_id>.<ordinal_position>` */
23
+ id: "string",
24
+ ordinal_position: "number",
25
+ name: "string",
26
+ default_value: "unknown",
27
+ data_type: "string",
28
+ format: "string",
29
+ /**
30
+ * Schema owning `format`'s type. Bare enum/composite/domain names are
31
+ * ambiguous across schemas (two schemas can each define an enum named
32
+ * `mood`); this disambiguates without changing `format` itself.
33
+ */
34
+ type_schema: "string",
35
+ is_identity: "boolean",
36
+ identity_generation: "'ALWAYS' | 'BY DEFAULT' | null",
37
+ is_generated: "boolean",
38
+ is_nullable: "boolean",
39
+ is_updatable: "boolean",
40
+ is_unique: "boolean",
41
+ enums: "string[]",
42
+ check: "string | null",
43
+ comment: "string | null",
44
+ });
45
+ const postgresForeignTableSchema = type({
46
+ id: "number",
47
+ schema: "string",
48
+ name: "string",
49
+ comment: "string | null",
50
+ "columns?": postgresColumnSchema.array(),
51
+ });
52
+ const postgresFunctionSchema = type({
53
+ id: "number",
54
+ schema: "string",
55
+ name: "string",
56
+ language: "string",
57
+ definition: "string",
58
+ complete_statement: "string",
59
+ args: type({
60
+ mode: "'in' | 'out' | 'inout' | 'variadic' | 'table'",
61
+ name: "string",
62
+ type_id: "number",
63
+ // `introspect()` emits `null` for the OUT columns of RETURNS TABLE /
64
+ // OUT-arg functions: the introspection SQL sizes `arg_has_defaults` from
65
+ // the input-arg count while `arg_modes`/`arg_types` include output args,
66
+ // so the trailing rows are padded with NULL. postgres-meta types this as a
67
+ // plain boolean, but the opt-in validator must accept the real output.
68
+ has_default: "boolean | null",
69
+ }).array(),
70
+ argument_types: "string",
71
+ identity_argument_types: "string",
72
+ return_type_id: "number",
73
+ return_type: "string",
74
+ return_type_relation_id: "number | null",
75
+ is_set_returning_function: "boolean",
76
+ prorows: "number | null",
77
+ behavior: "'IMMUTABLE' | 'STABLE' | 'VOLATILE'",
78
+ security_definer: "boolean",
79
+ config_params: type({ "[string]": "string" }).or("null"),
80
+ });
81
+ const postgresMaterializedViewSchema = type({
82
+ id: "number",
83
+ schema: "string",
84
+ name: "string",
85
+ is_populated: "boolean",
86
+ comment: "string | null",
87
+ "columns?": postgresColumnSchema.array(),
88
+ });
89
+ const postgresPrimaryKeySchema = type({
90
+ schema: "string",
91
+ table_name: "string",
92
+ name: "string",
93
+ table_id: "number",
94
+ });
95
+ const postgresRelationshipSchema = type({
96
+ foreign_key_name: "string",
97
+ schema: "string",
98
+ relation: "string",
99
+ columns: "string[]",
100
+ is_one_to_one: "boolean",
101
+ referenced_schema: "string",
102
+ referenced_relation: "string",
103
+ referenced_columns: "string[]",
104
+ });
105
+ const postgresSchemaSchema = type({
106
+ id: "number",
107
+ name: "string",
108
+ owner: "string",
109
+ });
110
+ const postgresTableSchema = type({
111
+ id: "number",
112
+ schema: "string",
113
+ name: "string",
114
+ rls_enabled: "boolean",
115
+ rls_forced: "boolean",
116
+ replica_identity: "'DEFAULT' | 'INDEX' | 'FULL' | 'NOTHING'",
117
+ bytes: "number",
118
+ size: "string",
119
+ live_rows_estimate: "number",
120
+ dead_rows_estimate: "number",
121
+ comment: "string | null",
122
+ "columns?": postgresColumnSchema.array(),
123
+ });
124
+ const postgresTypeSchema = type({
125
+ id: "number",
126
+ name: "string",
127
+ schema: "string",
128
+ format: "string",
129
+ enums: "string[]",
130
+ attributes: type({ name: "string", type_id: "number" }).array(),
131
+ comment: "string | null",
132
+ type_relation_id: "number | null",
133
+ });
134
+ const postgresViewSchema = type({
135
+ id: "number",
136
+ schema: "string",
137
+ name: "string",
138
+ is_updatable: "boolean",
139
+ comment: "string | null",
140
+ "columns?": postgresColumnSchema.array(),
141
+ });
142
+ /**
143
+ * Bumped whenever `GeneratorMetadata`'s shape changes in a way a consumer
144
+ * should branch on. Out-of-process consumers (a generator in another
145
+ * language's SDK repo, fed JSON over a process boundary) only see the
146
+ * serialized document, so a shape change is otherwise undetectable until
147
+ * something breaks at read time.
148
+ */
149
+ export const GENERATOR_METADATA_VERSION = 1;
150
+ /**
151
+ * The complete set of introspected metadata required to generate types.
152
+ * Produced by `introspect()` (the default SQL-based producer) or any other
153
+ * source able to satisfy this shape. The table-like collections drop the
154
+ * optional `columns` field (mirroring `Omit<…, "columns">`); columns are
155
+ * carried separately on `columns`.
156
+ */
157
+ export const generatorMetadataSchema = type({
158
+ version: `${GENERATOR_METADATA_VERSION}`,
159
+ schemas: postgresSchemaSchema.array(),
160
+ tables: postgresTableSchema.omit("columns").array(),
161
+ foreignTables: postgresForeignTableSchema.omit("columns").array(),
162
+ views: postgresViewSchema.omit("columns").array(),
163
+ materializedViews: postgresMaterializedViewSchema.omit("columns").array(),
164
+ columns: postgresColumnSchema.array(),
165
+ primaryKeys: postgresPrimaryKeySchema.array(),
166
+ relationships: postgresRelationshipSchema.array(),
167
+ functions: postgresFunctionSchema.array(),
168
+ types: postgresTypeSchema.array(),
169
+ });
170
+ /**
171
+ * JSON Schema for {@link generatorMetadataSchema}, published so out-of-process
172
+ * consumers (a generator living in its own SDK repo, not this package) can
173
+ * validate or generate types against the contract without depending on
174
+ * ArkType or this package's TypeScript types.
175
+ */
176
+ export const generatorMetadataJsonSchema = generatorMetadataSchema.toJsonSchema();
177
+ /**
178
+ * Serialize a {@link GeneratorMetadata} value to the JSON document that
179
+ * crosses the process boundary to an out-of-process generator (e.g. `dart
180
+ * run supabase_typegen`). `GeneratorMetadata` is plain, JSON-safe data, so
181
+ * this is a thin, explicit boundary rather than a transform, kept as a named
182
+ * export so every caller serializes the contract the same way.
183
+ */
184
+ export function serializeGeneratorMetadata(metadata) {
185
+ return JSON.stringify(metadata);
186
+ }
187
+ /**
188
+ * Validate an unknown value against the {@link generatorMetadataSchema} and
189
+ * return it typed as {@link GeneratorMetadata}. Throws a `TypeError` with
190
+ * ArkType's human-readable summary if validation fails.
191
+ *
192
+ * Offered as an opt-in helper so any integrator producing `GeneratorMetadata`
193
+ * through an injected adapter can verify the result at runtime instead of
194
+ * casting. `introspect()` does not call this — wrap its result yourself when
195
+ * you want the guarantee:
196
+ *
197
+ * ```ts
198
+ * const metadata = parseGeneratorMetadata(await introspect(db));
199
+ * ```
200
+ */
201
+ export function parseGeneratorMetadata(data) {
202
+ const out = generatorMetadataSchema(data);
203
+ if (out instanceof type.errors) {
204
+ throw new TypeError(`Invalid GeneratorMetadata: ${out.summary}`);
205
+ }
206
+ return out;
207
+ }
package/package.json ADDED
@@ -0,0 +1,87 @@
1
+ {
2
+ "name": "@supabase/postgrest-typegen",
3
+ "version": "0.0.1",
4
+ "description": "Type generation for PostgREST from PostgreSQL schemas",
5
+ "keywords": [
6
+ "codegen",
7
+ "pg",
8
+ "postgres",
9
+ "postgrest",
10
+ "typegen",
11
+ "types"
12
+ ],
13
+ "homepage": "https://github.com/supabase/sdk",
14
+ "bugs": "https://github.com/supabase/sdk/issues",
15
+ "license": "MIT",
16
+ "author": "Supabase",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/supabase/sdk.git",
20
+ "directory": "packages/postgrest-typegen"
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "src",
25
+ "README.md",
26
+ "LICENSE"
27
+ ],
28
+ "type": "module",
29
+ "sideEffects": false,
30
+ "main": "./dist/index.js",
31
+ "types": "./dist/index.d.ts",
32
+ "exports": {
33
+ ".": {
34
+ "bun": "./src/index.ts",
35
+ "types": "./dist/index.d.ts",
36
+ "import": "./dist/index.js",
37
+ "require": "./dist/index.js",
38
+ "default": "./dist/index.js"
39
+ },
40
+ "./introspection": {
41
+ "bun": "./src/introspection/index.ts",
42
+ "types": "./dist/introspection/index.d.ts",
43
+ "import": "./dist/introspection/index.js",
44
+ "require": "./dist/introspection/index.js",
45
+ "default": "./dist/introspection/index.js"
46
+ },
47
+ "./generation": {
48
+ "bun": "./src/generation/index.ts",
49
+ "types": "./dist/generation/index.d.ts",
50
+ "import": "./dist/generation/index.js",
51
+ "require": "./dist/generation/index.js",
52
+ "default": "./dist/generation/index.js"
53
+ }
54
+ },
55
+ "scripts": {
56
+ "build": "tsc --project tsconfig.build.json",
57
+ "check-types": "tsc --noEmit",
58
+ "format-and-lint": "oxfmt --check . && oxlint --deny-warnings src test scripts",
59
+ "format-and-lint:fix": "oxfmt . && oxlint --fix src test scripts",
60
+ "knip": "knip",
61
+ "test": "bun scripts/run-tests.ts test/"
62
+ },
63
+ "dependencies": {
64
+ "arktype": "2.2.3",
65
+ "pg-format": "1.0.4",
66
+ "prettier": "3.5.3"
67
+ },
68
+ "devDependencies": {
69
+ "@testcontainers/postgresql": "^12.1.0",
70
+ "@tsconfig/node-ts": "^23.6.4",
71
+ "@tsconfig/node24": "^24.0.5",
72
+ "@types/bun": "^1.4.0",
73
+ "@types/node": "^26.3.0",
74
+ "@types/pg": "^8.23.1",
75
+ "@types/pg-format": "^1.0.5",
76
+ "knip": "^6.32.3",
77
+ "oxfmt": "0.65.0",
78
+ "oxlint": "1.80.0",
79
+ "oxlint-tsgolint": "7.0.2001",
80
+ "pg": "^8.23.0",
81
+ "testcontainers": "^12.1.0",
82
+ "typescript": "7.0.2"
83
+ },
84
+ "engines": {
85
+ "node": ">=20.0.0"
86
+ }
87
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Type-generation constants ported verbatim from
3
+ * `postgres-meta/src/server/constants.ts`. Only the typegen-relevant values
4
+ * live here; server/runtime constants stay in postgres-meta.
5
+ *
6
+ * Consumed by the TypeScript generator's function-argument handling.
7
+ */
8
+
9
+ // json/jsonb/text types
10
+ export const VALID_UNNAMED_FUNCTION_ARG_TYPES = new Set([114, 3802, 25]);
11
+ export const VALID_FUNCTION_ARGS_MODE = new Set(["in", "inout", "variadic"]);