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 types from '../utils/types';
import * as identifiers from '../utils/identifiers';
import type { Metadata, Meta } from '../metadata'; import type { Metadata, Meta } from '../metadata';
import type { Column, Schema, Table } from './types'; import type { Column, Schema, Table } from './types';
import type { Attribute } 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 createColumn = (name: string, attribute: Attribute): Column => {
const { type, args = [], ...opts } = getColumnType(attribute); const { type, args = [], ...opts } = getColumnType(attribute);
@ -35,8 +50,11 @@ const createTable = (meta: Meta): Table => {
if ('morphColumn' in attribute && attribute.morphColumn && attribute.owner) { if ('morphColumn' in attribute && attribute.morphColumn && attribute.owner) {
const { idColumn, typeColumn } = attribute.morphColumn; const { idColumn, typeColumn } = attribute.morphColumn;
const idColumnName = identifiers.getName(idColumn.name);
const typeColumnName = identifiers.getName(typeColumn.name);
table.columns.push( table.columns.push(
createColumn(idColumn.name, { createColumn(idColumnName, {
type: 'integer', type: 'integer',
column: { column: {
unsigned: true, 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 ( } else if (
'joinColumn' in attribute && 'joinColumn' in attribute &&
attribute.joinColumn && 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 // NOTE: we could pass uniquness for oneToOne to avoid creating more than one to one
const { const {
name: columnName, name: columnNameFull,
referencedColumn, referencedColumn,
referencedTable, referencedTable,
columnType = 'integer', columnType = 'integer',
} = attribute.joinColumn; } = attribute.joinColumn;
const columnName = identifiers.getName(columnNameFull);
const column = createColumn(columnName, { const column = createColumn(columnName, {
// TODO: find the column type automatically, or allow passing all the column params // TODO: find the column type automatically, or allow passing all the column params
type: columnType, type: columnType,
@ -70,9 +90,10 @@ const createTable = (meta: Meta): Table => {
table.columns.push(column); table.columns.push(column);
const fkName = identifiers.getFkIndexName([table.name, columnName]);
table.foreignKeys.push({ table.foreignKeys.push({
name: `${table.name}_${columnName}_fk`, name: fkName,
columns: [columnName], columns: [column.name],
referencedTable, referencedTable,
referencedColumns: [referencedColumn], referencedColumns: [referencedColumn],
// NOTE: could allow configuration // NOTE: could allow configuration
@ -80,26 +101,28 @@ const createTable = (meta: Meta): Table => {
}); });
table.indexes.push({ table.indexes.push({
name: `${table.name}_${columnName}_fk`, name: fkName,
columns: [columnName], columns: [column.name],
}); });
} }
} else if (types.isScalarAttribute(attribute)) { } 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) { if (column.unique) {
table.indexes.push({ table.indexes.push({
type: 'unique', type: 'unique',
name: `${table.name}_${column.name}_unique`, name: identifiers.getUniqueIndexName([table.name, column.name]),
columns: [column.name], columns: [columnName],
}); });
} }
if (column.primary) { if (column.primary) {
table.indexes.push({ table.indexes.push({
type: 'primary', type: 'primary',
name: `${table.name}_${column.name}_primary`, name: identifiers.getPrimaryIndexName([table.name, column.name]),
columns: [column.name], columns: [columnName],
}); });
} }

View File

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