@@ -2,13 +2,14 @@ import { entries } from '@seedcompany/common';
2
2
import { difference , omit , pickBy } from 'lodash' ;
3
3
import { DateTime } from 'luxon' ;
4
4
import { ConditionalKeys } from 'type-fest' ;
5
+ import { LinkTo } from '~/core' ;
5
6
import {
6
7
EnhancedResource ,
7
8
ID ,
9
+ MaybeSecured ,
8
10
MaybeUnsecuredInstance ,
9
11
Resource ,
10
12
ResourceShape ,
11
- Secured ,
12
13
unwrapSecured ,
13
14
UnwrapSecured ,
14
15
} from '../../common' ;
@@ -58,14 +59,16 @@ type ChangeKey<Key extends keyof T & string, T> = T[Key] extends SetChangeType<
58
59
: never
59
60
: UnwrapSecured < T [ Key ] > extends FileId
60
61
? Key
61
- : NonNullable < UnwrapSecured < T [ Key ] > > extends ID
62
+ : NonNullable < UnwrapSecured < T [ Key ] > > extends ID | LinkTo < any >
62
63
? `${Key } Id` // our convention for single relationships
63
64
: Key ;
64
65
65
66
type ChangeOf < Val > = Val extends SetChangeType < any , infer Override >
66
67
? Override
67
68
: UnwrapSecured < Val > extends FileId
68
69
? CreateDefinedFileVersionInput
70
+ : UnwrapSecured < Val > extends LinkTo < any >
71
+ ? ID
69
72
: UnwrapSecured < Val > ;
70
73
71
74
/**
@@ -76,7 +79,7 @@ export type DbChanges<T> = DbAllowableChanges<T> &
76
79
77
80
type DbAllowableChanges < T > = {
78
81
[ K in Exclude <
79
- ConditionalKeys < Required < T > , NativeDbValue | Secured < NativeDbValue > > ,
82
+ ConditionalKeys < Required < T > , MaybeSecured < NativeDbValue | LinkTo < any > > > ,
80
83
keyof Resource
81
84
> ] ?: UnwrapSecured < T [ K ] > | Variable ;
82
85
} ;
@@ -113,7 +116,16 @@ export const getChanges =
113
116
return false ;
114
117
}
115
118
const key = isRelation ( res , prop ) ? prop . slice ( 0 , - 2 ) : prop ;
116
- const existing = unwrapSecured ( existingObject [ key ] ) ;
119
+ let existing = unwrapSecured ( existingObject [ key ] ) ;
120
+ // Unwrap existing refs of IDs to input IDs.
121
+ if (
122
+ typeof change === 'string' &&
123
+ existing &&
124
+ typeof existing === 'object' &&
125
+ typeof existing . id === 'string'
126
+ ) {
127
+ existing = existing . id ;
128
+ }
117
129
return ! isSame ( change , existing ) ;
118
130
} ) ;
119
131
0 commit comments