chore: get column names from metadata
This commit is contained in:
parent
18f5fb9cef
commit
2030556a5f
|
@ -80,38 +80,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 (this.metadata.columns.filter((column) => column.isGenerated).length) {
|
|
||||||
console.log(this.metadata.columns);
|
|
||||||
throw null;
|
|
||||||
}
|
|
||||||
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);
|
||||||
|
@ -119,7 +95,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
|
||||||
|
@ -140,7 +116,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