feat: add foreign key indexes (#17672)

This commit is contained in:
Jason Rasmussen
2025-04-17 14:41:06 -04:00
committed by GitHub
parent 81ed54aa61
commit e275f2d8b3
49 changed files with 382 additions and 285 deletions

View File

@@ -1,16 +0,0 @@
import { register } from 'src/sql-tools/from-code/register';
import { asOptions } from 'src/sql-tools/helpers';
export type ColumnIndexOptions = {
name?: string;
unique?: boolean;
expression?: string;
using?: string;
with?: string;
where?: string;
synchronize?: boolean;
};
export const ColumnIndex = (options: string | ColumnIndexOptions = {}): PropertyDecorator => {
return (object: object, propertyName: string | symbol) =>
void register({ type: 'columnIndex', item: { object, propertyName, options: asOptions(options) } });
};

View File

@@ -15,13 +15,15 @@ export type ColumnBaseOptions = {
synchronize?: boolean;
storage?: ColumnStorage;
identity?: boolean;
index?: boolean;
indexName?: string;
unique?: boolean;
uniqueConstraintName?: string;
};
export type ColumnOptions = ColumnBaseOptions & {
enum?: DatabaseEnum;
array?: boolean;
unique?: boolean;
uniqueConstraintName?: string;
};
export const Column = (options: string | ColumnOptions = {}): PropertyDecorator => {

View File

@@ -7,8 +7,6 @@ export type ForeignKeyColumnOptions = ColumnBaseOptions & {
onUpdate?: Action;
onDelete?: Action;
constraintName?: string;
unique?: boolean;
uniqueConstraintName?: string;
};
export const ForeignKeyColumn = (target: () => object, options: ForeignKeyColumnOptions): PropertyDecorator => {

View File

@@ -1,8 +1,13 @@
import { ColumnIndexOptions } from 'src/sql-tools/from-code/decorators/column-index.decorator';
import { register } from 'src/sql-tools/from-code/register';
import { asOptions } from 'src/sql-tools/helpers';
export type IndexOptions = ColumnIndexOptions & {
export type IndexOptions = {
name?: string;
unique?: boolean;
expression?: string;
using?: string;
with?: string;
where?: string;
columns?: string[];
synchronize?: boolean;
};

View File

@@ -1,6 +1,5 @@
import 'reflect-metadata';
import { processCheckConstraints } from 'src/sql-tools/from-code/processors/check-constraint.processor';
import { processColumnIndexes } from 'src/sql-tools/from-code/processors/column-index.processor';
import { processColumns } from 'src/sql-tools/from-code/processors/column.processor';
import { processConfigurationParameters } from 'src/sql-tools/from-code/processors/configuration-parameter.processor';
import { processDatabases } from 'src/sql-tools/from-code/processors/database.processor';
@@ -36,14 +35,21 @@ const processors: Processor[] = [
processUniqueConstraints,
processCheckConstraints,
processPrimaryKeyConstraints,
processIndexes,
processColumnIndexes,
processForeignKeyConstraints,
processIndexes,
processTriggers,
];
export const schemaFromCode = () => {
export type SchemaFromCodeOptions = {
/** automatically create indexes on foreign key columns */
createForeignKeyIndexes?: boolean;
};
export const schemaFromCode = (options: SchemaFromCodeOptions = {}) => {
if (!initialized) {
const globalOptions = {
createForeignKeyIndexes: options.createForeignKeyIndexes ?? true,
};
const builder: SchemaBuilder = {
name: 'postgres',
schemaName: 'public',
@@ -58,7 +64,7 @@ export const schemaFromCode = () => {
const items = getRegisteredItems();
for (const processor of processors) {
processor(builder, items);
processor(builder, items, globalOptions);
}
schema = { ...builder, tables: builder.tables.map(({ metadata: _, ...table }) => table) };

View File

@@ -1,6 +1,6 @@
import { onMissingTable, resolveTable } from 'src/sql-tools/from-code/processors/table.processor';
import { Processor } from 'src/sql-tools/from-code/processors/type';
import { asCheckConstraintName } from 'src/sql-tools/helpers';
import { asKey } from 'src/sql-tools/helpers';
import { DatabaseConstraintType } from 'src/sql-tools/types';
export const processCheckConstraints: Processor = (builder, items) => {
@@ -24,3 +24,5 @@ export const processCheckConstraints: Processor = (builder, items) => {
});
}
};
const asCheckConstraintName = (table: string, expression: string) => asKey('CHK_', table, [expression]);

View File

@@ -1,32 +0,0 @@
import { onMissingColumn, resolveColumn } from 'src/sql-tools/from-code/processors/column.processor';
import { onMissingTable } from 'src/sql-tools/from-code/processors/table.processor';
import { Processor } from 'src/sql-tools/from-code/processors/type';
import { asIndexName } from 'src/sql-tools/helpers';
export const processColumnIndexes: Processor = (builder, items) => {
for (const {
item: { object, propertyName, options },
} of items.filter((item) => item.type === 'columnIndex')) {
const { table, column } = resolveColumn(builder, object, propertyName);
if (!table) {
onMissingTable(builder, '@ColumnIndex', object);
continue;
}
if (!column) {
onMissingColumn(builder, `@ColumnIndex`, object, propertyName);
continue;
}
table.indexes.push({
name: options.name || asIndexName(table.name, [column.name], options.where),
tableName: table.name,
unique: options.unique ?? false,
expression: options.expression,
using: options.using,
where: options.where,
columnNames: [column.name],
synchronize: options.synchronize ?? true,
});
}
};

View File

@@ -1,8 +1,8 @@
import { ColumnOptions } from 'src/sql-tools/from-code/decorators/column.decorator';
import { onMissingTable, resolveTable } from 'src/sql-tools/from-code/processors/table.processor';
import { Processor, SchemaBuilder } from 'src/sql-tools/from-code/processors/type';
import { asMetadataKey, asUniqueConstraintName, fromColumnValue } from 'src/sql-tools/helpers';
import { DatabaseColumn, DatabaseConstraintType } from 'src/sql-tools/types';
import { asMetadataKey, fromColumnValue } from 'src/sql-tools/helpers';
import { DatabaseColumn } from 'src/sql-tools/types';
export const processColumns: Processor = (builder, items) => {
for (const {
@@ -54,16 +54,6 @@ export const processColumns: Processor = (builder, items) => {
writeMetadata(object, propertyName, { name: column.name, options });
table.columns.push(column);
if (type === 'column' && !options.primary && options.unique) {
table.constraints.push({
type: DatabaseConstraintType.UNIQUE,
name: options.uniqueConstraintName || asUniqueConstraintName(table.name, [column.name]),
tableName: table.name,
columnNames: [column.name],
synchronize: options.synchronize ?? true,
});
}
}
};

View File

@@ -1,7 +1,7 @@
import { onMissingColumn, resolveColumn } from 'src/sql-tools/from-code/processors/column.processor';
import { onMissingTable, resolveTable } from 'src/sql-tools/from-code/processors/table.processor';
import { Processor } from 'src/sql-tools/from-code/processors/type';
import { asForeignKeyConstraintName, asRelationKeyConstraintName } from 'src/sql-tools/helpers';
import { asKey } from 'src/sql-tools/helpers';
import { DatabaseActionType, DatabaseConstraintType } from 'src/sql-tools/types';
export const processForeignKeyConstraints: Processor = (builder, items) => {
@@ -46,7 +46,7 @@ export const processForeignKeyConstraints: Processor = (builder, items) => {
synchronize: options.synchronize ?? true,
});
if (options.unique) {
if (options.unique || options.uniqueConstraintName) {
table.constraints.push({
name: options.uniqueConstraintName || asRelationKeyConstraintName(table.name, columnNames),
tableName: table.name,
@@ -57,3 +57,6 @@ export const processForeignKeyConstraints: Processor = (builder, items) => {
}
}
};
const asForeignKeyConstraintName = (table: string, columns: string[]) => asKey('FK_', table, columns);
const asRelationKeyConstraintName = (table: string, columns: string[]) => asKey('REL_', table, columns);

View File

@@ -1,8 +1,9 @@
import { onMissingColumn, resolveColumn } from 'src/sql-tools/from-code/processors/column.processor';
import { onMissingTable, resolveTable } from 'src/sql-tools/from-code/processors/table.processor';
import { Processor } from 'src/sql-tools/from-code/processors/type';
import { asIndexName } from 'src/sql-tools/helpers';
import { asKey } from 'src/sql-tools/helpers';
export const processIndexes: Processor = (builder, items) => {
export const processIndexes: Processor = (builder, items, config) => {
for (const {
item: { object, options },
} of items.filter((item) => item.type === 'index')) {
@@ -24,4 +25,66 @@ export const processIndexes: Processor = (builder, items) => {
synchronize: options.synchronize ?? true,
});
}
// column indexes
for (const {
type,
item: { object, propertyName, options },
} of items.filter((item) => item.type === 'column' || item.type === 'foreignKeyColumn')) {
const { table, column } = resolveColumn(builder, object, propertyName);
if (!table) {
onMissingTable(builder, '@Column', object);
continue;
}
if (!column) {
// should be impossible since they are created in `column.processor.ts`
onMissingColumn(builder, '@Column', object, propertyName);
continue;
}
if (options.index === false) {
continue;
}
const isIndexRequested =
options.indexName || options.index || (type === 'foreignKeyColumn' && config.createForeignKeyIndexes);
if (!isIndexRequested) {
continue;
}
const indexName = options.indexName || asIndexName(table.name, [column.name]);
const isIndexPresent = table.indexes.some((index) => index.name === indexName);
if (isIndexPresent) {
continue;
}
const isOnlyPrimaryColumn = options.primary && table.columns.filter(({ primary }) => primary === true).length === 1;
if (isOnlyPrimaryColumn) {
// will have an index created by the primary key constraint
continue;
}
table.indexes.push({
name: indexName,
tableName: table.name,
unique: false,
columnNames: [column.name],
synchronize: options.synchronize ?? true,
});
}
};
const asIndexName = (table: string, columns?: string[], where?: string) => {
const items: string[] = [];
for (const columnName of columns ?? []) {
items.push(columnName);
}
if (where) {
items.push(where);
}
return asKey('IDX_', table, items);
};

View File

@@ -1,5 +1,5 @@
import { Processor } from 'src/sql-tools/from-code/processors/type';
import { asPrimaryKeyConstraintName } from 'src/sql-tools/helpers';
import { asKey } from 'src/sql-tools/helpers';
import { DatabaseConstraintType } from 'src/sql-tools/types';
export const processPrimaryKeyConstraints: Processor = (builder) => {
@@ -22,3 +22,5 @@ export const processPrimaryKeyConstraints: Processor = (builder) => {
}
}
};
const asPrimaryKeyConstraintName = (table: string, columns: string[]) => asKey('PK_', table, columns);

View File

@@ -1,6 +1,7 @@
import { TriggerOptions } from 'src/sql-tools/from-code/decorators/trigger.decorator';
import { onMissingTable, resolveTable } from 'src/sql-tools/from-code/processors/table.processor';
import { Processor } from 'src/sql-tools/from-code/processors/type';
import { asTriggerName } from 'src/sql-tools/helpers';
import { asKey } from 'src/sql-tools/helpers';
export const processTriggers: Processor = (builder, items) => {
for (const {
@@ -26,3 +27,6 @@ export const processTriggers: Processor = (builder, items) => {
});
}
};
const asTriggerName = (table: string, trigger: TriggerOptions) =>
asKey('TR_', table, [...trigger.actions, trigger.scope, trigger.timing, trigger.functionName]);

View File

@@ -1,3 +1,4 @@
import { SchemaFromCodeOptions } from 'src/sql-tools/from-code';
import { TableOptions } from 'src/sql-tools/from-code/decorators/table.decorator';
import { RegisterItem } from 'src/sql-tools/from-code/register-item';
import { DatabaseSchema, DatabaseTable } from 'src/sql-tools/types';
@@ -6,4 +7,4 @@ import { DatabaseSchema, DatabaseTable } from 'src/sql-tools/types';
export type TableWithMetadata = DatabaseTable & { metadata: { options: TableOptions; object: Function } };
export type SchemaBuilder = Omit<DatabaseSchema, 'tables'> & { tables: TableWithMetadata[] };
export type Processor = (builder: SchemaBuilder, items: RegisterItem[]) => void;
export type Processor = (builder: SchemaBuilder, items: RegisterItem[], options: SchemaFromCodeOptions) => void;

View File

@@ -1,6 +1,7 @@
import { onMissingColumn, resolveColumn } from 'src/sql-tools/from-code/processors/column.processor';
import { onMissingTable, resolveTable } from 'src/sql-tools/from-code/processors/table.processor';
import { Processor } from 'src/sql-tools/from-code/processors/type';
import { asUniqueConstraintName } from 'src/sql-tools/helpers';
import { asKey } from 'src/sql-tools/helpers';
import { DatabaseConstraintType } from 'src/sql-tools/types';
export const processUniqueConstraints: Processor = (builder, items) => {
@@ -24,4 +25,34 @@ export const processUniqueConstraints: Processor = (builder, items) => {
synchronize: options.synchronize ?? true,
});
}
// column level constraints
for (const {
type,
item: { object, propertyName, options },
} of items.filter((item) => item.type === 'column' || item.type === 'foreignKeyColumn')) {
const { table, column } = resolveColumn(builder, object, propertyName);
if (!table) {
onMissingTable(builder, '@Column', object);
continue;
}
if (!column) {
// should be impossible since they are created in `column.processor.ts`
onMissingColumn(builder, '@Column', object, propertyName);
continue;
}
if (type === 'column' && !options.primary && (options.unique || options.uniqueConstraintName)) {
table.constraints.push({
type: DatabaseConstraintType.UNIQUE,
name: options.uniqueConstraintName || asUniqueConstraintName(table.name, [column.name]),
tableName: table.name,
columnNames: [column.name],
synchronize: options.synchronize ?? true,
});
}
}
};
const asUniqueConstraintName = (table: string, columns: string[]) => asKey('UQ_', table, columns);

View File

@@ -1,5 +1,4 @@
import { register } from 'src/sql-tools/from-code/register';
import { asFunctionExpression } from 'src/sql-tools/helpers';
import { ColumnType, DatabaseFunction } from 'src/sql-tools/types';
export type FunctionOptions = {
@@ -27,3 +26,37 @@ export const registerFunction = (options: FunctionOptions) => {
return item;
};
const asFunctionExpression = (options: FunctionOptions) => {
const name = options.name;
const sql: string[] = [
`CREATE OR REPLACE FUNCTION ${name}(${(options.arguments || []).join(', ')})`,
`RETURNS ${options.returnType}`,
];
const flags = [
options.parallel ? `PARALLEL ${options.parallel.toUpperCase()}` : undefined,
options.strict ? 'STRICT' : undefined,
options.behavior ? options.behavior.toUpperCase() : undefined,
`LANGUAGE ${options.language ?? 'SQL'}`,
].filter((x) => x !== undefined);
if (flags.length > 0) {
sql.push(flags.join(' '));
}
if ('return' in options) {
sql.push(` RETURN ${options.return}`);
}
if ('body' in options) {
sql.push(
//
`AS $$`,
' ' + options.body.trim(),
`$$;`,
);
}
return sql.join('\n ').trim();
};

View File

@@ -1,5 +1,4 @@
import { CheckOptions } from 'src/sql-tools/from-code/decorators/check.decorator';
import { ColumnIndexOptions } from 'src/sql-tools/from-code/decorators/column-index.decorator';
import { ColumnOptions } from 'src/sql-tools/from-code/decorators/column.decorator';
import { ConfigurationParameterOptions } from 'src/sql-tools/from-code/decorators/configuration-parameter.decorator';
import { DatabaseOptions } from 'src/sql-tools/from-code/decorators/database.decorator';
@@ -21,7 +20,6 @@ export type RegisterItem =
| { type: 'uniqueConstraint'; item: ClassBased<{ options: UniqueOptions }> }
| { type: 'checkConstraint'; item: ClassBased<{ options: CheckOptions }> }
| { type: 'column'; item: PropertyBased<{ options: ColumnOptions }> }
| { type: 'columnIndex'; item: PropertyBased<{ options: ColumnIndexOptions }> }
| { type: 'function'; item: DatabaseFunction }
| { type: 'enum'; item: DatabaseEnum }
| { type: 'trigger'; item: ClassBased<{ options: TriggerOptions }> }

View File

@@ -1,7 +1,5 @@
import { createHash } from 'node:crypto';
import { ColumnValue } from 'src/sql-tools/from-code/decorators/column.decorator';
import { TriggerOptions } from 'src/sql-tools/from-code/decorators/trigger.decorator';
import { FunctionOptions } from 'src/sql-tools/from-code/register-function';
import {
Comparer,
DatabaseColumn,
@@ -18,25 +16,6 @@ export const asSnakeCase = (name: string): string => name.replaceAll(/([a-z])([A
// match TypeORM
export const asKey = (prefix: string, tableName: string, values: string[]) =>
(prefix + sha1(`${tableName}_${values.toSorted().join('_')}`)).slice(0, 30);
export const asPrimaryKeyConstraintName = (table: string, columns: string[]) => asKey('PK_', table, columns);
export const asForeignKeyConstraintName = (table: string, columns: string[]) => asKey('FK_', table, columns);
export const asTriggerName = (table: string, trigger: TriggerOptions) =>
asKey('TR_', table, [...trigger.actions, trigger.scope, trigger.timing, trigger.functionName]);
export const asRelationKeyConstraintName = (table: string, columns: string[]) => asKey('REL_', table, columns);
export const asUniqueConstraintName = (table: string, columns: string[]) => asKey('UQ_', table, columns);
export const asCheckConstraintName = (table: string, expression: string) => asKey('CHK_', table, [expression]);
export const asIndexName = (table: string, columns: string[] | undefined, where: string | undefined) => {
const items: string[] = [];
for (const columnName of columns ?? []) {
items.push(columnName);
}
if (where) {
items.push(where);
}
return asKey('IDX_', table, items);
};
export const asOptions = <T extends { name?: string }>(options: string | T): T => {
if (typeof options === 'string') {
@@ -46,40 +25,6 @@ export const asOptions = <T extends { name?: string }>(options: string | T): T =
return options;
};
export const asFunctionExpression = (options: FunctionOptions) => {
const name = options.name;
const sql: string[] = [
`CREATE OR REPLACE FUNCTION ${name}(${(options.arguments || []).join(', ')})`,
`RETURNS ${options.returnType}`,
];
const flags = [
options.parallel ? `PARALLEL ${options.parallel.toUpperCase()}` : undefined,
options.strict ? 'STRICT' : undefined,
options.behavior ? options.behavior.toUpperCase() : undefined,
`LANGUAGE ${options.language ?? 'SQL'}`,
].filter((x) => x !== undefined);
if (flags.length > 0) {
sql.push(flags.join(' '));
}
if ('return' in options) {
sql.push(` RETURN ${options.return}`);
}
if ('body' in options) {
sql.push(
//
`AS $$`,
' ' + options.body.trim(),
`$$;`,
);
}
return sql.join('\n ').trim();
};
export const sha1 = (value: string) => createHash('sha1').update(value).digest('hex');
export const hasMask = (input: number, mask: number) => (input & mask) === mask;

View File

@@ -3,7 +3,6 @@ export { schemaFromCode } from 'src/sql-tools/from-code';
export * from 'src/sql-tools/from-code/decorators/after-delete.decorator';
export * from 'src/sql-tools/from-code/decorators/before-update.decorator';
export * from 'src/sql-tools/from-code/decorators/check.decorator';
export * from 'src/sql-tools/from-code/decorators/column-index.decorator';
export * from 'src/sql-tools/from-code/decorators/column.decorator';
export * from 'src/sql-tools/from-code/decorators/configuration-parameter.decorator';
export * from 'src/sql-tools/from-code/decorators/create-date-column.decorator';