-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add lazy join decorator (#1240)
- Loading branch information
1 parent
036d7e4
commit 1b6b248
Showing
3 changed files
with
462 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
packages/datasource-customizer/src/decorators/lazy-join/collection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { | ||
AggregateResult, | ||
Aggregation, | ||
Caller, | ||
CollectionDecorator, | ||
FieldSchema, | ||
Filter, | ||
ManyToOneSchema, | ||
PaginatedFilter, | ||
Projection, | ||
RecordData, | ||
} from '@forestadmin/datasource-toolkit'; | ||
|
||
export default class LazyJoinDecorator extends CollectionDecorator { | ||
override async list( | ||
caller: Caller, | ||
filter: PaginatedFilter, | ||
projection: Projection, | ||
): Promise<RecordData[]> { | ||
const refinedProjection = projection.replace(field => this.refineField(field, projection)); | ||
const refinedFilter = await this.refineFilter(caller, filter); | ||
|
||
const records = await this.childCollection.list(caller, refinedFilter, refinedProjection); | ||
|
||
this.refineResults(projection, (relationName, foreignKey, foreignKeyTarget) => { | ||
records.forEach(record => { | ||
if (record[foreignKey]) { | ||
record[relationName] = { [foreignKeyTarget]: record[foreignKey] }; | ||
} | ||
|
||
delete record[foreignKey]; | ||
}); | ||
}); | ||
|
||
return records; | ||
} | ||
|
||
override async aggregate( | ||
caller: Caller, | ||
filter: Filter, | ||
aggregation: Aggregation, | ||
limit?: number, | ||
): Promise<AggregateResult[]> { | ||
const refinedAggregation = aggregation.replaceFields(field => | ||
this.refineField(field, aggregation.projection), | ||
); | ||
const refinedFilter = await this.refineFilter(caller, filter); | ||
|
||
const results = await this.childCollection.aggregate( | ||
caller, | ||
refinedFilter, | ||
refinedAggregation, | ||
limit, | ||
); | ||
|
||
this.refineResults(aggregation.projection, (relationName, foreignKey, foreignKeyTarget) => { | ||
results.forEach(result => { | ||
if (result.group[foreignKey]) { | ||
result.group[`${relationName}:${foreignKeyTarget}`] = result.group[foreignKey]; | ||
} | ||
|
||
delete result.group[foreignKey]; | ||
}); | ||
}); | ||
|
||
return results; | ||
} | ||
|
||
private isLazyRelationProjection(relation: FieldSchema, relationProjection: Projection) { | ||
return ( | ||
relation.type === 'ManyToOne' && | ||
relationProjection.length === 1 && | ||
relationProjection[0] === relation.foreignKeyTarget | ||
); | ||
} | ||
|
||
private refineField(field: string, projection: Projection): string { | ||
const relationName = field.split(':')[0]; | ||
const relation = this.schema.fields[relationName] as ManyToOneSchema; | ||
const relationProjection = projection.relations[relationName]; | ||
|
||
return this.isLazyRelationProjection(relation, relationProjection) | ||
? relation.foreignKey | ||
: field; | ||
} | ||
|
||
override async refineFilter(caller: Caller, filter: PaginatedFilter): Promise<PaginatedFilter> { | ||
if (filter.conditionTree) { | ||
filter.conditionTree = filter.conditionTree.replaceFields(field => | ||
this.refineField(field, filter.conditionTree.projection), | ||
); | ||
} | ||
|
||
return filter; | ||
} | ||
|
||
private refineResults( | ||
projection: Projection, | ||
handler: (relationName: string, foreignKey: string, foreignKeyTarget: string) => void, | ||
) { | ||
Object.entries(projection.relations).forEach(([relationName, relationProjection]) => { | ||
const relation = this.schema.fields[relationName] as ManyToOneSchema; | ||
|
||
if (this.isLazyRelationProjection(relation, relationProjection)) { | ||
const { foreignKeyTarget, foreignKey } = relation; | ||
handler(relationName, foreignKey, foreignKeyTarget); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.