Commit b956863f authored by nanahira's avatar nanahira

better relation handle

parent 41c4c7a7
...@@ -28,10 +28,23 @@ export type EntityId<T extends { id: any }> = T['id']; ...@@ -28,10 +28,23 @@ export type EntityId<T extends { id: any }> = T['id'];
export interface RelationDef { export interface RelationDef {
name: string; name: string;
inner?: boolean; inner?: boolean;
extraCondition?: string;
extraConditionFields?: Record<string, any>;
noSelect?: boolean;
} }
export const Inner = (name: string): RelationDef => { export const Relation = (
return { name, inner: true }; name: string,
options: Omit<RelationDef, 'name'> = {},
): RelationDef => {
return { name, inner: false, ...options };
};
export const Inner = (
name: string,
options: Omit<RelationDef, 'name'> = {},
): RelationDef => {
return Relation(name, { inner: true, ...options });
}; };
export type ValidCrudEntity<T> = Record<string, any> & { export type ValidCrudEntity<T> = Record<string, any> & {
...@@ -200,10 +213,16 @@ export class CrudBase<T extends ValidCrudEntity<T>> { ...@@ -200,10 +213,16 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
: relationUnit.slice(0, relationUnit.length - 1).join('_'); : relationUnit.slice(0, relationUnit.length - 1).join('_');
const property = relationUnit[relationUnit.length - 1]; const property = relationUnit[relationUnit.length - 1];
const properyAlias = relationUnit.join('_'); const properyAlias = relationUnit.join('_');
const methodName = relation.inner const methodName = relation.inner ? 'innerJoin' : 'leftJoin';
? 'innerJoinAndSelect' if (!relation.noSelect) {
: ('leftJoinAndSelect' as const); qb.addSelect(properyAlias);
qb[methodName](`${base}.${property}`, properyAlias); }
qb[methodName](
`${base}.${property}`,
properyAlias,
relation.extraCondition || undefined,
relation.extraConditionFields || undefined,
);
} }
_applyRelationsToQuery(qb: SelectQueryBuilder<T>) { _applyRelationsToQuery(qb: SelectQueryBuilder<T>) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment