cry-synced-db-client 0.1.79 → 0.1.83
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 +40 -5
- package/dist/src/db/SyncedDb.d.ts +10 -0
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -3290,7 +3290,7 @@ var NetworkStatusManager = class {
|
|
|
3290
3290
|
};
|
|
3291
3291
|
|
|
3292
3292
|
// src/db/SyncedDb.ts
|
|
3293
|
-
var SyncedDb = class {
|
|
3293
|
+
var SyncedDb = class _SyncedDb {
|
|
3294
3294
|
constructor(config) {
|
|
3295
3295
|
this.collections = /* @__PURE__ */ new Map();
|
|
3296
3296
|
// State
|
|
@@ -3669,6 +3669,7 @@ var SyncedDb = class {
|
|
|
3669
3669
|
async findById(collection, id, opts) {
|
|
3670
3670
|
var _a;
|
|
3671
3671
|
this.assertCollection(collection);
|
|
3672
|
+
id = this.normalizeId(id);
|
|
3672
3673
|
opts = this.resolveOpts(opts);
|
|
3673
3674
|
if ((_a = this.collections.get(collection)) == null ? void 0 : _a.writeOnly) {
|
|
3674
3675
|
return this.writeOnlyFindById(collection, id);
|
|
@@ -3705,6 +3706,7 @@ var SyncedDb = class {
|
|
|
3705
3706
|
async findByIds(collection, ids, opts) {
|
|
3706
3707
|
var _a;
|
|
3707
3708
|
this.assertCollection(collection);
|
|
3709
|
+
ids = ids.map((id) => this.normalizeId(id));
|
|
3708
3710
|
opts = this.resolveOpts(opts);
|
|
3709
3711
|
if ((_a = this.collections.get(collection)) == null ? void 0 : _a.writeOnly) {
|
|
3710
3712
|
return this.writeOnlyFindByIds(collection, ids);
|
|
@@ -3743,6 +3745,7 @@ var SyncedDb = class {
|
|
|
3743
3745
|
async findOne(collection, query, opts) {
|
|
3744
3746
|
var _a, _b;
|
|
3745
3747
|
this.assertCollection(collection);
|
|
3748
|
+
query = _SyncedDb.stringifyObjectIds(query);
|
|
3746
3749
|
opts = this.resolveOpts(opts);
|
|
3747
3750
|
if ((_a = this.collections.get(collection)) == null ? void 0 : _a.writeOnly) {
|
|
3748
3751
|
return this.writeOnlyFindOne(collection, query);
|
|
@@ -3788,6 +3791,7 @@ var SyncedDb = class {
|
|
|
3788
3791
|
async find(collection, query, opts) {
|
|
3789
3792
|
var _a;
|
|
3790
3793
|
this.assertCollection(collection);
|
|
3794
|
+
if (query) query = _SyncedDb.stringifyObjectIds(query);
|
|
3791
3795
|
opts = this.resolveOpts(opts);
|
|
3792
3796
|
if ((_a = this.collections.get(collection)) == null ? void 0 : _a.writeOnly) {
|
|
3793
3797
|
return this.writeOnlyFind(collection, query);
|
|
@@ -3902,6 +3906,8 @@ var SyncedDb = class {
|
|
|
3902
3906
|
async save(collection, id, update) {
|
|
3903
3907
|
var _a;
|
|
3904
3908
|
this.assertCollection(collection);
|
|
3909
|
+
id = this.normalizeId(id);
|
|
3910
|
+
update = _SyncedDb.stringifyObjectIds(update);
|
|
3905
3911
|
const existing = await this.dexieDb.getById(collection, id);
|
|
3906
3912
|
if (!existing) {
|
|
3907
3913
|
console.warn(
|
|
@@ -3928,18 +3934,21 @@ var SyncedDb = class {
|
|
|
3928
3934
|
}
|
|
3929
3935
|
async upsert(collection, query, update) {
|
|
3930
3936
|
this.assertCollection(collection);
|
|
3937
|
+
query = _SyncedDb.stringifyObjectIds(query);
|
|
3938
|
+
update = _SyncedDb.stringifyObjectIds(update);
|
|
3931
3939
|
const existing = await this.findOne(collection, query);
|
|
3932
3940
|
if (existing) {
|
|
3933
3941
|
return this.save(collection, existing._id, update);
|
|
3934
3942
|
} else {
|
|
3935
3943
|
const id = new ObjectId2().toHexString();
|
|
3936
|
-
const newDoc = __spreadValues({ _id: id }
|
|
3944
|
+
const newDoc = __spreadProps(__spreadValues({}, update), { _id: id });
|
|
3937
3945
|
return this.insert(collection, newDoc);
|
|
3938
3946
|
}
|
|
3939
3947
|
}
|
|
3940
3948
|
async insert(collection, data) {
|
|
3941
3949
|
var _a;
|
|
3942
3950
|
this.assertCollection(collection);
|
|
3951
|
+
data = _SyncedDb.stringifyObjectIds(data);
|
|
3943
3952
|
const id = data._id ? String(data._id) : new ObjectId2().toHexString();
|
|
3944
3953
|
const existing = await this.dexieDb.getById(collection, id);
|
|
3945
3954
|
if (existing && !existing._deleted && !existing._archived) {
|
|
@@ -3953,9 +3962,8 @@ var SyncedDb = class {
|
|
|
3953
3962
|
_ts: void 0,
|
|
3954
3963
|
_rev: void 0
|
|
3955
3964
|
});
|
|
3956
|
-
const newData = __spreadProps(__spreadValues({
|
|
3957
|
-
_id: id
|
|
3958
|
-
}, data), {
|
|
3965
|
+
const newData = __spreadProps(__spreadValues({}, data), {
|
|
3966
|
+
_id: id,
|
|
3959
3967
|
_lastUpdaterId: this.updaterId
|
|
3960
3968
|
});
|
|
3961
3969
|
this.pendingChanges.schedule(collection, id, newData, 0, "insert");
|
|
@@ -3967,6 +3975,7 @@ var SyncedDb = class {
|
|
|
3967
3975
|
async deleteOne(collection, id) {
|
|
3968
3976
|
var _a;
|
|
3969
3977
|
this.assertCollection(collection);
|
|
3978
|
+
id = this.normalizeId(id);
|
|
3970
3979
|
const existing = await this.dexieDb.getById(collection, id);
|
|
3971
3980
|
if (!existing || existing._deleted) {
|
|
3972
3981
|
return null;
|
|
@@ -4027,6 +4036,7 @@ var SyncedDb = class {
|
|
|
4027
4036
|
// ==================== Hard Delete Operations (online only) ====================
|
|
4028
4037
|
async hardDeleteOne(collection, id) {
|
|
4029
4038
|
this.assertCollection(collection);
|
|
4039
|
+
id = this.normalizeId(id);
|
|
4030
4040
|
if (!this.isOnline()) {
|
|
4031
4041
|
throw new Error("hardDeleteOne requires online connection");
|
|
4032
4042
|
}
|
|
@@ -4332,6 +4342,31 @@ var SyncedDb = class {
|
|
|
4332
4342
|
throw new Error(`Collection "${name}" not configured`);
|
|
4333
4343
|
}
|
|
4334
4344
|
}
|
|
4345
|
+
/** Stringify an Id parameter (ObjectId → hex string). */
|
|
4346
|
+
normalizeId(id) {
|
|
4347
|
+
return typeof id === "object" && id !== null ? String(id) : id;
|
|
4348
|
+
}
|
|
4349
|
+
/**
|
|
4350
|
+
* Recursively stringify all ObjectId values in a document/query.
|
|
4351
|
+
* Mirrors rdb2's preprocessForPack approach: any value with
|
|
4352
|
+
* _bsontype==="ObjectId" or a toHexString() method is converted to string.
|
|
4353
|
+
* Ensures Dexie compatibility and correct local query matching.
|
|
4354
|
+
*/
|
|
4355
|
+
static stringifyObjectIds(data) {
|
|
4356
|
+
if (data === null || data === void 0) return data;
|
|
4357
|
+
if (_SyncedDb.isObjectIdLike(data)) return String(data);
|
|
4358
|
+
if (typeof data !== "object") return data;
|
|
4359
|
+
if (data instanceof Date) return data;
|
|
4360
|
+
if (Array.isArray(data)) return data.map((v) => _SyncedDb.stringifyObjectIds(v));
|
|
4361
|
+
const result = {};
|
|
4362
|
+
for (const key of Object.keys(data)) {
|
|
4363
|
+
result[key] = _SyncedDb.stringifyObjectIds(data[key]);
|
|
4364
|
+
}
|
|
4365
|
+
return result;
|
|
4366
|
+
}
|
|
4367
|
+
static isObjectIdLike(v) {
|
|
4368
|
+
return !!(v && typeof v === "object" && (v._bsontype === "ObjectId" || typeof v.toHexString === "function"));
|
|
4369
|
+
}
|
|
4335
4370
|
/**
|
|
4336
4371
|
* Asserts write-only collection has online connectivity for reads.
|
|
4337
4372
|
* @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.83",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"@types/bun": "latest",
|
|
29
29
|
"bson": "^7.2.0",
|
|
30
30
|
"cry-ebus-proxy": "^1.0.3",
|
|
31
|
-
"dexie": "^4.4.
|
|
31
|
+
"dexie": "^4.4.2",
|
|
32
32
|
"esbuild": "^0.27.4",
|
|
33
33
|
"fake-indexeddb": "^6.2.5",
|
|
34
34
|
"typescript": "^6",
|
|
@@ -37,6 +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.81",
|
|
40
41
|
"msgpackr": "^1.11.9",
|
|
41
42
|
"notepack": "^0.0.2",
|
|
42
43
|
"notepack.io": "^3.0.1",
|