Skip to content

Commit

Permalink
feat: composite id backend support
Browse files Browse the repository at this point in the history
  • Loading branch information
yelhouti committed Jul 7, 2023
1 parent efede86 commit b288416
Show file tree
Hide file tree
Showing 40 changed files with 1,940 additions and 1,323 deletions.
46 changes: 14 additions & 32 deletions generators/base-application/support/prepare-entity.mts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ export function prepareEntityPrimaryKeyForTemplates(entityWithConfig, generator,
// MapsId copy the id from the relationship.
autoGenerate: true,
get fields() {
return this.derivedFields;
return this.derivedFields.map(field => preparePrimaryKeyFields(field));
},
get derivedFields() {
return relationshipId.derivedPrimaryKey.derivedFields;
Expand All @@ -380,9 +380,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 +411,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 +420,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', {
get() {
return field.derivedPath ? field.derivedPath.join('.') : field.fieldName;
},
get nameDottedAsserted() {
enumerable: true,
configurable: true,
});
Object.defineProperty(field, 'fieldNameDottedAsserted', {
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

0 comments on commit b288416

Please sign in to comment.