Skip to content

Commit f531e47

Browse files
committed
Update db changes types to support link refs & logic to unwrap
1 parent 163fa76 commit f531e47

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/core/database/changes.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import { entries } from '@seedcompany/common';
22
import { difference, omit, pickBy } from 'lodash';
33
import { DateTime } from 'luxon';
44
import { ConditionalKeys } from 'type-fest';
5+
import { LinkTo } from '~/core';
56
import {
67
EnhancedResource,
78
ID,
9+
MaybeSecured,
810
MaybeUnsecuredInstance,
911
Resource,
1012
ResourceShape,
11-
Secured,
1213
unwrapSecured,
1314
UnwrapSecured,
1415
} from '../../common';
@@ -58,14 +59,16 @@ type ChangeKey<Key extends keyof T & string, T> = T[Key] extends SetChangeType<
5859
: never
5960
: UnwrapSecured<T[Key]> extends FileId
6061
? Key
61-
: NonNullable<UnwrapSecured<T[Key]>> extends ID
62+
: NonNullable<UnwrapSecured<T[Key]>> extends ID | LinkTo<any>
6263
? `${Key}Id` // our convention for single relationships
6364
: Key;
6465

6566
type ChangeOf<Val> = Val extends SetChangeType<any, infer Override>
6667
? Override
6768
: UnwrapSecured<Val> extends FileId
6869
? CreateDefinedFileVersionInput
70+
: UnwrapSecured<Val> extends LinkTo<any>
71+
? ID
6972
: UnwrapSecured<Val>;
7073

7174
/**
@@ -76,7 +79,7 @@ export type DbChanges<T> = DbAllowableChanges<T> &
7679

7780
type DbAllowableChanges<T> = {
7881
[K in Exclude<
79-
ConditionalKeys<Required<T>, NativeDbValue | Secured<NativeDbValue>>,
82+
ConditionalKeys<Required<T>, MaybeSecured<NativeDbValue | LinkTo<any>>>,
8083
keyof Resource
8184
>]?: UnwrapSecured<T[K]> | Variable;
8285
};
@@ -113,7 +116,16 @@ export const getChanges =
113116
return false;
114117
}
115118
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+
}
117129
return !isSame(change, existing);
118130
});
119131

0 commit comments

Comments
 (0)