@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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @niscorp/vex
2
2
 
3
+ ## 0.4.3
4
+
5
+ ### Patch Changes
6
+
7
+ - c5f8e64: `insertEach` converts each `{ $item }` value by its column's own type, as Postgres converts an `insert`'s value. It cast through a fixed list of five types before, so Postgres refused every other column type (`column is of type … but expression is of type text`).
8
+ - **Columns it now writes:** `time`, enums, `inet`, `interval`, arrays (an item is a JSON array) and pgvector `vector`, also when the extension lives in another schema. A vector item is a JSON array of numbers (`[0.1, 0.2]`) or the pgvector text form (`"[0.1,0.2]"`), so a list of embeddings goes in one statement.
9
+ - **A `timestamp` (without time zone) is stored as `insert` stores it.** It was read as `timestamptz`, so with the session's time zone at Europe/Vienna, `2026-10-26T09:30:00Z` landed as 10:30; it now lands as 09:30.
10
+ - **A fraction for an integer column is refused, as `insert` refuses it.** It was rounded: 7.5 stored 8.
11
+
12
+ **What to change:** an app that sends fractions to an integer column through `insertEach` rounds them itself. An app whose database sessions are not on UTC and that wrote a zoned value into a `timestamp` column through `insertEach` has shifted rows from before; new rows are not shifted.
13
+
3
14
  ## 0.4.2
4
15
 
5
16
  ### Patch Changes
package/DOCS.md CHANGED
@@ -1022,11 +1022,15 @@ Two things to know when using it:
1022
1022
  ### `insertEach` — one statement for a caller-sized list
1023
1023
 
1024
1024
  `items` names a context key holding an array of objects; `{ $item: "key" }`
1025
- values read from the current element (cast to the column's type from the
1026
- schema); every other value — literal, `$context`, `$lookup`, `$returned`,
1027
- engine-injected `$scope` — is constant across rows. Compiles to a single
1028
- `INSERT … SELECT … FROM jsonb_array_elements($items)`, so "one row per ticked
1029
- weekday" is one authored statement, not a code loop:
1025
+ values read from the current element; every other value — literal,
1026
+ `$context`, `$lookup`, `$returned`, engine-injected `$scope` — is constant
1027
+ across rows. vex binds each element under the column names its `$item`s fill
1028
+ (a key no `$item` names is dropped) and compiles to a single
1029
+ `INSERT … SELECT … FROM jsonb_to_recordset($items) AS item(<column> <type>, …)`,
1030
+ each type the column's own from the introspected schema. Postgres converts
1031
+ every item as it converts an `insert`'s value, a column the statement does not
1032
+ write takes its default, and "one row per ticked weekday" is one authored
1033
+ statement, not a code loop:
1030
1034
 
