@palbase/backend 24.0.0 → 24.0.2

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.
@@ -1,4 +1,4 @@
1
- export { C as ColumnBuilder, a as ColumnDef, b as ColumnMap, c as ColumnType, d as EXTENSION_DEPENDENCIES, e as EmbeddingModelRef, f as EnvServiceDatabase, E as EnvTypedDatabase, I as InsertShape, O as OnDeleteAction, P as PALBASE_EXTENSIONS, i as PalbaseExtension, j as PolicyBuilder, k as PolicyCommand, l as PolicyDef, m as PolicyMode, R as RawConstraintDef, n as RowShape, S as SchemaDef, o as SchemaInput, T as TableDef, p as TableInput, q as TxPlan, r as TxTables, s as TypedDB, t as TypedTable, u as TypedTx, v as bigint, w as boolean, x as defineSchema, y as enumType, z as integer, A as isPalbaseExtension, B as jsonb, D as makeTypedDB, G as openai, H as policy, J as raw, K as text, L as timestamp, M as uuid, N as vector } from '../index-D-4-PNuQ.cjs';
1
+ export { C as ColumnBuilder, a as ColumnDef, b as ColumnMap, c as ColumnType, d as EXTENSION_DEPENDENCIES, e as EmbeddingModelRef, f as EnvServiceDatabase, E as EnvTypedDatabase, I as InsertShape, O as OnDeleteAction, P as PALBASE_EXTENSIONS, i as PalbaseExtension, j as PolicyBuilder, k as PolicyCommand, l as PolicyDef, m as PolicyMode, R as RawConstraintDef, n as RowShape, S as SchemaDef, o as SchemaInput, T as TableDef, p as TableInput, q as TxPlan, r as TxTables, s as TypedDB, t as TypedTable, u as TypedTx, v as bigint, w as boolean, x as defineSchema, y as enumType, z as integer, A as isPalbaseExtension, B as jsonb, D as makeTypedDB, G as openai, H as policy, J as raw, K as text, L as timestamp, M as uuid, N as vector } from '../index-MoQ31B6M.cjs';
2
2
  export { M as Materialized, b1 as Ref, b3 as TxColumnExpr, b4 as TxInsertShape, b6 as TxNow, T as TxPlanBody, b7 as TxPlanError, b8 as TxPlanHandle, b9 as TxPlanOpResult, ba as TxPlanRejection, g as TxPlanResponse, bb as TxRefError, bc as TxRow, bd as TxRows, be as TxSelectOptions, bf as TxSetShape, bh as TxTable, bi as TxWhere, bs as dec, bu as inc, bv as now } from '../endpoint-CcQ1a36a.cjs';
3
3
  import './env.cjs';
4
4
  import '../stack.cjs';
@@ -1,4 +1,4 @@
1
- export { C as ColumnBuilder, a as ColumnDef, b as ColumnMap, c as ColumnType, d as EXTENSION_DEPENDENCIES, e as EmbeddingModelRef, f as EnvServiceDatabase, E as EnvTypedDatabase, I as InsertShape, O as OnDeleteAction, P as PALBASE_EXTENSIONS, i as PalbaseExtension, j as PolicyBuilder, k as PolicyCommand, l as PolicyDef, m as PolicyMode, R as RawConstraintDef, n as RowShape, S as SchemaDef, o as SchemaInput, T as TableDef, p as TableInput, q as TxPlan, r as TxTables, s as TypedDB, t as TypedTable, u as TypedTx, v as bigint, w as boolean, x as defineSchema, y as enumType, z as integer, A as isPalbaseExtension, B as jsonb, D as makeTypedDB, G as openai, H as policy, J as raw, K as text, L as timestamp, M as uuid, N as vector } from '../index-CJiJU9ux.js';
1
+ export { C as ColumnBuilder, a as ColumnDef, b as ColumnMap, c as ColumnType, d as EXTENSION_DEPENDENCIES, e as EmbeddingModelRef, f as EnvServiceDatabase, E as EnvTypedDatabase, I as InsertShape, O as OnDeleteAction, P as PALBASE_EXTENSIONS, i as PalbaseExtension, j as PolicyBuilder, k as PolicyCommand, l as PolicyDef, m as PolicyMode, R as RawConstraintDef, n as RowShape, S as SchemaDef, o as SchemaInput, T as TableDef, p as TableInput, q as TxPlan, r as TxTables, s as TypedDB, t as TypedTable, u as TypedTx, v as bigint, w as boolean, x as defineSchema, y as enumType, z as integer, A as isPalbaseExtension, B as jsonb, D as makeTypedDB, G as openai, H as policy, J as raw, K as text, L as timestamp, M as uuid, N as vector } from '../index-BOS_rFBO.js';
2
2
  export { M as Materialized, b1 as Ref, b3 as TxColumnExpr, b4 as TxInsertShape, b6 as TxNow, T as TxPlanBody, b7 as TxPlanError, b8 as TxPlanHandle, b9 as TxPlanOpResult, ba as TxPlanRejection, g as TxPlanResponse, bb as TxRefError, bc as TxRow, bd as TxRows, be as TxSelectOptions, bf as TxSetShape, bh as TxTable, bi as TxWhere, bs as dec, bu as inc, bv as now } from '../endpoint-0_DGBajf.js';
