chore(database): refactor database schema to use naming functions

This commit is contained in:
Ben Irvin 2024-02-26 15:35:47 +01:00 committed by GitHub
parent 3cea9ffa5c
commit 6111d69ad8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 13 deletions

View File

@ -1,9 +1,24 @@
import * as types from '../utils/types';
import * as identifiers from '../utils/identifiers';
import type { Metadata, Meta } from '../metadata';
import type { Column, Schema, Table } from './types';
import type { Attribute } from '../types';
/**
* TODO: This needs to be refactored to support incoming names such as
* (column, table, index) that are of the form string | NameToken[] so
* that pieces can be passed through and shortened here.
*
* Currently, we are potentially shortening twice, although in reality
* that won't happen since the shortened attribute column names will
* fit here because they are already shortened to the max identifier
* length
*
* That is the reason we use getName() here and not getColumnName();
* we just want the exact shortened name for the value without doing
* any other potential manipulation to it
* */
const createColumn = (name: string, attribute: Attribute): Column => {
const { type, args = [], ...opts } = getColumnType(attribute);
@ -35,8 +50,11 @@ const createTable = (meta: Meta): Table => {
if ('morphColumn' in attribute && attribute.morphColumn && attribute.owner) {
const { idColumn, typeColumn } = attribute.morphColumn;
const idColumnName = identifiers.getName(idColumn.name);
const typeColumnName = identifiers.getName(typeColumn.name);
table.columns.push(
createColumn(idColumn.name, {
createColumn(idColumnName, {
type: 'integer',
column: {
unsigned: true,
@ -44,7 +62,7 @@ const createTable = (meta: Meta): Table => {
})
);
table.columns.push(createColumn(typeColumn.name, { type: 'string' }));
table.columns.push(createColumn(typeColumnName, { type: 'string' }));
} else if (
'joinColumn' in attribute &&
attribute.joinColumn &&
@ -54,12 +72,14 @@ const createTable = (meta: Meta): Table => {
// NOTE: we could pass uniquness for oneToOne to avoid creating more than one to one
const {
name: columnName,
name: columnNameFull,
referencedColumn,
referencedTable,
columnType = 'integer',
} = attribute.joinColumn;
const columnName = identifiers.getName(columnNameFull);
const column = createColumn(columnName, {
// TODO: find the column type automatically, or allow passing all the column params
type: columnType,
@ -70,9 +90,10 @@ const createTable = (meta: Meta): Table => {
table.columns.push(column);
const fkName = identifiers.getFkIndexName([table.name, columnName]);
table.foreignKeys.push({
name: `${table.name}_${columnName}_fk`,
columns: [columnName],
name: fkName,
columns: [column.name],
referencedTable,
referencedColumns: [referencedColumn],
// NOTE: could allow configuration
@ -80,26 +101,28 @@ const createTable = (meta: Meta): Table => {
});
table.indexes.push({
name: `${table.name}_${columnName}_fk`,
columns: [columnName],
name: fkName,
columns: [column.name],
});
}
} else if (types.isScalarAttribute(attribute)) {
const column = createColumn(attribute.columnName || key, attribute);
const columnName = identifiers.getName(attribute.columnName || key);
const column = createColumn(columnName, attribute);
if (column.unique) {
table.indexes.push({
type: 'unique',
name: `${table.name}_${column.name}_unique`,
columns: [column.name],
name: identifiers.getUniqueIndexName([table.name, column.name]),
columns: [columnName],
});
}
if (column.primary) {
table.indexes.push({
type: 'primary',
name: `${table.name}_${column.name}_primary`,
columns: [column.name],
name: identifiers.getPrimaryIndexName([table.name, column.name]),
columns: [columnName],
});
}

View File

@ -162,3 +162,7 @@ export const getOrderInverseFkIndexName = (names: NameInput) => {
export const getUniqueIndexName = (names: NameInput) => {
return getName(names, { suffix: 'unique' });
};
export const getPrimaryIndexName = (names: NameInput) => {
return getName(names, { suffix: 'primary' });
};