@palbase/backend 24.0.1 → 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.
@@ -2329,7 +2329,7 @@ function isRef(v) {
2329
2329
  function isExpr(v) {
2330
2330
  return typeof v === "object" && v !== null && "$expr" in v;
2331
2331
  }
2332
- function renderValue(value, column, args, results, vectorCols) {
2332
+ function renderValue(value, column, args, results, vectorCols, transforms) {
2333
2333
  if (isRef(value)) {
2334
2334
  const source = results[value.$ref.op];
2335
2335
  const row = source?.rows[0];
@@ -2338,15 +2338,15 @@ function renderValue(value, column, args, results, vectorCols) {
2338
2338
  error_code: "tx_ref_unresolved"
2339
2339
  });
2340
2340
  }
2341
- return bindMaybeVector(args, column, vectorCols, row[value.$ref.field]);
2341
+ return bindMaybeVector(args, column, vectorCols, row[value.$ref.field], transforms);
2342
2342
  }
2343
2343
  if (isExpr(value)) {
2344
2344
  const fn = value.$expr;
2345
2345
  if (fn.fn === "now") return "now()";
2346
2346
  const operator = fn.fn === "inc" ? "+" : "-";
2347
- return `${quoteIdent(column)} ${operator} ${bindMaybeVector(args, column, vectorCols, fn.by)}`;
2347
+ return `${quoteIdent(column)} ${operator} ${bindMaybeVector(args, column, vectorCols, fn.by, transforms)}`;
2348
2348
  }
2349
- return bindMaybeVector(args, column, vectorCols, value);
2349
+ return bindMaybeVector(args, column, vectorCols, value, transforms);
2350
2350
  }
2351
2351
  function renderWhere(where, args, results) {
2352
2352
  const cols = Object.keys(where ?? {});
@@ -2358,21 +2358,26 @@ function renderWhere(where, args, results) {
2358
2358
  });
2359
2359
  return ` WHERE ${terms.join(" AND ")}`;
2360
2360
  }
2361
- function bindMaybeVector(args, column, vectorCols, value) {
2361
+ function bindMaybeVector(args, column, vectorCols, value, transforms) {
2362
2362
  if (column !== void 0 && vectorCols?.has(column) && Array.isArray(value)) {
2363
2363
  return args.bind(toVectorLiteral(value));
2364
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
+ }
2365
2369
  return args.bind(value);
2366
2370
  }
2367
2371
  async function runPlanOp(sp, op, results) {
2368
2372
  const opVectorCols = vectorColumnsOf(currentSchema, op.table);
2373
+ const opTransforms = transformsOf(currentSchema, op.table);
2369
2374
  const args = new Args();
2370
2375
  const table = quoteIdent(op.table);
2371
2376
  let sql;
2372
2377
  switch (op.op) {
2373
2378
  case "insert": {
2374
2379
  const cols = Object.keys(op.values ?? {});
2375
- 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));
2376
2381
  sql = cols.length ? `INSERT INTO ${table} (${cols.map(quoteIdent).join(", ")}) VALUES (${rendered.join(", ")}) RETURNING *` : `INSERT INTO ${table} DEFAULT VALUES RETURNING *`;
2377
2382
  break;
2378
2383
  }
@@ -2385,7 +2390,7 @@ async function runPlanOp(sp, op, results) {
2385
2390
  });
2386
2391
  }
2387
2392
  const rendered = cols.map(
2388
- (c) => renderValue(op.values[c], c, args, results, opVectorCols)
2393
+ (c) => renderValue(op.values[c], c, args, results, opVectorCols, opTransforms)
2389
2394
  );
2390
2395
  sql = `INSERT INTO ${table} (${cols.map(quoteIdent).join(", ")}) VALUES (${rendered.join(", ")}) ${onConflictTail(cols, conflict, "update")} RETURNING *`;
2391
2396
  break;
@@ -2395,7 +2400,7 @@ async function runPlanOp(sp, op, results) {
2395
2400
  if (rows.length === 0 || !rows[0]) return [];
2396
2401
  const cols = Object.keys(rows[0]);
2397
2402
  const tuples = rows.map(
2398
- (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(", ")})`
2399
2404
  );
2400
2405
  const conflictCols = op.onConflict ?? [];
2401
2406
  const tail = op.action !== void 0 && conflictCols.length > 0 ? ` ${onConflictTail(cols, conflictCols, op.action)}` : "";
@@ -2406,7 +2411,7 @@ async function runPlanOp(sp, op, results) {
2406
2411
  const cols = Object.keys(op.set ?? {});
2407
2412
  if (cols.length === 0) throw new Error(`update ${op.table}: nothing to set`);
2408
2413
  const assignments = cols.map(
2409
- (c) => `${quoteIdent(c)} = ${renderValue(op.set[c], c, args, results, opVectorCols)}`
2414
+ (c) => `${quoteIdent(c)} = ${renderValue(op.set[c], c, args, results, opVectorCols, opTransforms)}`
2410
2415
  );
2411
2416
  sql = `UPDATE ${table} SET ${assignments.join(", ")}${renderWhere(op.where, args, results)} RETURNING *`;
2412
2417
  break;