Skip to content

Commit d523759

Browse files
rxdb-db-collection fixes (#667)
* Refactor types and cleanup in rxdb.ts Replaces generic 'any' types with more specific types such as 'Subscription' and 'Record<string, unknown>' for improved type safety. Removes unnecessary type assertions and updates function signatures and variable declarations accordingly. * 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. * 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. * Add rxjs as peer dependency Added 'rxjs' with version >=7.8.2 to peerDependencies in package.json to ensure compatibility and proper dependency management. * changeset --------- Co-authored-by: Sam Willis <sam.willis@gmail.com>
1 parent 7c8b531 commit d523759

5 files changed

Lines changed: 30 additions & 19 deletions

File tree

.changeset/smooth-candies-visit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tanstack/rxdb-db-collection": patch
3+
---
4+
5+
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.

packages/rxdb-db-collection/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"packageManager": "pnpm@10.18.2",
3636
"peerDependencies": {
3737
"rxdb": ">=16.17.2",
38+
"rxjs": ">=7.8.2",
3839
"typescript": ">=4.7"
3940
},
4041
"author": "Kyle Mathews",

packages/rxdb-db-collection/src/helper.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ const RESERVED_RXDB_FIELDS = new Set([
55
`_meta`,
66
])
77

8-
export function stripRxdbFields<T extends Record<string, any>>(
9-
obj: T | any
10-
): T {
11-
if (!obj) return obj
12-
const out: any = Array.isArray(obj) ? [] : {}
8+
export function stripRxdbFields<T extends Record<string, unknown>>(obj: T): T {
9+
const out: Record<string, unknown> = {}
1310
for (const k of Object.keys(obj)) {
1411
if (RESERVED_RXDB_FIELDS.has(k)) continue
1512
out[k] = obj[k]

packages/rxdb-db-collection/src/rxdb.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
RxCollection,
1414
RxDocumentData,
1515
} from "rxdb/plugins/core"
16+
import type { Subscription } from "rxjs"
1617

1718
import type {
1819
BaseCollectionConfig,
@@ -27,7 +28,10 @@ const debug = DebugModule.debug(`ts/db:rxdb`)
2728
/**
2829
* Used in tests to ensure proper cleanup
2930
*/
30-
export const OPEN_RXDB_SUBSCRIPTIONS = new WeakMap<RxCollection, Set<any>>()
31+
export const OPEN_RXDB_SUBSCRIPTIONS = new WeakMap<
32+
RxCollection,
33+
Set<Subscription>
34+
>()
3135

3236
/**
3337
* Configuration interface for RxDB collection options
@@ -106,7 +110,7 @@ export function rxdbCollectionOptions(config: RxDBCollectionConfig<any, any>) {
106110

107111
// "getKey"
108112
const primaryPath = rxCollection.schema.primaryPath
109-
function getKey(item: any): string {
113+
function getKey(item: Record<string, unknown>): string {
110114
const key: string = item[primaryPath] as string
111115
return key
112116
}
@@ -147,15 +151,16 @@ export function rxdbCollectionOptions(config: RxDBCollectionConfig<any, any>) {
147151
},
148152
},
149153
],
150-
} as any,
151-
sort: [{ "_meta.lwt": `asc` }, { [primaryPath]: `asc` } as any],
154+
_deleted: false,
155+
},
156+
sort: [{ "_meta.lwt": `asc` }, { [primaryPath]: `asc` }],
152157
limit: syncBatchSize,
153158
skip: 0,
154159
}
155160
} else {
156161
query = {
157-
selector: {},
158-
sort: [{ "_meta.lwt": `asc` }, { [primaryPath]: `asc` } as any],
162+
selector: { _deleted: false },
163+
sort: [{ "_meta.lwt": `asc` }, { [primaryPath]: `asc` }],
159164
limit: syncBatchSize,
160165
skip: 0,
161166
}
@@ -198,11 +203,11 @@ export function rxdbCollectionOptions(config: RxDBCollectionConfig<any, any>) {
198203
return
199204
}
200205
begin()
201-
write(msg as any)
206+
write(msg)
202207
commit()
203208
}
204209

205-
let sub: any
210+
let sub: Subscription
206211
function startOngoingFetch() {
207212
// Subscribe early and buffer live changes during initial load and ongoing
208213
sub = rxCollection.$.subscribe((ev) => {
@@ -234,7 +239,7 @@ export function rxdbCollectionOptions(config: RxDBCollectionConfig<any, any>) {
234239

235240
if (buffer.length) {
236241
begin()
237-
for (const msg of buffer) write(msg as any)
242+
for (const msg of buffer) write(msg)
238243
commit()
239244
buffer.length = 0
240245
}
@@ -258,14 +263,14 @@ export function rxdbCollectionOptions(config: RxDBCollectionConfig<any, any>) {
258263
getSyncMetadata: undefined,
259264
}
260265

261-
const collectionConfig: CollectionConfig<Row, string, any> = {
266+
const collectionConfig: CollectionConfig<Row, string> = {
262267
...restConfig,
263-
getKey: getKey as any,
268+
getKey,
264269
sync,
265270
onInsert: async (params) => {
266271
debug(`insert`, params)
267272
const newItems = params.transaction.mutations.map((m) => m.modified)
268-
return rxCollection.bulkUpsert(newItems as Array<any>).then((result) => {
273+
return rxCollection.bulkUpsert(newItems).then((result) => {
269274
if (result.error.length > 0) {
270275
throw rxStorageWriteErrorToRxError(ensureNotFalsy(result.error[0]))
271276
}
@@ -285,15 +290,15 @@ export function rxdbCollectionOptions(config: RxDBCollectionConfig<any, any>) {
285290
if (!doc) {
286291
continue
287292
}
288-
await doc.incrementalPatch(newValue as any)
293+
await doc.incrementalPatch(newValue)
289294
}
290295
},
291296
onDelete: async (params) => {
292297
debug(`delete`, params)
293298
const mutations = params.transaction.mutations.filter(
294299
(m) => m.type === `delete`
295300
)
296-
const ids = mutations.map((mutation: any) => getKey(mutation.original))
301+
const ids = mutations.map((mutation) => getKey(mutation.original))
297302
return rxCollection.bulkRemove(ids).then((result) => {
298303
if (result.error.length > 0) {
299304
throw result.error

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)