Skip to content

Commit

Permalink
chore: identify join tables in generic data schema (#682)
Browse files Browse the repository at this point in the history
Co-authored-by: Hein Jeong <heinje@amazon.com>
  • Loading branch information
hein-j and Hein Jeong authored Sep 28, 2022
1 parent 791def0 commit 8452ff5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/codegen-ui/lib/__tests__/generic-from-datastore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('getGenericFromDataStore', () => {
});
});

it('should handle schema with assumed associated fields and modldkjld', () => {
it('should handle schema with assumed associated fields and models', () => {
const genericSchema = getGenericFromDataStore(schemaWithAssumptions);
const userFields = genericSchema.models.User.fields;

Expand All @@ -140,4 +140,13 @@ describe('getGenericFromDataStore', () => {
relatedModelField: 'userPostsId',
});
});

it('should correctly identify join tables', () => {
const genericSchema = getGenericFromDataStore(schemaWithRelationships);
const joinTables = Object.entries(genericSchema.models)
.filter(([, model]) => model.isJoinTable)
.map(([name]) => name);
expect(joinTables).toHaveLength(1);
expect(joinTables).toStrictEqual(['StudentTeacher']);
});
});
11 changes: 11 additions & 0 deletions packages/codegen-ui/lib/generic-from-datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export function getGenericFromDataStore(dataStoreSchema: DataStoreSchema): Gener
[modelName: string]: { [fieldName: string]: GenericDataField['relationship'] };
} = {};

const joinTableNames: string[] = [];

Object.values(dataStoreSchema.models).forEach((model) => {
const genericFields: { [fieldName: string]: GenericDataField } = {};

Expand All @@ -85,6 +87,8 @@ export function getGenericFromDataStore(dataStoreSchema: DataStoreSchema): Gener
'model' in associatedField.type &&
associatedField.type.model === model.name
) {
joinTableNames.push(associatedModel.name);

const relatedJoinField = Object.values(associatedModel.fields).find(
(joinField) =>
joinField.name !== associatedFieldName &&
Expand Down Expand Up @@ -136,6 +140,13 @@ export function getGenericFromDataStore(dataStoreSchema: DataStoreSchema): Gener
});
});

joinTableNames.forEach((joinTableName) => {
const model = genericSchema.models[joinTableName];
if (model) {
model.isJoinTable = true;
}
});

genericSchema.enums = dataStoreSchema.enums;

if (dataStoreSchema.nonModels) {
Expand Down
1 change: 1 addition & 0 deletions packages/codegen-ui/lib/types/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export type GenericDataField = {

export type GenericDataModel = {
fields: { [fieldName: string]: GenericDataField };
isJoinTable?: boolean;
};

export type GenericDataSchema = {
Expand Down

0 comments on commit 8452ff5

Please sign in to comment.