@palbase/db 0.3.0 → 0.4.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/admin.js +221 -6
- package/dist/admin.js.map +1 -1
- package/dist/index.cjs +19 -230
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +49 -6
- package/dist/index.d.ts +49 -6
- package/dist/index.js +19 -12
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/dist/chunk-AQ6ZQNXC.js +0 -229
- package/dist/chunk-AQ6ZQNXC.js.map +0 -1
package/dist/admin.js
CHANGED
|
@@ -1,9 +1,224 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
1
|
+
// src/admin-validation.ts
|
|
2
|
+
var IDENTIFIER_RE = /^[a-zA-Z_][a-zA-Z0-9_.]*$/;
|
|
3
|
+
function validateIdentifier(value, label) {
|
|
4
|
+
if (!IDENTIFIER_RE.test(value)) {
|
|
5
|
+
throw new Error(
|
|
6
|
+
`Invalid ${label}: "${value}". Identifiers must match ${IDENTIFIER_RE.source}`
|
|
7
|
+
);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// src/admin-columns.ts
|
|
12
|
+
var ColumnsClient = class {
|
|
13
|
+
httpClient;
|
|
14
|
+
constructor(httpClient) {
|
|
15
|
+
this.httpClient = httpClient;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create a new column on the given table. Sends `POST /v1/meta/columns`.
|
|
19
|
+
*
|
|
20
|
+
* The column name is validated against the standard SQL identifier regex.
|
|
21
|
+
* The `type` field is forwarded as-is to postgres-meta — callers SHOULD
|
|
22
|
+
* restrict types to a known allowlist before calling this.
|
|
23
|
+
*/
|
|
24
|
+
async create(def) {
|
|
25
|
+
validateIdentifier(def.name, "column name");
|
|
26
|
+
const response = await this.httpClient.request("POST", "/v1/meta/columns", {
|
|
27
|
+
body: def
|
|
28
|
+
});
|
|
29
|
+
if (response.error) {
|
|
30
|
+
throw response.error;
|
|
31
|
+
}
|
|
32
|
+
return response.data;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Drop a column. Sends `DELETE /v1/meta/columns/:tableId.:ordinalPosition`.
|
|
36
|
+
*
|
|
37
|
+
* postgres-meta identifies columns by `{tableId}.{ordinalPosition}` (the
|
|
38
|
+
* column's 1-based attnum within its table). Use `TablesClient.get(id)` to
|
|
39
|
+
* find a column id from a column name if needed.
|
|
40
|
+
*/
|
|
41
|
+
async drop(columnId, options) {
|
|
42
|
+
if (!/^\d+\.\d+$/.test(columnId)) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
`Invalid column id: "${columnId}". Expected format "{tableId}.{ordinalPosition}"`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
const params = new URLSearchParams();
|
|
48
|
+
if (options?.cascade) {
|
|
49
|
+
params.set("cascade", "true");
|
|
50
|
+
}
|
|
51
|
+
const queryString = params.toString();
|
|
52
|
+
const path = queryString ? `/v1/meta/columns/${columnId}?${queryString}` : `/v1/meta/columns/${columnId}`;
|
|
53
|
+
const response = await this.httpClient.request("DELETE", path);
|
|
54
|
+
if (response.error) {
|
|
55
|
+
throw response.error;
|
|
56
|
+
}
|
|
57
|
+
return response.data;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// src/admin-schemas.ts
|
|
62
|
+
var SchemasClient = class {
|
|
63
|
+
httpClient;
|
|
64
|
+
constructor(httpClient) {
|
|
65
|
+
this.httpClient = httpClient;
|
|
66
|
+
}
|
|
67
|
+
async list() {
|
|
68
|
+
const response = await this.httpClient.request("GET", "/v1/meta/schemas");
|
|
69
|
+
if (response.error) {
|
|
70
|
+
throw response.error;
|
|
71
|
+
}
|
|
72
|
+
return response.data ?? [];
|
|
73
|
+
}
|
|
74
|
+
async create(name) {
|
|
75
|
+
validateIdentifier(name, "schema name");
|
|
76
|
+
const response = await this.httpClient.request("POST", "/v1/meta/schemas", {
|
|
77
|
+
body: { name }
|
|
78
|
+
});
|
|
79
|
+
if (response.error) {
|
|
80
|
+
throw response.error;
|
|
81
|
+
}
|
|
82
|
+
return response.data;
|
|
83
|
+
}
|
|
84
|
+
async drop(name, options) {
|
|
85
|
+
validateIdentifier(name, "schema name");
|
|
86
|
+
const schemas = await this.list();
|
|
87
|
+
const schema = schemas.find((s) => s.name === name);
|
|
88
|
+
if (!schema) {
|
|
89
|
+
throw new Error(`Schema "${name}" not found`);
|
|
90
|
+
}
|
|
91
|
+
const params = new URLSearchParams();
|
|
92
|
+
if (options?.cascade) {
|
|
93
|
+
params.set("cascade", "true");
|
|
94
|
+
}
|
|
95
|
+
const queryString = params.toString();
|
|
96
|
+
const path = queryString ? `/v1/meta/schemas/${schema.id}?${queryString}` : `/v1/meta/schemas/${schema.id}`;
|
|
97
|
+
const response = await this.httpClient.request("DELETE", path);
|
|
98
|
+
if (response.error) {
|
|
99
|
+
throw response.error;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// src/admin-tables.ts
|
|
105
|
+
var TablesClient = class {
|
|
106
|
+
httpClient;
|
|
107
|
+
constructor(httpClient) {
|
|
108
|
+
this.httpClient = httpClient;
|
|
109
|
+
}
|
|
110
|
+
async list(options) {
|
|
111
|
+
const params = new URLSearchParams();
|
|
112
|
+
if (options?.schema) {
|
|
113
|
+
validateIdentifier(options.schema, "schema name");
|
|
114
|
+
params.set("included_schemas", options.schema);
|
|
115
|
+
}
|
|
116
|
+
const queryString = params.toString();
|
|
117
|
+
const path = queryString ? `/v1/meta/tables?${queryString}` : "/v1/meta/tables";
|
|
118
|
+
const response = await this.httpClient.request("GET", path);
|
|
119
|
+
if (response.error) {
|
|
120
|
+
throw response.error;
|
|
121
|
+
}
|
|
122
|
+
return response.data ?? [];
|
|
123
|
+
}
|
|
124
|
+
async get(id) {
|
|
125
|
+
const response = await this.httpClient.request("GET", `/v1/meta/tables/${id}`);
|
|
126
|
+
if (response.error) {
|
|
127
|
+
throw response.error;
|
|
128
|
+
}
|
|
129
|
+
return response.data;
|
|
130
|
+
}
|
|
131
|
+
async create(def) {
|
|
132
|
+
validateIdentifier(def.name, "table name");
|
|
133
|
+
if (def.schema) {
|
|
134
|
+
validateIdentifier(def.schema, "schema name");
|
|
135
|
+
}
|
|
136
|
+
const response = await this.httpClient.request("POST", "/v1/meta/tables", {
|
|
137
|
+
body: def
|
|
138
|
+
});
|
|
139
|
+
if (response.error) {
|
|
140
|
+
throw response.error;
|
|
141
|
+
}
|
|
142
|
+
return response.data;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Apply table-level updates (rename, schema move, RLS toggle, comment, etc).
|
|
146
|
+
* Sends `PATCH /v1/meta/tables/:id`.
|
|
147
|
+
*
|
|
148
|
+
* Note: this does NOT add or drop columns. For column changes, use
|
|
149
|
+
* `ColumnsClient.create()` / `ColumnsClient.drop()`.
|
|
150
|
+
*/
|
|
151
|
+
async update(id, patch) {
|
|
152
|
+
if (patch.name !== void 0) {
|
|
153
|
+
validateIdentifier(patch.name, "table name");
|
|
154
|
+
}
|
|
155
|
+
if (patch.schema !== void 0) {
|
|
156
|
+
validateIdentifier(patch.schema, "schema name");
|
|
157
|
+
}
|
|
158
|
+
if (patch.primary_keys) {
|
|
159
|
+
for (const pk of patch.primary_keys) {
|
|
160
|
+
validateIdentifier(pk.name, "primary key column name");
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const response = await this.httpClient.request("PATCH", `/v1/meta/tables/${id}`, {
|
|
164
|
+
body: patch
|
|
165
|
+
});
|
|
166
|
+
if (response.error) {
|
|
167
|
+
throw response.error;
|
|
168
|
+
}
|
|
169
|
+
return response.data;
|
|
170
|
+
}
|
|
171
|
+
async drop(id, options) {
|
|
172
|
+
const params = new URLSearchParams();
|
|
173
|
+
if (options?.cascade) {
|
|
174
|
+
params.set("cascade", "true");
|
|
175
|
+
}
|
|
176
|
+
const queryString = params.toString();
|
|
177
|
+
const path = queryString ? `/v1/meta/tables/${id}?${queryString}` : `/v1/meta/tables/${id}`;
|
|
178
|
+
const response = await this.httpClient.request("DELETE", path);
|
|
179
|
+
if (response.error) {
|
|
180
|
+
throw response.error;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// src/admin-client.ts
|
|
186
|
+
var AdminClient = class {
|
|
187
|
+
schemas;
|
|
188
|
+
tables;
|
|
189
|
+
columns;
|
|
190
|
+
httpClient;
|
|
191
|
+
constructor(httpClient) {
|
|
192
|
+
this.httpClient = httpClient;
|
|
193
|
+
this.schemas = new SchemasClient(httpClient);
|
|
194
|
+
this.tables = new TablesClient(httpClient);
|
|
195
|
+
this.columns = new ColumnsClient(httpClient);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Execute a raw SQL query with full privileges (service role).
|
|
199
|
+
*
|
|
200
|
+
* Always use parameterized queries to prevent SQL injection.
|
|
201
|
+
* Never interpolate user input directly into the SQL string.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ```ts
|
|
205
|
+
* // GOOD — parameterized
|
|
206
|
+
* await admin.query('SELECT * FROM users WHERE id = $1', [userId]);
|
|
207
|
+
*
|
|
208
|
+
* // BAD — string interpolation (SQL injection risk)
|
|
209
|
+
* await admin.query(`SELECT * FROM users WHERE id = '${userId}'`);
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
async query(sql, params) {
|
|
213
|
+
const response = await this.httpClient.request("POST", "/v1/meta/query", {
|
|
214
|
+
body: { query: sql, params }
|
|
215
|
+
});
|
|
216
|
+
if (response.error) {
|
|
217
|
+
throw response.error;
|
|
218
|
+
}
|
|
219
|
+
return response.data ?? [];
|
|
220
|
+
}
|
|
221
|
+
};
|
|
7
222
|
export {
|
|
8
223
|
AdminClient,
|
|
9
224
|
ColumnsClient,
|
package/dist/admin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/admin-validation.ts","../src/admin-columns.ts","../src/admin-schemas.ts","../src/admin-tables.ts","../src/admin-client.ts"],"sourcesContent":["const IDENTIFIER_RE = /^[a-zA-Z_][a-zA-Z0-9_.]*$/;\n\nexport function validateIdentifier(value: string, label: string): void {\n if (!IDENTIFIER_RE.test(value)) {\n throw new Error(\n `Invalid ${label}: \"${value}\". Identifiers must match ${IDENTIFIER_RE.source}`,\n );\n }\n}\n","import type { HttpClient } from '@palbase/core';\nimport type { Column, CreateColumnDef } from './admin-types.js';\nimport { validateIdentifier } from './admin-validation.js';\n\n/**\n * Postgres-meta column admin client.\n *\n * Wraps the `/v1/meta/columns` endpoints exposed by postgres-meta.\n * Use this to add or drop columns on an existing table — `TablesClient.update()`\n * does not handle column structure changes.\n */\nexport class ColumnsClient {\n private readonly httpClient: HttpClient;\n\n constructor(httpClient: HttpClient) {\n this.httpClient = httpClient;\n }\n\n /**\n * Create a new column on the given table. Sends `POST /v1/meta/columns`.\n *\n * The column name is validated against the standard SQL identifier regex.\n * The `type` field is forwarded as-is to postgres-meta — callers SHOULD\n * restrict types to a known allowlist before calling this.\n */\n async create(def: CreateColumnDef): Promise<Column> {\n validateIdentifier(def.name, 'column name');\n\n const response = await this.httpClient.request<Column>('POST', '/v1/meta/columns', {\n body: def,\n });\n if (response.error) {\n throw response.error;\n }\n return response.data as Column;\n }\n\n /**\n * Drop a column. Sends `DELETE /v1/meta/columns/:tableId.:ordinalPosition`.\n *\n * postgres-meta identifies columns by `{tableId}.{ordinalPosition}` (the\n * column's 1-based attnum within its table). Use `TablesClient.get(id)` to\n * find a column id from a column name if needed.\n */\n async drop(columnId: string, options?: { cascade?: boolean }): Promise<Column> {\n if (!/^\\d+\\.\\d+$/.test(columnId)) {\n throw new Error(\n `Invalid column id: \"${columnId}\". Expected format \"{tableId}.{ordinalPosition}\"`,\n );\n }\n\n const params = new URLSearchParams();\n if (options?.cascade) {\n params.set('cascade', 'true');\n }\n\n const queryString = params.toString();\n const path = queryString\n ? `/v1/meta/columns/${columnId}?${queryString}`\n : `/v1/meta/columns/${columnId}`;\n\n const response = await this.httpClient.request<Column>('DELETE', path);\n if (response.error) {\n throw response.error;\n }\n return response.data as Column;\n }\n}\n","import type { HttpClient } from '@palbase/core';\nimport type { Schema } from './admin-types.js';\nimport { validateIdentifier } from './admin-validation.js';\n\nexport class SchemasClient {\n private readonly httpClient: HttpClient;\n\n constructor(httpClient: HttpClient) {\n this.httpClient = httpClient;\n }\n\n async list(): Promise<Schema[]> {\n const response = await this.httpClient.request<Schema[]>('GET', '/v1/meta/schemas');\n if (response.error) {\n throw response.error;\n }\n return response.data ?? [];\n }\n\n async create(name: string): Promise<Schema> {\n validateIdentifier(name, 'schema name');\n\n const response = await this.httpClient.request<Schema>('POST', '/v1/meta/schemas', {\n body: { name },\n });\n if (response.error) {\n throw response.error;\n }\n return response.data as Schema;\n }\n\n async drop(name: string, options?: { cascade?: boolean }): Promise<void> {\n validateIdentifier(name, 'schema name');\n\n // Resolve name to id first\n const schemas = await this.list();\n const schema = schemas.find((s) => s.name === name);\n if (!schema) {\n throw new Error(`Schema \"${name}\" not found`);\n }\n\n const params = new URLSearchParams();\n if (options?.cascade) {\n params.set('cascade', 'true');\n }\n\n const queryString = params.toString();\n const path = queryString\n ? `/v1/meta/schemas/${schema.id}?${queryString}`\n : `/v1/meta/schemas/${schema.id}`;\n\n const response = await this.httpClient.request<void>('DELETE', path);\n if (response.error) {\n throw response.error;\n }\n }\n}\n","import type { HttpClient } from '@palbase/core';\nimport type { CreateTableDef, Table, UpdateTableDef } from './admin-types.js';\nimport { validateIdentifier } from './admin-validation.js';\n\nexport class TablesClient {\n private readonly httpClient: HttpClient;\n\n constructor(httpClient: HttpClient) {\n this.httpClient = httpClient;\n }\n\n async list(options?: { schema?: string }): Promise<Table[]> {\n const params = new URLSearchParams();\n if (options?.schema) {\n validateIdentifier(options.schema, 'schema name');\n params.set('included_schemas', options.schema);\n }\n\n const queryString = params.toString();\n const path = queryString ? `/v1/meta/tables?${queryString}` : '/v1/meta/tables';\n\n const response = await this.httpClient.request<Table[]>('GET', path);\n if (response.error) {\n throw response.error;\n }\n return response.data ?? [];\n }\n\n async get(id: number): Promise<Table> {\n const response = await this.httpClient.request<Table>('GET', `/v1/meta/tables/${id}`);\n if (response.error) {\n throw response.error;\n }\n return response.data as Table;\n }\n\n async create(def: CreateTableDef): Promise<Table> {\n validateIdentifier(def.name, 'table name');\n if (def.schema) {\n validateIdentifier(def.schema, 'schema name');\n }\n\n const response = await this.httpClient.request<Table>('POST', '/v1/meta/tables', {\n body: def,\n });\n if (response.error) {\n throw response.error;\n }\n return response.data as Table;\n }\n\n /**\n * Apply table-level updates (rename, schema move, RLS toggle, comment, etc).\n * Sends `PATCH /v1/meta/tables/:id`.\n *\n * Note: this does NOT add or drop columns. For column changes, use\n * `ColumnsClient.create()` / `ColumnsClient.drop()`.\n */\n async update(id: number, patch: UpdateTableDef): Promise<Table> {\n if (patch.name !== undefined) {\n validateIdentifier(patch.name, 'table name');\n }\n if (patch.schema !== undefined) {\n validateIdentifier(patch.schema, 'schema name');\n }\n if (patch.primary_keys) {\n for (const pk of patch.primary_keys) {\n validateIdentifier(pk.name, 'primary key column name');\n }\n }\n\n const response = await this.httpClient.request<Table>('PATCH', `/v1/meta/tables/${id}`, {\n body: patch,\n });\n if (response.error) {\n throw response.error;\n }\n return response.data as Table;\n }\n\n async drop(id: number, options?: { cascade?: boolean }): Promise<void> {\n const params = new URLSearchParams();\n if (options?.cascade) {\n params.set('cascade', 'true');\n }\n\n const queryString = params.toString();\n const path = queryString\n ? `/v1/meta/tables/${id}?${queryString}`\n : `/v1/meta/tables/${id}`;\n\n const response = await this.httpClient.request<void>('DELETE', path);\n if (response.error) {\n throw response.error;\n }\n }\n}\n","import type { HttpClient } from '@palbase/core';\nimport { ColumnsClient } from './admin-columns.js';\nimport { SchemasClient } from './admin-schemas.js';\nimport { TablesClient } from './admin-tables.js';\n\nexport class AdminClient {\n readonly schemas: SchemasClient;\n readonly tables: TablesClient;\n readonly columns: ColumnsClient;\n\n private readonly httpClient: HttpClient;\n\n constructor(httpClient: HttpClient) {\n this.httpClient = httpClient;\n this.schemas = new SchemasClient(httpClient);\n this.tables = new TablesClient(httpClient);\n this.columns = new ColumnsClient(httpClient);\n }\n\n /**\n * Execute a raw SQL query with full privileges (service role).\n *\n * Always use parameterized queries to prevent SQL injection.\n * Never interpolate user input directly into the SQL string.\n *\n * @example\n * ```ts\n * // GOOD — parameterized\n * await admin.query('SELECT * FROM users WHERE id = $1', [userId]);\n *\n * // BAD — string interpolation (SQL injection risk)\n * await admin.query(`SELECT * FROM users WHERE id = '${userId}'`);\n * ```\n */\n async query<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<T[]> {\n const response = await this.httpClient.request<T[]>('POST', '/v1/meta/query', {\n body: { query: sql, params },\n });\n if (response.error) {\n throw response.error;\n }\n return response.data ?? [];\n }\n}\n"],"mappings":";AAAA,IAAM,gBAAgB;AAEf,SAAS,mBAAmB,OAAe,OAAqB;AACrE,MAAI,CAAC,cAAc,KAAK,KAAK,GAAG;AAC9B,UAAM,IAAI;AAAA,MACR,WAAW,KAAK,MAAM,KAAK,6BAA6B,cAAc,MAAM;AAAA,IAC9E;AAAA,EACF;AACF;;;ACGO,IAAM,gBAAN,MAAoB;AAAA,EACR;AAAA,EAEjB,YAAY,YAAwB;AAClC,SAAK,aAAa;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAM,OAAO,KAAuC;AAClD,uBAAmB,IAAI,MAAM,aAAa;AAE1C,UAAM,WAAW,MAAM,KAAK,WAAW,QAAgB,QAAQ,oBAAoB;AAAA,MACjF,MAAM;AAAA,IACR,CAAC;AACD,QAAI,SAAS,OAAO;AAClB,YAAM,SAAS;AAAA,IACjB;AACA,WAAO,SA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c,UAAU;AAC3C,SAAK,SAAS,IAAI,aAAa,UAAU;AACzC,SAAK,UAAU,IAAI,cAAc,UAAU;AAAA,EAC7C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBA,MAAM,MAAmC,KAAa,QAAkC;AACtF,UAAM,WAAW,MAAM,KAAK,WAAW,QAAa,QAAQ,kBAAkB;AAAA,MAC5E,MAAM,EAAE,OAAO,KAAK,OAAO;AAAA,IAC7B,CAAC;AACD,QAAI,SAAS,OAAO;AAClB,YAAM,SAAS;AAAA,IACjB;AACA,WAAO,SAAS,QAAQ,CAAC;AAAA,EAC3B;AACF;","names":[]}
|
package/dist/index.cjs
CHANGED
|
@@ -29,228 +29,6 @@ __export(index_exports, {
|
|
|
29
29
|
});
|
|
30
30
|
module.exports = __toCommonJS(index_exports);
|
|
31
31
|
|
|
32
|
-
// src/admin-validation.ts
|
|
33
|
-
var IDENTIFIER_RE = /^[a-zA-Z_][a-zA-Z0-9_.]*$/;
|
|
34
|
-
function validateIdentifier(value, label) {
|
|
35
|
-
if (!IDENTIFIER_RE.test(value)) {
|
|
36
|
-
throw new Error(
|
|
37
|
-
`Invalid ${label}: "${value}". Identifiers must match ${IDENTIFIER_RE.source}`
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// src/admin-columns.ts
|
|
43
|
-
var ColumnsClient = class {
|
|
44
|
-
httpClient;
|
|
45
|
-
constructor(httpClient) {
|
|
46
|
-
this.httpClient = httpClient;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Create a new column on the given table. Sends `POST /v1/meta/columns`.
|
|
50
|
-
*
|
|
51
|
-
* The column name is validated against the standard SQL identifier regex.
|
|
52
|
-
* The `type` field is forwarded as-is to postgres-meta — callers SHOULD
|
|
53
|
-
* restrict types to a known allowlist before calling this.
|
|
54
|
-
*/
|
|
55
|
-
async create(def) {
|
|
56
|
-
validateIdentifier(def.name, "column name");
|
|
57
|
-
const response = await this.httpClient.request("POST", "/v1/meta/columns", {
|
|
58
|
-
body: def
|
|
59
|
-
});
|
|
60
|
-
if (response.error) {
|
|
61
|
-
throw response.error;
|
|
62
|
-
}
|
|
63
|
-
return response.data;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Drop a column. Sends `DELETE /v1/meta/columns/:tableId.:ordinalPosition`.
|
|
67
|
-
*
|
|
68
|
-
* postgres-meta identifies columns by `{tableId}.{ordinalPosition}` (the
|
|
69
|
-
* column's 1-based attnum within its table). Use `TablesClient.get(id)` to
|
|
70
|
-
* find a column id from a column name if needed.
|
|
71
|
-
*/
|
|
72
|
-
async drop(columnId, options) {
|
|
73
|
-
if (!/^\d+\.\d+$/.test(columnId)) {
|
|
74
|
-
throw new Error(
|
|
75
|
-
`Invalid column id: "${columnId}". Expected format "{tableId}.{ordinalPosition}"`
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
const params = new URLSearchParams();
|
|
79
|
-
if (options?.cascade) {
|
|
80
|
-
params.set("cascade", "true");
|
|
81
|
-
}
|
|
82
|
-
const queryString = params.toString();
|
|
83
|
-
const path = queryString ? `/v1/meta/columns/${columnId}?${queryString}` : `/v1/meta/columns/${columnId}`;
|
|
84
|
-
const response = await this.httpClient.request("DELETE", path);
|
|
85
|
-
if (response.error) {
|
|
86
|
-
throw response.error;
|
|
87
|
-
}
|
|
88
|
-
return response.data;
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
// src/admin-schemas.ts
|
|
93
|
-
var SchemasClient = class {
|
|
94
|
-
httpClient;
|
|
95
|
-
constructor(httpClient) {
|
|
96
|
-
this.httpClient = httpClient;
|
|
97
|
-
}
|
|
98
|
-
async list() {
|
|
99
|
-
const response = await this.httpClient.request("GET", "/v1/meta/schemas");
|
|
100
|
-
if (response.error) {
|
|
101
|
-
throw response.error;
|
|
102
|
-
}
|
|
103
|
-
return response.data ?? [];
|
|
104
|
-
}
|
|
105
|
-
async create(name) {
|
|
106
|
-
validateIdentifier(name, "schema name");
|
|
107
|
-
const response = await this.httpClient.request("POST", "/v1/meta/schemas", {
|
|
108
|
-
body: { name }
|
|
109
|
-
});
|
|
110
|
-
if (response.error) {
|
|
111
|
-
throw response.error;
|
|
112
|
-
}
|
|
113
|
-
return response.data;
|
|
114
|
-
}
|
|
115
|
-
async drop(name, options) {
|
|
116
|
-
validateIdentifier(name, "schema name");
|
|
117
|
-
const schemas = await this.list();
|
|
118
|
-
const schema = schemas.find((s) => s.name === name);
|
|
119
|
-
if (!schema) {
|
|
120
|
-
throw new Error(`Schema "${name}" not found`);
|
|
121
|
-
}
|
|
122
|
-
const params = new URLSearchParams();
|
|
123
|
-
if (options?.cascade) {
|
|
124
|
-
params.set("cascade", "true");
|
|
125
|
-
}
|
|
126
|
-
const queryString = params.toString();
|
|
127
|
-
const path = queryString ? `/v1/meta/schemas/${schema.id}?${queryString}` : `/v1/meta/schemas/${schema.id}`;
|
|
128
|
-
const response = await this.httpClient.request("DELETE", path);
|
|
129
|
-
if (response.error) {
|
|
130
|
-
throw response.error;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
// src/admin-tables.ts
|
|
136
|
-
var TablesClient = class {
|
|
137
|
-
httpClient;
|
|
138
|
-
constructor(httpClient) {
|
|
139
|
-
this.httpClient = httpClient;
|
|
140
|
-
}
|
|
141
|
-
async list(options) {
|
|
142
|
-
const params = new URLSearchParams();
|
|
143
|
-
if (options?.schema) {
|
|
144
|
-
validateIdentifier(options.schema, "schema name");
|
|
145
|
-
params.set("included_schemas", options.schema);
|
|
146
|
-
}
|
|
147
|
-
const queryString = params.toString();
|
|
148
|
-
const path = queryString ? `/v1/meta/tables?${queryString}` : "/v1/meta/tables";
|
|
149
|
-
const response = await this.httpClient.request("GET", path);
|
|
150
|
-
if (response.error) {
|
|
151
|
-
throw response.error;
|
|
152
|
-
}
|
|
153
|
-
return response.data ?? [];
|
|
154
|
-
}
|
|
155
|
-
async get(id) {
|
|
156
|
-
const response = await this.httpClient.request("GET", `/v1/meta/tables/${id}`);
|
|
157
|
-
if (response.error) {
|
|
158
|
-
throw response.error;
|
|
159
|
-
}
|
|
160
|
-
return response.data;
|
|
161
|
-
}
|
|
162
|
-
async create(def) {
|
|
163
|
-
validateIdentifier(def.name, "table name");
|
|
164
|
-
if (def.schema) {
|
|
165
|
-
validateIdentifier(def.schema, "schema name");
|
|
166
|
-
}
|
|
167
|
-
const response = await this.httpClient.request("POST", "/v1/meta/tables", {
|
|
168
|
-
body: def
|
|
169
|
-
});
|
|
170
|
-
if (response.error) {
|
|
171
|
-
throw response.error;
|
|
172
|
-
}
|
|
173
|
-
return response.data;
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Apply table-level updates (rename, schema move, RLS toggle, comment, etc).
|
|
177
|
-
* Sends `PATCH /v1/meta/tables/:id`.
|
|
178
|
-
*
|
|
179
|
-
* Note: this does NOT add or drop columns. For column changes, use
|
|
180
|
-
* `ColumnsClient.create()` / `ColumnsClient.drop()`.
|
|
181
|
-
*/
|
|
182
|
-
async update(id, patch) {
|
|
183
|
-
if (patch.name !== void 0) {
|
|
184
|
-
validateIdentifier(patch.name, "table name");
|
|
185
|
-
}
|
|
186
|
-
if (patch.schema !== void 0) {
|
|
187
|
-
validateIdentifier(patch.schema, "schema name");
|
|
188
|
-
}
|
|
189
|
-
if (patch.primary_keys) {
|
|
190
|
-
for (const pk of patch.primary_keys) {
|
|
191
|
-
validateIdentifier(pk.name, "primary key column name");
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
const response = await this.httpClient.request("PATCH", `/v1/meta/tables/${id}`, {
|
|
195
|
-
body: patch
|
|
196
|
-
});
|
|
197
|
-
if (response.error) {
|
|
198
|
-
throw response.error;
|
|
199
|
-
}
|
|
200
|
-
return response.data;
|
|
201
|
-
}
|
|
202
|
-
async drop(id, options) {
|
|
203
|
-
const params = new URLSearchParams();
|
|
204
|
-
if (options?.cascade) {
|
|
205
|
-
params.set("cascade", "true");
|
|
206
|
-
}
|
|
207
|
-
const queryString = params.toString();
|
|
208
|
-
const path = queryString ? `/v1/meta/tables/${id}?${queryString}` : `/v1/meta/tables/${id}`;
|
|
209
|
-
const response = await this.httpClient.request("DELETE", path);
|
|
210
|
-
if (response.error) {
|
|
211
|
-
throw response.error;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
// src/admin-client.ts
|
|
217
|
-
var AdminClient = class {
|
|
218
|
-
schemas;
|
|
219
|
-
tables;
|
|
220
|
-
columns;
|
|
221
|
-
httpClient;
|
|
222
|
-
constructor(httpClient) {
|
|
223
|
-
this.httpClient = httpClient;
|
|
224
|
-
this.schemas = new SchemasClient(httpClient);
|
|
225
|
-
this.tables = new TablesClient(httpClient);
|
|
226
|
-
this.columns = new ColumnsClient(httpClient);
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Execute a raw SQL query with full privileges (service role).
|
|
230
|
-
*
|
|
231
|
-
* Always use parameterized queries to prevent SQL injection.
|
|
232
|
-
* Never interpolate user input directly into the SQL string.
|
|
233
|
-
*
|
|
234
|
-
* @example
|
|
235
|
-
* ```ts
|
|
236
|
-
* // GOOD — parameterized
|
|
237
|
-
* await admin.query('SELECT * FROM users WHERE id = $1', [userId]);
|
|
238
|
-
*
|
|
239
|
-
* // BAD — string interpolation (SQL injection risk)
|
|
240
|
-
* await admin.query(`SELECT * FROM users WHERE id = '${userId}'`);
|
|
241
|
-
* ```
|
|
242
|
-
*/
|
|
243
|
-
async query(sql, params) {
|
|
244
|
-
const response = await this.httpClient.request("POST", "/v1/meta/query", {
|
|
245
|
-
body: { query: sql, params }
|
|
246
|
-
});
|
|
247
|
-
if (response.error) {
|
|
248
|
-
throw response.error;
|
|
249
|
-
}
|
|
250
|
-
return response.data ?? [];
|
|
251
|
-
}
|
|
252
|
-
};
|
|
253
|
-
|
|
254
32
|
// src/query-builder.ts
|
|
255
33
|
var TABLE_NAME_RE = /^[a-zA-Z_][a-zA-Z0-9_.]*$/;
|
|
256
34
|
var COLUMN_NAME_RE = /^[a-zA-Z_][a-zA-Z0-9_.:\->#]*$/;
|
|
@@ -307,17 +85,32 @@ var QueryBuilder = class {
|
|
|
307
85
|
filters: [],
|
|
308
86
|
params: {},
|
|
309
87
|
isSingle: false,
|
|
310
|
-
isMaybeSingle: false
|
|
88
|
+
isMaybeSingle: false,
|
|
89
|
+
isHead: false
|
|
311
90
|
};
|
|
312
91
|
}
|
|
313
92
|
// --- Operation methods ---
|
|
314
|
-
select(columns) {
|
|
93
|
+
select(columns, options) {
|
|
94
|
+
const nextHeaders = { ...this.state.headers };
|
|
95
|
+
const prefers = [];
|
|
96
|
+
if (this.state.headers.Prefer) {
|
|
97
|
+
prefers.push(this.state.headers.Prefer);
|
|
98
|
+
}
|
|
99
|
+
if (options?.count) {
|
|
100
|
+
prefers.push(`count=${options.count}`);
|
|
101
|
+
}
|
|
102
|
+
if (prefers.length > 0) {
|
|
103
|
+
nextHeaders.Prefer = prefers.join(",");
|
|
104
|
+
}
|
|
105
|
+
const isHead = options?.head === true;
|
|
315
106
|
this.state = {
|
|
316
107
|
...this.state,
|
|
317
108
|
// Only set GET if no mutation method (POST/PATCH/DELETE) is already set
|
|
318
109
|
// insert().select() should stay POST with Prefer: return=representation
|
|
319
|
-
method: this.state.body !== void 0 ? this.state.method : "GET",
|
|
320
|
-
params: { ...this.state.params, select: columns ?? "*" }
|
|
110
|
+
method: isHead ? "HEAD" : this.state.body !== void 0 ? this.state.method : "GET",
|
|
111
|
+
params: { ...this.state.params, select: columns ?? "*" },
|
|
112
|
+
headers: nextHeaders,
|
|
113
|
+
isHead
|
|
321
114
|
};
|
|
322
115
|
return this;
|
|
323
116
|
}
|
|
@@ -628,13 +421,9 @@ var FN_NAME_RE = /^[a-zA-Z_][a-zA-Z0-9_.]*$/;
|
|
|
628
421
|
var DatabaseClient = class {
|
|
629
422
|
httpClient;
|
|
630
423
|
pathPrefix;
|
|
631
|
-
admin;
|
|
632
424
|
constructor(httpClient, options) {
|
|
633
425
|
this.httpClient = httpClient;
|
|
634
426
|
this.pathPrefix = options?.pathPrefix ?? "/v1/db";
|
|
635
|
-
if (options?.enableAdmin) {
|
|
636
|
-
this.admin = new AdminClient(httpClient);
|
|
637
|
-
}
|
|
638
427
|
}
|
|
639
428
|
from(table) {
|
|
640
429
|
return new QueryBuilder(this.httpClient, table, {
|