@supawatch/target-supabase-types 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Omar Dulaimi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,10 @@
1
+ import { type Snapshot, type SnapshotFile, type Target, type TargetCapabilities, type TargetOptions } from "@supawatch/core";
2
+ export type SupabaseTypesOptions = TargetOptions;
3
+ export declare class SupabaseTypesTarget implements Target<SupabaseTypesOptions> {
4
+ readonly name = "supabase-types";
5
+ readonly fileExtension = ".ts";
6
+ readonly capabilities: TargetCapabilities;
7
+ renderTable(): never;
8
+ renderSnapshot(snapshot: Snapshot, _opts: SupabaseTypesOptions): SnapshotFile[];
9
+ }
10
+ export default SupabaseTypesTarget;
package/dist/index.js ADDED
@@ -0,0 +1,163 @@
1
+ import { arrayRuntimeFor, runtimeFor, } from "@supawatch/core";
2
+ function tsType(runtime, jsonRef) {
3
+ switch (runtime.kind) {
4
+ case "number":
5
+ return "number";
6
+ case "string":
7
+ return "string";
8
+ case "boolean":
9
+ return "boolean";
10
+ case "date":
11
+ return "string"; // never reached under the supabase-js profile; defensive
12
+ case "bytes":
13
+ return "string";
14
+ case "json":
15
+ return jsonRef;
16
+ case "unknown":
17
+ return "unknown";
18
+ case "array": {
19
+ const el = tsType(runtime.element, jsonRef);
20
+ return el.includes("|") ? `(${el})[]` : `${el}[]`;
21
+ }
22
+ case "enum":
23
+ return runtime.labels.map((l) => JSON.stringify(l)).join(" | ");
24
+ }
25
+ }
26
+ // Re-resolve a column's runtime under the supabase-js profile from its
27
+ // catalog identity, so the bridge is correct even when the rest of the
28
+ // run uses the postgres-js profile.
29
+ function bridgeRuntime(col, snapshot) {
30
+ if (col.runtime.kind === "array") {
31
+ return arrayRuntimeFor(runtimeFromName(col.runtime.element, snapshot), 1, "supabase-js");
32
+ }
33
+ if (col.runtime.kind === "string" && col.runtime.format === "array-literal") {
34
+ // postgres-js collapses enum arrays to a raw literal; PostgREST
35
+ // parses them. Recover the element labels via the enum reference.
36
+ const e = snapshot.enums.find((x) => x.name === col.enumRef);
37
+ if (e) {
38
+ return { kind: "array", element: { kind: "enum", labels: e.labels } };
39
+ }
40
+ }
41
+ return runtimeFor(col.pgTypeName, kindOf(col, snapshot), { enums: snapshot.enums }, "supabase-js");
42
+ }
43
+ function kindOf(col, snapshot) {
44
+ if (snapshot.enums.some((e) => e.name === col.pgTypeName))
45
+ return "e";
46
+ if (snapshot.composites.some((c) => c.name === col.pgTypeName))
47
+ return "c";
48
+ return "b";
49
+ }
50
+ function runtimeFromName(element, _snapshot) {
51
+ return element;
52
+ }
53
+ function writableColumns(table) {
54
+ return table.columns.filter((c) => !c.generated && c.identity !== "always");
55
+ }
56
+ function insertOptional(col) {
57
+ return col.hasDefault || col.nullable || col.identity === "default";
58
+ }
59
+ function fieldLine(col, snapshot, optional, indent) {
60
+ const runtime = bridgeRuntime(col, snapshot);
61
+ let t = tsType(runtime, "Json");
62
+ if (col.nullable && t !== "unknown")
63
+ t = `${t} | null`;
64
+ return `${indent}${col.name}${optional ? "?" : ""}: ${t}`;
65
+ }
66
+ export class SupabaseTypesTarget {
67
+ name = "supabase-types";
68
+ fileExtension = ".ts";
69
+ capabilities = {
70
+ strictObjects: false,
71
+ brandedTypes: false,
72
+ dateInstances: false,
73
+ };
74
+ renderTable() {
75
+ throw new Error("supabase-types is a snapshot-level target");
76
+ }
77
+ renderSnapshot(snapshot, _opts) {
78
+ const schemas = [...new Set(snapshot.tables.map((t) => t.schema))].sort();
79
+ const lines = [
80
+ "// Generated by supawatch. Do not edit.",
81
+ "export type Json =",
82
+ " | string",
83
+ " | number",
84
+ " | boolean",
85
+ " | null",
86
+ " | { [key: string]: Json | undefined }",
87
+ " | Json[];",
88
+ "",
89
+ "export interface Database {",
90
+ ];
91
+ for (const schema of schemas) {
92
+ const tables = snapshot.tables.filter((t) => t.schema === schema && t.kind === "table");
93
+ const views = snapshot.tables.filter((t) => t.schema === schema && t.kind === "view");
94
+ const enums = snapshot.enums.filter((e) => e.schema === schema);
95
+ const composites = snapshot.composites.filter((c) => c.schema === schema);
96
+ lines.push(` ${schema}: {`);
97
+ lines.push(" Tables: {");
98
+ for (const table of tables) {
99
+ lines.push(` ${table.name}: {`);
100
+ lines.push(" Row: {");
101
+ for (const col of table.columns) {
102
+ lines.push(fieldLine(col, snapshot, false, " ") + ";");
103
+ }
104
+ lines.push(" };");
105
+ lines.push(" Insert: {");
106
+ for (const col of writableColumns(table)) {
107
+ lines.push(fieldLine(col, snapshot, insertOptional(col), " ") + ";");
108
+ }
109
+ lines.push(" };");
110
+ lines.push(" Update: {");
111
+ for (const col of writableColumns(table)) {
112
+ lines.push(fieldLine(col, snapshot, true, " ") + ";");
113
+ }
114
+ lines.push(" };");
115
+ lines.push(" Relationships: [");
116
+ for (const fk of table.foreignKeys) {
117
+ lines.push(" {");
118
+ lines.push(` foreignKeyName: ${JSON.stringify(fk.name)};`);
119
+ lines.push(` columns: [${fk.columns.map((c) => JSON.stringify(c)).join(", ")}];`);
120
+ lines.push(" isOneToOne: false;");
121
+ lines.push(` referencedRelation: ${JSON.stringify(fk.referencedTable)};`);
122
+ lines.push(` referencedColumns: [${fk.referencedColumns.map((c) => JSON.stringify(c)).join(", ")}];`);
123
+ lines.push(" },");
124
+ }
125
+ lines.push(" ];");
126
+ lines.push(" };");
127
+ }
128
+ lines.push(" };");
129
+ lines.push(" Views: {");
130
+ for (const view of views) {
131
+ lines.push(` ${view.name}: {`);
132
+ lines.push(" Row: {");
133
+ for (const col of view.columns) {
134
+ lines.push(fieldLine(col, snapshot, false, " ") + ";");
135
+ }
136
+ lines.push(" };");
137
+ lines.push(" };");
138
+ }
139
+ lines.push(" };");
140
+ lines.push(" Functions: Record<string, never>;");
141
+ lines.push(" Enums: {");
142
+ for (const e of enums) {
143
+ lines.push(` ${e.name}: ${e.labels.map((l) => JSON.stringify(l)).join(" | ")};`);
144
+ }
145
+ lines.push(" };");
146
+ lines.push(" CompositeTypes: {");
147
+ for (const c of composites) {
148
+ lines.push(` ${c.name}: {`);
149
+ for (const f of c.fields) {
150
+ const t = tsType(f.runtime, "Json");
151
+ lines.push(` ${f.name}: ${t} | null;`);
152
+ }
153
+ lines.push(" };");
154
+ }
155
+ lines.push(" };");
156
+ lines.push(" };");
157
+ }
158
+ lines.push("}");
159
+ lines.push("");
160
+ return [{ file: "database.types.ts", content: lines.join("\n") }];
161
+ }
162
+ }
163
+ export default SupabaseTypesTarget;
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@supawatch/target-supabase-types",
3
+ "version": "0.1.1",
4
+ "license": "MIT",
5
+ "author": "Omar Dulaimi",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/omar-dulaimi/supawatch.git",
9
+ "directory": "packages/target-supabase-types"
10
+ },
11
+ "type": "module",
12
+ "main": "./dist/index.js",
13
+ "types": "./dist/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.js"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "dependencies": {
24
+ "@supawatch/core": "0.1.1"
25
+ },
26
+ "devDependencies": {
27
+ "@types/node": "^22.20.1",
28
+ "typescript": "^5.9.0"
29
+ },
30
+ "scripts": {
31
+ "build": "tsc -p tsconfig.json"
32
+ }
33
+ }