@supawatch/target-supabase-types 0.7.0 → 0.8.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/dist/index.js +39 -29
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -30,14 +30,6 @@ function bridgeRuntime(col, snapshot) {
|
|
|
30
30
|
if (col.runtime.kind === "array") {
|
|
31
31
|
return arrayRuntimeFor(runtimeFromName(col.runtime.element, snapshot), 1, "supabase-js");
|
|
32
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
33
|
return runtimeFor(col.pgTypeName, kindOf(col, snapshot), { enums: snapshot.enums }, "supabase-js");
|
|
42
34
|
}
|
|
43
35
|
function kindOf(col, snapshot) {
|
|
@@ -50,6 +42,11 @@ function kindOf(col, snapshot) {
|
|
|
50
42
|
function runtimeFromName(element, _snapshot) {
|
|
51
43
|
return element;
|
|
52
44
|
}
|
|
45
|
+
// Database identifiers are free-form ("Order Log", "café", "select");
|
|
46
|
+
// quote any key that is not a plain TS identifier.
|
|
47
|
+
function tsKey(name) {
|
|
48
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name) ? name : JSON.stringify(name);
|
|
49
|
+
}
|
|
53
50
|
function writableColumns(table) {
|
|
54
51
|
return table.columns.filter((c) => !c.generated && c.identity !== "always");
|
|
55
52
|
}
|
|
@@ -61,7 +58,7 @@ function fieldLine(col, snapshot, optional, indent) {
|
|
|
61
58
|
let t = tsType(runtime, "Json");
|
|
62
59
|
if (col.nullable && t !== "unknown")
|
|
63
60
|
t = `${t} | null`;
|
|
64
|
-
return `${indent}${col.name}${optional ? "?" : ""}: ${t}`;
|
|
61
|
+
return `${indent}${tsKey(col.name)}${optional ? "?" : ""}: ${t}`;
|
|
65
62
|
}
|
|
66
63
|
export class SupabaseTypesTarget {
|
|
67
64
|
name = "supabase-types";
|
|
@@ -89,14 +86,15 @@ export class SupabaseTypesTarget {
|
|
|
89
86
|
"export interface Database {",
|
|
90
87
|
];
|
|
91
88
|
for (const schema of schemas) {
|
|
92
|
-
|
|
89
|
+
// PostgREST exposes foreign tables beside plain ones.
|
|
90
|
+
const tables = snapshot.tables.filter((t) => t.schema === schema && t.kind !== "view");
|
|
93
91
|
const views = snapshot.tables.filter((t) => t.schema === schema && t.kind === "view");
|
|
94
92
|
const enums = snapshot.enums.filter((e) => e.schema === schema);
|
|
95
93
|
const composites = snapshot.composites.filter((c) => c.schema === schema);
|
|
96
|
-
lines.push(` ${schema}: {`);
|
|
94
|
+
lines.push(` ${tsKey(schema)}: {`);
|
|
97
95
|
lines.push(" Tables: {");
|
|
98
96
|
for (const table of tables) {
|
|
99
|
-
lines.push(` ${table.name}: {`);
|
|
97
|
+
lines.push(` ${tsKey(table.name)}: {`);
|
|
100
98
|
lines.push(" Row: {");
|
|
101
99
|
for (const col of table.columns) {
|
|
102
100
|
lines.push(fieldLine(col, snapshot, false, " ") + ";");
|
|
@@ -128,7 +126,7 @@ export class SupabaseTypesTarget {
|
|
|
128
126
|
lines.push(" };");
|
|
129
127
|
lines.push(" Views: {");
|
|
130
128
|
for (const view of views) {
|
|
131
|
-
lines.push(` ${view.name}: {`);
|
|
129
|
+
lines.push(` ${tsKey(view.name)}: {`);
|
|
132
130
|
lines.push(" Row: {");
|
|
133
131
|
for (const col of view.columns) {
|
|
134
132
|
lines.push(fieldLine(col, snapshot, false, " ") + ";");
|
|
@@ -143,36 +141,48 @@ export class SupabaseTypesTarget {
|
|
|
143
141
|
}
|
|
144
142
|
else {
|
|
145
143
|
lines.push(" Functions: {");
|
|
144
|
+
// Overloaded names share one key: Args and Returns become unions
|
|
145
|
+
// of the signatures, never duplicate keys (invalid TS).
|
|
146
|
+
const byName = new Map();
|
|
146
147
|
for (const fn of fns) {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
148
|
+
const group = byName.get(fn.name) ?? [];
|
|
149
|
+
group.push(fn);
|
|
150
|
+
byName.set(fn.name, group);
|
|
151
|
+
}
|
|
152
|
+
for (const [name, group] of byName) {
|
|
153
|
+
const argShapes = group.map((fn) => {
|
|
154
|
+
if (fn.args.length === 0)
|
|
155
|
+
return "Record<string, never>";
|
|
156
|
+
const fields = fn.args.map((arg) => {
|
|
154
157
|
const t = tsType(runtimeFor(arg.pgTypeName, "b", { enums: snapshot.enums }, "supabase-js"), "Json");
|
|
155
|
-
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
const
|
|
160
|
-
|
|
158
|
+
return `${tsKey(arg.name)}${arg.hasDefault ? "?" : ""}: ${t}`;
|
|
159
|
+
});
|
|
160
|
+
return `{ ${fields.join("; ")} }`;
|
|
161
|
+
});
|
|
162
|
+
const returnShapes = [
|
|
163
|
+
...new Set(group.map((fn) => {
|
|
164
|
+
const ret = tsType(runtimeFor(fn.returns.pgTypeName, "b", { enums: snapshot.enums }, "supabase-js"), "Json");
|
|
165
|
+
return fn.returns.isSet ? `(${ret})[]` : ret;
|
|
166
|
+
})),
|
|
167
|
+
];
|
|
168
|
+
lines.push(` ${tsKey(name)}: {`);
|
|
169
|
+
lines.push(` Args: ${[...new Set(argShapes)].join(" | ")};`);
|
|
170
|
+
lines.push(` Returns: ${returnShapes.join(" | ")};`);
|
|
161
171
|
lines.push(" };");
|
|
162
172
|
}
|
|
163
173
|
lines.push(" };");
|
|
164
174
|
}
|
|
165
175
|
lines.push(" Enums: {");
|
|
166
176
|
for (const e of enums) {
|
|
167
|
-
lines.push(` ${e.name}: ${e.labels.map((l) => JSON.stringify(l)).join(" | ")};`);
|
|
177
|
+
lines.push(` ${tsKey(e.name)}: ${e.labels.map((l) => JSON.stringify(l)).join(" | ")};`);
|
|
168
178
|
}
|
|
169
179
|
lines.push(" };");
|
|
170
180
|
lines.push(" CompositeTypes: {");
|
|
171
181
|
for (const c of composites) {
|
|
172
|
-
lines.push(` ${c.name}: {`);
|
|
182
|
+
lines.push(` ${tsKey(c.name)}: {`);
|
|
173
183
|
for (const f of c.fields) {
|
|
174
184
|
const t = tsType(f.runtime, "Json");
|
|
175
|
-
lines.push(` ${f.name}: ${t} | null;`);
|
|
185
|
+
lines.push(` ${tsKey(f.name)}: ${t} | null;`);
|
|
176
186
|
}
|
|
177
187
|
lines.push(" };");
|
|
178
188
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supawatch/target-supabase-types",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "A supabase-js compatible Database interface generated from live Postgres by supawatch, foreign-key relationships included.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"supabase",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"dist"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@supawatch/core": "0.
|
|
36
|
+
"@supawatch/core": "0.8.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@types/node": "^22.20.1",
|