mirror of
https://github.com/strapi/strapi.git
synced 2025-12-25 22:23:10 +00:00
chore(database): clean up db naming methods
This commit is contained in:
parent
c338fa844e
commit
390c3754d1
@ -17,6 +17,7 @@ export const COMPONENT_JOIN_TABLE_SUFFIX = 'components';
|
||||
export const DZ_JOIN_TABLE_SUFFIX = 'components';
|
||||
export const COMPONENT_INVERSE_COLUMN_NAME = 'component';
|
||||
export const COMPONENT_TYPE_COLUMN = 'component_type';
|
||||
export const ENTITY = 'entity';
|
||||
|
||||
export const getComponentJoinTableName = (collectionName: string) =>
|
||||
identifiers.getTableName(collectionName, { suffix: COMPONENT_JOIN_TABLE_SUFFIX });
|
||||
@ -24,7 +25,7 @@ export const getComponentJoinTableName = (collectionName: string) =>
|
||||
export const getDzJoinTableName = (collectionName: string) =>
|
||||
identifiers.getTableName(collectionName, { suffix: DZ_JOIN_TABLE_SUFFIX });
|
||||
|
||||
const { ID_COLUMN: id, FIELD_COLUMN: field, ORDER_COLUMN: order, ENTITY: entity } = identifiers;
|
||||
const { ID_COLUMN: id, FIELD_COLUMN: field, ORDER_COLUMN: order } = identifiers;
|
||||
|
||||
export type LoadedContentTypeModel = Schema.ContentType &
|
||||
Required<Pick<Schema.ContentType, 'collectionName' | 'uid' | 'modelName'>>;
|
||||
@ -46,7 +47,7 @@ export const transformAttribute = (
|
||||
}
|
||||
case 'component': {
|
||||
const joinTableName = getComponentJoinTableName(contentType.collectionName);
|
||||
const joinColumnEntityName = identifiers.getJoinColumnEntityIdName();
|
||||
const joinColumnEntityName = identifiers.getJoinColumnAttributeIdName(ENTITY);
|
||||
const joinColumnInverseName = identifiers.getJoinColumnAttributeIdName(
|
||||
COMPONENT_INVERSE_COLUMN_NAME
|
||||
);
|
||||
@ -81,7 +82,7 @@ export const transformAttribute = (
|
||||
}
|
||||
case 'dynamiczone': {
|
||||
const joinTableName = getDzJoinTableName(contentType.collectionName);
|
||||
const joinColumnEntityName = identifiers.getJoinColumnEntityIdName();
|
||||
const joinColumnEntityName = identifiers.getJoinColumnAttributeIdName(ENTITY);
|
||||
const joinColumnInverseName = identifiers.getJoinColumnAttributeIdName(
|
||||
COMPONENT_INVERSE_COLUMN_NAME
|
||||
);
|
||||
@ -146,9 +147,9 @@ export const createDocumentId = createId;
|
||||
const createCompoLinkModel = (contentType: LoadedContentTypeModel): Model => {
|
||||
const name = getComponentJoinTableName(contentType.collectionName);
|
||||
|
||||
const entityId = identifiers.getJoinColumnEntityIdName();
|
||||
const entityId = identifiers.getJoinColumnAttributeIdName(ENTITY);
|
||||
const componentId = identifiers.getJoinColumnAttributeIdName(COMPONENT_INVERSE_COLUMN_NAME);
|
||||
const fkIndex = identifiers.getFkIndexName([contentType.collectionName, entity]);
|
||||
const fkIndex = identifiers.getFkIndexName([contentType.collectionName, ENTITY]);
|
||||
|
||||
return {
|
||||
// TODO: make sure there can't be any conflicts with a prefix
|
||||
|
||||
@ -434,12 +434,14 @@ const createJoinTable = (
|
||||
|
||||
const joinTableName = identifiers.getJoinTableName(meta.tableName, attributeName);
|
||||
|
||||
const joinColumnName = identifiers.getJoinColumnIdName(meta.singularName);
|
||||
let inverseJoinColumnName = identifiers.getJoinColumnIdName(targetMeta.singularName);
|
||||
const joinColumnName = identifiers.getJoinColumnAttributeIdName(meta.singularName);
|
||||
let inverseJoinColumnName = identifiers.getJoinColumnAttributeIdName(targetMeta.singularName);
|
||||
|
||||
// if relation is self referencing
|
||||
if (joinColumnName === inverseJoinColumnName) {
|
||||
inverseJoinColumnName = identifiers.getInverseJoinColumnIdName(targetMeta.singularName);
|
||||
inverseJoinColumnName = identifiers.getInverseJoinColumnAttributeIdName(
|
||||
targetMeta.singularName
|
||||
);
|
||||
}
|
||||
|
||||
const orderColumnName = identifiers.getOrderColumnName(targetMeta.singularName);
|
||||
|
||||
@ -1,8 +1,7 @@
|
||||
import _ from 'lodash/fp';
|
||||
import { getNameFromTokens } from './shortener';
|
||||
import { MAX_DB_IDENTIFIER_LENGTH, getNameFromTokens } from './shortener';
|
||||
|
||||
// Constants for column names used in naming methods
|
||||
export const ENTITY = 'entity';
|
||||
export const ID_COLUMN = 'id';
|
||||
export const ORDER_COLUMN = 'order';
|
||||
export const FIELD_COLUMN = 'field';
|
||||
@ -22,7 +21,7 @@ type NameOptions = {
|
||||
* we get names 'myModel' and 'my_model' they would be converted to the same
|
||||
* final string my_model which generally works but is not entirely safe
|
||||
* */
|
||||
export const getName = (names: NameInput, options: NameOptions = {}) => {
|
||||
export const getName = (names: NameInput, options?: NameOptions) => {
|
||||
const tokens = _.castArray(names).map((name) => {
|
||||
return {
|
||||
name,
|
||||
@ -30,15 +29,17 @@ export const getName = (names: NameInput, options: NameOptions = {}) => {
|
||||
};
|
||||
});
|
||||
|
||||
if (options.suffix) {
|
||||
if (options?.suffix) {
|
||||
tokens.push({ name: options.suffix, compressible: false });
|
||||
}
|
||||
|
||||
if (options.prefix) {
|
||||
if (options?.prefix) {
|
||||
tokens.unshift({ name: options.prefix, compressible: false });
|
||||
}
|
||||
|
||||
return getNameFromTokens(tokens, options?.maxLength);
|
||||
const maxLength = options?.maxLength ?? MAX_DB_IDENTIFIER_LENGTH; // nullish coalesce because 0 is a valid maxLength
|
||||
|
||||
return getNameFromTokens(tokens, maxLength);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -46,105 +47,93 @@ export const getName = (names: NameInput, options: NameOptions = {}) => {
|
||||
*/
|
||||
|
||||
export const getTableName = (name: string, options?: NameOptions) => {
|
||||
const tokens = [
|
||||
{
|
||||
name,
|
||||
compressible: true,
|
||||
},
|
||||
];
|
||||
|
||||
if (options?.suffix) {
|
||||
tokens.push({ name: options.suffix, compressible: false });
|
||||
}
|
||||
|
||||
if (options?.prefix) {
|
||||
tokens.unshift({ name: options?.prefix, compressible: false });
|
||||
}
|
||||
|
||||
return getNameFromTokens(tokens);
|
||||
return getName(name, options);
|
||||
};
|
||||
|
||||
export const getJoinTableName = (collectionName: string, attributeName: string) => {
|
||||
return getName([collectionName, attributeName], { suffix: 'links' }); // _.snakeCase(`${tableName}_${attributeName}_links`);
|
||||
export const getJoinTableName = (
|
||||
collectionName: string,
|
||||
attributeName: string,
|
||||
options?: NameOptions
|
||||
) => {
|
||||
return getName([collectionName, attributeName], { suffix: 'links', ...options });
|
||||
};
|
||||
|
||||
export const getMorphTableName = (collectionName: string, attributeName: string) => {
|
||||
return getName([collectionName, attributeName], { suffix: 'morphs' }); // _.snakeCase(`${tableName}_${attributeName}_morphs`);
|
||||
export const getMorphTableName = (
|
||||
collectionName: string,
|
||||
attributeName: string,
|
||||
options?: NameOptions
|
||||
) => {
|
||||
return getName([collectionName, attributeName], { suffix: 'morphs', ...options });
|
||||
};
|
||||
|
||||
/**
|
||||
* COLUMNS
|
||||
*/
|
||||
|
||||
export const getColumnName = (attributeName: string) => {
|
||||
return getName(attributeName);
|
||||
export const getColumnName = (attributeName: string, options?: NameOptions) => {
|
||||
return getName(attributeName, options);
|
||||
};
|
||||
|
||||
export const getJoinColumnEntityIdName = () => {
|
||||
return getName(ENTITY, { suffix: 'id' });
|
||||
export const getJoinColumnAttributeIdName = (attributeName: string, options?: NameOptions) => {
|
||||
return getName(attributeName, { suffix: 'id', ...options });
|
||||
};
|
||||
|
||||
export const getJoinColumnAttributeIdName = (attributeName: string) => {
|
||||
return getName(attributeName, { suffix: 'id' });
|
||||
export const getInverseJoinColumnAttributeIdName = (
|
||||
attributeName: string,
|
||||
options?: NameOptions
|
||||
) => {
|
||||
return getName(attributeName, { suffix: 'id', prefix: 'inv', ...options });
|
||||
};
|
||||
|
||||
export const getJoinColumnIdName = (singularName: string) => {
|
||||
return getName(singularName, { suffix: 'id' });
|
||||
export const getOrderColumnName = (singularName: string, options?: NameOptions) => {
|
||||
return getName(singularName, { suffix: 'order', ...options });
|
||||
};
|
||||
|
||||
export const getInverseJoinColumnIdName = (singularName: string) => {
|
||||
return getName(singularName, { suffix: 'id', prefix: 'inv' });
|
||||
};
|
||||
|
||||
export const getOrderColumnName = (singularName: string) => {
|
||||
return getName(singularName, { suffix: 'order' });
|
||||
};
|
||||
|
||||
export const getInverseOrderColumnName = (singularName: string) => {
|
||||
return getName(singularName, { suffix: 'order', prefix: 'inv' });
|
||||
export const getInverseOrderColumnName = (singularName: string, options?: NameOptions) => {
|
||||
return getName(singularName, { suffix: 'order', prefix: 'inv', ...options });
|
||||
};
|
||||
|
||||
/**
|
||||
* Morph Join Tables
|
||||
*/
|
||||
export const getMorphColumnJoinTableIdName = (singularName: string) => {
|
||||
return getName(singularName, { suffix: 'id' });
|
||||
export const getMorphColumnJoinTableIdName = (singularName: string, options?: NameOptions) => {
|
||||
return getName(singularName, { suffix: 'id', ...options });
|
||||
};
|
||||
|
||||
export const getMorphColumnAttributeIdName = (attributeName: string) => {
|
||||
return getName(attributeName, { suffix: 'id' });
|
||||
export const getMorphColumnAttributeIdName = (attributeName: string, options?: NameOptions) => {
|
||||
return getName(attributeName, { suffix: 'id', ...options });
|
||||
};
|
||||
|
||||
export const getMorphColumnTypeName = (attributeName: string) => {
|
||||
return getName(attributeName, { suffix: 'type' });
|
||||
export const getMorphColumnTypeName = (attributeName: string, options?: NameOptions) => {
|
||||
return getName(attributeName, { suffix: 'type', ...options });
|
||||
};
|
||||
|
||||
/**
|
||||
* INDEXES
|
||||
*/
|
||||
|
||||
export const getIndexName = (names: NameInput) => {
|
||||
return getName(names, { suffix: 'index' });
|
||||
export const getIndexName = (names: NameInput, options?: NameOptions) => {
|
||||
return getName(names, { suffix: 'index', ...options });
|
||||
};
|
||||
|
||||
export const getFkIndexName = (names: NameInput) => {
|
||||
return getName(names, { suffix: 'fk' });
|
||||
export const getFkIndexName = (names: NameInput, options?: NameOptions) => {
|
||||
return getName(names, { suffix: 'fk', ...options });
|
||||
};
|
||||
|
||||
export const getInverseFkIndexName = (names: NameInput) => {
|
||||
return getName(names, { suffix: 'inv_fk' });
|
||||
export const getInverseFkIndexName = (names: NameInput, options?: NameOptions) => {
|
||||
return getName(names, { suffix: 'inv_fk', ...options });
|
||||
};
|
||||
|
||||
export const getOrderFkIndexName = (names: NameInput) => {
|
||||
return getName(names, { suffix: 'order_fk' });
|
||||
export const getOrderFkIndexName = (names: NameInput, options?: NameOptions) => {
|
||||
return getName(names, { suffix: 'order_fk', ...options });
|
||||
};
|
||||
|
||||
export const getOrderInverseFkIndexName = (names: NameInput) => {
|
||||
return getName(names, { suffix: 'order_inv_fk' });
|
||||
export const getOrderInverseFkIndexName = (names: NameInput, options?: NameOptions) => {
|
||||
return getName(names, { suffix: 'order_inv_fk', ...options });
|
||||
};
|
||||
|
||||
export const getUniqueIndexName = (names: NameInput) => {
|
||||
return getName(names, { suffix: 'unique' });
|
||||
export const getUniqueIndexName = (names: NameInput, options?: NameOptions) => {
|
||||
return getName(names, { suffix: 'unique', ...options });
|
||||
};
|
||||
|
||||
export const getPrimaryIndexName = (names: NameInput) => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user