n8n/packages/cli/src/databases/dsl/Indices.ts
कारतोफ्फेलस्क्रिप्ट™ 8e392cfc1d
feat(core): Migrate to n8n's typeorm fork (#8590)
2024-02-08 15:13:29 +01:00

58 lines
1.4 KiB
TypeScript

import type { QueryRunner } from '@n8n/typeorm';
import { TableIndex } from '@n8n/typeorm';
import LazyPromise from 'p-lazy';
abstract class IndexOperation extends LazyPromise<void> {
abstract execute(queryRunner: QueryRunner): Promise<void>;
get fullTableName() {
return [this.tablePrefix, this.tableName].join('');
}
get fullIndexName() {
return ['IDX', `${this.tablePrefix}${this.tableName}`, ...this.columnNames].join('_');
}
constructor(
protected tableName: string,
protected columnNames: string[],
protected tablePrefix: string,
queryRunner: QueryRunner,
protected customIndexName?: string,
) {
super((resolve) => {
void this.execute(queryRunner).then(resolve);
});
}
}
export class CreateIndex extends IndexOperation {
constructor(
tableName: string,
columnNames: string[],
protected isUnique: boolean,
tablePrefix: string,
queryRunner: QueryRunner,
customIndexName?: string,
) {
super(tableName, columnNames, tablePrefix, queryRunner, customIndexName);
}
async execute(queryRunner: QueryRunner) {
const { columnNames, isUnique } = this;
return await queryRunner.createIndex(
this.fullTableName,
new TableIndex({ name: this.customIndexName ?? this.fullIndexName, columnNames, isUnique }),
);
}
}
export class DropIndex extends IndexOperation {
async execute(queryRunner: QueryRunner) {
return await queryRunner.dropIndex(
this.fullTableName,
this.customIndexName ?? this.fullIndexName,
);
}
}