From 0168fbbe4dd43e3fc8269fabc510ef452f133fbe Mon Sep 17 00:00:00 2001 From: Dane Pilcher Date: Thu, 14 Sep 2023 10:32:19 -0600 Subject: [PATCH] fix: generate OptionallyManagedCompositeIdentifier when pk is id and sort key fields are used --- .../appsync-javascript-visitor.test.ts.snap | 30 +++++++++---------- .../appsync-javascript-visitor.test.ts | 6 ++-- .../visitors/appsync-typescript-visitor.ts | 7 ++++- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-javascript-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-javascript-visitor.test.ts.snap index 9de2d826f..966884e42 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-javascript-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-javascript-visitor.test.ts.snap @@ -75,7 +75,7 @@ export declare const Comment: (new (init: ModelInit) => Comment) & { `; exports[`Javascript visitor with connected models of custom pk hasMany/belongsTo relation should generate correct declaration for hasMany uni-connection model when custom pk support is enabled 1`] = ` -"import { ModelInit, MutableModel, __modelMeta__, CompositeIdentifier } from \\"@aws-amplify/datastore\\"; +"import { ModelInit, MutableModel, __modelMeta__, OptionalManagedCompositeIdentifier } from \\"@aws-amplify/datastore\\"; // @ts-ignore import { LazyLoading, LazyLoadingDisabled, AsyncCollection } from \\"@aws-amplify/datastore\\"; @@ -85,7 +85,7 @@ import { LazyLoading, LazyLoadingDisabled, AsyncCollection } from \\"@aws-amplif type EagerPost = { readonly [__modelMeta__]: { - identifier: CompositeIdentifier; + identifier: OptionallyManagedCompositeIdentifier; readOnlyFields: 'createdAt' | 'updatedAt'; }; readonly id: string; @@ -97,7 +97,7 @@ type EagerPost = { type LazyPost = { readonly [__modelMeta__]: { - identifier: CompositeIdentifier; + identifier: OptionallyManagedCompositeIdentifier; readOnlyFields: 'createdAt' | 'updatedAt'; }; readonly id: string; @@ -115,7 +115,7 @@ export declare const Post: (new (init: ModelInit) => Post) & { type EagerComment = { readonly [__modelMeta__]: { - identifier: CompositeIdentifier; + identifier: OptionallyManagedCompositeIdentifier; readOnlyFields: 'createdAt' | 'updatedAt'; }; readonly id: string; @@ -128,7 +128,7 @@ type EagerComment = { type LazyComment = { readonly [__modelMeta__]: { - identifier: CompositeIdentifier; + identifier: OptionallyManagedCompositeIdentifier; readOnlyFields: 'createdAt' | 'updatedAt'; }; readonly id: string; @@ -147,7 +147,7 @@ export declare const Comment: (new (init: ModelInit) => Comment) & { `; exports[`Javascript visitor with connected models of custom pk hasMany/belongsTo relation should generate correct declaration for hasMany uni-connection model with custom index 1`] = ` -"import { ModelInit, MutableModel, __modelMeta__, CompositeIdentifier } from \\"@aws-amplify/datastore\\"; +"import { ModelInit, MutableModel, __modelMeta__, OptionalManagedCompositeIdentifier } from \\"@aws-amplify/datastore\\"; // @ts-ignore import { LazyLoading, LazyLoadingDisabled, AsyncCollection } from \\"@aws-amplify/datastore\\"; @@ -157,7 +157,7 @@ import { LazyLoading, LazyLoadingDisabled, AsyncCollection } from \\"@aws-amplif type EagerPost = { readonly [__modelMeta__]: { - identifier: CompositeIdentifier; + identifier: OptionallyManagedCompositeIdentifier; readOnlyFields: 'createdAt' | 'updatedAt'; }; readonly id: string; @@ -169,7 +169,7 @@ type EagerPost = { type LazyPost = { readonly [__modelMeta__]: { - identifier: CompositeIdentifier; + identifier: OptionallyManagedCompositeIdentifier; readOnlyFields: 'createdAt' | 'updatedAt'; }; readonly id: string; @@ -187,7 +187,7 @@ export declare const Post: (new (init: ModelInit) => Post) & { type EagerComment = { readonly [__modelMeta__]: { - identifier: CompositeIdentifier; + identifier: OptionallyManagedCompositeIdentifier; readOnlyFields: 'createdAt' | 'updatedAt'; }; readonly id: string; @@ -200,7 +200,7 @@ type EagerComment = { type LazyComment = { readonly [__modelMeta__]: { - identifier: CompositeIdentifier; + identifier: OptionallyManagedCompositeIdentifier; readOnlyFields: 'createdAt' | 'updatedAt'; }; readonly id: string; @@ -219,7 +219,7 @@ export declare const Comment: (new (init: ModelInit) => Comment) & { `; exports[`Javascript visitor with connected models of custom pk hasOne/belongsTo relation should generate correct declaration when custom pk support is enabled 1`] = ` -"import { ModelInit, MutableModel, __modelMeta__, CompositeIdentifier } from \\"@aws-amplify/datastore\\"; +"import { ModelInit, MutableModel, __modelMeta__, OptionalManagedCompositeIdentifier } from \\"@aws-amplify/datastore\\"; // @ts-ignore import { LazyLoading, LazyLoadingDisabled, AsyncItem } from \\"@aws-amplify/datastore\\"; @@ -229,7 +229,7 @@ import { LazyLoading, LazyLoadingDisabled, AsyncItem } from \\"@aws-amplify/data type EagerProject = { readonly [__modelMeta__]: { - identifier: CompositeIdentifier; + identifier: OptionallyManagedCompositeIdentifier; readOnlyFields: 'createdAt' | 'updatedAt'; }; readonly id: string; @@ -243,7 +243,7 @@ type EagerProject = { type LazyProject = { readonly [__modelMeta__]: { - identifier: CompositeIdentifier; + identifier: OptionallyManagedCompositeIdentifier; readOnlyFields: 'createdAt' | 'updatedAt'; }; readonly id: string; @@ -263,7 +263,7 @@ export declare const Project: (new (init: ModelInit) => Project) & { type EagerTeam = { readonly [__modelMeta__]: { - identifier: CompositeIdentifier; + identifier: OptionallyManagedCompositeIdentifier; readOnlyFields: 'createdAt' | 'updatedAt'; }; readonly id: string; @@ -277,7 +277,7 @@ type EagerTeam = { type LazyTeam = { readonly [__modelMeta__]: { - identifier: CompositeIdentifier; + identifier: OptionallyManagedCompositeIdentifier; readOnlyFields: 'createdAt' | 'updatedAt'; }; readonly id: string; diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-javascript-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-javascript-visitor.test.ts index 1702b6f59..129256c32 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-javascript-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-javascript-visitor.test.ts @@ -952,7 +952,7 @@ describe('New model meta field test', () => { const declarations = visitor.generate(); validateTs(declarations); expect(declarations).toMatchInlineSnapshot(` - "import { ModelInit, MutableModel, __modelMeta__, ManagedIdentifier, OptionallyManagedIdentifier, CompositeIdentifier, CustomIdentifier } from \\"@aws-amplify/datastore\\"; + "import { ModelInit, MutableModel, __modelMeta__, ManagedIdentifier, OptionallyManagedIdentifier, OptionalManagedCompositeIdentifier, CustomIdentifier, CompositeIdentifier } from \\"@aws-amplify/datastore\\"; // @ts-ignore import { LazyLoading, LazyLoadingDisabled } from \\"@aws-amplify/datastore\\"; @@ -1050,7 +1050,7 @@ describe('New model meta field test', () => { type EagerModelExplicitIdWithSk = { readonly [__modelMeta__]: { - identifier: CompositeIdentifier; + identifier: OptionallyManagedCompositeIdentifier; readOnlyFields: 'createdAt' | 'updatedAt'; }; readonly id: string; @@ -1062,7 +1062,7 @@ describe('New model meta field test', () => { type LazyModelExplicitIdWithSk = { readonly [__modelMeta__]: { - identifier: CompositeIdentifier; + identifier: OptionallyManagedCompositeIdentifier; readOnlyFields: 'createdAt' | 'updatedAt'; }; readonly id: string; diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-typescript-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-typescript-visitor.ts index de4e66d5c..e8f5efce5 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-typescript-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-typescript-visitor.ts @@ -9,6 +9,7 @@ import { ParsedAppSyncModelConfig, RawAppSyncModelConfig, } from './appsync-visitor'; +import { DEFAULT_HASH_KEY_FIELD } from '../utils/constants'; export interface RawAppSyncModelTypeScriptConfig extends RawAppSyncModelConfig {} export interface ParsedAppSyncModelTypeScriptConfig extends ParsedAppSyncModelConfig { @@ -48,7 +49,7 @@ export class AppSyncModelTypeScriptVisitor< this.processDirectives( shouldUseModelNameFieldInHasManyAndBelongsTo, shouldImputeKeyForUniDirectionalHasMany, - shouldUseFieldsInAssociatedWithInHasOne + shouldUseFieldsInAssociatedWithInHasOne, ); const imports = this.generateImports(); const enumDeclarations = Object.values(this.enumMap) @@ -149,6 +150,10 @@ export class AppSyncModelTypeScriptVisitor< const identifierFieldsStr = identifierFields.length === 1 ? `'${identifierFields[0]}'` : `[${identifierFields.map(fieldStr => `'${fieldStr}'`).join(', ')}]`; if (identifierFields.length > 1) { + if (primaryKeyField.name === DEFAULT_HASH_KEY_FIELD) { + this.BASE_DATASTORE_IMPORT.add('OptionalManagedCompositeIdentifier'); + return `OptionallyManagedCompositeIdentifier<${modelObj.name}, ${identifierFieldsStr}>`; + } this.BASE_DATASTORE_IMPORT.add('CompositeIdentifier'); return `CompositeIdentifier<${modelObj.name}, ${identifierFieldsStr}>`; }