Skip to content

Commit

Permalink
fix: generate OptionallyManagedCompositeIdentifier when pk is id and …
Browse files Browse the repository at this point in the history
…sort key fields are used
  • Loading branch information
dpilch committed Sep 15, 2023
1 parent d9ce4ea commit efb2c3f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ export declare const Comment: (new (init: ModelInit<Comment>) => 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\\";
// @ts-ignore
import { LazyLoading, LazyLoadingDisabled, AsyncCollection } from \\"@aws-amplify/datastore\\";
import { LazyLoading, LazyLoadingDisabled, OptionallyManagedCompositeIdentifier, OptionallyManagedCompositeIdentifierDisabled, AsyncCollection } from \\"@aws-amplify/datastore\\";
type EagerPost = {
readonly [__modelMeta__]: {
identifier: CompositeIdentifier<Post, ['id', 'title']>;
identifier: OptionallyManagedCompositeIdentifier extends OptionallyManagedCompositeIdentifierDisabled ? CompositeIdentifier<Post, ['id', 'title']> : OptionallyManagedCompositeIdentifier<Post, ['id', 'title']>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
Expand All @@ -97,7 +97,7 @@ type EagerPost = {
type LazyPost = {
readonly [__modelMeta__]: {
identifier: CompositeIdentifier<Post, ['id', 'title']>;
identifier: OptionallyManagedCompositeIdentifier extends OptionallyManagedCompositeIdentifierDisabled ? CompositeIdentifier<Post, ['id', 'title']> : OptionallyManagedCompositeIdentifier<Post, ['id', 'title']>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
Expand All @@ -115,7 +115,7 @@ export declare const Post: (new (init: ModelInit<Post>) => Post) & {
type EagerComment = {
readonly [__modelMeta__]: {
identifier: CompositeIdentifier<Comment, ['id', 'content']>;
identifier: OptionallyManagedCompositeIdentifier extends OptionallyManagedCompositeIdentifierDisabled ? CompositeIdentifier<Comment, ['id', 'content']> : OptionallyManagedCompositeIdentifier<Comment, ['id', 'content']>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
Expand All @@ -128,7 +128,7 @@ type EagerComment = {
type LazyComment = {
readonly [__modelMeta__]: {
identifier: CompositeIdentifier<Comment, ['id', 'content']>;
identifier: OptionallyManagedCompositeIdentifier extends OptionallyManagedCompositeIdentifierDisabled ? CompositeIdentifier<Comment, ['id', 'content']> : OptionallyManagedCompositeIdentifier<Comment, ['id', 'content']>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
Expand All @@ -149,15 +149,15 @@ export declare const Comment: (new (init: ModelInit<Comment>) => 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\\";
// @ts-ignore
import { LazyLoading, LazyLoadingDisabled, AsyncCollection } from \\"@aws-amplify/datastore\\";
import { LazyLoading, LazyLoadingDisabled, OptionallyManagedCompositeIdentifier, OptionallyManagedCompositeIdentifierDisabled, AsyncCollection } from \\"@aws-amplify/datastore\\";
type EagerPost = {
readonly [__modelMeta__]: {
identifier: CompositeIdentifier<Post, ['id', 'title']>;
identifier: OptionallyManagedCompositeIdentifier extends OptionallyManagedCompositeIdentifierDisabled ? CompositeIdentifier<Post, ['id', 'title']> : OptionallyManagedCompositeIdentifier<Post, ['id', 'title']>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
Expand All @@ -169,7 +169,7 @@ type EagerPost = {
type LazyPost = {
readonly [__modelMeta__]: {
identifier: CompositeIdentifier<Post, ['id', 'title']>;
identifier: OptionallyManagedCompositeIdentifier extends OptionallyManagedCompositeIdentifierDisabled ? CompositeIdentifier<Post, ['id', 'title']> : OptionallyManagedCompositeIdentifier<Post, ['id', 'title']>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
Expand All @@ -187,7 +187,7 @@ export declare const Post: (new (init: ModelInit<Post>) => Post) & {
type EagerComment = {
readonly [__modelMeta__]: {
identifier: CompositeIdentifier<Comment, ['id', 'content']>;
identifier: OptionallyManagedCompositeIdentifier extends OptionallyManagedCompositeIdentifierDisabled ? CompositeIdentifier<Comment, ['id', 'content']> : OptionallyManagedCompositeIdentifier<Comment, ['id', 'content']>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
Expand All @@ -200,7 +200,7 @@ type EagerComment = {
type LazyComment = {
readonly [__modelMeta__]: {
identifier: CompositeIdentifier<Comment, ['id', 'content']>;
identifier: OptionallyManagedCompositeIdentifier extends OptionallyManagedCompositeIdentifierDisabled ? CompositeIdentifier<Comment, ['id', 'content']> : OptionallyManagedCompositeIdentifier<Comment, ['id', 'content']>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
Expand All @@ -221,15 +221,15 @@ export declare const Comment: (new (init: ModelInit<Comment>) => 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\\";
// @ts-ignore
import { LazyLoading, LazyLoadingDisabled, AsyncItem } from \\"@aws-amplify/datastore\\";
import { LazyLoading, LazyLoadingDisabled, OptionallyManagedCompositeIdentifier, OptionallyManagedCompositeIdentifierDisabled, AsyncItem } from \\"@aws-amplify/datastore\\";
type EagerProject = {
readonly [__modelMeta__]: {
identifier: CompositeIdentifier<Project, ['id', 'name']>;
identifier: OptionallyManagedCompositeIdentifier extends OptionallyManagedCompositeIdentifierDisabled ? CompositeIdentifier<Project, ['id', 'name']> : OptionallyManagedCompositeIdentifier<Project, ['id', 'name']>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
Expand All @@ -243,7 +243,7 @@ type EagerProject = {
type LazyProject = {
readonly [__modelMeta__]: {
identifier: CompositeIdentifier<Project, ['id', 'name']>;
identifier: OptionallyManagedCompositeIdentifier extends OptionallyManagedCompositeIdentifierDisabled ? CompositeIdentifier<Project, ['id', 'name']> : OptionallyManagedCompositeIdentifier<Project, ['id', 'name']>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
Expand All @@ -263,7 +263,7 @@ export declare const Project: (new (init: ModelInit<Project>) => Project) & {
type EagerTeam = {
readonly [__modelMeta__]: {
identifier: CompositeIdentifier<Team, ['id', 'name']>;
identifier: OptionallyManagedCompositeIdentifier extends OptionallyManagedCompositeIdentifierDisabled ? CompositeIdentifier<Team, ['id', 'name']> : OptionallyManagedCompositeIdentifier<Team, ['id', 'name']>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
Expand All @@ -277,7 +277,7 @@ type EagerTeam = {
type LazyTeam = {
readonly [__modelMeta__]: {
identifier: CompositeIdentifier<Team, ['id', 'name']>;
identifier: OptionallyManagedCompositeIdentifier extends OptionallyManagedCompositeIdentifierDisabled ? CompositeIdentifier<Team, ['id', 'name']> : OptionallyManagedCompositeIdentifier<Team, ['id', 'name']>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ describe('New model meta field test', () => {
expect(declarations).toMatchInlineSnapshot(`
"import { ModelInit, MutableModel, __modelMeta__, ManagedIdentifier, OptionallyManagedIdentifier, CompositeIdentifier, CustomIdentifier } from \\"@aws-amplify/datastore\\";
// @ts-ignore
import { LazyLoading, LazyLoadingDisabled } from \\"@aws-amplify/datastore\\";
import { LazyLoading, LazyLoadingDisabled, OptionallyManagedCompositeIdentifier, OptionallyManagedCompositeIdentifierDisabled } from \\"@aws-amplify/datastore\\";
Expand Down Expand Up @@ -1050,7 +1050,7 @@ describe('New model meta field test', () => {
type EagerModelExplicitIdWithSk = {
readonly [__modelMeta__]: {
identifier: CompositeIdentifier<ModelExplicitIdWithSk, ['id', 'name']>;
identifier: OptionallyManagedCompositeIdentifier extends OptionallyManagedCompositeIdentifierDisabled ? CompositeIdentifier<ModelExplicitIdWithSk, ['id', 'name']> : OptionallyManagedCompositeIdentifier<ModelExplicitIdWithSk, ['id', 'name']>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
Expand All @@ -1062,7 +1062,7 @@ describe('New model meta field test', () => {
type LazyModelExplicitIdWithSk = {
readonly [__modelMeta__]: {
identifier: CompositeIdentifier<ModelExplicitIdWithSk, ['id', 'name']>;
identifier: OptionallyManagedCompositeIdentifier extends OptionallyManagedCompositeIdentifierDisabled ? CompositeIdentifier<ModelExplicitIdWithSk, ['id', 'name']> : OptionallyManagedCompositeIdentifier<ModelExplicitIdWithSk, ['id', 'name']>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -48,7 +49,7 @@ export class AppSyncModelTypeScriptVisitor<
this.processDirectives(
shouldUseModelNameFieldInHasManyAndBelongsTo,
shouldImputeKeyForUniDirectionalHasMany,
shouldUseFieldsInAssociatedWithInHasOne
shouldUseFieldsInAssociatedWithInHasOne,
);
const imports = this.generateImports();
const enumDeclarations = Object.values(this.enumMap)
Expand Down Expand Up @@ -150,7 +151,15 @@ export class AppSyncModelTypeScriptVisitor<
identifierFields.length === 1 ? `'${identifierFields[0]}'` : `[${identifierFields.map(fieldStr => `'${fieldStr}'`).join(', ')}]`;
if (identifierFields.length > 1) {
this.BASE_DATASTORE_IMPORT.add('CompositeIdentifier');
return `CompositeIdentifier<${modelObj.name}, ${identifierFieldsStr}>`;
const compositeIdentifierType = `CompositeIdentifier<${modelObj.name}, ${identifierFieldsStr}>`;
if (primaryKeyField.name === DEFAULT_HASH_KEY_FIELD) {
this.TS_IGNORE_DATASTORE_IMPORT.add('OptionallyManagedCompositeIdentifier');
this.TS_IGNORE_DATASTORE_IMPORT.add('OptionallyManagedCompositeIdentifierDisabled');
const optionallyManagedType = `OptionallyManagedCompositeIdentifier<${modelObj.name}, ${identifierFieldsStr}>`;
const condition = 'OptionallyManagedCompositeIdentifier extends OptionallyManagedCompositeIdentifierDisabled';
return `${condition} ? ${compositeIdentifierType} : ${optionallyManagedType}`;
}
return compositeIdentifierType;
}
this.BASE_DATASTORE_IMPORT.add('CustomIdentifier');
return `CustomIdentifier<${modelObj.name}, ${identifierFieldsStr}>`;
Expand Down

0 comments on commit efb2c3f

Please sign in to comment.