refactor(backend): get column names from metadata (#13943)
* ci: enable * chore: stop when generated column found * chore: get column names from metadata * ci: disable
This commit is contained in:
parent
9bddb81efc
commit
1a82a41f92
@ -82,34 +82,14 @@ import { MiReversiGame } from '@/models/ReversiGame.js';
|
|||||||
import type { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity.js';
|
import type { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity.js';
|
||||||
|
|
||||||
export interface MiRepository<T extends ObjectLiteral> {
|
export interface MiRepository<T extends ObjectLiteral> {
|
||||||
createTableColumnNames(this: Repository<T> & MiRepository<T>, queryBuilder: InsertQueryBuilder<T>): string[];
|
createTableColumnNames(this: Repository<T> & MiRepository<T>): string[];
|
||||||
createTableColumnNamesWithPrimaryKey(this: Repository<T> & MiRepository<T>, queryBuilder: InsertQueryBuilder<T>): string[];
|
|
||||||
insertOne(this: Repository<T> & MiRepository<T>, entity: QueryDeepPartialEntity<T>, findOptions?: Pick<FindOneOptions<T>, 'relations'>): Promise<T>;
|
insertOne(this: Repository<T> & MiRepository<T>, entity: QueryDeepPartialEntity<T>, findOptions?: Pick<FindOneOptions<T>, 'relations'>): Promise<T>;
|
||||||
selectAliasColumnNames(this: Repository<T> & MiRepository<T>, queryBuilder: InsertQueryBuilder<T>, builder: SelectQueryBuilder<T>): void;
|
selectAliasColumnNames(this: Repository<T> & MiRepository<T>, queryBuilder: InsertQueryBuilder<T>, builder: SelectQueryBuilder<T>): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const miRepository = {
|
export const miRepository = {
|
||||||
createTableColumnNames(queryBuilder) {
|
createTableColumnNames() {
|
||||||
// @ts-expect-error -- protected
|
return this.metadata.columns.filter(column => column.isSelect && !column.isVirtual).map(column => column.databaseName);
|
||||||
const insertedColumns = queryBuilder.getInsertedColumns();
|
|
||||||
if (insertedColumns.length) {
|
|
||||||
return insertedColumns.map(column => column.databaseName);
|
|
||||||
}
|
|
||||||
if (!queryBuilder.expressionMap.mainAlias?.hasMetadata && !queryBuilder.expressionMap.insertColumns.length) {
|
|
||||||
// @ts-expect-error -- protected
|
|
||||||
const valueSets = queryBuilder.getValueSets();
|
|
||||||
if (valueSets.length === 1) {
|
|
||||||
return Object.keys(valueSets[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return queryBuilder.expressionMap.insertColumns;
|
|
||||||
},
|
|
||||||
createTableColumnNamesWithPrimaryKey(queryBuilder) {
|
|
||||||
const columnNames = this.createTableColumnNames(queryBuilder);
|
|
||||||
if (!columnNames.includes('id')) {
|
|
||||||
columnNames.unshift('id');
|
|
||||||
}
|
|
||||||
return columnNames;
|
|
||||||
},
|
},
|
||||||
async insertOne(entity, findOptions?) {
|
async insertOne(entity, findOptions?) {
|
||||||
const queryBuilder = this.createQueryBuilder().insert().values(entity);
|
const queryBuilder = this.createQueryBuilder().insert().values(entity);
|
||||||
@ -117,7 +97,7 @@ export const miRepository = {
|
|||||||
const mainAlias = queryBuilder.expressionMap.mainAlias!;
|
const mainAlias = queryBuilder.expressionMap.mainAlias!;
|
||||||
const name = mainAlias.name;
|
const name = mainAlias.name;
|
||||||
mainAlias.name = 't';
|
mainAlias.name = 't';
|
||||||
const columnNames = this.createTableColumnNamesWithPrimaryKey(queryBuilder);
|
const columnNames = this.createTableColumnNames();
|
||||||
queryBuilder.returning(columnNames.reduce((a, c) => `${a}, ${queryBuilder.escape(c)}`, '').slice(2));
|
queryBuilder.returning(columnNames.reduce((a, c) => `${a}, ${queryBuilder.escape(c)}`, '').slice(2));
|
||||||
const builder = this.createQueryBuilder().addCommonTableExpression(queryBuilder, 'cte', { columnNames });
|
const builder = this.createQueryBuilder().addCommonTableExpression(queryBuilder, 'cte', { columnNames });
|
||||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||||
@ -138,7 +118,7 @@ export const miRepository = {
|
|||||||
selectOrAddSelect = (selection, selectionAliasName) => builder.addSelect(selection, selectionAliasName);
|
selectOrAddSelect = (selection, selectionAliasName) => builder.addSelect(selection, selectionAliasName);
|
||||||
return builder.select(selection, selectionAliasName);
|
return builder.select(selection, selectionAliasName);
|
||||||
};
|
};
|
||||||
for (const columnName of this.createTableColumnNamesWithPrimaryKey(queryBuilder)) {
|
for (const columnName of this.createTableColumnNames()) {
|
||||||
selectOrAddSelect(`${builder.alias}.${columnName}`, `${builder.alias}_${columnName}`);
|
selectOrAddSelect(`${builder.alias}.${columnName}`, `${builder.alias}_${columnName}`);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user