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

[DONOT MERGE]fix: handle Enums correctly on Typescript data schema generation #2965

Draft
wants to merge 105 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
105 commits
Select commit Hold shift + click to select a range
3e4a3a2
added enums via ref for single models
tejas2008 Sep 12, 2024
7bc5b64
extended implementation to enums for multiple models and added filte…
tejas2008 Sep 12, 2024
6161264
added naming in npascal case
tejas2008 Sep 13, 2024
07535fa
added enum support for postgresql multiple models same enum
tejas2008 Sep 16, 2024
3436237
added unit tests to test working enum handling
tejas2008 Sep 17, 2024
6aed951
Merge branch 'aws-amplify:main' into Handle-enums-sql
tejas2008 Sep 17, 2024
4030828
added error handling for unsupported engines removed unused code and …
tejas2008 Sep 19, 2024
c4f9b4d
variable naming updated for enums
tejas2008 Sep 20, 2024
c3d3ee1
fix linting issues
tejas2008 Sep 20, 2024
8227f72
refactored code for readbility and updated unit tests
tejas2008 Sep 24, 2024
bb0a235
fixed linting issues
tejas2008 Sep 24, 2024
5ab00c2
updated one comment and fixed lint issue
tejas2008 Sep 24, 2024
b064a2d
fixed nit
tejas2008 Sep 26, 2024
4e52a85
fixed nit naming
tejas2008 Sep 26, 2024
2a88fb9
fixed nit naming
tejas2008 Sep 26, 2024
3734e8f
lint errors fixed
tejas2008 Oct 2, 2024
94b0767
extracted api
tejas2008 Oct 2, 2024
c383ec5
Merge branch 'main' into Handle-enums-sql
tejas2008 Oct 2, 2024
47697f4
extracted api transformer common
tejas2008 Oct 2, 2024
1b80f3b
Merge branch 'Handle-enums-sql' of https://github.com/tejas2008/ampli…
tejas2008 Oct 2, 2024
f11e13c
helper lint
tejas2008 Oct 2, 2024
9b04649
lint fix
tejas2008 Oct 3, 2024
19273c7
Merge branch 'main' of https://github.com/aws-amplify/amplify-categor…
tejas2008 Oct 9, 2024
0e24671
chore: update .jsii assembly
tejas2008 Oct 16, 2024
1fe43f3
chore: update .jsii assembly
tejas2008 Oct 16, 2024
2f59ab1
chore: update .jsii assembly
tejas2008 Oct 18, 2024
715256c
Merge branch 'main' into Handle-enums-sql
tejas2008 Oct 18, 2024
b65f9c0
resolved merge
tejas2008 Oct 18, 2024
3163fac
updated postgresql enum naming
tejas2008 Oct 22, 2024
da372cd
Merge branch 'main' into Handle-enums-sql
tejas2008 Oct 23, 2024
dc106d1
ran prettier
tejas2008 Oct 25, 2024
8221d5c
updated .jsii data-construct to merge conflict
tejas2008 Oct 29, 2024
2b99eba
added enums via ref for single models
tejas2008 Sep 12, 2024
e8a1303
extended implementation to enums for multiple models and added filte…
tejas2008 Sep 12, 2024
56fed89
added naming in npascal case
tejas2008 Sep 13, 2024
3044abb
added enum support for postgresql multiple models same enum
tejas2008 Sep 16, 2024
eb0b4a9
added unit tests to test working enum handling
tejas2008 Sep 17, 2024
3f19913
added error handling for unsupported engines removed unused code and …
tejas2008 Sep 19, 2024
ac37b49
variable naming updated for enums
tejas2008 Sep 20, 2024
94f7c96
fix linting issues
tejas2008 Sep 20, 2024
4e2ad9c
merge conflicts
tejas2008 Oct 29, 2024
fd4a885
fixed linting issues
tejas2008 Sep 24, 2024
4d7bfca
updated one comment and fixed lint issue
tejas2008 Sep 24, 2024
e950fd6
fixed nit
tejas2008 Sep 26, 2024
3a26d1f
fixed nit naming
tejas2008 Sep 26, 2024
6336e8a
fixed nit naming
tejas2008 Sep 26, 2024
2799133
lint errors fixed
tejas2008 Oct 2, 2024
ddcdb34
extracted api
tejas2008 Oct 2, 2024
54ad185
extracted api transformer common
tejas2008 Oct 2, 2024
aac9470
fix(model-transformer) IndexName -> index in query list resolver (#2912)
atierian Oct 2, 2024
eb45d75
helper lint
tejas2008 Oct 2, 2024
c4ddbe1
lint fix
tejas2008 Oct 3, 2024
fe4ba03
chore: upgrade cdk library dependency to 2.158.0 (#2876)
phani-srikar Oct 3, 2024
62eec22
test: fix gen 1 init (#2924)
dpilch Oct 7, 2024
2564460
fix(conversation): allow changes to systemPrompt, inferenceConfig, ai…
atierian Oct 7, 2024
5f2c4f7
feat: auto increment support (#2883)
p5quared Oct 7, 2024
d1e128b
fix(conversation): use functionMap for custom handler IFunction refer…
atierian Oct 8, 2024
45a134b
fix(generation): gracefully handle stringified tool_use responses (#2…
atierian Oct 8, 2024
4f22e64
feat(conversation): per message items and lambda history retrieval pa…
atierian Oct 8, 2024
4e6bb2f
fix: sql default value e2e failures (#2932)
p5quared Oct 9, 2024
0b8faaf
fix(generation): remove trailing comma in inferenceConfig resolver co…
atierian Oct 9, 2024
42470d1
chore: update .jsii assembly
tejas2008 Oct 16, 2024
a2e52e0
chore: update .jsii assembly
tejas2008 Oct 16, 2024
43b66f3
chore: update .jsii assembly
tejas2008 Oct 18, 2024
a14dde5
fix: add aws_iam to custom operations when enableIamAuthorization is …
palpatim Oct 10, 2024
55a91d7
fix: appsync ttl correct duration time unit in ms (#2928)
Siqi-Shan Oct 10, 2024
75fdbb2
chore: setup node version during deploy
palpatim Oct 10, 2024
32fd92f
chore(release): Publish [ci skip]
amplify-data-ci Oct 10, 2024
2de626e
chore: update .jsii assembly
amplify-data-ci Oct 10, 2024
57e858a
chore: update .jsii assembly
amplify-data-ci Oct 10, 2024
ae8ff19
test: miscellaneous test fixes (#2941)
palpatim Oct 11, 2024
d33567f
chore: upgrade cookie version (#2954)
phani-srikar Oct 15, 2024
824bb98
chore: cleanup imports and update deprecated jest functions (#2953)
p5quared Oct 16, 2024
c7e9132
feat: constrain custom conversation handler event version (#2958)
sobolk Oct 16, 2024
3960534
chore: migrate pg model dynamic auth e2e test in gen2 cdk (#2920)
Siqi-Shan Oct 17, 2024
99aecd9
fix: enable IAM auth for custom types (#2961)
palpatim Oct 17, 2024
aea44b6
Revert "fix: enable IAM auth for custom types (#2961)" (#2964)
palpatim Oct 17, 2024
37c6e99
chore(release): Publish [ci skip]
amplify-data-ci Oct 17, 2024
1d49202
chore: update .jsii assembly
amplify-data-ci Oct 17, 2024
a422175
chore: update .jsii assembly
amplify-data-ci Oct 17, 2024
0199eeb
updated postgresql enum naming
tejas2008 Oct 22, 2024
ac0492c
feat: import existing table to amplify managed table (#2634)
dpilch Jul 12, 2024
867c356
feat: get datasource map for migration (#2668)
dpilch Jul 12, 2024
0f55f48
feat: validate imported tables (#2696)
dpilch Jul 24, 2024
21c4037
test: basic migration e2es (#2703)
dpilch Aug 2, 2024
ea70f6d
fix: adjust default table properties (#2726)
dpilch Aug 5, 2024
1941b78
feat: add overrideIndexName to configure index name (#2738)
dpilch Aug 8, 2024
990a850
refactor: a few migration pr followups (#2753)
dpilch Aug 9, 2024
981798a
test: migration validation e2e (#2752)
dpilch Aug 20, 2024
4e51d2a
test: add additional migration validation test cases (#2792)
dpilch Aug 22, 2024
7ab0cd3
Revert "feat: get datasource map for migration (#2668)" (#2907)
dpilch Sep 25, 2024
89c3f71
fix: allowlist comparison properties on imported table (#2908)
dpilch Oct 11, 2024
e85f50f
feat: add metrics metadata for auth mode and custom operations (#2966)
dpilch Oct 21, 2024
0a3b929
fix: enable IAM auth for custom types (#2961) (#2971)
palpatim Oct 21, 2024
cb2113b
feat: generate ts schema from pg serial field (#2952)
p5quared Oct 22, 2024
19d0734
chore: add region to sync with the lambda layer (#2973)
Siqi-Shan Oct 22, 2024
a395e8d
feat(conversation): enable update mutations on conversation model (#2…
atierian Oct 22, 2024
f329f1e
Revert "fix: enable IAM auth for custom types (#2961) (#2971)" (#2975)
palpatim Oct 22, 2024
bb2325a
Revert "Merge pull request #2976 from aws-amplify/merge-main-to-migra…
dpilch Oct 23, 2024
9730c1b
refactor(conversation): consolidate resolver generation logic (#2957)
atierian Oct 23, 2024
ecf79ac
updated .jsii data-construct to merge conflict
tejas2008 Oct 29, 2024
a418d5e
rebased with main
tejas2008 Oct 29, 2024
e892963
Merge branch 'main' into Handle-enums-sql
tejas2008 Oct 29, 2024
855ddd4
merge errors
tejas2008 Oct 29, 2024
bf29468
Merge branch 'Handle-enums-sql' of https://github.com/aws-amplify/amp…
tejas2008 Oct 29, 2024
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 @@ -85,12 +85,120 @@ export const schema = a.schema({
\\"User\\": a.model({
id: a.string().required(),
name: a.string(),
status: a.enum([
\\"ACTIVE\\",
\\"INACTIVE\\"
])
status: a.ref(\\"UserStatus\\")
}).identifier([
\\"id\\"
]),
UserStatus: a.enum([
\\"ACTIVE\\",
\\"INACTIVE\\"
])
});
"
`;

exports[`Type name conversions generates enums for models in mysql 1`] = `
"/* eslint-disable */
/* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. */
import { a } from \\"@aws-amplify/data-schema\\";

export const schema = a.schema({
\\"User\\": a.model({
id: a.string().required(),
name: a.string(),
status: a.ref(\\"UserStatus\\").required()
}).identifier([
\\"id\\"
]),
\\"Test\\": a.model({
id: a.string().required(),
name: a.string(),
age: a.ref(\\"TestAge\\").required()
}).identifier([
\\"id\\"
]),
UserStatus: a.enum([
\\"ACTIVE\\",
\\"INACTIVE\\"
]),
TestAge: a.enum([
\\"Above18\\",
\\"Below18\\"
])
});
"
`;

exports[`Type name conversions generates required and null enums correctly in the same model 1`] = `
"/* eslint-disable */
/* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. */
import { a } from \\"@aws-amplify/data-schema\\";

export const schema = a.schema({
\\"User\\": a.model({
id: a.string().required(),
name: a.string(),
statsu1: a.ref(\\"UserStatus1\\"),
status: a.ref(\\"UserStatus\\").required()
}).identifier([
\\"id\\"
]),
UserStatus1: a.enum([
\\"ACTIVE\\",
\\"INACTIVE\\"
]),
UserStatus: a.enum([
\\"ACTIVE\\",
\\"INACTIVE\\"
])
});
"
`;

exports[`Type name conversions generates required enums correctly 1`] = `
"/* eslint-disable */
/* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. */
import { a } from \\"@aws-amplify/data-schema\\";

export const schema = a.schema({
\\"User\\": a.model({
id: a.string().required(),
name: a.string(),
status: a.ref(\\"UserStatus\\").required()
}).identifier([
\\"id\\"
]),
UserStatus: a.enum([
\\"ACTIVE\\",
\\"INACTIVE\\"
])
});
"
`;

exports[`Type name conversions generates single enum referenced for two different models 1`] = `
"/* eslint-disable */
/* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. */
import { a } from \\"@aws-amplify/data-schema\\";

export const schema = a.schema({
\\"User\\": a.model({
id: a.string().required(),
name: a.string(),
status: a.ref(\\"UserStatus\\").required()
}).identifier([
\\"id\\"
]),
\\"Test\\": a.model({
id: a.string().required(),
name: a.string(),
status: a.ref(\\"UserStatus\\").required()
}).identifier([
\\"id\\"
]),
UserStatus: a.enum([
\\"ACTIVE\\",
\\"INACTIVE\\"
])
});
"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,77 @@ describe('Type name conversions', () => {
expect(graphqlSchema).toMatchSnapshot();
});

it('generates required enums correctly', () => {
const dbschema = new Schema(new Engine('Postgres'));
const model = new Model('User');
model.addField(new Field('id', { kind: 'NonNull', type: { kind: 'Scalar', name: 'String' } }));
model.addField(new Field('name', { kind: 'Scalar', name: 'String' }));
model.addField(new Field('status', { kind: 'NonNull', type: { kind: 'Enum', name: 'UserStatus', values: ['ACTIVE', 'INACTIVE'] } }));
model.setPrimaryKey(['id']);
dbschema.addModel(model);

const graphqlSchema = generateTypescriptDataSchema(dbschema);
expect(graphqlSchema).toMatchSnapshot();
});

it('generates required and null enums correctly in the same model', () => {
const dbschema = new Schema(new Engine('Postgres'));
const model = new Model('User');
model.addField(new Field('id', { kind: 'NonNull', type: { kind: 'Scalar', name: 'String' } }));
model.addField(new Field('name', { kind: 'Scalar', name: 'String' }));
model.addField(new Field('statsu1', { kind: 'Enum', name: 'UserStatus1', values: ['ACTIVE', 'INACTIVE'] }));
model.addField(new Field('status', { kind: 'NonNull', type: { kind: 'Enum', name: 'UserStatus', values: ['ACTIVE', 'INACTIVE'] } }));
model.setPrimaryKey(['id']);
dbschema.addModel(model);

const graphqlSchema = generateTypescriptDataSchema(dbschema);
expect(graphqlSchema).toMatchSnapshot();
});

it('generates single enum referenced for two different models', () => {
const dbschema = new Schema(new Engine('Postgres'));
const modelUser = new Model('User');
const modelTest = new Model('Test');
modelUser.addField(new Field('id', { kind: 'NonNull', type: { kind: 'Scalar', name: 'String' } }));
modelUser.addField(new Field('name', { kind: 'Scalar', name: 'String' }));
modelUser.addField(
new Field('status', { kind: 'NonNull', type: { kind: 'Enum', name: 'UserStatus', values: ['ACTIVE', 'INACTIVE'] } }),
);
modelUser.setPrimaryKey(['id']);
dbschema.addModel(modelUser);
modelTest.addField(new Field('id', { kind: 'NonNull', type: { kind: 'Scalar', name: 'String' } }));
modelTest.addField(new Field('name', { kind: 'Scalar', name: 'String' }));
modelTest.addField(
new Field('status', { kind: 'NonNull', type: { kind: 'Enum', name: 'UserStatus', values: ['ACTIVE', 'INACTIVE'] } }),
);
modelTest.setPrimaryKey(['id']);
dbschema.addModel(modelTest);

const graphqlSchema = generateTypescriptDataSchema(dbschema);
expect(graphqlSchema).toMatchSnapshot();
});

it('generates enums for models in mysql', () => {
const dbschema = new Schema(new Engine('MySQL'));
const modelUser = new Model('User');
const modelTest = new Model('Test');
modelUser.addField(new Field('id', { kind: 'NonNull', type: { kind: 'Scalar', name: 'String' } }));
modelUser.addField(new Field('name', { kind: 'Scalar', name: 'String' }));
modelUser.addField(
new Field('status', { kind: 'NonNull', type: { kind: 'Enum', name: 'userStatus', values: ['ACTIVE', 'INACTIVE'] } }),
);
modelUser.setPrimaryKey(['id']);
dbschema.addModel(modelUser);
modelTest.addField(new Field('id', { kind: 'NonNull', type: { kind: 'Scalar', name: 'String' } }));
modelTest.addField(new Field('name', { kind: 'Scalar', name: 'String' }));
modelTest.addField(new Field('age', { kind: 'NonNull', type: { kind: 'Enum', name: 'TestAge', values: ['Above18', 'Below18'] } }));
modelTest.setPrimaryKey(['id']);
dbschema.addModel(modelTest);

const graphqlSchema = generateTypescriptDataSchema(dbschema);
expect(graphqlSchema).toMatchSnapshot();
});

it('schema with database config secret and vpc should generate typescript data schema with configure', () => {
const dbschema = new Schema(new Engine('MySQL'));
let model = new Model('User');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('testStringDataSourceAdapter', () => {
});
expect(adapter.mapDataType('enum', true, 'table', 'field', 'enum("OPEN","CLOSED")')).toEqual({
kind: 'Enum',
name: 'table_field',
name: 'TableField',
values: ['OPEN', 'CLOSED'],
});
expect(adapter.mapDataType('bool', true, 'table', 'field', 'varchar(50)')).toEqual({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { toPascalCase } from 'graphql-transformer-common';
import { EnumType, Field, FieldDataType, FieldType, Index } from '../schema-representation';
import { StringDataSourceAdapter, EmptySchemaError, InvalidSchemaError } from './string-datasource-adapter';

Expand Down Expand Up @@ -279,12 +280,12 @@ export class MySQLStringDataSourceAdapter extends StringDataSourceAdapter {
return value.match(regex).map((a) => a.slice(1, -1));
}

private generateEnumName(tableName: string, fieldName: string) {
const enumNamePrefix = [tableName, fieldName].join('_');
private generateEnumName(tableName: string, fieldName: string): string {
const enumNamePrefix = toPascalCase([tableName, fieldName]);
let enumName = enumNamePrefix;
let counter = 0;
while (this.enums.has(enumName)) {
enumName = [enumNamePrefix, counter.toString()].join('_');
enumName = toPascalCase([enumNamePrefix, counter.toString()]);
counter++;
}
return enumName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EnumType, Field, FieldDataType, FieldType, Index } from '../schema-representation';
import { StringDataSourceAdapter, EmptySchemaError, InvalidSchemaError } from './string-datasource-adapter';
import { toPascalCase } from 'graphql-transformer-common';

export interface PostgresIndex {
tableName: string;
Expand Down Expand Up @@ -147,12 +148,18 @@ export class PostgresStringDataSourceAdapter extends StringDataSourceAdapter {
return tableIndexes;
}

private generateEnumName(tableName: string, fieldName: string): string {
return toPascalCase([tableName, fieldName]);
}

protected setEnums(parsedSchema: PostgresSchema): void {
this.enums = new Map<string, EnumType>();
parsedSchema
.filter(({ enum_name }) => !!enum_name)
.forEach((row) => {
const enumName = row.enum_name;
const tableName = row.table_name;
const columnName = row.column_name;
const enumName = this.generateEnumName(tableName, columnName);
const enumValues = row.enum_values.substring(1, row.enum_values.length - 1).split(',');
const enumType: EnumType = {
kind: 'Enum',
Expand Down Expand Up @@ -262,20 +269,19 @@ export class PostgresStringDataSourceAdapter extends StringDataSourceAdapter {
fieldDatatype = 'AWSIPAddress';
break;
default:
if (this.enums.has(columntype)) {
if (this.enums.has(this.generateEnumName(tableName, fieldName))) {
fieldDatatype = 'ENUM';
}
break;
}

let result: FieldType;
if (fieldDatatype === 'ENUM') {
const enumName = this.getEnumName(columntype);
const enumRef = this.enums.get(columntype);
const enumRef = this.enums.get(this.generateEnumName(tableName, fieldName));
result = {
kind: 'Enum',
values: enumRef.values,
name: enumName,
name: enumRef.name,
};
} else {
result = {
Expand Down
Loading
Loading