@aws-amplify/data-schema
Advanced tools
Comparing version 0.0.0-top-down-types-refactor-20240528214341 to 0.0.0-top-down-types-refactor-20240529170336
@@ -1,2 +0,2 @@ | ||
import type { deferredRefResolvingPrefix, ModelTypeParamShape } from '../../ModelType'; | ||
import type { deferredRefResolvingPrefix, ModelTypeParamShape, ModelDefaultIdentifier } from '../../ModelType'; | ||
import type { ClientSchemaProperty } from './ClientSchemaProperty'; | ||
@@ -17,4 +17,4 @@ import type { Authorization, ImpliedAuthFields } from '../../Authorization'; | ||
updateType: Prettify<UpdateModelInput<ClientModel<Bag, Metadata, IsRDS, T, K>>>; | ||
deleteType: Prettify<ModelIdentifier<T>>; | ||
identifier: ShallowPretty<ModelIdentifier<T>>; | ||
deleteType: Prettify<ModelIdentifier<Bag, T>>; | ||
identifier: ShallowPretty<ModelIdentifier<Bag, T>>; | ||
nestedTypes: NestedTypes<ClientFields<Bag, Metadata, IsRDS, T>, T>; | ||
@@ -29,3 +29,3 @@ secondaryIndexes: IndexQueryMethodsFromIR<Bag, T['secondaryIndexes'], K>; | ||
}; | ||
type ClientFields<Bag extends Record<string, unknown>, Metadata extends SchemaMetadata<any>, IsRDS extends boolean, T extends ModelTypeParamShape> = ResolveFields<Bag, T['fields']> & If<ModelIdentifier<T>, Not<IsRDS>> & AuthFields<Metadata, T> & Omit<SystemFields<IsRDS>, keyof ResolveFields<Bag, T['fields']>>; | ||
type ClientFields<Bag extends Record<string, unknown>, Metadata extends SchemaMetadata<any>, IsRDS extends boolean, T extends ModelTypeParamShape> = ResolveFields<Bag, T['fields']> & If<Not<IsRDS>, ImplicitIdentifier<T>> & AuthFields<Metadata, T> & Omit<SystemFields<IsRDS>, keyof ResolveFields<Bag, T['fields']>>; | ||
type SystemFields<IsRDS extends boolean> = IsRDS extends false ? { | ||
@@ -35,4 +35,16 @@ readonly createdAt: string; | ||
} : object; | ||
type ModelIdentifier<T extends ModelTypeParamShape> = T['identifier']['pk'] & (T['identifier']['sk'] extends never ? unknown : T['identifier']['sk']); | ||
type If<T, IfTrue extends boolean, Default = unknown> = IfTrue extends true ? T : Default; | ||
type ModelIdentifier<Bag extends Record<string, unknown>, T extends ModelTypeParamShape> = ResolveIdentifierFields<ResolveFields<Bag, T['fields']>, T['identifier']['pk'] & (T['identifier']['sk'] extends never ? unknown : T['identifier']['sk'])>; | ||
/** | ||
* Separate util for *injecting* the default implicit identifier for performance | ||
* reasons. The full ModelIdentifer util needs to extract types from the fields | ||
* matching the explicitly defined field types. Contrast that to **injecting** PK | ||
* fields into the model, which is only done specifically when the default of | ||
* `readonly id: string` is being injected IF AND ONLY IF another `id` field is | ||
* not already present on the model. | ||
*/ | ||
type ImplicitIdentifier<T extends ModelTypeParamShape> = T['identifier']['pk'] extends ModelDefaultIdentifier['pk'] ? 'id' extends keyof T['fields'] ? unknown : ModelDefaultIdentifier['pk'] : unknown; | ||
type ResolveIdentifierFields<Model, IdentifierFields> = { | ||
[K in keyof IdentifierFields]: K extends keyof Model ? Model[K] : string; | ||
}; | ||
type If<ConditionResult extends boolean, IfTrueValue, IfFalseValue = unknown> = ConditionResult extends true ? IfTrueValue : IfFalseValue; | ||
type Not<T extends boolean> = T extends true ? false : true; | ||
@@ -39,0 +51,0 @@ /** |
@@ -83,3 +83,3 @@ import type { SetTypeSubArg } from '@aws-amplify/data-schema-types'; | ||
export declare const isSchemaModelType: (modelType: any | SchemaModelType) => modelType is ModelType<ModelTypeParamShape, "identifier">; | ||
type ModelDefaultIdentifier = { | ||
export type ModelDefaultIdentifier = { | ||
pk: { | ||
@@ -86,0 +86,0 @@ readonly id: string; |
{ | ||
"name": "@aws-amplify/data-schema", | ||
"version": "0.0.0-top-down-types-refactor-20240528214341", | ||
"version": "0.0.0-top-down-types-refactor-20240529170336", | ||
"license": "Apache-2.0", | ||
@@ -5,0 +5,0 @@ "repository": { |
import type { | ||
deferredRefResolvingPrefix, | ||
ModelTypeParamShape, | ||
ModelDefaultIdentifier, | ||
} from '../../ModelType'; | ||
@@ -44,4 +45,4 @@ import type { ClientSchemaProperty } from './ClientSchemaProperty'; | ||
>; | ||
deleteType: Prettify<ModelIdentifier<T>>; | ||
identifier: ShallowPretty<ModelIdentifier<T>>; | ||
deleteType: Prettify<ModelIdentifier<Bag, T>>; | ||
identifier: ShallowPretty<ModelIdentifier<Bag, T>>; | ||
nestedTypes: NestedTypes<ClientFields<Bag, Metadata, IsRDS, T>, T>; | ||
@@ -64,3 +65,3 @@ secondaryIndexes: IndexQueryMethodsFromIR<Bag, T['secondaryIndexes'], K>; | ||
> = ResolveFields<Bag, T['fields']> & | ||
If<ModelIdentifier<T>, Not<IsRDS>> & | ||
If<Not<IsRDS>, ImplicitIdentifier<T>> & | ||
AuthFields<Metadata, T> & | ||
@@ -77,11 +78,36 @@ Omit<SystemFields<IsRDS>, keyof ResolveFields<Bag, T['fields']>>; | ||
// refs are not being resolved here ... yet. | ||
type ModelIdentifier<T extends ModelTypeParamShape> = T['identifier']['pk'] & | ||
(T['identifier']['sk'] extends never | ||
? unknown // unknown collapses in an intersection | ||
: T['identifier']['sk']); | ||
type ModelIdentifier< | ||
Bag extends Record<string, unknown>, | ||
T extends ModelTypeParamShape, | ||
> = ResolveIdentifierFields< | ||
ResolveFields<Bag, T['fields']>, | ||
T['identifier']['pk'] & | ||
(T['identifier']['sk'] extends never ? unknown : T['identifier']['sk']) | ||
>; | ||
type If<T, IfTrue extends boolean, Default = unknown> = IfTrue extends true | ||
? T | ||
: Default; | ||
/** | ||
* Separate util for *injecting* the default implicit identifier for performance | ||
* reasons. The full ModelIdentifer util needs to extract types from the fields | ||
* matching the explicitly defined field types. Contrast that to **injecting** PK | ||
* fields into the model, which is only done specifically when the default of | ||
* `readonly id: string` is being injected IF AND ONLY IF another `id` field is | ||
* not already present on the model. | ||
*/ | ||
type ImplicitIdentifier<T extends ModelTypeParamShape> = | ||
T['identifier']['pk'] extends ModelDefaultIdentifier['pk'] | ||
? 'id' extends keyof T['fields'] | ||
? unknown | ||
: ModelDefaultIdentifier['pk'] | ||
: unknown; | ||
type ResolveIdentifierFields<Model, IdentifierFields> = { | ||
[K in keyof IdentifierFields]: K extends keyof Model ? Model[K] : string; | ||
}; | ||
type If< | ||
ConditionResult extends boolean, | ||
IfTrueValue, | ||
IfFalseValue = unknown, | ||
> = ConditionResult extends true ? IfTrueValue : IfFalseValue; | ||
type Not<T extends boolean> = T extends true ? false : true; | ||
@@ -88,0 +114,0 @@ |
@@ -358,3 +358,3 @@ import type { SetTypeSubArg } from '@aws-amplify/data-schema-types'; | ||
type ModelDefaultIdentifier = { | ||
export type ModelDefaultIdentifier = { | ||
pk: { readonly id: string }; | ||
@@ -361,0 +361,0 @@ sk: never; |
Sorry, the diff of this file is not supported yet
1715243
23658