@mdbase-dev/sdk 0.1.0-beta.212 → 0.1.0-beta.213

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/README.md CHANGED
@@ -154,6 +154,13 @@ Guides: [writes](../../../docs/sdk-writes.md),
154
154
  [values](../../../docs/sdk-values.md),
155
155
  [editing one record](../../../docs/sdk-record-editing.md).
156
156
 
157
+ For editor drafts, `db.records.open(ref)` shares a record session with body and
158
+ frontmatter setters, `flush`, and explicit conflict resolution. A definite save
159
+ refusal keeps the draft dirty and allows a later deliberate save; a revision
160
+ conflict requires `resolve` against the current remote record. An uncertain write
161
+ keeps its original mutation identity for recovery, including after release/reopen
162
+ on the same client. See the [record editing guide](../../../docs/sdk-record-editing.md).
163
+
157
164
  ## Status
158
165
 
159
166
  `snapshot.sync` is the one status to show for the open collection: saved,
@@ -21500,7 +21500,13 @@ function schemaToError(e, what) {
21500
21500
  function withMutation(e, what, ids) {
21501
21501
  return new MdbaseError(schemaToError(e, what).toProblem(), { ...ids, cause: e });
21502
21502
  }
21503
+ var WriteRefusal = class extends MdbaseError {
21504
+ constructor(error4, context = {}) {
21505
+ super(schemaToError(error4, "submit").toProblem(), { ...context, cause: error4 });
21506
+ }
21507
+ };
21503
21508
  var replicaProblems = /* @__PURE__ */ new WeakSet();
21509
+ var isReplicaResponseProblem = (e) => e instanceof MdbaseError && replicaProblems.has(e);
21504
21510
  var Session = class _Session {
21505
21511
  constructor(port, hello) {
21506
21512
  this.port = port;
@@ -22855,11 +22861,17 @@ var RecordSessionImpl = class {
22855
22861
  }
22856
22862
  if (sets.size) changes.patch = sets;
22857
22863
  if (unset.length) changes.unset = unset;
22858
- const write = await this.client.update(
22859
- copy(this.base),
22860
- changes,
22861
- { mutationId, include: include2, conflictMode: "reject", signal }
22862
- );
22864
+ let write;
22865
+ try {
22866
+ write = await this.client.update(
22867
+ copy(this.base),
22868
+ changes,
22869
+ { mutationId, include: include2, conflictMode: "reject", signal }
22870
+ );
22871
+ } catch (error4) {
22872
+ if (error4 instanceof WriteRefusal) this.captured = null;
22873
+ throw error4;
22874
+ }
22863
22875
  signal.throwIfAborted();
22864
22876
  if (write.mutationId !== mutationId)
22865
22877
  throw refusal(
@@ -22876,6 +22888,15 @@ var RecordSessionImpl = class {
22876
22888
  }
22877
22889
  } catch (error4) {
22878
22890
  this.why = problem2(error4);
22891
+ if (!this.captured && this.why.code === "conflict" && !this.remote) {
22892
+ try {
22893
+ const current2 = await this.read(signal);
22894
+ this.charge(this.body, this.patch, this.base, current2);
22895
+ this.remote = copy(current2);
22896
+ } catch (readError) {
22897
+ this.why = problem2(readError);
22898
+ }
22899
+ }
22879
22900
  this.state = this.captured ? "recovery" : this.why.code === "conflict" ? "conflict" : this.why.code === "not_found" ? "deleted" : this.why.code === "outcome_unknown" ? "recovery" : "error";
22880
22901
  this.publish();
22881
22902
  return { ok: false, problem: this.why };
@@ -24918,6 +24939,7 @@ var MdbaseClient = class _MdbaseClient {
24918
24939
  async call(method, p, o = {}) {
24919
24940
  const codec = o.codec;
24920
24941
  const read = this.fenceReads && READ_METHODS.has(method);
24942
+ let retried = false;
24921
24943
  for (; ; ) {
24922
24944
  const s = read ? await this.readyForRead(o.signal, !o.retry) : o.retry ? await this.ready({ signal: o.signal }) : this.currentSession();
24923
24945
  try {
@@ -24934,7 +24956,11 @@ var MdbaseClient = class _MdbaseClient {
24934
24956
  return r;
24935
24957
  } catch (e) {
24936
24958
  const lost = isMdbaseError(e, "unavailable") && s.closed;
24937
- if (!(o.retry && lost && this.opts.reconnect !== false && !this.closing)) throw e;
24959
+ if (!(o.retry && lost && this.opts.reconnect !== false && !this.closing)) {
24960
+ if (retried && isReplicaResponseProblem(e)) throw new MdbaseError(e.toProblem(), { cause: e });
24961
+ throw e;
24962
+ }
24963
+ retried = true;
24938
24964
  }
24939
24965
  }
24940
24966
  }
@@ -25912,7 +25938,7 @@ var MdbaseClient = class _MdbaseClient {
25912
25938
  // ---------------------------------------------------------------- writes (§5, §6)
25913
25939
  /** Submit operations as one mutation: they apply together or not at all. */
25914
25940
  async submit(ops, o = {}) {
25915
- if (this.hello.version.minor >= 3 && this.hello.grant.role === "viewer") throw mdbaseError("forbidden", "You have view access, so changes can't be saved.", "read_only");
25941
+ if (this.hello.version.minor >= 3 && this.hello.grant.role === "viewer") throw new WriteRefusal(mdbaseError("forbidden", "You have view access, so changes can't be saved.", "read_only"));
25916
25942
  if (o.intent !== void 0 && o.mutationId === void 0) o = { ...o, mutationId: (await intentIds(this.collectionId, o.intent))[0] };
25917
25943
  const mutationId = o.mutationId ?? uuidv7();
25918
25944
  const p = { ops, mutationId };
@@ -25938,6 +25964,18 @@ var MdbaseClient = class _MdbaseClient {
25938
25964
  } catch (e) {
25939
25965
  if (o.dryRun) throw e;
25940
25966
  void this.writeLedger.settle(mutationId);
25967
+ if (isReplicaResponseProblem(e) && [
25968
+ "invalid_request",
25969
+ "invalid_record",
25970
+ "not_found",
25971
+ "conflict",
25972
+ "unauthenticated",
25973
+ "forbidden",
25974
+ "collection_invalid",
25975
+ "rate_limited",
25976
+ "too_large",
25977
+ "upgrade_required"
25978
+ ].includes(e.code)) throw new WriteRefusal(e, { mutationId });
25941
25979
  throw withMutation(e, "submit", { mutationId });
25942
25980
  }
25943
25981
  return o.dryRun ? new Write(receipt3) : this._track(receipt3);
@@ -3647,7 +3647,7 @@ var MdbaseSDK = (() => {
3647
3647
  function withMutation(e, what, ids) {
3648
3648
  return new MdbaseError(schemaToError(e, what).toProblem(), { ...ids, cause: e });
3649
3649
  }
3650
- var API_VERSIONS, replicaProblems, isReplicaResponseProblem, Session;
3650
+ var API_VERSIONS, WriteRefusal, replicaProblems, isReplicaResponseProblem, Session;
3651
3651
  var init_session = __esm({
3652
3652
  "src/session.ts"() {
3653
3653
  "use strict";
@@ -3658,6 +3658,11 @@ var MdbaseSDK = (() => {
3658
3658
  init_original_session();
3659
3659
  init_wire();
3660
3660
  API_VERSIONS = [{ major: 1, minor: 3 }, { major: 1, minor: 2 }, { major: 1, minor: 1 }, { major: 1, minor: 0 }];
3661
+ WriteRefusal = class extends MdbaseError {
3662
+ constructor(error5, context = {}) {
3663
+ super(schemaToError(error5, "submit").toProblem(), { ...context, cause: error5 });
3664
+ }
3665
+ };
3661
3666
  replicaProblems = /* @__PURE__ */ new WeakSet();
3662
3667
  isReplicaResponseProblem = (e) => e instanceof MdbaseError && replicaProblems.has(e);
3663
3668
  Session = class _Session {
@@ -4847,6 +4852,7 @@ var MdbaseSDK = (() => {
4847
4852
  "use strict";
4848
4853
  init_cbor();
4849
4854
  init_errors();
4855
+ init_session();
4850
4856
  init_values();
4851
4857
  init_witness();
4852
4858
  RecordSessionImpl = class {
@@ -5212,11 +5218,17 @@ var MdbaseSDK = (() => {
5212
5218
  }
5213
5219
  if (sets.size) changes.patch = sets;
5214
5220
  if (unset.length) changes.unset = unset;
5215
- const write = await this.client.update(
5216
- copy(this.base),
5217
- changes,
5218
- { mutationId, include: include2, conflictMode: "reject", signal }
5219
- );
5221
+ let write;
5222
+ try {
5223
+ write = await this.client.update(
5224
+ copy(this.base),
5225
+ changes,
5226
+ { mutationId, include: include2, conflictMode: "reject", signal }
5227
+ );
5228
+ } catch (error5) {
5229
+ if (error5 instanceof WriteRefusal) this.captured = null;
5230
+ throw error5;
5231
+ }
5220
5232
  signal.throwIfAborted();
5221
5233
  if (write.mutationId !== mutationId)
5222
5234
  throw refusal(
@@ -5233,6 +5245,15 @@ var MdbaseSDK = (() => {
5233
5245
  }
5234
5246
  } catch (error5) {
5235
5247
  this.why = problem2(error5);
5248
+ if (!this.captured && this.why.code === "conflict" && !this.remote) {
5249
+ try {
5250
+ const current3 = await this.read(signal);
5251
+ this.charge(this.body, this.patch, this.base, current3);
5252
+ this.remote = copy(current3);
5253
+ } catch (readError) {
5254
+ this.why = problem2(readError);
5255
+ }
5256
+ }
5236
5257
  this.state = this.captured ? "recovery" : this.why.code === "conflict" ? "conflict" : this.why.code === "not_found" ? "deleted" : this.why.code === "outcome_unknown" ? "recovery" : "error";
5237
5258
  this.publish();
5238
5259
  return { ok: false, problem: this.why };
@@ -7385,6 +7406,7 @@ var MdbaseSDK = (() => {
7385
7406
  async call(method, p, o = {}) {
7386
7407
  const codec = o.codec;
7387
7408
  const read2 = this.fenceReads && READ_METHODS.has(method);
7409
+ let retried = false;
7388
7410
  for (; ; ) {
7389
7411
  const s = read2 ? await this.readyForRead(o.signal, !o.retry) : o.retry ? await this.ready({ signal: o.signal }) : this.currentSession();
7390
7412
  try {
@@ -7401,7 +7423,11 @@ var MdbaseSDK = (() => {
7401
7423
  return r;
7402
7424
  } catch (e) {
7403
7425
  const lost = isMdbaseError(e, "unavailable") && s.closed;
7404
- if (!(o.retry && lost && this.opts.reconnect !== false && !this.closing)) throw e;
7426
+ if (!(o.retry && lost && this.opts.reconnect !== false && !this.closing)) {
7427
+ if (retried && isReplicaResponseProblem(e)) throw new MdbaseError(e.toProblem(), { cause: e });
7428
+ throw e;
7429
+ }
7430
+ retried = true;
7405
7431
  }
7406
7432
  }
7407
7433
  }
@@ -8379,7 +8405,7 @@ var MdbaseSDK = (() => {
8379
8405
  // ---------------------------------------------------------------- writes (§5, §6)
8380
8406
  /** Submit operations as one mutation: they apply together or not at all. */
8381
8407
  async submit(ops, o = {}) {
8382
- if (this.hello.version.minor >= 3 && this.hello.grant.role === "viewer") throw mdbaseError("forbidden", "You have view access, so changes can't be saved.", "read_only");
8408
+ if (this.hello.version.minor >= 3 && this.hello.grant.role === "viewer") throw new WriteRefusal(mdbaseError("forbidden", "You have view access, so changes can't be saved.", "read_only"));
8383
8409
  if (o.intent !== void 0 && o.mutationId === void 0) o = { ...o, mutationId: (await intentIds(this.collectionId, o.intent))[0] };
8384
8410
  const mutationId = o.mutationId ?? uuidv7();
8385
8411
  const p = { ops, mutationId };
@@ -8405,6 +8431,18 @@ var MdbaseSDK = (() => {
8405
8431
  } catch (e) {
8406
8432
  if (o.dryRun) throw e;
8407
8433
  void this.writeLedger.settle(mutationId);
8434
+ if (isReplicaResponseProblem(e) && [
8435
+ "invalid_request",
8436
+ "invalid_record",
8437
+ "not_found",
8438
+ "conflict",
8439
+ "unauthenticated",
8440
+ "forbidden",
8441
+ "collection_invalid",
8442
+ "rate_limited",
8443
+ "too_large",
8444
+ "upgrade_required"
8445
+ ].includes(e.code)) throw new WriteRefusal(e, { mutationId });
8408
8446
  throw withMutation(e, "submit", { mutationId });
8409
8447
  }
8410
8448
  return o.dryRun ? new Write(receipt4) : this._track(receipt4);