From dd1a42b00af381e9149be8498859477b65a03210 Mon Sep 17 00:00:00 2001 From: daveycodez Date: Thu, 9 Oct 2025 21:36:48 -0700 Subject: [PATCH 1/5] Refactor types and cleanup in rxdb.ts Replaces generic 'any' types with more specific types such as 'Subscription' and 'Record' for improved type safety. Removes unnecessary type assertions and updates function signatures and variable declarations accordingly. --- packages/rxdb-db-collection/src/rxdb.ts | 30 ++++++++++++++----------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/packages/rxdb-db-collection/src/rxdb.ts b/packages/rxdb-db-collection/src/rxdb.ts index 6c1c12968..a8340e0c5 100644 --- a/packages/rxdb-db-collection/src/rxdb.ts +++ b/packages/rxdb-db-collection/src/rxdb.ts @@ -13,6 +13,7 @@ import type { RxCollection, RxDocumentData, } from "rxdb/plugins/core" +import type { Subscription } from "rxjs" import type { BaseCollectionConfig, @@ -27,7 +28,10 @@ const debug = DebugModule.debug(`ts/db:rxdb`) /** * Used in tests to ensure proper cleanup */ -export const OPEN_RXDB_SUBSCRIPTIONS = new WeakMap>() +export const OPEN_RXDB_SUBSCRIPTIONS = new WeakMap< + RxCollection, + Set +>() /** * Configuration interface for RxDB collection options @@ -106,7 +110,7 @@ export function rxdbCollectionOptions(config: RxDBCollectionConfig) { // "getKey" const primaryPath = rxCollection.schema.primaryPath - function getKey(item: any): string { + function getKey(item: Record): string { const key: string = item[primaryPath] as string return key } @@ -147,15 +151,15 @@ export function rxdbCollectionOptions(config: RxDBCollectionConfig) { }, }, ], - } as any, - sort: [{ "_meta.lwt": `asc` }, { [primaryPath]: `asc` } as any], + }, + sort: [{ "_meta.lwt": `asc` }, { [primaryPath]: `asc` }], limit: syncBatchSize, skip: 0, } } else { query = { selector: {}, - sort: [{ "_meta.lwt": `asc` }, { [primaryPath]: `asc` } as any], + sort: [{ "_meta.lwt": `asc` }, { [primaryPath]: `asc` }], limit: syncBatchSize, skip: 0, } @@ -198,11 +202,11 @@ export function rxdbCollectionOptions(config: RxDBCollectionConfig) { return } begin() - write(msg as any) + write(msg) commit() } - let sub: any + let sub: Subscription function startOngoingFetch() { // Subscribe early and buffer live changes during initial load and ongoing sub = rxCollection.$.subscribe((ev) => { @@ -234,7 +238,7 @@ export function rxdbCollectionOptions(config: RxDBCollectionConfig) { if (buffer.length) { begin() - for (const msg of buffer) write(msg as any) + for (const msg of buffer) write(msg) commit() buffer.length = 0 } @@ -258,14 +262,14 @@ export function rxdbCollectionOptions(config: RxDBCollectionConfig) { getSyncMetadata: undefined, } - const collectionConfig: CollectionConfig = { + const collectionConfig: CollectionConfig = { ...restConfig, - getKey: getKey as any, + getKey, sync, onInsert: async (params) => { debug(`insert`, params) const newItems = params.transaction.mutations.map((m) => m.modified) - return rxCollection.bulkUpsert(newItems as Array).then((result) => { + return rxCollection.bulkUpsert(newItems).then((result) => { if (result.error.length > 0) { throw rxStorageWriteErrorToRxError(ensureNotFalsy(result.error[0])) } @@ -285,7 +289,7 @@ export function rxdbCollectionOptions(config: RxDBCollectionConfig) { if (!doc) { continue } - await doc.incrementalPatch(newValue as any) + await doc.incrementalPatch(newValue) } }, onDelete: async (params) => { @@ -293,7 +297,7 @@ export function rxdbCollectionOptions(config: RxDBCollectionConfig) { const mutations = params.transaction.mutations.filter( (m) => m.type === `delete` ) - const ids = mutations.map((mutation: any) => getKey(mutation.original)) + const ids = mutations.map((mutation) => getKey(mutation.original)) return rxCollection.bulkRemove(ids).then((result) => { if (result.error.length > 0) { throw result.error From d393f6d013c5cfc0ac7669041609525a290c8ce8 Mon Sep 17 00:00:00 2001 From: daveycodez Date: Thu, 9 Oct 2025 21:39:37 -0700 Subject: [PATCH 2/5] Filter out deleted documents in sync queries Added '_deleted': false to query selectors to ensure only non-deleted documents are included during synchronization. This improves data consistency by excluding soft-deleted records from sync operations. --- packages/rxdb-db-collection/src/rxdb.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/rxdb-db-collection/src/rxdb.ts b/packages/rxdb-db-collection/src/rxdb.ts index a8340e0c5..dfb1f9011 100644 --- a/packages/rxdb-db-collection/src/rxdb.ts +++ b/packages/rxdb-db-collection/src/rxdb.ts @@ -151,6 +151,7 @@ export function rxdbCollectionOptions(config: RxDBCollectionConfig) { }, }, ], + _deleted: false, }, sort: [{ "_meta.lwt": `asc` }, { [primaryPath]: `asc` }], limit: syncBatchSize, @@ -158,7 +159,7 @@ export function rxdbCollectionOptions(config: RxDBCollectionConfig) { } } else { query = { - selector: {}, + selector: { _deleted: false }, sort: [{ "_meta.lwt": `asc` }, { [primaryPath]: `asc` }], limit: syncBatchSize, skip: 0, From c765a540099c79243b9675d2e090223e77883a06 Mon Sep 17 00:00:00 2001 From: daveycodez Date: Thu, 9 Oct 2025 21:53:57 -0700 Subject: [PATCH 3/5] Refactor stripRxdbFields function signature Updated the stripRxdbFields function to use a stricter type for the input parameter and removed unnecessary handling for arrays and nullish values. --- packages/rxdb-db-collection/src/helper.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/rxdb-db-collection/src/helper.ts b/packages/rxdb-db-collection/src/helper.ts index 096005edd..151ca1fc8 100644 --- a/packages/rxdb-db-collection/src/helper.ts +++ b/packages/rxdb-db-collection/src/helper.ts @@ -5,11 +5,8 @@ const RESERVED_RXDB_FIELDS = new Set([ `_meta`, ]) -export function stripRxdbFields>( - obj: T | any -): T { - if (!obj) return obj - const out: any = Array.isArray(obj) ? [] : {} +export function stripRxdbFields>(obj: T): T { + const out: Record = {} for (const k of Object.keys(obj)) { if (RESERVED_RXDB_FIELDS.has(k)) continue out[k] = obj[k] From 3f2552aa9dd8e7cec48f57483f176308d09e8128 Mon Sep 17 00:00:00 2001 From: daveycodez Date: Fri, 10 Oct 2025 12:19:39 -0700 Subject: [PATCH 4/5] Add rxjs as peer dependency Added 'rxjs' with version >=7.8.2 to peerDependencies in package.json to ensure compatibility and proper dependency management. --- packages/rxdb-db-collection/package.json | 1 + pnpm-lock.yaml | 3 +++ 2 files changed, 4 insertions(+) diff --git a/packages/rxdb-db-collection/package.json b/packages/rxdb-db-collection/package.json index 1e90642d3..0cb237a82 100644 --- a/packages/rxdb-db-collection/package.json +++ b/packages/rxdb-db-collection/package.json @@ -35,6 +35,7 @@ "packageManager": "pnpm@10.18.1", "peerDependencies": { "rxdb": ">=16.17.2", + "rxjs": ">=7.8.2", "typescript": ">=4.7" }, "author": "Kyle Mathews", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index eb4e5b5b0..2c4e70314 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -720,6 +720,9 @@ importers: rxdb: specifier: 16.19.1 version: 16.19.1(rxjs@7.8.2)(socks@2.8.7) + rxjs: + specifier: '>=7.8.2' + version: 7.8.2 typescript: specifier: '>=4.7' version: 5.9.2 From ee9eca89156e4d9040df2560a3de573e6ee44d7a Mon Sep 17 00:00:00 2001 From: Sam Willis Date: Tue, 14 Oct 2025 12:12:24 +0100 Subject: [PATCH 5/5] changeset --- .changeset/smooth-candies-visit.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/smooth-candies-visit.md diff --git a/.changeset/smooth-candies-visit.md b/.changeset/smooth-candies-visit.md new file mode 100644 index 000000000..2e4f11428 --- /dev/null +++ b/.changeset/smooth-candies-visit.md @@ -0,0 +1,5 @@ +--- +"@tanstack/rxdb-db-collection": patch +--- + +Addressed the majority of "any" type usage in the rxdb-db-collection adapter as well as fixed querying for documents where \_deleted is false, which fixes issues when using localStorage or other persistent storage mechanisms.