@naturalcycles/db-lib 10.52.0 → 10.53.0

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.
@@ -653,12 +653,17 @@ export class CommonDao {
653
653
  async compress(dbm) {
654
654
  if (!this.cfg.compress?.keys.length)
655
655
  return; // No compression requested
656
- const { keys, level = 1 } = this.cfg.compress;
656
+ const { keys, level = 1, warnSizeBytes, onOversizeWarning } = this.cfg.compress;
657
657
  const properties = _pick(dbm, keys);
658
658
  const bufferString = JSON.stringify(properties);
659
659
  // Unlike `decompress`, we're testing to use async zstd compression.
660
660
  // Async Decompression leaks memory severely. But Compression seems fine.
661
661
  const __compressed = await zip2.zstdCompress(bufferString, level);
662
+ if (warnSizeBytes && onOversizeWarning && __compressed.byteLength > warnSizeBytes) {
663
+ // Shallow-clone: `dbm` is mutated below (omit source keys + assign __compressed),
664
+ // so we hand the hook a snapshot of the pre-mutation state.
665
+ onOversizeWarning({ ...dbm }, __compressed.byteLength);
666
+ }
662
667
  _omitWithUndefined(dbm, _objectKeys(properties), { mutate: true });
663
668
  Object.assign(dbm, { __compressed });
664
669
  }
@@ -185,6 +185,22 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity
185
185
  * Undefined will default to level 1 (not the 3, which is the zstd default)
186
186
  */
187
187
  level?: Integer;
188
+ /**
189
+ * If set, `onOversizeWarning` is invoked whenever the resulting `__compressed`
190
+ * Buffer exceeds this size in bytes. Useful for monitoring rows approaching
191
+ * DB row-size limits (e.g. Datastore's 1MB per entity).
192
+ *
193
+ * No-op unless `onOversizeWarning` is also provided.
194
+ */
195
+ warnSizeBytes?: number;
196
+ /**
197
+ * Invoked when the `__compressed` Buffer exceeds `warnSizeBytes`.
198
+ *
199
+ * Called with a shallow clone of the DBM (since the original is mutated
200
+ * later in the compression step) and the resulting compressed size in bytes.
201
+ * The consumer decides how to surface the warning (Sentry, logger, metrics, etc.).
202
+ */
203
+ onOversizeWarning?: (dbm: DBM, size: number) => void;
188
204
  };
189
205
  }
190
206
  /**
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@naturalcycles/db-lib",
3
3
  "type": "module",
4
- "version": "10.52.0",
4
+ "version": "10.53.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@naturalcycles/nodejs-lib": "^15"
8
8
  },
9
9
  "devDependencies": {
10
10
  "@typescript/native-preview": "beta",
11
- "@naturalcycles/dev-lib": "20.48.0"
11
+ "@naturalcycles/dev-lib": "18.4.2"
12
12
  },
13
13
  "files": [
14
14
  "dist",
@@ -224,6 +224,22 @@ export interface CommonDaoCfg<
224
224
  * Undefined will default to level 1 (not the 3, which is the zstd default)
225
225
  */
226
226
  level?: Integer
227
+ /**
228
+ * If set, `onOversizeWarning` is invoked whenever the resulting `__compressed`
229
+ * Buffer exceeds this size in bytes. Useful for monitoring rows approaching
230
+ * DB row-size limits (e.g. Datastore's 1MB per entity).
231
+ *
232
+ * No-op unless `onOversizeWarning` is also provided.
233
+ */
234
+ warnSizeBytes?: number
235
+ /**
236
+ * Invoked when the `__compressed` Buffer exceeds `warnSizeBytes`.
237
+ *
238
+ * Called with a shallow clone of the DBM (since the original is mutated
239
+ * later in the compression step) and the resulting compressed size in bytes.
240
+ * The consumer decides how to surface the warning (Sentry, logger, metrics, etc.).
241
+ */
242
+ onOversizeWarning?: (dbm: DBM, size: number) => void
227
243
  }
228
244
  }
229
245
 
@@ -849,12 +849,17 @@ export class CommonDao<
849
849
  private async compress(dbm: DBM): Promise<void> {
850
850
  if (!this.cfg.compress?.keys.length) return // No compression requested
851
851
 
852
- const { keys, level = 1 } = this.cfg.compress
852
+ const { keys, level = 1, warnSizeBytes, onOversizeWarning } = this.cfg.compress
853
853
  const properties = _pick(dbm, keys)
854
854
  const bufferString = JSON.stringify(properties)
855
855
  // Unlike `decompress`, we're testing to use async zstd compression.
856
856
  // Async Decompression leaks memory severely. But Compression seems fine.
857
857
  const __compressed = await zip2.zstdCompress(bufferString, level)
858
+ if (warnSizeBytes && onOversizeWarning && __compressed.byteLength > warnSizeBytes) {
859
+ // Shallow-clone: `dbm` is mutated below (omit source keys + assign __compressed),
860
+ // so we hand the hook a snapshot of the pre-mutation state.
861
+ onOversizeWarning({ ...dbm }, __compressed.byteLength)
862
+ }
858
863
  _omitWithUndefined(dbm as any, _objectKeys(properties), { mutate: true })
859
864
  Object.assign(dbm, { __compressed })
860
865
  }