cry-synced-db-client 0.1.122 → 0.1.124

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.
@@ -1,10 +1,16 @@
1
1
  # Versions
2
2
 
3
- ## 0.1.122 (2026-04-08)
3
+ ## 0.1.124 (2026-04-08)
4
4
 
5
- - Ensure `_id` is never falsy (undefined/null/0/"") in `upsert`, `insert`, and `save`
6
- - `upsert`/`insert`: falsy `_id` replaced with new ObjectId before any operation
5
+ - Fix `ensureId` not generating `_id` when key is absent (caused `"undefined"` string ids)
6
+
7
+ ## 0.1.123 (2026-04-08)
8
+
9
+ - Falsy `_id` guard on all public DB operations with detailed `console.error` messages
10
+ - `insert`/`upsert`: falsy `_id` replaced with new ObjectId before any operation
7
11
  - `save`: falsy `_id` stripped from update partial to prevent overwriting valid id
12
+ - `findById`/`findByIds`/`deleteOne`/`hardDeleteOne`: warn on falsy `id` parameter
13
+ - `findOne`/`find`/`deleteMany`/`hardDelete`: warn on falsy `_id` in query object
8
14
 
9
15
  ## 0.1.120 (2026-04-08)
10
16
 
package/dist/index.js CHANGED
@@ -3877,7 +3877,7 @@ var SyncedDb = class _SyncedDb {
3877
3877
  async findById(collection, id, opts) {
3878
3878
  var _a;
3879
3879
  this.assertCollection(collection);
3880
- id = this.normalizeId(id);
3880
+ id = this.normalizeId(id, "findById", collection);
3881
3881
  opts = this.resolveOpts(opts);
3882
3882
  if ((_a = this.collections.get(collection)) == null ? void 0 : _a.writeOnly) {
3883
3883
  return this.writeOnlyFindById(collection, id);
@@ -3922,7 +3922,7 @@ var SyncedDb = class _SyncedDb {
3922
3922
  async findByIds(collection, ids, opts) {
3923
3923
  var _a;
3924
3924
  this.assertCollection(collection);
3925
- ids = ids.map((id) => this.normalizeId(id));
3925
+ ids = ids.map((id) => this.normalizeId(id, "findByIds", collection));
3926
3926
  opts = this.resolveOpts(opts);
3927
3927
  if ((_a = this.collections.get(collection)) == null ? void 0 : _a.writeOnly) {
3928
3928
  return this.writeOnlyFindByIds(collection, ids);
@@ -3964,6 +3964,7 @@ var SyncedDb = class _SyncedDb {
3964
3964
  async findOne(collection, query, opts) {
3965
3965
  var _a, _b;
3966
3966
  this.assertCollection(collection);
3967
+ if (query) this.warnFalsyQueryId(query, "findOne", collection);
3967
3968
  query = _SyncedDb.stringifyObjectIds(query);
3968
3969
  opts = this.resolveOpts(opts);
3969
3970
  if ((_a = this.collections.get(collection)) == null ? void 0 : _a.writeOnly) {
@@ -4010,6 +4011,7 @@ var SyncedDb = class _SyncedDb {
4010
4011
  async find(collection, query, opts) {
4011
4012
  var _a;
4012
4013
  this.assertCollection(collection);
4014
+ if (query) this.warnFalsyQueryId(query, "find", collection);
4013
4015
  if (query) query = _SyncedDb.stringifyObjectIds(query);
4014
4016
  opts = this.resolveOpts(opts);
4015
4017
  if ((_a = this.collections.get(collection)) == null ? void 0 : _a.writeOnly) {
@@ -4126,8 +4128,11 @@ var SyncedDb = class _SyncedDb {
4126
4128
  async save(collection, id, update) {
4127
4129
  var _a, _b;
4128
4130
  this.assertCollection(collection);
4129
- id = this.normalizeId(id);
4131
+ id = this.normalizeId(id, "save", collection);
4130
4132
  if ("_id" in update && !update._id) {
4133
+ console.error(
4134
+ `SyncedDb.save("${collection}", "${String(id)}"): update._id is present but falsy (${JSON.stringify(update._id)}). Stripped from update to prevent overwriting valid id. This is a bug \u2014 the caller should not pass falsy _id in update. Data keys: [${Object.keys(update).join(", ")}]`
4135
+ );
4131
4136
  delete update._id;
4132
4137
  }
4133
4138
  update = _SyncedDb.stringifyObjectIds(update);
@@ -4157,7 +4162,7 @@ var SyncedDb = class _SyncedDb {
4157
4162
  }
4158
4163
  async upsert(collection, query, update) {
4159
4164
  this.assertCollection(collection);
4160
- this.ensureId(update);
4165
+ this.ensureId(update, "upsert", collection);
4161
4166
  query = _SyncedDb.stringifyObjectIds(query);
4162
4167
  update = _SyncedDb.stringifyObjectIds(update);
4163
4168
  const existing = await this.findOne(collection, query);
@@ -4170,7 +4175,7 @@ var SyncedDb = class _SyncedDb {
4170
4175
  async insert(collection, data) {
4171
4176
  var _a;
4172
4177
  this.assertCollection(collection);
4173
- this.ensureId(data);
4178
+ this.ensureId(data, "insert", collection);
4174
4179
  data = _SyncedDb.stringifyObjectIds(data);
4175
4180
  const id = String(data._id);
4176
4181
  const existing = await this.dexieDb.getById(collection, id);
@@ -4198,7 +4203,7 @@ var SyncedDb = class _SyncedDb {
4198
4203
  async deleteOne(collection, id) {
4199
4204
  var _a;
4200
4205
  this.assertCollection(collection);
4201
- id = this.normalizeId(id);
4206
+ id = this.normalizeId(id, "deleteOne", collection);
4202
4207
  const existing = await this.dexieDb.getById(collection, id);
4203
4208
  if (!existing || existing._deleted) {
4204
4209
  return null;
@@ -4221,6 +4226,7 @@ var SyncedDb = class _SyncedDb {
4221
4226
  }
4222
4227
  async deleteMany(collection, query) {
4223
4228
  this.assertCollection(collection);
4229
+ if (query) this.warnFalsyQueryId(query, "deleteMany", collection);
4224
4230
  const items = await this.find(collection, query);
4225
4231
  if (items.length === 0) return 0;
4226
4232
  const ids = items.map((item) => item._id);
@@ -4259,7 +4265,7 @@ var SyncedDb = class _SyncedDb {
4259
4265
  // ==================== Hard Delete Operations (online only) ====================
4260
4266
  async hardDeleteOne(collection, id) {
4261
4267
  this.assertCollection(collection);
4262
- id = this.normalizeId(id);
4268
+ id = this.normalizeId(id, "hardDeleteOne", collection);
4263
4269
  if (!this.isOnline()) {
4264
4270
  throw new Error("hardDeleteOne requires online connection");
4265
4271
  }
@@ -4277,6 +4283,7 @@ var SyncedDb = class _SyncedDb {
4277
4283
  }
4278
4284
  async hardDelete(collection, query) {
4279
4285
  this.assertCollection(collection);
4286
+ if (query) this.warnFalsyQueryId(query, "hardDelete", collection);
4280
4287
  if (!this.isOnline()) {
4281
4288
  throw new Error("hardDelete requires online connection");
4282
4289
  }
@@ -4656,17 +4663,40 @@ var SyncedDb = class _SyncedDb {
4656
4663
  }
4657
4664
  }
4658
4665
  /** Stringify an Id parameter (ObjectId → hex string). */
4659
- normalizeId(id) {
4666
+ normalizeId(id, method, collection) {
4667
+ if (!id && id !== void 0) {
4668
+ console.error(
4669
+ `SyncedDb.${method != null ? method : "?"}("${collection != null ? collection : "?"}"): id parameter is falsy (${JSON.stringify(id)}). This is a bug \u2014 the caller must provide a valid _id.`
4670
+ );
4671
+ }
4660
4672
  return typeof id === "object" && id !== null ? String(id) : id;
4661
4673
  }
4662
4674
  /**
4663
- * Ensure `_id` on a data object is never falsy.
4664
- * Replaces undefined / null / 0 / "" with a new ObjectId hex string.
4675
+ * Warn if a query/data object has `_id` present but falsy.
4676
+ * Does NOT mutate only logs.
4677
+ */
4678
+ warnFalsyQueryId(data, method, collection) {
4679
+ if ("_id" in data && !data._id) {
4680
+ console.error(
4681
+ `SyncedDb.${method}("${collection}"): _id is present in query/data but falsy (${JSON.stringify(data._id)}). This is a bug \u2014 _id must be valid when specified. Data keys: [${Object.keys(data).join(", ")}]`
4682
+ );
4683
+ }
4684
+ }
4685
+ /**
4686
+ * Ensure `_id` on a data object is never falsy or absent.
4687
+ * Replaces undefined / null / 0 / "" / missing with a new ObjectId hex string.
4688
+ * Logs console.error only when `_id` key is present but falsy (caller bug).
4665
4689
  * @mutates — modifies the object in-place and returns it.
4666
4690
  */
4667
- ensureId(data) {
4691
+ ensureId(data, method, collection) {
4668
4692
  if (!data._id) {
4669
- data._id = new ObjectId2().toHexString();
4693
+ const newId = new ObjectId2().toHexString();
4694
+ if ("_id" in data) {
4695
+ console.error(
4696
+ `SyncedDb.${method}("${collection}"): _id is present but falsy (${JSON.stringify(data._id)}). Replaced with "${newId}". This is a bug \u2014 the caller should provide a valid _id. Data keys: [${Object.keys(data).join(", ")}]`
4697
+ );
4698
+ }
4699
+ data._id = newId;
4670
4700
  }
4671
4701
  return data;
4672
4702
  }
@@ -211,8 +211,14 @@ export declare class SyncedDb implements I_SyncedDb {
211
211
  /** Stringify an Id parameter (ObjectId → hex string). */
212
212
  private normalizeId;
213
213
  /**
214
- * Ensure `_id` on a data object is never falsy.
215
- * Replaces undefined / null / 0 / "" with a new ObjectId hex string.
214
+ * Warn if a query/data object has `_id` present but falsy.
215
+ * Does NOT mutate only logs.
216
+ */
217
+ private warnFalsyQueryId;
218
+ /**
219
+ * Ensure `_id` on a data object is never falsy or absent.
220
+ * Replaces undefined / null / 0 / "" / missing with a new ObjectId hex string.
221
+ * Logs console.error only when `_id` key is present but falsy (caller bug).
216
222
  * @mutates — modifies the object in-place and returns it.
217
223
  */
218
224
  private ensureId;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cry-synced-db-client",
3
- "version": "0.1.122",
3
+ "version": "0.1.124",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -13,7 +13,7 @@
13
13
  },
14
14
  "files": [
15
15
  "dist",
16
- "VERSIONS.md"
16
+ "CHANGELOG.md"
17
17
  ],
18
18
  "scripts": {
19
19
  "clean": "rm -rf dist",