mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-06 05:59:25 +08:00
* fix: slow join query issue by appends field in repository.find * feat: handle appending query in multiple relation repository * feat: handle appending query in single relation repository Co-authored-by: chenos <chenlinxh@gmail.com> (cherry picked from commit 9222ff4f0ca92bc554107fccc0cf2a9d6290e56d) # Conflicts: # packages/core/database/src/__tests__/relation-repository/hasone-repository.test.ts # packages/core/database/src/repository.ts
46 lines
1.0 KiB
TypeScript
46 lines
1.0 KiB
TypeScript
import { omit } from 'lodash';
|
|
import { Model } from './model';
|
|
|
|
type HandleAppendsQueryOptions = {
|
|
templateModel: any;
|
|
queryPromises: Array<any>;
|
|
};
|
|
|
|
export async function handleAppendsQuery(options: HandleAppendsQueryOptions) {
|
|
const { templateModel, queryPromises } = options;
|
|
|
|
const results = await Promise.all(queryPromises);
|
|
|
|
let rows: Array<Model>;
|
|
|
|
for (const appendedResult of results) {
|
|
if (!rows) {
|
|
rows = appendedResult.rows;
|
|
|
|
if (rows.length == 0) {
|
|
return [];
|
|
}
|
|
|
|
const modelOptions = templateModel['_options'];
|
|
for (const row of rows) {
|
|
row['_options'] = {
|
|
...row['_options'],
|
|
include: modelOptions['include'],
|
|
includeNames: modelOptions['includeNames'],
|
|
includeMap: modelOptions['includeMap'],
|
|
};
|
|
}
|
|
continue;
|
|
}
|
|
|
|
for (let i = 0; i < appendedResult.rows.length; i++) {
|
|
const key = appendedResult.include.association;
|
|
const val = appendedResult.rows[i].get(key);
|
|
|
|
rows[i].set(key, val);
|
|
}
|
|
}
|
|
|
|
return rows;
|
|
}
|