3
3
  import './env.js';
4
4
  import '../stack.js';
package/dist/db/index.js CHANGED
@@ -17,7 +17,7 @@ import {
17
17
  timestamp,
18
18
  uuid,
19
19
  vector
20
- } from "../chunk-ZUGY7RGS.js";
20
+ } from "../chunk-LCL7TUAI.js";
21
21
  import {
22
22
  TxPlanError,
23
23
  TxRefError,
@@ -1408,11 +1408,17 @@ function offsetClause(offset, limit) {
1408
1408
  }
1409
1409
  function compileWhere(table, colSet, where, add, caller = "search") {
1410
1410
  const parts = [];
1411
+ const transforms = transformsOf(currentSchema, table);
1412
+ const bindFor = (col) => {
1413
+ const t = transforms.get(col);
1414
+ return t?.toDb === void 0 ? add : (v) => add(v === null || v === void 0 ? v : t.toDb(v));
1415
+ };
1411
1416
  for (const [col, cond] of Object.entries(where)) {
1412
1417
  if (colSet !== null && !colSet.has(col)) {
1413
1418
  throw new Error(`${caller}(${table}): where kolonu "${col}" tabloda yok (FR-016)`);
1414
1419
  }
1415
1420
  const q = `t.${quoteIdent(col)}`;
1421
+ const bind = bindFor(col);
1416
1422
  if (cond !== null && typeof cond === "object" && !Array.isArray(cond)) {
1417
1423
  for (const [op, v] of Object.entries(cond)) {
1418
1424
  if (op === "in") {
@@ -1421,15 +1427,15 @@ function compileWhere(table, colSet, where, add, caller = "search") {
1421
1427
  parts.push("false");
1422
1428
  continue;
1423
1429
  }
1424
- parts.push(`${q} IN (${v.map((x) => add(x)).join(", ")})`);
1430
+ parts.push(`${q} IN (${v.map((x) => bind(x)).join(", ")})`);
1425
1431
  } else if (op in WHERE_OPS) {
1426
- parts.push(`${q} ${WHERE_OPS[op]} ${add(v)}`);
1432
+ parts.push(`${q} ${WHERE_OPS[op]} ${bind(v)}`);
1427
1433
  } else {
1428
1434
  throw new Error(`${caller}(${table}): where.${col} bilinmeyen operat\xF6r "${op}" (gt/gte/lt/lte/neq/in)`);
1429
1435
  }
1430
1436
  }
1431
1437
  } else {
1432
- parts.push(`${q} = ${add(cond)}`);
1438
+ parts.push(`${q} = ${bind(cond)}`);
1433
1439
  }
1434
1440
  }
1435
1441
  return parts.length === 0 ? "" : ` AND ${parts.join(" AND ")}`;
@@ -1628,10 +1634,9 @@ function createOps(tx) {
1628
1634
  */
1629
1635
  async updateMany(table, where, set) {
1630
1636
  const cols = Object.keys(set);
1631
- if (cols.length === 0) return [];
1632
- if (Object.keys(where).length === 0) {
1637
+ if (cols.length === 0) {
1633
1638
  throw new Error(
1634
- `updateMany(${table}): bo\u015F filtre T\xDCM sat\u0131rlar\u0131 g\xFCncellerdi \u2014 bir filtre verin, ya da her sat\u0131ra uyan bir ko\u015Ful yaz\u0131n`
1639
+ `updateMany(${table}): nothing to set. An update with no columns is not a no-op worth pretending happened \u2014 pass the columns to write.`
1635
1640
  );
1636
1641
  }
1637
1642
  const params = [];
@@ -1642,6 +1647,7 @@ function createOps(tx) {
1642
1647
  const assignments = cols.map((c) => `${quoteIdent(c)} = ${add(asBindParams(table, [c], set)[0])}`).join(", ");
1643
1648
  const known = schemaColumns(table);
1644
1649
  const whereSql = compileWhere(table, known, where, add, "updateMany");
1650
+ assertHasPredicate(whereSql, "updateMany", table);
1645
1651
  const sql = `UPDATE ${quoteIdent(table)} AS t SET ${assignments} WHERE true${whereSql} RETURNING *`;
1646
1652
  return asTableRows(table, await (await at()).unsafe(sql, params));
1647
1653
  },
@@ -1651,11 +1657,6 @@ function createOps(tx) {
1651
1657
  * {@link updateMany} — and here the accident is worse.
1652
1658
  */
1653
1659
  async deleteMany(table, where) {
1654
- if (Object.keys(where).length === 0) {
1655
- throw new Error(
1656
- `deleteMany(${table}): bo\u015F filtre T\xDCM sat\u0131rlar\u0131 silerdi \u2014 bir filtre verin, ya da her sat\u0131ra uyan bir ko\u015Ful yaz\u0131n`
1657
- );
1658
- }
1659
1660
  const params = [];
1660
1661
  const add = (v) => {
1661
1662
  params.push(v);
@@ -1663,6 +1664,7 @@ function createOps(tx) {
1663
1664
  };
1664
1665
  const known = schemaColumns(table);
1665
1666
  const whereSql = compileWhere(table, known, where, add, "deleteMany");
1667
+ assertHasPredicate(whereSql, "deleteMany", table);
1666
1668
  const sql = `DELETE FROM ${quoteIdent(table)} AS t WHERE true${whereSql} RETURNING 1`;
1667
1669
  const rows = await (await at()).unsafe(sql, params);
1668
1670
  return rows.length;
@@ -2213,6 +2215,12 @@ function isAbortedTransaction(e) {
2213
2215
  const message = String(e?.message ?? "");
2214
2216
  return sqlstateOf(e) === "25P02" || /current transaction is aborted/i.test(message);
2215
2217
  }
2218
+ function assertHasPredicate(whereSql, op, table) {
2219
+ if (whereSql.trim().length > 0) return;
2220
+ throw new Error(
2221
+ `${op}(${table}): the filter compiled to no condition, so this would have written EVERY row. An empty filter \u2014 or an operator object with no operators in it, like { col: {} } \u2014 is refused. Name a condition.`
2222
+ );
2223
+ }
2216
2224
  function sqlstateOf(e) {
2217
2225
  const err = e;
2218
2226
  const isState = (v) => typeof v === "string" && /^[0-9A-Z]{5}$/.test(v);
@@ -2321,7 +2329,7 @@ function isRef(v) {
2321
2329
  function isExpr(v) {
2322
2330
  return typeof v === "object" && v !== null && "$expr" in v;
2323
2331
  }
2324
- function renderValue(value, column, args, results, vectorCols) {
2332
+ function renderValue(value, column, args, results, vectorCols, transforms) {
2325
2333
  if (isRef(value)) {
2326
2334
  const source = results[value.$ref.op];
2327
2335
  const row = source?.rows[0];
@@ -2330,15 +2338,15 @@ function renderValue(value, column, args, results, vectorCols) {
2330
2338
  error_code: "tx_ref_unresolved"
2331
2339
  });
2332
2340
  }
2333
- return bindMaybeVector(args, column, vectorCols, row[value.$ref.field]);
2341
+ return bindMaybeVector(args, column, vectorCols, row[value.$ref.field], transforms);
2334
2342
  }
2335
2343
  if (isExpr(value)) {
2336
2344
  const fn = value.$expr;
2337
2345
  if (fn.fn === "now") return "now()";
2338
2346
  const operator = fn.fn === "inc" ? "+" : "-";
2339
- return `${quoteIdent(column)} ${operator} ${bindMaybeVector(args, column, vectorCols, fn.by)}`;
2347
+ return `${quoteIdent(column)} ${operator} ${bindMaybeVector(args, column, vectorCols, fn.by, transforms)}`;
2340
2348
  }
2341
- return bindMaybeVector(args, column, vectorCols, value);
2349
+ return bindMaybeVector(args, column, vectorCols, value, transforms);
2342
2350
  }
2343
2351
  function renderWhere(where, args, results) {
2344
2352
  const cols = Object.keys(where ?? {});
@@ -2350,21 +2358,26 @@ function renderWhere(where, args, results) {
2350
2358
  });
2351
2359
  return ` WHERE ${terms.join(" AND ")}`;
2352
2360
  }
2353
- function bindMaybeVector(args, column, vectorCols, value) {
2361
+ function bindMaybeVector(args, column, vectorCols, value, transforms) {
2354
2362
  if (column !== void 0 && vectorCols?.has(column) && Array.isArray(value)) {
2355
2363
  return args.bind(toVectorLiteral(value));
2356
2364
  }
2365
+ const t = column !== void 0 ? transforms?.get(column) : void 0;
2366
+ if (t?.toDb !== void 0 && value !== null && value !== void 0) {
2367
+ return args.bind(t.toDb(value));
2368
+ }
2357
2369
  return args.bind(value);
2358
2370
  }
2359
2371
  async function runPlanOp(sp, op, results) {
2360
2372
  const opVectorCols = vectorColumnsOf(currentSchema, op.table);
2373
+ const opTransforms = transformsOf(currentSchema, op.table);
2361
2374
  const args = new Args();
2362
2375
  const table = quoteIdent(op.table);
2363
2376
  let sql;
2364
2377
  switch (op.op) {
2365
2378
  case "insert": {
2366
2379
  const cols = Object.keys(op.values ?? {});
2367
- const rendered = cols.map((c) => renderValue(op.values[c], c, args, results, opVectorCols));
2380
+ const rendered = cols.map((c) => renderValue(op.values[c], c, args, results, opVectorCols, opTransforms));
2368
2381
  sql = cols.length ? `INSERT INTO ${table} (${cols.map(quoteIdent).join(", ")}) VALUES (${rendered.join(", ")}) RETURNING *` : `INSERT INTO ${table} DEFAULT VALUES RETURNING *`;
2369
2382
  break;
2370
2383
  }
@@ -2377,7 +2390,7 @@ async function runPlanOp(sp, op, results) {
2377
2390
  });
2378
2391
  }
2379
2392
  const rendered = cols.map(
2380
- (c) => renderValue(op.values[c], c, args, results, opVectorCols)
2393
+ (c) => renderValue(op.values[c], c, args, results, opVectorCols, opTransforms)
2381
2394
  );
2382
2395
  sql = `INSERT INTO ${table} (${cols.map(quoteIdent).join(", ")}) VALUES (${rendered.join(", ")}) ${onConflictTail(cols, conflict, "update")} RETURNING *`;
2383
2396
  break;
@@ -2387,7 +2400,7 @@ async function runPlanOp(sp, op, results) {
2387
2400
  if (rows.length === 0 || !rows[0]) return [];
2388
2401
  const cols = Object.keys(rows[0]);
2389
2402
  const tuples = rows.map(
2390
- (r) => `(${cols.map((c) => renderValue(r[c], c, args, results, opVectorCols)).join(", ")})`
2403
+ (r) => `(${cols.map((c) => renderValue(r[c], c, args, results, opVectorCols, opTransforms)).join(", ")})`
2391
2404
  );
2392
2405
  const conflictCols = op.onConflict ?? [];
2393
2406
  const tail = op.action !== void 0 && conflictCols.length > 0 ? ` ${onConflictTail(cols, conflictCols, op.action)}` : "";
@@ -2398,7 +2411,7 @@ async function runPlanOp(sp, op, results) {
2398
2411
  const cols = Object.keys(op.set ?? {});
2399
2412
  if (cols.length === 0) throw new Error(`update ${op.table}: nothing to set`);
2400
2413
  const assignments = cols.map(
2401
- (c) => `${quoteIdent(c)} = ${renderValue(op.set[c], c, args, results, opVectorCols)}`
2414
+ (c) => `${quoteIdent(c)} = ${renderValue(op.set[c], c, args, results, opVectorCols, opTransforms)}`
2402
2415
  );
2403
2416
  sql = `UPDATE ${table} SET ${assignments.join(", ")}${renderWhere(op.where, args, results)} RETURNING *`;
2404
2417
  break;