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

feat: composite id backend support #19849

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
2 changes: 1 addition & 1 deletion generators/angular/entity-files-angular.mts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export async function writeEntitiesFiles(
for (const entity of entities.filter(entity => !entity.skipClient && !entity.builtIn)) {
await this.writeFiles({
sections: angularFiles,
context: { ...application, ...entity, getWebappTranslation: control.getWebappTranslation },
context: { ...application, ...entity, entity, getWebappTranslation: control.getWebappTranslation },
});
}
}
Expand Down
52 changes: 18 additions & 34 deletions generators/base-application/support/prepare-entity.mts
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,12 @@ export function prepareEntityPrimaryKeyForTemplates(entityWithConfig, generator,
entityWithConfig.primaryKey = {
fieldName: 'id',
derived: true,
// MapsId copy the id from the relationship.
autoGenerate: true,
get fields() {
return this.derivedFields;
return this.derivedFields.map(field => preparePrimaryKeyFields(field));
},
get autoGenerate() {
// MapsId=!composite is autoGenerate because it copies the id from the relationship.
return !this.composite;
},
get derivedFields() {
return relationshipId.derivedPrimaryKey.derivedFields;
Expand All @@ -380,9 +382,6 @@ export function prepareEntityPrimaryKeyForTemplates(entityWithConfig, generator,
get composite() {
return relationshipId.otherEntity.primaryKey.composite;
},
get ids() {
return this.fields.map(field => fieldToId(field));
},
};
} else {
const composite = enableCompositeId ? idCount > 1 : false;
Expand Down Expand Up @@ -414,7 +413,7 @@ export function prepareEntityPrimaryKeyForTemplates(entityWithConfig, generator,
ownFields: idFields,
// Fields declared and inherited
get fields() {
return [...this.ownFields, ...this.derivedFields];
return [...this.ownFields, ...this.derivedFields].map(field => preparePrimaryKeyFields(field));
},
get autoGenerate() {
return this.composite ? false : this.fields[0].autoGenerate;
Expand All @@ -423,42 +422,27 @@ export function prepareEntityPrimaryKeyForTemplates(entityWithConfig, generator,
get derivedFields() {
return this.relationships.map(rel => rel.derivedPrimaryKey.derivedFields).flat();
},
get ids() {
return this.fields.map(field => fieldToId(field));
},
};
}
return entityWithConfig;
}

function fieldToId(field) {
return {
field,
get name() {
return field.fieldName;
},
get nameCapitalized() {
return field.fieldNameCapitalized;
},
get nameDotted() {
function preparePrimaryKeyFields(field) {
Object.defineProperty(field, 'fieldNameDotted', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more meaningful name

Suggested change
Object.defineProperty(field, 'fieldNameDotted', {
Object.defineProperty(field, 'tsFieldPath', {

get() {
return field.derivedPath ? field.derivedPath.join('.') : field.fieldName;
},
get nameDottedAsserted() {
enumerable: true,
configurable: true,
});
Object.defineProperty(field, 'fieldNameDottedAsserted', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more meaningful name

Suggested change
Object.defineProperty(field, 'fieldNameDottedAsserted', {
Object.defineProperty(field, 'tsFieldPathRequired', {

get() {
return field.derivedPath ? `${field.derivedPath.join('!.')}!` : `${field.fieldName}!`;
},
get setter() {
return `set${this.nameCapitalized}`;
},
get getter() {
return (field.fieldType === BOOLEAN ? 'is' : 'get') + this.nameCapitalized;
},
get autoGenerate() {
return !!field.autoGenerate;
},
get relationshipsPath() {
return field.relationshipsPath;
},
};
enumerable: true,
configurable: true,
});
return field;
}

/**
Expand Down
28 changes: 14 additions & 14 deletions generators/base-application/support/prepare-entity.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ describe('generator - base-application - support - prepareEntity', () => {
expect(field).to.deep.include({
...entity1.primaryKey.fields[0],
fieldName: 'otherEntity1Id',
fieldNameDotted: 'otherEntity1.id',
fieldNameDottedAsserted: 'otherEntity1!.id!',
fieldNameCapitalized: 'OtherEntity1Id',
columnName: 'other_entity1_id',
derivedPath: ['otherEntity1', 'id'],
Expand All @@ -258,14 +260,12 @@ describe('generator - base-application - support - prepareEntity', () => {
});

it('should prepare correct relationship id ids', () => {
const field = entity4.primaryKey.ids[1];
const field = entity4.primaryKey.fields[1];
expect(field).to.deep.include({
name: 'otherEntity1Id',
nameCapitalized: 'OtherEntity1Id',
nameDotted: 'otherEntity1.id',
nameDottedAsserted: 'otherEntity1!.id!',
setter: 'setOtherEntity1Id',
getter: 'getOtherEntity1Id',
fieldName: 'otherEntity1Id',
fieldNameCapitalized: 'OtherEntity1Id',
fieldNameDotted: 'otherEntity1.id',
fieldNameDottedAsserted: 'otherEntity1!.id!',
});
});

Expand All @@ -275,6 +275,8 @@ describe('generator - base-application - support - prepareEntity', () => {
...entity3.primaryKey.fields[0],
derived: true,
fieldName: 'otherEntity3Uuid',
fieldNameDotted: 'otherEntity3.uuid',
fieldNameDottedAsserted: 'otherEntity3!.uuid!',
fieldNameCapitalized: 'OtherEntity3Uuid',
columnName: 'other_entity3_uuid',
derivedPath: ['otherEntity3', 'uuid'],
Expand All @@ -286,14 +288,12 @@ describe('generator - base-application - support - prepareEntity', () => {
});

it('should prepare correct relationship id with derived primaryKey field ids', () => {
const field = entity4.primaryKey.ids[2];
const field = entity4.primaryKey.fields[2];
expect(field).to.deep.include({
name: 'otherEntity3Uuid',
nameCapitalized: 'OtherEntity3Uuid',
nameDotted: 'otherEntity3.uuid',
nameDottedAsserted: 'otherEntity3!.uuid!',
setter: 'setOtherEntity3Uuid',
getter: 'getOtherEntity3Uuid',
fieldName: 'otherEntity3Uuid',
fieldNameCapitalized: 'OtherEntity3Uuid',
fieldNameDotted: 'otherEntity3.uuid',
fieldNameDottedAsserted: 'otherEntity3!.uuid!',
});
});
});
Expand Down
Loading