@niscorp/vex 0.4.2 → 0.4.3

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/index.cjs CHANGED
@@ -3719,6 +3719,10 @@ var scopeMutation = (m, policy) => {
3719
3719
  }
3720
3720
  return { op: "delete", table: m.table, where };
3721
3721
  };
3722
+ var itemRow = (element, itemKeys) => {
3723
+ const fields = element !== null && typeof element === "object" && !Array.isArray(element) ? element : {};
3724
+ return Object.fromEntries(Object.entries(itemKeys).map(([column, key]) => [column, fields[key]]));
3725
+ };
3722
3726
  var newCtx = () => ({
3723
3727
  resolvedPaths: /* @__PURE__ */ new Map(),
3724
3728
  aliasMap: /* @__PURE__ */ new Map(),
@@ -3765,13 +3769,6 @@ var compileConflict = (m, ctx, schema) => {
3765
3769
  }
3766
3770
  return ` ON CONFLICT (${c.target.join(", ")}) DO UPDATE SET ${sets.join(", ")}${where}`;
3767
3771
  };
3768
- var ITEM_CAST = {
3769
- number: "numeric",
3770
- boolean: "boolean",
3771
- date: "date",
3772
- timestamp: "timestamptz",
3773
- uuid: "uuid"
3774
- };
3775
3772
  var compileMutation = (m, schema) => {
3776
3773
  const ctx = newCtx();
3777
3774
  if (m.op === "insert") {
@@ -3781,26 +3778,28 @@ var compileMutation = (m, schema) => {
3781
3778
  return { sql: `INSERT INTO ${m.table} (${cols.join(", ")}) VALUES (${vals.join(", ")})${compileConflict(m, ctx, schema)} RETURNING *`, slots: ctx.paramSlots, returned: ctx.returned };
3782
3779
  }
3783
3780
  if (m.op === "insertEach") {
3784
- const entity = findEntity2(m.table, schema);
3781
+ const types = new Map(findEntity2(m.table, schema)?.fields.map((f) => [f.name, f.type]));
3785
3782
  const entries = Object.entries(m.values);
3786
3783
  const cols = entries.map(([c]) => c);
3784
+ const itemKeys = {};
3785
+ const itemColumns = [];
3787
3786
  const exprs = entries.map(([col, v]) => {
3788
3787
  if (isItemRef(v)) {
3789
- const nt = entity?.fields.find((f) => f.name === col)?.normalizedType ?? "string";
3790
- if (nt === "json") return `(item.value->${quoteLiteral(v.$item)})`;
3791
- const cast = ITEM_CAST[nt];
3792
- const raw = `(item.value->>${quoteLiteral(v.$item)})`;
3793
- return cast === void 0 ? raw : `${raw}::${cast}`;
3788
+ itemKeys[col] = v.$item;
3789
+ itemColumns.push(`${col} ${String(types.get(col))}`);
3790
+ return `item.${col}`;
3794
3791
  }
3795
3792
  return isReturned(v) ? compileColumnValue(m.table, col, v, ctx, schema) : compileValue(v, ctx);
3796
3793
  });
3797
3794
  ctx.paramCounter.value += 1;
3798
3795
  ctx.paramSlots.push({ key: m.items.$context, kind: "context", type: "json" });
3799
- const source = `jsonb_array_elements($${ctx.paramCounter.value}::jsonb) AS item`;
3796
+ const items = `$${ctx.paramCounter.value}::jsonb`;
3797
+ const source = itemColumns.length === 0 ? `jsonb_array_elements(${items}) AS item` : `jsonb_to_recordset(${items}) AS item(${itemColumns.join(", ")})`;
3800
3798
  return {
3801
3799
  sql: `INSERT INTO ${m.table} (${cols.join(", ")}) SELECT ${exprs.join(", ")} FROM ${source}${compileConflict(m, ctx, schema)} RETURNING *`,
3802
3800
  slots: ctx.paramSlots,
3803
- returned: ctx.returned
3801
+ returned: ctx.returned,
3802
+ itemKeys
3804
3803
  };
3805
3804
  }
3806
3805
  if (m.op === "update") {
@@ -3892,7 +3891,8 @@ var executeWrites = async (client, def, mctx) => {
3892
3891
  if (!Array.isArray(value)) {
3893
3892
  throw new VexError("invalid_request", `"${c.slots[i]?.key ?? ""}" must be an array of objects (one inserted row per element).`);
3894
3893
  }
3895
- return JSON.stringify(value);
3894
+ const { itemKeys } = c;
3895
+ return JSON.stringify(itemKeys === void 0 ? value : value.map((element) => itemRow(element, itemKeys)));
3896
3896
  });
3897
3897
  const res = await q.query(c.sql, bound);
3898
3898
  results.push({ table: c.table, op: c.op, rows: res.rows });