cry-synced-db-client 0.1.81 → 0.1.84
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 +49 -9
- package/dist/src/db/SyncedDb.d.ts +10 -0
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1970,9 +1970,9 @@ var PendingChangesManager = class {
|
|
|
1970
1970
|
if (existing) {
|
|
1971
1971
|
clearTimeout(existing.timer);
|
|
1972
1972
|
}
|
|
1973
|
-
const deltaWithId = existing ? data : __spreadValues({ _id: id }
|
|
1973
|
+
const deltaWithId = existing ? data : __spreadProps(__spreadValues({}, data), { _id: id });
|
|
1974
1974
|
savePendingWrite(this.tenant, collection, id, deltaWithId);
|
|
1975
|
-
const fullData = existing ? Object.assign(existing.data, data) : __spreadValues({ _id: id }
|
|
1975
|
+
const fullData = existing ? Object.assign(existing.data, data, { _id: id }) : __spreadProps(__spreadValues({}, data), { _id: id });
|
|
1976
1976
|
const timer = setTimeout(() => {
|
|
1977
1977
|
this.executePendingChange(key);
|
|
1978
1978
|
}, this.debounceDexieWritesMs);
|
|
@@ -2144,9 +2144,14 @@ var PendingChangesManager = class {
|
|
|
2144
2144
|
pending.data
|
|
2145
2145
|
);
|
|
2146
2146
|
} else {
|
|
2147
|
-
|
|
2147
|
+
const insertData = __spreadProps(__spreadValues({}, pending.data), {
|
|
2148
2148
|
_id: pending.id
|
|
2149
|
-
|
|
2149
|
+
// ensure _id is after spread
|
|
2150
|
+
});
|
|
2151
|
+
if (typeof insertData._id === "object") {
|
|
2152
|
+
console.error(`Dexie: _id is object type in ${pending.collection}:`, typeof insertData._id, insertData._id);
|
|
2153
|
+
}
|
|
2154
|
+
await this.deps.dexieDb.insert(pending.collection, insertData);
|
|
2150
2155
|
}
|
|
2151
2156
|
if (this.callbacks.onDexieWriteResult) {
|
|
2152
2157
|
try {
|
|
@@ -3290,7 +3295,7 @@ var NetworkStatusManager = class {
|
|
|
3290
3295
|
};
|
|
3291
3296
|
|
|
3292
3297
|
// src/db/SyncedDb.ts
|
|
3293
|
-
var SyncedDb = class {
|
|
3298
|
+
var SyncedDb = class _SyncedDb {
|
|
3294
3299
|
constructor(config) {
|
|
3295
3300
|
this.collections = /* @__PURE__ */ new Map();
|
|
3296
3301
|
// State
|
|
@@ -3669,6 +3674,7 @@ var SyncedDb = class {
|
|
|
3669
3674
|
async findById(collection, id, opts) {
|
|
3670
3675
|
var _a;
|
|
3671
3676
|
this.assertCollection(collection);
|
|
3677
|
+
id = this.normalizeId(id);
|
|
3672
3678
|
opts = this.resolveOpts(opts);
|
|
3673
3679
|
if ((_a = this.collections.get(collection)) == null ? void 0 : _a.writeOnly) {
|
|
3674
3680
|
return this.writeOnlyFindById(collection, id);
|
|
@@ -3705,6 +3711,7 @@ var SyncedDb = class {
|
|
|
3705
3711
|
async findByIds(collection, ids, opts) {
|
|
3706
3712
|
var _a;
|
|
3707
3713
|
this.assertCollection(collection);
|
|
3714
|
+
ids = ids.map((id) => this.normalizeId(id));
|
|
3708
3715
|
opts = this.resolveOpts(opts);
|
|
3709
3716
|
if ((_a = this.collections.get(collection)) == null ? void 0 : _a.writeOnly) {
|
|
3710
3717
|
return this.writeOnlyFindByIds(collection, ids);
|
|
@@ -3743,6 +3750,7 @@ var SyncedDb = class {
|
|
|
3743
3750
|
async findOne(collection, query, opts) {
|
|
3744
3751
|
var _a, _b;
|
|
3745
3752
|
this.assertCollection(collection);
|
|
3753
|
+
query = _SyncedDb.stringifyObjectIds(query);
|
|
3746
3754
|
opts = this.resolveOpts(opts);
|
|
3747
3755
|
if ((_a = this.collections.get(collection)) == null ? void 0 : _a.writeOnly) {
|
|
3748
3756
|
return this.writeOnlyFindOne(collection, query);
|
|
@@ -3788,6 +3796,7 @@ var SyncedDb = class {
|
|
|
3788
3796
|
async find(collection, query, opts) {
|
|
3789
3797
|
var _a;
|
|
3790
3798
|
this.assertCollection(collection);
|
|
3799
|
+
if (query) query = _SyncedDb.stringifyObjectIds(query);
|
|
3791
3800
|
opts = this.resolveOpts(opts);
|
|
3792
3801
|
if ((_a = this.collections.get(collection)) == null ? void 0 : _a.writeOnly) {
|
|
3793
3802
|
return this.writeOnlyFind(collection, query);
|
|
@@ -3902,6 +3911,8 @@ var SyncedDb = class {
|
|
|
3902
3911
|
async save(collection, id, update) {
|
|
3903
3912
|
var _a;
|
|
3904
3913
|
this.assertCollection(collection);
|
|
3914
|
+
id = this.normalizeId(id);
|
|
3915
|
+
update = _SyncedDb.stringifyObjectIds(update);
|
|
3905
3916
|
const existing = await this.dexieDb.getById(collection, id);
|
|
3906
3917
|
if (!existing) {
|
|
3907
3918
|
console.warn(
|
|
@@ -3928,18 +3939,21 @@ var SyncedDb = class {
|
|
|
3928
3939
|
}
|
|
3929
3940
|
async upsert(collection, query, update) {
|
|
3930
3941
|
this.assertCollection(collection);
|
|
3942
|
+
query = _SyncedDb.stringifyObjectIds(query);
|
|
3943
|
+
update = _SyncedDb.stringifyObjectIds(update);
|
|
3931
3944
|
const existing = await this.findOne(collection, query);
|
|
3932
3945
|
if (existing) {
|
|
3933
3946
|
return this.save(collection, existing._id, update);
|
|
3934
3947
|
} else {
|
|
3935
3948
|
const id = new ObjectId2().toHexString();
|
|
3936
|
-
const newDoc = __spreadValues({ _id: id }
|
|
3949
|
+
const newDoc = __spreadProps(__spreadValues({}, update), { _id: id });
|
|
3937
3950
|
return this.insert(collection, newDoc);
|
|
3938
3951
|
}
|
|
3939
3952
|
}
|
|
3940
3953
|
async insert(collection, data) {
|
|
3941
3954
|
var _a;
|
|
3942
3955
|
this.assertCollection(collection);
|
|
3956
|
+
data = _SyncedDb.stringifyObjectIds(data);
|
|
3943
3957
|
const id = data._id ? String(data._id) : new ObjectId2().toHexString();
|
|
3944
3958
|
const existing = await this.dexieDb.getById(collection, id);
|
|
3945
3959
|
if (existing && !existing._deleted && !existing._archived) {
|
|
@@ -3953,9 +3967,8 @@ var SyncedDb = class {
|
|
|
3953
3967
|
_ts: void 0,
|
|
3954
3968
|
_rev: void 0
|
|
3955
3969
|
});
|
|
3956
|
-
const newData = __spreadProps(__spreadValues({
|
|
3957
|
-
_id: id
|
|
3958
|
-
}, data), {
|
|
3970
|
+
const newData = __spreadProps(__spreadValues({}, data), {
|
|
3971
|
+
_id: id,
|
|
3959
3972
|
_lastUpdaterId: this.updaterId
|
|
3960
3973
|
});
|
|
3961
3974
|
this.pendingChanges.schedule(collection, id, newData, 0, "insert");
|
|
@@ -3967,6 +3980,7 @@ var SyncedDb = class {
|
|
|
3967
3980
|
async deleteOne(collection, id) {
|
|
3968
3981
|
var _a;
|
|
3969
3982
|
this.assertCollection(collection);
|
|
3983
|
+
id = this.normalizeId(id);
|
|
3970
3984
|
const existing = await this.dexieDb.getById(collection, id);
|
|
3971
3985
|
if (!existing || existing._deleted) {
|
|
3972
3986
|
return null;
|
|
@@ -4027,6 +4041,7 @@ var SyncedDb = class {
|
|
|
4027
4041
|
// ==================== Hard Delete Operations (online only) ====================
|
|
4028
4042
|
async hardDeleteOne(collection, id) {
|
|
4029
4043
|
this.assertCollection(collection);
|
|
4044
|
+
id = this.normalizeId(id);
|
|
4030
4045
|
if (!this.isOnline()) {
|
|
4031
4046
|
throw new Error("hardDeleteOne requires online connection");
|
|
4032
4047
|
}
|
|
@@ -4332,6 +4347,31 @@ var SyncedDb = class {
|
|
|
4332
4347
|
throw new Error(`Collection "${name}" not configured`);
|
|
4333
4348
|
}
|
|
4334
4349
|
}
|
|
4350
|
+
/** Stringify an Id parameter (ObjectId → hex string). */
|
|
4351
|
+
normalizeId(id) {
|
|
4352
|
+
return typeof id === "object" && id !== null ? String(id) : id;
|
|
4353
|
+
}
|
|
4354
|
+
/**
|
|
4355
|
+
* Recursively stringify all ObjectId values in a document/query.
|
|
4356
|
+
* Mirrors rdb2's preprocessForPack approach: any value with
|
|
4357
|
+
* _bsontype==="ObjectId" or a toHexString() method is converted to string.
|
|
4358
|
+
* Ensures Dexie compatibility and correct local query matching.
|
|
4359
|
+
*/
|
|
4360
|
+
static stringifyObjectIds(data) {
|
|
4361
|
+
if (data === null || data === void 0) return data;
|
|
4362
|
+
if (_SyncedDb.isObjectIdLike(data)) return String(data);
|
|
4363
|
+
if (typeof data !== "object") return data;
|
|
4364
|
+
if (data instanceof Date) return data;
|
|
4365
|
+
if (Array.isArray(data)) return data.map((v) => _SyncedDb.stringifyObjectIds(v));
|
|
4366
|
+
const result = {};
|
|
4367
|
+
for (const key of Object.keys(data)) {
|
|
4368
|
+
result[key] = _SyncedDb.stringifyObjectIds(data[key]);
|
|
4369
|
+
}
|
|
4370
|
+
return result;
|
|
4371
|
+
}
|
|
4372
|
+
static isObjectIdLike(v) {
|
|
4373
|
+
return !!(v && typeof v === "object" && (v._bsontype === "ObjectId" || typeof v.toHexString === "function"));
|
|
4374
|
+
}
|
|
4335
4375
|
/**
|
|
4336
4376
|
* Asserts write-only collection has online connectivity for reads.
|
|
4337
4377
|
* @throws Error if offline
|
|
@@ -142,6 +142,16 @@ export declare class SyncedDb implements I_SyncedDb {
|
|
|
142
142
|
*/
|
|
143
143
|
private loadCollectionToInMem;
|
|
144
144
|
private assertCollection;
|
|
145
|
+
/** Stringify an Id parameter (ObjectId → hex string). */
|
|
146
|
+
private normalizeId;
|
|
147
|
+
/**
|
|
148
|
+
* Recursively stringify all ObjectId values in a document/query.
|
|
149
|
+
* Mirrors rdb2's preprocessForPack approach: any value with
|
|
150
|
+
* _bsontype==="ObjectId" or a toHexString() method is converted to string.
|
|
151
|
+
* Ensures Dexie compatibility and correct local query matching.
|
|
152
|
+
*/
|
|
153
|
+
private static stringifyObjectIds;
|
|
154
|
+
private static isObjectIdLike;
|
|
145
155
|
/**
|
|
146
156
|
* Asserts write-only collection has online connectivity for reads.
|
|
147
157
|
* @throws Error if offline
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cry-synced-db-client",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.84",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"cry-db": "^2.4.23",
|
|
39
39
|
"cry-helpers": "^2.1.193",
|
|
40
|
-
"cry-synced-db-client": "0.1.
|
|
40
|
+
"cry-synced-db-client": "0.1.83",
|
|
41
41
|
"msgpackr": "^1.11.9",
|
|
42
42
|
"notepack": "^0.0.2",
|
|
43
43
|
"notepack.io": "^3.0.1",
|