Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shiny-phones-stick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tanstack/db": patch
---

mark item drafts as a `mutable` type
13 changes: 8 additions & 5 deletions packages/db/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import type {
Transaction as TransactionType,
TransactionWithMutations,
UtilsRecord,
WritableDeep,
} from "./types"
import type { IndexOptions } from "./indexes/index-options.js"
import type { BaseIndex, IndexResolver } from "./indexes/base-index.js"
Expand Down Expand Up @@ -1810,32 +1811,34 @@ export class CollectionImpl<
// Overload 1: Update multiple items with a callback
update<TItem extends object = T>(
key: Array<TKey | unknown>,
callback: (drafts: Array<TItem>) => void
callback: (drafts: Array<WritableDeep<TItem>>) => void
): TransactionType

// Overload 2: Update multiple items with config and a callback
update<TItem extends object = T>(
keys: Array<TKey | unknown>,
config: OperationConfig,
callback: (drafts: Array<TItem>) => void
callback: (drafts: Array<WritableDeep<TItem>>) => void
): TransactionType

// Overload 3: Update a single item with a callback
update<TItem extends object = T>(
id: TKey | unknown,
callback: (draft: TItem) => void
callback: (draft: WritableDeep<TItem>) => void
): TransactionType

// Overload 4: Update a single item with config and a callback
update<TItem extends object = T>(
id: TKey | unknown,
config: OperationConfig,
callback: (draft: TItem) => void
callback: (draft: WritableDeep<TItem>) => void
): TransactionType

update<TItem extends object = T>(
keys: (TKey | unknown) | Array<TKey | unknown>,
configOrCallback: ((draft: TItem | Array<TItem>) => void) | OperationConfig,
configOrCallback:
| ((draft: WritableDeep<TItem> | Array<WritableDeep<TItem>>) => void)
| OperationConfig,
maybeCallback?: (draft: TItem | Array<TItem>) => void
) {
if (typeof keys === `undefined`) {
Expand Down
74 changes: 74 additions & 0 deletions packages/db/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,3 +628,77 @@ export type ChangeListener<
T extends object = Record<string, unknown>,
TKey extends string | number = string | number,
> = (changes: Array<ChangeMessage<T, TKey>>) => void

// Adapted from https://github.com/sindresorhus/type-fest
// MIT License Copyright (c) Sindre Sorhus

type BuiltIns =
| null
| undefined
| string
| number
| boolean
| symbol
| bigint
| void
| Date
| RegExp

type HasMultipleCallSignatures<
T extends (...arguments_: Array<any>) => unknown,
> = T extends {
(...arguments_: infer A): unknown
(...arguments_: infer B): unknown
}
? B extends A
? A extends B
? false
: true
: true
: false

type WritableMapDeep<MapType extends ReadonlyMap<unknown, unknown>> =
MapType extends ReadonlyMap<infer KeyType, infer ValueType>
? Map<WritableDeep<KeyType>, WritableDeep<ValueType>>
: MapType

type WritableSetDeep<SetType extends ReadonlySet<unknown>> =
SetType extends ReadonlySet<infer ItemType>
? Set<WritableDeep<ItemType>>
: SetType

type WritableObjectDeep<ObjectType extends object> = {
-readonly [KeyType in keyof ObjectType]: WritableDeep<ObjectType[KeyType]>
}

type WritableArrayDeep<ArrayType extends ReadonlyArray<unknown>> =
ArrayType extends readonly []
? []
: ArrayType extends readonly [...infer U, infer V]
? [...WritableArrayDeep<U>, WritableDeep<V>]
: ArrayType extends readonly [infer U, ...infer V]
? [WritableDeep<U>, ...WritableArrayDeep<V>]
: ArrayType extends ReadonlyArray<infer U>
? Array<WritableDeep<U>>
: ArrayType extends Array<infer U>
? Array<WritableDeep<U>>
: ArrayType

export type WritableDeep<T> = T extends BuiltIns
? T
: T extends (...arguments_: Array<any>) => unknown
? {} extends WritableObjectDeep<T>
? T
: HasMultipleCallSignatures<T> extends true
? T
: ((...arguments_: Parameters<T>) => ReturnType<T>) &
WritableObjectDeep<T>
: T extends ReadonlyMap<unknown, unknown>
? WritableMapDeep<T>
: T extends ReadonlySet<unknown>
? WritableSetDeep<T>
: T extends ReadonlyArray<unknown>
? WritableArrayDeep<T>
: T extends object
? WritableObjectDeep<T>
: unknown
Loading