Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: generate OptionallyManagedCompositeIdentifier #701

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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