@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.
@@ -2295,7 +2295,7 @@ function isRef(v) {
2295
2295
  function isExpr(v) {
2296
2296
  return typeof v === "object" && v !== null && "$expr" in v;
2297
2297
  }
2298
- function renderValue(value, column, args, results, vectorCols) {
2298
+ function renderValue(value, column, args, results, vectorCols, transforms) {
2299
2299
  if (isRef(value)) {
2300
2300
  const source = results[value.$ref.op];
2301
2301
  const row = source?.rows[0];
@@ -2304,15 +2304,15 @@ function renderValue(value, column, args, results, vectorCols) {
2304
2304
  error_code: "tx_ref_unresolved"
2305
2305
  });
2306
2306
  }
2307
- return bindMaybeVector(args, column, vectorCols, row[value.$ref.field]);
2307
+ return bindMaybeVector(args, column, vectorCols, row[value.$ref.field], transforms);
2308
2308
  }
2309
2309
  if (isExpr(value)) {
2310
2310
  const fn = value.$expr;
2311
2311
  if (fn.fn === "now") return "now()";
2312
2312
  const operator = fn.fn === "inc" ? "+" : "-";
2313
- return `${quoteIdent(column)} ${operator} ${bindMaybeVector(args, column, vectorCols, fn.by)}`;
2313
+ return `${quoteIdent(column)} ${operator} ${bindMaybeVector(args, column, vectorCols, fn.by, transforms)}`;
2314
2314
  }
2315
- return bindMaybeVector(args, column, vectorCols, value);
2315
+ return bindMaybeVector(args, column, vectorCols, value, transforms);
2316
2316
  }
2317
2317
  function renderWhere(where, args, results) {
2318
2318
  const cols = Object.keys(where ?? {});
@@ -2324,21 +2324,26 @@ function renderWhere(where, args, results) {
2324
2324
  });
2325
2325
  return ` WHERE ${terms.join(" AND ")}`;
2326
2326
  }
2327
- function bindMaybeVector(args, column, vectorCols, value) {
2327
+ function bindMaybeVector(args, column, vectorCols, value, transforms) {
2328
2328
  if (column !== void 0 && vectorCols?.has(column) && Array.isArray(value)) {
2329
2329
  return args.bind(toVectorLiteral(value));
2330
2330
  }
2331
+ const t = column !== void 0 ? transforms?.get(column) : void 0;
2332
+ if (t?.toDb !== void 0 && value !== null && value !== void 0) {
2333
+ return args.bind(t.toDb(value));
2334
+ }
2331
2335
  return args.bind(value);
2332
2336
  }
2333
2337
  async function runPlanOp(sp, op, results) {
2334
2338
  const opVectorCols = vectorColumnsOf(currentSchema, op.table);
2339
+ const opTransforms = transformsOf(currentSchema, op.table);
2335
2340
  const args = new Args();
2336
2341
  const table = quoteIdent(op.table);
2337
2342
  let sql;
2338
2343
  switch (op.op) {
2339
2344
  case "insert": {
2340
2345
  const cols = Object.keys(op.values ?? {});
2341
- const rendered = cols.map((c) => renderValue(op.values[c], c, args, results, opVectorCols));
2346
+ const rendered = cols.map((c) => renderValue(op.values[c], c, args, results, opVectorCols, opTransforms));
2342
2347
  sql = cols.length ? `INSERT INTO ${table} (${cols.map(quoteIdent).join(", ")}) VALUES (${rendered.join(", ")}) RETURNING *` : `INSERT INTO ${table} DEFAULT VALUES RETURNING *`;
2343
2348
  break;
2344
2349
  }
@@ -2351,7 +2356,7 @@ async function runPlanOp(sp, op, results) {
2351
2356
  });
2352
2357
  }
2353
2358
  const rendered = cols.map(
2354
- (c) => renderValue(op.values[c], c, args, results, opVectorCols)
2359
+ (c) => renderValue(op.values[c], c, args, results, opVectorCols, opTransforms)
2355
2360
  );
2356
2361
  sql = `INSERT INTO ${table} (${cols.map(quoteIdent).join(", ")}) VALUES (${rendered.join(", ")}) ${onConflictTail(cols, conflict, "update")} RETURNING *`;
2357
2362
  break;
@@ -2361,7 +2366,7 @@ async function runPlanOp(sp, op, results) {
2361
2366
  if (rows.length === 0 || !rows[0]) return [];
2362
2367
  const cols = Object.keys(rows[0]);
2363
2368
  const tuples = rows.map(
2364
- (r) => `(${cols.map((c) => renderValue(r[c], c, args, results, opVectorCols)).join(", ")})`
2369
+ (r) => `(${cols.map((c) => renderValue(r[c], c, args, results, opVectorCols, opTransforms)).join(", ")})`
2365
2370
  );
2366
2371
  const conflictCols = op.onConflict ?? [];
2367
2372
  const tail = op.action !== void 0 && conflictCols.length > 0 ? ` ${onConflictTail(cols, conflictCols, op.action)}` : "";
@@ -2372,7 +2377,7 @@ async function runPlanOp(sp, op, results) {
2372
2377
  const cols = Object.keys(op.set ?? {});
2373
2378
  if (cols.length === 0) throw new Error(`update ${op.table}: nothing to set`);
2374
2379
  const assignments = cols.map(
2375
- (c) => `${quoteIdent(c)} = ${renderValue(op.set[c], c, args, results, opVectorCols)}`
2380
+ (c) => `${quoteIdent(c)} = ${renderValue(op.set[c], c, args, results, opVectorCols, opTransforms)}`
2376
2381
  );
2377
2382
  sql = `UPDATE ${table} SET ${assignments.join(", ")}${renderWhere(op.where, args, results)} RETURNING *`;
2378
2383
  break;