Skip to content

Commit 975b9d8

Browse files
committed
more precisely type original/changes
1 parent 41546ba commit 975b9d8

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

packages/db/src/collection.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ export class CollectionImpl<
743743
}
744744

745745
const items = Array.isArray(data) ? data : [data]
746-
const mutations: Array<PendingMutation<T>> = []
746+
const mutations: Array<PendingMutation<T, `insert`>> = []
747747

748748
// Create mutations for each item
749749
items.forEach((item) => {
@@ -757,7 +757,7 @@ export class CollectionImpl<
757757
}
758758
const globalKey = this.generateGlobalKey(key, item)
759759

760-
const mutation: PendingMutation<T> = {
760+
const mutation: PendingMutation<T, `insert`> = {
761761
mutationId: crypto.randomUUID(),
762762
original: {},
763763
modified: validatedData,
@@ -927,7 +927,7 @@ export class CollectionImpl<
927927
}
928928

929929
// Create mutations for each object that has changes
930-
const mutations: Array<PendingMutation<T>> = keysArray
930+
const mutations: Array<PendingMutation<T, `update`>> = keysArray
931931
.map((key, index) => {
932932
const itemChanges = changesArray[index] // User-provided changes for this specific item
933933

@@ -981,7 +981,7 @@ export class CollectionImpl<
981981
collection: this,
982982
}
983983
})
984-
.filter(Boolean) as Array<PendingMutation<T>>
984+
.filter(Boolean) as Array<PendingMutation<T, `update`>>
985985

986986
// If no changes were made, return an empty transaction early
987987
if (mutations.length === 0) {
@@ -1057,7 +1057,7 @@ export class CollectionImpl<
10571057
}
10581058

10591059
const keysArray = Array.isArray(keys) ? keys : [keys]
1060-
const mutations: Array<PendingMutation<T>> = []
1060+
const mutations: Array<PendingMutation<T, `delete`>> = []
10611061

10621062
for (const key of keysArray) {
10631063
if (!this.has(key)) {
@@ -1066,7 +1066,7 @@ export class CollectionImpl<
10661066
)
10671067
}
10681068
const globalKey = this.generateGlobalKey(key, this.get(key)!)
1069-
const mutation: PendingMutation<T> = {
1069+
const mutation: PendingMutation<T, `delete`> = {
10701070
mutationId: crypto.randomUUID(),
10711071
original: this.get(key)!,
10721072
modified: this.get(key)!,

packages/db/src/types.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@ export type UtilsRecord = Record<string, Fn>
5252
* Represents a pending mutation within a transaction
5353
* Contains information about the original and modified data, as well as metadata
5454
*/
55-
export interface PendingMutation<T extends object = Record<string, unknown>> {
55+
export interface PendingMutation<
56+
T extends object = Record<string, unknown>,
57+
TOperation extends OperationType = OperationType,
58+
> {
5659
mutationId: string
57-
original: T | {}
60+
original: TOperation extends `insert` ? {} : T
5861
modified: T
59-
changes: Partial<T>
62+
changes: TOperation extends `insert` ? T : Partial<T>
6063
globalKey: string
6164
key: any
6265
type: OperationType

0 commit comments

Comments
 (0)