mirror of
https://github.com/Shadowfita/docmost.git
synced 2025-11-12 15:52:32 +10:00
implement custom typeorm naming strategy
This commit is contained in:
61
apps/server/src/database/naming-strategy.ts
Normal file
61
apps/server/src/database/naming-strategy.ts
Normal file
@ -0,0 +1,61 @@
|
||||
import { DefaultNamingStrategy, Table } from 'typeorm';
|
||||
|
||||
export class NamingStrategy extends DefaultNamingStrategy {
|
||||
primaryKeyName(tableOrName: Table | string, columnNames: string[]): string {
|
||||
const tableName = this.normalizeTableName(tableOrName);
|
||||
return `pk_${tableName}_${columnNames.join('_')}`;
|
||||
}
|
||||
|
||||
indexName(
|
||||
tableOrName: Table | string,
|
||||
columnNames: string[],
|
||||
where?: string,
|
||||
): string {
|
||||
const tableName = this.normalizeTableName(tableOrName);
|
||||
|
||||
let name = `${tableName}_${columnNames.join('_')}`;
|
||||
if (where) name += '_' + where;
|
||||
|
||||
return `idx_${name}`;
|
||||
}
|
||||
|
||||
uniqueConstraintName(
|
||||
tableOrName: Table | string,
|
||||
columnNames: string[],
|
||||
): string {
|
||||
const tableName = this.normalizeTableName(tableOrName);
|
||||
|
||||
return `uq_${tableName}_${columnNames.join('_')}`;
|
||||
}
|
||||
|
||||
foreignKeyName(
|
||||
tableOrName: Table | string,
|
||||
columnNames: string[],
|
||||
_referencedTablePath?: string,
|
||||
_referencedColumnNames?: string[],
|
||||
): string {
|
||||
const tableName = this.normalizeTableName(tableOrName);
|
||||
const targetTable = this.normalizeTableName(_referencedTablePath);
|
||||
|
||||
const name = `${tableName}_${targetTable}_${columnNames.join('_')}`;
|
||||
return `fk_${name}`;
|
||||
}
|
||||
|
||||
relationConstraintName(
|
||||
tableOrName: Table | string,
|
||||
columnNames: string[],
|
||||
where?: string,
|
||||
): string {
|
||||
const tableName = this.normalizeTableName(tableOrName);
|
||||
|
||||
let name = `${tableName}_${columnNames.join('_')}`;
|
||||
if (where) name += '_' + where;
|
||||
|
||||
return `rel_${name}`;
|
||||
}
|
||||
|
||||
normalizeTableName(tableOrName: Table | string): string {
|
||||
const tableName = this.getTableName(tableOrName);
|
||||
return tableName.replace('.', '_');
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
import { DataSource } from 'typeorm';
|
||||
import * as dotenv from 'dotenv';
|
||||
import { NamingStrategy } from './naming-strategy';
|
||||
dotenv.config();
|
||||
export const AppDataSource: DataSource = new DataSource({
|
||||
type: 'postgres',
|
||||
@ -10,5 +11,6 @@ export const AppDataSource: DataSource = new DataSource({
|
||||
migrations: ['src/**/migrations/*.{ts,js}'],
|
||||
subscribers: [],
|
||||
synchronize: false,
|
||||
//namingStrategy: new NamingStrategy(),
|
||||
logging: process.env.NODE_ENV === 'development',
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user