postgrest-parser 0.1.1 → 0.2.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.
@@ -39,6 +39,43 @@ export class WasmQueryResult {
39
39
  */
40
40
  export function buildFilterClause(filters_json: any): any;
41
41
 
42
+ /**
43
+ * Clear all schema cache entries, freeing all cached memory.
44
+ *
45
+ * Useful as a safety net during shutdown or when all tenants are being evicted.
46
+ *
47
+ * # Example (TypeScript)
48
+ *
49
+ * ```typescript
50
+ * import { clearAllSchemas } from './pkg/postgrest_parser.js';
51
+ *
52
+ * // Clear everything on shutdown
53
+ * clearAllSchemas();
54
+ * ```
55
+ */
56
+ export function clearAllSchemas(): void;
57
+
58
+ /**
59
+ * Clear a schema cache entry, freeing its memory.
60
+ *
61
+ * Call this when a tenant is paused or evicted to prevent memory leaks.
62
+ * If the schema ID does not exist, this is a no-op.
63
+ *
64
+ * # Arguments
65
+ *
66
+ * * `schema_id` - The schema key to remove. If empty, removes "default".
67
+ *
68
+ * # Example (TypeScript)
69
+ *
70
+ * ```typescript
71
+ * import { clearSchema } from './pkg/postgrest_parser.js';
72
+ *
73
+ * // Clear a tenant's schema when they are paused/evicted
74
+ * clearSchema("tenant-123");
75
+ * ```
76
+ */
77
+ export function clearSchema(schema_id: string): void;
78
+
42
79
  /**
43
80
  * Initialize schema cache from a database query executor.
44
81
  *
@@ -46,8 +83,18 @@ export function buildFilterClause(filters_json: any): any;
46
83
  * and returns results. The schema introspection queries will be executed via
47
84
  * this callback to populate the relationship cache.
48
85
  *
86
+ * # Trust
87
+ *
88
+ * The `query_executor` is **fully trusted**. It receives a hardcoded SQL
89
+ * introspection query and its result is used verbatim to build the FK cache.
90
+ * Only pass executors that connect to the correct tenant database.
91
+ *
49
92
  * # Arguments
50
93
  *
94
+ * * `schema_id` - A unique key to store this schema under (e.g., tenant ID).
95
+ * If empty, uses "default". Callers must ensure schema IDs are not
96
+ * attacker-controlled — passing another tenant's ID would overwrite their
97
+ * cached schema.
51
98
  * * `query_executor` - An async JavaScript function with signature:
52
99
  * `async (sql: string) => { rows: any[] }`
53
100
  *
@@ -65,11 +112,11 @@ export function buildFilterClause(filters_json: any): any;
65
112
  * return { rows: result.rows };
66
113
  * };
67
114
  *
68
- * // Initialize schema from database
69
- * await initSchemaFromDb(queryExecutor);
115
+ * // Initialize schema for a specific tenant
116
+ * await initSchemaFromDb("tenant-123", queryExecutor);
70
117
  * ```
71
118
  */
72
- export function initSchemaFromDb(query_executor: Function): Promise<void>;
119
+ export function initSchemaFromDb(schema_id: string, query_executor: Function): Promise<void>;
73
120
 
74
121
  /**
75
122
  * Initialize WASM module (call this first from JavaScript)
@@ -84,16 +131,9 @@ export function init_panic_hook(): void;
84
131
  * * `table` - The table name
85
132
  * * `query_string` - Query string with filters and optional returning
86
133
  * * `headers` - Optional headers as JSON string
87
- *
88
- * # Example (TypeScript)
89
- *
90
- * ```typescript
91
- * const result = parseDelete("users", "id=eq.123&returning=id", null);
92
- * console.log(result.query); // DELETE FROM "users" WHERE ...
93
- * console.log(result.params); // ["123"]
94
- * ```
134
+ * * `schema_id` - Optional schema cache key for per-tenant schema resolution
95
135
  */
96
- export function parseDelete(table: string, query_string: string, headers?: string | null): WasmQueryResult;
136
+ export function parseDelete(table: string, query_string: string, headers?: string | null, schema_id?: string | null): WasmQueryResult;
97
137
 
98
138
  /**
99
139
  * Parse and generate SQL for an INSERT operation.
@@ -104,20 +144,9 @@ export function parseDelete(table: string, query_string: string, headers?: strin
104
144
  * * `body` - JSON body (single object or array of objects)
105
145
  * * `query_string` - Optional query string for returning, on_conflict, etc.
106
146
  * * `headers` - Optional headers as JSON string (e.g., '{"Prefer":"return=representation"}')
107
- *
108
- * # Example (TypeScript)
109
- *
110
- * ```typescript
111
- * const result = parseInsert("users",
112
- * JSON.stringify({ name: "Alice", email: "alice@example.com" }),
113
- * "on_conflict=email&returning=id,name",
114
- * JSON.stringify({ Prefer: "return=representation" })
115
- * );
116
- * console.log(result.query); // INSERT INTO "users" ...
117
- * console.log(result.params); // ["Alice", "alice@example.com"]
118
- * ```
147
+ * * `schema_id` - Optional schema cache key for per-tenant schema resolution
119
148
  */
120
- export function parseInsert(table: string, body: string, query_string?: string | null, headers?: string | null): WasmQueryResult;
149
+ export function parseInsert(table: string, body: string, query_string?: string | null, headers?: string | null, schema_id?: string | null): WasmQueryResult;
121
150
 
122
151
  /**
123
152
  * Parse only the query string without generating SQL.
@@ -141,21 +170,13 @@ export function parseOnly(query_string: string): any;
141
170
  *
142
171
  * * `table` - The table name to query
143
172
  * * `query_string` - The PostgREST query string (e.g., "select=id,name&age=gte.18")
173
+ * * `schema_id` - Optional schema cache key for per-tenant schema resolution
144
174
  *
145
175
  * # Returns
146
176
  *
147
177
  * Returns a `WasmQueryResult` containing the SQL query, parameters, and affected tables.
148
- *
149
- * # Example (TypeScript)
150
- *
151
- * ```typescript
152
- * const result = parseQueryString("users", "age=gte.18&status=eq.active");
153
- * console.log(result.query); // SELECT * FROM "users" WHERE ...
154
- * console.log(result.params); // ["18", "active"]
155
- * console.log(result.tables); // ["users"]
156
- * ```
157
178
  */
158
- export function parseQueryString(table: string, query_string: string): WasmQueryResult;
179
+ export function parseQueryString(table: string, query_string: string, schema_id?: string | null): WasmQueryResult;
159
180
 
160
181
  /**
161
182
  * Parse a complete HTTP request and generate appropriate SQL.
@@ -170,28 +191,11 @@ export function parseQueryString(table: string, query_string: string): WasmQuery
170
191
  * * `query_string` - URL query string
171
192
  * * `body` - Request body as JSON string (or null)
172
193
  * * `headers` - Optional headers as JSON object (for Prefer header)
173
- *
174
- * # Example (TypeScript)
175
- *
176
- * ```typescript
177
- * // SELECT query
178
- * const getResult = parseRequest("GET", "users", "age=gte.18&limit=10", null, null);
179
- *
180
- * // INSERT with upsert
181
- * const postResult = parseRequest("POST", "users", "on_conflict=email",
182
- * JSON.stringify({ name: "Alice", email: "alice@example.com" }),
183
- * JSON.stringify({ Prefer: "return=representation" })
184
- * );
185
- *
186
- * // RPC call
187
- * const rpcResult = parseRequest("POST", "rpc/my_function",
188
- * "select=result",
189
- * JSON.stringify({ arg1: "value" }),
190
- * null
191
- * );
192
- * ```
194
+ * * `schema_id` - Optional schema cache key for per-tenant schema resolution.
195
+ * Must match a key previously passed to [`init_schema_from_db`]. If not
196
+ * provided, falls back to the "default" cache entry (if one exists).
193
197
  */
194
- export function parseRequest(method: string, path: string, query_string: string, body?: string | null, headers?: string | null): WasmQueryResult;
198
+ export function parseRequest(method: string, path: string, query_string: string, body?: string | null, headers?: string | null, schema_id?: string | null): WasmQueryResult;
195
199
 
196
200
  /**
197
201
  * Parse and generate SQL for an RPC (stored procedure/function) call.
@@ -202,20 +206,9 @@ export function parseRequest(method: string, path: string, query_string: string,
202
206
  * * `body` - JSON object with function arguments (or null for no args)
203
207
  * * `query_string` - Optional query string for filtering/ordering results
204
208
  * * `headers` - Optional headers as JSON string
205
- *
206
- * # Example (TypeScript)
207
- *
208
- * ```typescript
209
- * const result = parseRpc("calculate_total",
210
- * JSON.stringify({ order_id: 123, tax_rate: 0.08 }),
211
- * "select=total,tax&limit=1",
212
- * null
213
- * );
214
- * console.log(result.query); // SELECT * FROM calculate_total(...)
215
- * console.log(result.params); // [123, 0.08]
216
- * ```
209
+ * * `schema_id` - Optional schema cache key for per-tenant schema resolution
217
210
  */
218
- export function parseRpc(function_name: string, body?: string | null, query_string?: string | null, headers?: string | null): WasmQueryResult;
211
+ export function parseRpc(function_name: string, body?: string | null, query_string?: string | null, headers?: string | null, schema_id?: string | null): WasmQueryResult;
219
212
 
220
213
  /**
221
214
  * Parse and generate SQL for an UPDATE operation.
@@ -226,20 +219,9 @@ export function parseRpc(function_name: string, body?: string | null, query_stri
226
219
  * * `body` - JSON object with fields to update
227
220
  * * `query_string` - Query string with filters and optional returning
228
221
  * * `headers` - Optional headers as JSON string
229
- *
230
- * # Example (TypeScript)
231
- *
232
- * ```typescript
233
- * const result = parseUpdate("users",
234
- * JSON.stringify({ status: "active" }),
235
- * "id=eq.123&returning=id,status",
236
- * null
237
- * );
238
- * console.log(result.query); // UPDATE "users" SET ...
239
- * console.log(result.params); // ["active", "123"]
240
- * ```
222
+ * * `schema_id` - Optional schema cache key for per-tenant schema resolution
241
223
  */
242
- export function parseUpdate(table: string, body: string, query_string: string, headers?: string | null): WasmQueryResult;
224
+ export function parseUpdate(table: string, body: string, query_string: string, headers?: string | null, schema_id?: string | null): WasmQueryResult;
243
225
 
244
226
  export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
245
227
 
@@ -247,26 +229,28 @@ export interface InitOutput {
247
229
  readonly memory: WebAssembly.Memory;
248
230
  readonly __wbg_wasmqueryresult_free: (a: number, b: number) => void;
249
231
  readonly buildFilterClause: (a: number, b: number) => void;
250
- readonly initSchemaFromDb: (a: number) => number;
251
- readonly parseDelete: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
252
- readonly parseInsert: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => void;
232
+ readonly clearAllSchemas: () => void;
233
+ readonly clearSchema: (a: number, b: number) => void;
234
+ readonly initSchemaFromDb: (a: number, b: number, c: number) => number;
235
+ readonly parseDelete: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => void;
236
+ readonly parseInsert: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number) => void;
253
237
  readonly parseOnly: (a: number, b: number, c: number) => void;
254
- readonly parseQueryString: (a: number, b: number, c: number, d: number, e: number) => void;
255
- readonly parseRequest: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number) => void;
256
- readonly parseRpc: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => void;
257
- readonly parseUpdate: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => void;
238
+ readonly parseQueryString: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
239
+ readonly parseRequest: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number) => void;
240
+ readonly parseRpc: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number) => void;
241
+ readonly parseUpdate: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number) => void;
258
242
  readonly wasmqueryresult_params: (a: number) => number;
259
243
  readonly wasmqueryresult_query: (a: number, b: number) => void;
260
244
  readonly wasmqueryresult_tables: (a: number) => number;
261
245
  readonly wasmqueryresult_toJSON: (a: number) => number;
262
246
  readonly init_panic_hook: () => void;
263
- readonly __wasm_bindgen_func_elem_363: (a: number, b: number) => void;
264
- readonly __wasm_bindgen_func_elem_443: (a: number, b: number, c: number, d: number) => void;
265
- readonly __wasm_bindgen_func_elem_364: (a: number, b: number, c: number) => void;
247
+ readonly __wasm_bindgen_func_elem_403: (a: number, b: number, c: number, d: number) => void;
248
+ readonly __wasm_bindgen_func_elem_408: (a: number, b: number, c: number, d: number) => void;
266
249
  readonly __wbindgen_export: (a: number, b: number) => number;
267
250
  readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
268
251
  readonly __wbindgen_export3: (a: number) => void;
269
252
  readonly __wbindgen_export4: (a: number, b: number, c: number) => void;
253
+ readonly __wbindgen_export5: (a: number, b: number) => void;
270
254
  readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
271
255
  readonly __wbindgen_start: () => void;
272
256
  }