cry-synced-db-client 0.1.119 → 0.1.121

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.js CHANGED
@@ -1087,7 +1087,7 @@ var ConnectionManager = class {
1087
1087
  // Private Methods
1088
1088
  // ============================================================
1089
1089
  async tryGoOnline() {
1090
- var _a, _b, _c, _d;
1090
+ var _a, _b, _c, _d, _e;
1091
1091
  if (this.forcedOffline) {
1092
1092
  return;
1093
1093
  }
@@ -1097,13 +1097,14 @@ var ConnectionManager = class {
1097
1097
  "ping"
1098
1098
  );
1099
1099
  if (!pingResult) {
1100
- console.warn("Ping failed - staying offline");
1100
+ const url = (_a = this.restInterface.endpoint) != null ? _a : "unknown";
1101
+ console.warn(`Ping to ${url} failed - staying offline`);
1101
1102
  return;
1102
1103
  }
1103
1104
  const wasOffline = !this.online;
1104
1105
  this.online = true;
1105
1106
  if (wasOffline) {
1106
- (_b = (_a = this.callbacks).onOnlineStatusChange) == null ? void 0 : _b.call(_a, true);
1107
+ (_c = (_b = this.callbacks).onOnlineStatusChange) == null ? void 0 : _c.call(_b, true);
1107
1108
  if (!this.deps.isLeader()) {
1108
1109
  this.deps.tryBecomeLeader();
1109
1110
  }
@@ -1115,7 +1116,7 @@ var ConnectionManager = class {
1115
1116
  const wasOnline = this.online;
1116
1117
  this.online = false;
1117
1118
  if (wasOnline) {
1118
- (_d = (_c = this.callbacks).onOnlineStatusChange) == null ? void 0 : _d.call(_c, false);
1119
+ (_e = (_d = this.callbacks).onOnlineStatusChange) == null ? void 0 : _e.call(_d, false);
1119
1120
  }
1120
1121
  }
1121
1122
  }
@@ -3691,7 +3692,7 @@ var SyncedDb = class _SyncedDb {
3691
3692
  }
3692
3693
  // ==================== Lifecycle ====================
3693
3694
  async init() {
3694
- var _a, _b;
3695
+ var _a, _b, _c, _d;
3695
3696
  if (this.initialized) return;
3696
3697
  if (typeof localStorage === "undefined") {
3697
3698
  this.connectionManager.reportInfrastructureError(
@@ -3741,10 +3742,14 @@ var SyncedDb = class _SyncedDb {
3741
3742
  );
3742
3743
  try {
3743
3744
  await this.serverUpdateNotifier.connect();
3745
+ const ep = (_c = this.serverUpdateNotifier.endpoint) != null ? _c : "unknown";
3746
+ console.log(`SyncedDb: ebus-proxy connected to ${ep}`);
3744
3747
  } catch (err) {
3748
+ const ep = (_d = this.serverUpdateNotifier.endpoint) != null ? _d : "unknown";
3749
+ console.warn(`SyncedDb: ebus-proxy connection to ${ep} failed`);
3745
3750
  this.connectionManager.reportInfrastructureError(
3746
3751
  "WEBSOCKET_CONNECTION_FAILED",
3747
- "WebSocket connection failed during initialization",
3752
+ `WebSocket connection to ${ep} failed during initialization`,
3748
3753
  err instanceof Error ? err : new Error(String(err))
3749
3754
  );
3750
3755
  }
@@ -4122,6 +4127,9 @@ var SyncedDb = class _SyncedDb {
4122
4127
  var _a, _b;
4123
4128
  this.assertCollection(collection);
4124
4129
  id = this.normalizeId(id);
4130
+ if ("_id" in update && !update._id) {
4131
+ delete update._id;
4132
+ }
4125
4133
  update = _SyncedDb.stringifyObjectIds(update);
4126
4134
  const existing = await this.dexieDb.getById(collection, id);
4127
4135
  if (!existing && !((_a = this.collections.get(collection)) == null ? void 0 : _a.writeOnly)) {
@@ -4149,22 +4157,22 @@ var SyncedDb = class _SyncedDb {
4149
4157
  }
4150
4158
  async upsert(collection, query, update) {
4151
4159
  this.assertCollection(collection);
4160
+ this.ensureId(update);
4152
4161
  query = _SyncedDb.stringifyObjectIds(query);
4153
4162
  update = _SyncedDb.stringifyObjectIds(update);
4154
4163
  const existing = await this.findOne(collection, query);
4155
4164
  if (existing) {
4156
4165
  return this.save(collection, existing._id, update);
4157
4166
  } else {
4158
- const id = new ObjectId2().toHexString();
4159
- const newDoc = __spreadProps(__spreadValues({}, update), { _id: id });
4160
- return this.insert(collection, newDoc);
4167
+ return this.insert(collection, update);
4161
4168
  }
4162
4169
  }
4163
4170
  async insert(collection, data) {
4164
4171
  var _a;
4165
4172
  this.assertCollection(collection);
4173
+ this.ensureId(data);
4166
4174
  data = _SyncedDb.stringifyObjectIds(data);
4167
- const id = data._id ? String(data._id) : new ObjectId2().toHexString();
4175
+ const id = String(data._id);
4168
4176
  const existing = await this.dexieDb.getById(collection, id);
4169
4177
  if (existing && !existing._deleted && !existing._archived) {
4170
4178
  console.warn(
@@ -4651,6 +4659,17 @@ var SyncedDb = class _SyncedDb {
4651
4659
  normalizeId(id) {
4652
4660
  return typeof id === "object" && id !== null ? String(id) : id;
4653
4661
  }
4662
+ /**
4663
+ * Ensure `_id` on a data object is never falsy.
4664
+ * Replaces undefined / null / 0 / "" with a new ObjectId hex string.
4665
+ * @mutates — modifies the object in-place and returns it.
4666
+ */
4667
+ ensureId(data) {
4668
+ if (!data._id) {
4669
+ data._id = new ObjectId2().toHexString();
4670
+ }
4671
+ return data;
4672
+ }
4654
4673
  /**
4655
4674
  * Recursively stringify all ObjectId values in a document/query.
4656
4675
  * Mirrors rdb2's preprocessForPack approach: any value with
@@ -7523,6 +7542,7 @@ var Ebus2ProxyServerUpdateNotifier = class {
7523
7542
  this.forcedOffline = false;
7524
7543
  this.subscribedChannels = /* @__PURE__ */ new Set();
7525
7544
  var _a, _b, _c, _d, _e;
7545
+ this.endpoint = config.wsUrl;
7526
7546
  this.wsUrl = config.wsUrl;
7527
7547
  this.dbName = config.dbName;
7528
7548
  this.collections = config.collections;
@@ -7670,6 +7690,9 @@ var Ebus2ProxyServerUpdateNotifier = class {
7670
7690
  this.connected = true;
7671
7691
  this.reconnectAttempt = 0;
7672
7692
  this.currentReconnectDelay = this.reconnectDelayMs;
7693
+ console.log(
7694
+ `Ebus2Proxy connected to ${this.wsUrl} (db: ${this.dbName})`
7695
+ );
7673
7696
  this.sendSubscribe(`db/${this.dbName}`);
7674
7697
  if (this.subscribeServices) {
7675
7698
  this.sendSubscribe("ebus/services");
@@ -36,6 +36,7 @@ export interface EbusService {
36
36
  requests: number;
37
37
  }
38
38
  export declare class Ebus2ProxyServerUpdateNotifier implements I_ServerUpdateNotifier {
39
+ readonly endpoint: string;
39
40
  private wsUrl;
40
41
  private dbName;
41
42
  private collections;
@@ -61,7 +61,7 @@ export interface RestProxyConfig {
61
61
  * complex types (Date, Map, Set, RegExp, etc.)
62
62
  */
63
63
  export declare class RestProxy implements I_RestInterface {
64
- private endpoint;
64
+ readonly endpoint: string;
65
65
  private tenant;
66
66
  private apiKey?;
67
67
  private defaultTimeoutMs;
@@ -210,6 +210,12 @@ export declare class SyncedDb implements I_SyncedDb {
210
210
  private assertCollection;
211
211
  /** Stringify an Id parameter (ObjectId → hex string). */
212
212
  private normalizeId;
213
+ /**
214
+ * Ensure `_id` on a data object is never falsy.
215
+ * Replaces undefined / null / 0 / "" with a new ObjectId hex string.
216
+ * @mutates — modifies the object in-place and returns it.
217
+ */
218
+ private ensureId;
213
219
  /**
214
220
  * Recursively stringify all ObjectId values in a document/query.
215
221
  * Mirrors rdb2's preprocessForPack approach: any value with
@@ -65,6 +65,8 @@ export type CollectionUpdateRequest<T> = {
65
65
  * Vsebuje samo metode, potrebne za sinhronizacijo
66
66
  */
67
67
  export interface I_RestInterface {
68
+ /** Server endpoint URL (for diagnostics/logging). */
69
+ readonly endpoint?: string;
68
70
  /** Preveri povezljivost s serverjem - vrne true če je server dosegljiv */
69
71
  ping(): Promise<boolean>;
70
72
  /** Fetch a single item by ID, returns null if not found */
@@ -20,6 +20,8 @@ export interface ServerUpdateNotifierCallbacks {
20
20
  * Implementacija je odvisna od transporta (WebSocket, SSE, polling, etc.)
21
21
  */
22
22
  export interface I_ServerUpdateNotifier {
23
+ /** Server/WebSocket endpoint URL (for diagnostics/logging). */
24
+ readonly endpoint?: string;
23
25
  /**
24
26
  * Registriraj callback za prejemanje update notifikacij
25
27
  * @param callback Funkcija, ki se pokliče ob vsaki notifikaciji
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cry-synced-db-client",
3
- "version": "0.1.119",
3
+ "version": "0.1.121",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",