cry-synced-db-client 0.1.120 → 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 +18 -4
- package/dist/src/db/SyncedDb.d.ts +6 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -4127,6 +4127,9 @@ var SyncedDb = class _SyncedDb {
|
|
|
4127
4127
|
var _a, _b;
|
|
4128
4128
|
this.assertCollection(collection);
|
|
4129
4129
|
id = this.normalizeId(id);
|
|
4130
|
+
if ("_id" in update && !update._id) {
|
|
4131
|
+
delete update._id;
|
|
4132
|
+
}
|
|
4130
4133
|
update = _SyncedDb.stringifyObjectIds(update);
|
|
4131
4134
|
const existing = await this.dexieDb.getById(collection, id);
|
|
4132
4135
|
if (!existing && !((_a = this.collections.get(collection)) == null ? void 0 : _a.writeOnly)) {
|
|
@@ -4154,22 +4157,22 @@ var SyncedDb = class _SyncedDb {
|
|
|
4154
4157
|
}
|
|
4155
4158
|
async upsert(collection, query, update) {
|
|
4156
4159
|
this.assertCollection(collection);
|
|
4160
|
+
this.ensureId(update);
|
|
4157
4161
|
query = _SyncedDb.stringifyObjectIds(query);
|
|
4158
4162
|
update = _SyncedDb.stringifyObjectIds(update);
|
|
4159
4163
|
const existing = await this.findOne(collection, query);
|
|
4160
4164
|
if (existing) {
|
|
4161
4165
|
return this.save(collection, existing._id, update);
|
|
4162
4166
|
} else {
|
|
4163
|
-
|
|
4164
|
-
const newDoc = __spreadProps(__spreadValues({}, update), { _id: id });
|
|
4165
|
-
return this.insert(collection, newDoc);
|
|
4167
|
+
return this.insert(collection, update);
|
|
4166
4168
|
}
|
|
4167
4169
|
}
|
|
4168
4170
|
async insert(collection, data) {
|
|
4169
4171
|
var _a;
|
|
4170
4172
|
this.assertCollection(collection);
|
|
4173
|
+
this.ensureId(data);
|
|
4171
4174
|
data = _SyncedDb.stringifyObjectIds(data);
|
|
4172
|
-
const id =
|
|
4175
|
+
const id = String(data._id);
|
|
4173
4176
|
const existing = await this.dexieDb.getById(collection, id);
|
|
4174
4177
|
if (existing && !existing._deleted && !existing._archived) {
|
|
4175
4178
|
console.warn(
|
|
@@ -4656,6 +4659,17 @@ var SyncedDb = class _SyncedDb {
|
|
|
4656
4659
|
normalizeId(id) {
|
|
4657
4660
|
return typeof id === "object" && id !== null ? String(id) : id;
|
|
4658
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
|
+
}
|
|
4659
4673
|
/**
|
|
4660
4674
|
* Recursively stringify all ObjectId values in a document/query.
|
|
4661
4675
|
* Mirrors rdb2's preprocessForPack approach: any value with
|
|
@@ -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
|