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 14, 2023
1 parent 74eefdd commit 8de0aaf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ 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\\";
"import { ModelInit, MutableModel, __modelMeta__, OptionalManagedCompositeIdentifier } from \\"@aws-amplify/datastore\\";
// @ts-ignore
import { LazyLoading, LazyLoadingDisabled, AsyncCollection } from \\"@aws-amplify/datastore\\";
Expand All @@ -85,7 +85,7 @@ import { LazyLoading, LazyLoadingDisabled, AsyncCollection } from \\"@aws-amplif
type EagerPost = {
readonly [__modelMeta__]: {
identifier: CompositeIdentifier<Post, ['id', 'title']>;
identifier: 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<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<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<Comment, ['id', 'content']>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
Expand All @@ -147,7 +147,7 @@ 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\\";
"import { ModelInit, MutableModel, __modelMeta__, OptionalManagedCompositeIdentifier } from \\"@aws-amplify/datastore\\";
// @ts-ignore
import { LazyLoading, LazyLoadingDisabled, AsyncCollection } from \\"@aws-amplify/datastore\\";
Expand All @@ -157,7 +157,7 @@ import { LazyLoading, LazyLoadingDisabled, AsyncCollection } from \\"@aws-amplif
type EagerPost = {
readonly [__modelMeta__]: {
identifier: CompositeIdentifier<Post, ['id', 'title']>;
identifier: 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<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<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<Comment, ['id', 'content']>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
Expand All @@ -219,7 +219,7 @@ 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\\";
"import { ModelInit, MutableModel, __modelMeta__, OptionalManagedCompositeIdentifier } from \\"@aws-amplify/datastore\\";
// @ts-ignore
import { LazyLoading, LazyLoadingDisabled, AsyncItem } from \\"@aws-amplify/datastore\\";
Expand All @@ -229,7 +229,7 @@ import { LazyLoading, LazyLoadingDisabled, AsyncItem } from \\"@aws-amplify/data
type EagerProject = {
readonly [__modelMeta__]: {
identifier: CompositeIdentifier<Project, ['id', 'name']>;
identifier: 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<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<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<Team, ['id', 'name']>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\\";
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<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<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 @@ -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}>`;
}
Expand Down

0 comments on commit 8de0aaf

Please sign in to comment.