@niscorp/vex 0.4.1 → 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.
@@ -1,4 +1,4 @@
1
- import { handleDiscovery, handleQuery, handleFingerprintPatch, handleFingerprintDelete } from '../../chunk-AKR6HU34.js';
1
+ import { handleDiscovery, handleQuery, handleFingerprintPatch, handleFingerprintDelete } from '../../chunk-W5MYFJKT.js';
2
2
  import '../../chunk-XTOK7YOR.js';
3
3
  import '../../chunk-UPQMKAAX.js';
4
4
 
@@ -1018,7 +1018,7 @@ var scopeInsertParts = (table, rawValues, rawConflict, policy) => {
1018
1018
  for (const r of [...gate.rule.write ?? [], ...gate.rule.update ?? []]) {
1019
1019
  if ("set" in r) {
1020
1020
  conflict = { ...conflict, set: { ...conflict.set ?? {}, [r.set]: { $scope: r.to } } };
1021
- } else if (!conflict.target.includes(r.match)) {
1021
+ } else {
1022
1022
  const boundary = boundaryOf(table, r);
1023
1023
  conflict = { ...conflict, where: conflict.where === void 0 ? boundary : { and: [conflict.where, boundary] } };
1024
1024
  }
@@ -1056,6 +1056,10 @@ var scopeMutation = (m, policy) => {
1056
1056
  }
1057
1057
  return { op: "delete", table: m.table, where };
1058
1058
  };
1059
+ var itemRow = (element, itemKeys) => {
1060
+ const fields = element !== null && typeof element === "object" && !Array.isArray(element) ? element : {};
1061
+ return Object.fromEntries(Object.entries(itemKeys).map(([column, key]) => [column, fields[key]]));
1062
+ };
1059
1063
  var newCtx = () => ({
1060
1064
  resolvedPaths: /* @__PURE__ */ new Map(),
1061
1065
  aliasMap: /* @__PURE__ */ new Map(),
@@ -1102,13 +1106,6 @@ var compileConflict = (m, ctx, schema) => {
1102
1106
  }
1103
1107
  return ` ON CONFLICT (${c.target.join(", ")}) DO UPDATE SET ${sets.join(", ")}${where}`;
1104
1108
  };
1105
- var ITEM_CAST = {
1106
- number: "numeric",
1107
- boolean: "boolean",
1108
- date: "date",
1109
- timestamp: "timestamptz",
1110
- uuid: "uuid"
1111
- };
1112
1109
  var compileMutation = (m, schema) => {
1113
1110
  const ctx = newCtx();
1114
1111
  if (m.op === "insert") {
@@ -1118,26 +1115,28 @@ var compileMutation = (m, schema) => {
1118
1115
  return { sql: `INSERT INTO ${m.table} (${cols.join(", ")}) VALUES (${vals.join(", ")})${compileConflict(m, ctx, schema)} RETURNING *`, slots: ctx.paramSlots, returned: ctx.returned };
1119
1116
  }
1120
1117
  if (m.op === "insertEach") {
1121
- const entity = findEntity(m.table, schema);
1118
+ const types = new Map(findEntity(m.table, schema)?.fields.map((f) => [f.name, f.type]));
1122
1119
  const entries = Object.entries(m.values);
1123
1120
  const cols = entries.map(([c]) => c);
1121
+ const itemKeys = {};
1122
+ const itemColumns = [];
1124
1123
  const exprs = entries.map(([col, v]) => {
1125
1124
  if (isItemRef(v)) {
1126
- const nt = entity?.fields.find((f) => f.name === col)?.normalizedType ?? "string";
1127
- if (nt === "json") return `(item.value->${quoteLiteral(v.$item)})`;
1128
- const cast = ITEM_CAST[nt];
1129
- const raw = `(item.value->>${quoteLiteral(v.$item)})`;
1130
- return cast === void 0 ? raw : `${raw}::${cast}`;
1125
+ itemKeys[col] = v.$item;
1126
+ itemColumns.push(`${col} ${String(types.get(col))}`);
1127
+ return `item.${col}`;
1131
1128
  }
1132
1129
  return isReturned(v) ? compileColumnValue(m.table, col, v, ctx, schema) : compileValue(v, ctx);
1133
1130
  });
1134
1131
  ctx.paramCounter.value += 1;
1135
1132
  ctx.paramSlots.push({ key: m.items.$context, kind: "context", type: "json" });
1136
- const source = `jsonb_array_elements($${ctx.paramCounter.value}::jsonb) AS item`;
1133
+ const items = `$${ctx.paramCounter.value}::jsonb`;
1134
+ const source = itemColumns.length === 0 ? `jsonb_array_elements(${items}) AS item` : `jsonb_to_recordset(${items}) AS item(${itemColumns.join(", ")})`;
1137
1135
  return {
1138
1136
  sql: `INSERT INTO ${m.table} (${cols.join(", ")}) SELECT ${exprs.join(", ")} FROM ${source}${compileConflict(m, ctx, schema)} RETURNING *`,
1139
1137
  slots: ctx.paramSlots,
1140
- returned: ctx.returned
1138
+ returned: ctx.returned,
1139
+ itemKeys
1141
1140
  };
1142
1141
  }
1143
1142
  if (m.op === "update") {
@@ -1210,7 +1209,8 @@ var executeWrites = async (client, def, mctx) => {
1210
1209
  if (!Array.isArray(value)) {
1211
1210
  throw new VexError("invalid_request", `"${c.slots[i]?.key ?? ""}" must be an array of objects (one inserted row per element).`);
1212
1211
  }
1213
- return JSON.stringify(value);
1212
+ const { itemKeys } = c;
1213
+ return JSON.stringify(itemKeys === void 0 ? value : value.map((element) => itemRow(element, itemKeys)));
1214
1214
  });
1215
1215
  const res = await q.query(c.sql, bound);
1216
1216
  results.push({ table: c.table, op: c.op, rows: res.rows });