1031
1035
  ```ts
1032
1036
  { op: 'insertEach', table: 'class_templates', items: { $context: 'slots' },
@@ -1040,6 +1044,10 @@ and `set`/`match` rules pin their columns identically on every row. A
1040
1044
  non-array `items` value is refused loudly before any SQL runs; an empty array
1041
1045
  inserts nothing and returns no rows.
1042
1046
 
1047
+ An array column's item is a JSON array, a `jsonb` column's any JSON value, and
1048
+ a pgvector `vector` column's a JSON array of numbers (`[0.1, 0.2]`) or the
1049
+ pgvector text form (`"[0.1,0.2]"`).
1050
+
1043
1051
  ### Replay
1044
1052
 
1045
1053
  The handler dispatches by the entry's kind — one wire shape:
@@ -1055,6 +1055,10 @@ var scopeMutation = (m, policy) => {
1055
1055
  }
1056
1056
  return { op: "delete", table: m.table, where };
1057
1057
  };
1058
+ var itemRow = (element, itemKeys) => {
1059
+ const fields = element !== null && typeof element === "object" && !Array.isArray(element) ? element : {};
1060
+ return Object.fromEntries(Object.entries(itemKeys).map(([column, key]) => [column, fields[key]]));
1061
+ };
1058
1062
  var newCtx = () => ({
1059
1063
  resolvedPaths: /* @__PURE__ */ new Map(),
1060
1064
  aliasMap: /* @__PURE__ */ new Map(),
@@ -1101,13 +1105,6 @@ var compileConflict = (m, ctx, schema) => {
1101
1105
  }
1102
1106
  return ` ON CONFLICT (${c.target.join(", ")}) DO UPDATE SET ${sets.join(", ")}${where}`;
1103
1107
  };
1104
- var ITEM_CAST = {
1105
- number: "numeric",
1106
- boolean: "boolean",
1107
- date: "date",
1108
- timestamp: "timestamptz",
1109
- uuid: "uuid"
1110
- };
1111
1108
  var compileMutation = (m, schema) => {
1112
1109
  const ctx = newCtx();
1113
1110
  if (m.op === "insert") {
@@ -1117,26 +1114,28 @@ var compileMutation = (m, schema) => {
1117
1114
  return { sql: `INSERT INTO ${m.table} (${cols.join(", ")}) VALUES (${vals.join(", ")})${compileConflict(m, ctx, schema)} RETURNING *`, slots: ctx.paramSlots, returned: ctx.returned };
1118
1115
  }
1119
1116
  if (m.op === "insertEach") {
1120
- const entity = findEntity(m.table, schema);
1117
+ const types = new Map(findEntity(m.table, schema)?.fields.map((f) => [f.name, f.type]));
1121
1118
  const entries = Object.entries(m.values);
1122
1119
  const cols = entries.map(([c]) => c);
1120
+ const itemKeys = {};
1121
+ const itemColumns = [];
1123
1122
  const exprs = entries.map(([col, v]) => {
1124
1123
  if (isItemRef(v)) {
1125
- const nt = entity?.fields.find((f) => f.name === col)?.normalizedType ?? "string";
1126
- if (nt === "json") return `(item.value->${quoteLiteral(v.$item)})`;
1127
- const cast = ITEM_CAST[nt];
1128
- const raw = `(item.value->>${quoteLiteral(v.$item)})`;
1129
- return cast === void 0 ? raw : `${raw}::${cast}`;
1124
+ itemKeys[col] = v.$item;
1125
+ itemColumns.push(`${col} ${String(types.get(col))}`);
1126
+ return `item.${col}`;
1130
1127
  }
1131
1128
  return isReturned(v) ? compileColumnValue(m.table, col, v, ctx, schema) : compileValue(v, ctx);
1132
1129
  });
1133
1130
  ctx.paramCounter.value += 1;
1134
1131
  ctx.paramSlots.push({ key: m.items.$context, kind: "context", type: "json" });
1135
- const source = `jsonb_array_elements($${ctx.paramCounter.value}::jsonb) AS item`;
1132
+ const items = `$${ctx.paramCounter.value}::jsonb`;
1133
+ const source = itemColumns.length === 0 ? `jsonb_array_elements(${items}) AS item` : `jsonb_to_recordset(${items}) AS item(${itemColumns.join(", ")})`;
1136
1134
  return {
1137
1135
  sql: `INSERT INTO ${m.table} (${cols.join(", ")}) SELECT ${exprs.join(", ")} FROM ${source}${compileConflict(m, ctx, schema)} RETURNING *`,
1138
1136
  slots: ctx.paramSlots,
1139
- returned: ctx.returned
1137
+ returned: ctx.returned,
1138
+ itemKeys
1140
1139
  };
1141
1140
  }
1142
1141
  if (m.op === "update") {
@@ -1209,7 +1208,8 @@ var executeWrites = async (client, def, mctx) => {
1209
1208
  if (!Array.isArray(value)) {
1210
1209
  throw new VexError("invalid_request", `"${c.slots[i]?.key ?? ""}" must be an array of objects (one inserted row per element).`);
1211
1210
  }
1212
- return JSON.stringify(value);
1211
+ const { itemKeys } = c;
1212
+ return JSON.stringify(itemKeys === void 0 ? value : value.map((element) => itemRow(element, itemKeys)));
1213
1213
  });
1214
1214
  const res = await q.query(c.sql, bound);
1215
1215
  results.push({ table: c.table, op: c.op, rows: res.rows });