@ai-matrx/records-ui 0.115.87 → 0.115.89

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/pickers.js CHANGED
@@ -4611,6 +4611,11 @@ function knownPerson(id) {
4611
4611
  return null;
4612
4612
  }
4613
4613
 
4614
+ // src/recordVersions.ts
4615
+ var recordVersions_exports = {};
4616
+ __reExport(recordVersions_exports, versions_star3);
4617
+ import * as versions_star3 from "@ai-matrx/records/versions";
4618
+
4614
4619
  // src/memberNames.ts
4615
4620
  import { useEffect as useEffect13, useMemo as useMemo10, useState as useState19 } from "react";
4616
4621
  import { useOptionalRecordsClient as useOptionalRecordsClient7 } from "@ai-matrx/records/react";
@@ -5729,7 +5734,7 @@ async function readPersonColumn(client, tableId, key) {
5729
5734
  const out = [];
5730
5735
  for (let offset = 0; offset < CEILING; offset += PAGE) {
5731
5736
  const page = await client.list({ table_id: tableId, limit: PAGE, offset });
5732
- if (!page.ok) throw new Error(page.error.message ?? "The records could not be read");
5737
+ if (!page.ok) throw new Error(refusalSentence(page.error));
5733
5738
  for (const row of page.data.rows) {
5734
5739
  const doc = row.document ?? {};
5735
5740
  let ids = idsIn(doc[key]);
@@ -5767,17 +5772,43 @@ function previewPersonWords(rows, people, as) {
5767
5772
  }
5768
5773
  return { converts, keptAside, examples, partial: false };
5769
5774
  }
5775
+ async function versionsNow(client, ids) {
5776
+ const out = /* @__PURE__ */ new Map();
5777
+ for (let i = 0; i < ids.length; i += 200) {
5778
+ const got = await client.recordHeaders({ ids: ids.slice(i, i + 200) });
5779
+ if (!got.ok) throw new Error(refusalSentence(got.error));
5780
+ for (const h of got.data ?? []) if (typeof h.version === "number") out.set(h.id, h.version);
5781
+ }
5782
+ return out;
5783
+ }
5784
+ async function writeRecordPatches(client, tableId, patches) {
5785
+ const versions = await versionsNow(client, patches.map((p) => p.recordId));
5786
+ for (let i = 0; i < patches.length; i += 100) {
5787
+ const changes = patches.slice(i, i + 100).map((p) => ({
5788
+ op: "update",
5789
+ record_id: p.recordId,
5790
+ patch: p.patch,
5791
+ expected_version: versions.get(p.recordId) ?? null
5792
+ }));
5793
+ const done = await (0, recordVersions_exports.changeRecordsAt)(client, { table_id: tableId, changes });
5794
+ if (!done.ok) throw new Error(refusalSentence(done.error));
5795
+ }
5796
+ }
5770
5797
  async function writePersonWords(client, tableId, rows, people, key, as) {
5771
- const changes = [];
5798
+ const patches = personWordsPatches(rows, people, key, as);
5799
+ await writeRecordPatches(client, tableId, patches);
5800
+ return patches.length;
5801
+ }
5802
+ function personWordsWritten(rows, people, as) {
5803
+ const out = {};
5772
5804
  for (const row of rows) {
5773
5805
  const words = wordsFor(row.ids, people, as);
5774
- if (words !== null) changes.push({ op: "update", record_id: row.recordId, patch: { [key]: words } });
5775
- }
5776
- for (let i = 0; i < changes.length; i += 100) {
5777
- const done = await client.recordChangeMany({ table_id: tableId, changes: changes.slice(i, i + 100) });
5778
- if (!done.ok) throw new Error(done.error.message ?? "The words could not be written");
5806
+ if (words !== null) out[row.recordId] = words;
5779
5807
  }
5780
- return changes.length;
5808
+ return out;
5809
+ }
5810
+ function personWordsPatches(rows, people, key, as) {
5811
+ return Object.entries(personWordsWritten(rows, people, as)).map(([recordId, words]) => ({ recordId, patch: { [key]: words } }));
5781
5812
  }
5782
5813
 
5783
5814
  // src/FieldRetype.tsx
@@ -5899,6 +5930,7 @@ function FieldRetype({ field, tableId, onRetyped }) {
5899
5930
  setRefusal(null);
5900
5931
  let holding;
5901
5932
  let aside;
5933
+ let wordsWritten;
5902
5934
  const offerUndo = (migration) => {
5903
5935
  if (!migration || !notify?.reversible) return;
5904
5936
  const wasPerson = person && read && (to.id === "text" || to.id === "email") ? read : null;
@@ -5907,14 +5939,14 @@ function FieldRetype({ field, tableId, onRetyped }) {
5907
5939
  noun: "column",
5908
5940
  subject: name,
5909
5941
  undo: async () => {
5910
- const back = await client.migrateUndo({ migration_id: migration });
5942
+ const back = await client.migrateUndo({ migration_id: migration, ...wordsWritten ? { written: wordsWritten } : {} });
5911
5943
  if (!back.ok) throw new Error(refusalSentence(back.error));
5912
5944
  if (wasPerson) {
5913
- const changes = wasPerson.rows.map((r) => ({ op: "update", record_id: r.recordId, patch: { [field.key]: field.multi ? r.ids : r.ids[0] ?? null } }));
5914
- for (let i = 0; i < changes.length; i += 100) {
5915
- const put = await client.recordChangeMany({ table_id: tableId, changes: changes.slice(i, i + 100) });
5916
- if (!put.ok) throw new Error(refusalSentence(put.error));
5917
- }
5945
+ await writeRecordPatches(
5946
+ client,
5947
+ tableId,
5948
+ wasPerson.rows.map((r) => ({ recordId: r.recordId, patch: { [field.key]: field.multi ? r.ids : r.ids[0] ?? null } }))
5949
+ );
5918
5950
  }
5919
5951
  tableShapeChanged2(tableId);
5920
5952
  onRetyped?.();
@@ -5947,6 +5979,7 @@ function FieldRetype({ field, tableId, onRetyped }) {
5947
5979
  if (person && read && (to.id === "text" || to.id === "email")) {
5948
5980
  const as = to.id === "email" ? "email" : "name";
5949
5981
  try {
5982
+ wordsWritten = personWordsWritten(read.rows, read.people, as);
5950
5983
  const written = await writePersonWords(client, tableId, read.rows, read.people, field.key, as);
5951
5984
  holding = written;
5952
5985
  aside = read.rows.length - written;