diff --git a/packages/core/database/src/dialects/dialect.ts b/packages/core/database/src/dialects/dialect.ts index 342f1ccc4a..d2632ab2ba 100644 --- a/packages/core/database/src/dialects/dialect.ts +++ b/packages/core/database/src/dialects/dialect.ts @@ -45,7 +45,7 @@ export default class Dialect { return true; } - supportsOperator(): boolean { + supportsOperator(operator?: string): boolean { return true; } diff --git a/packages/core/database/src/metadata/index.ts b/packages/core/database/src/metadata/index.ts index 7197a747ef..f123ddf20d 100644 --- a/packages/core/database/src/metadata/index.ts +++ b/packages/core/database/src/metadata/index.ts @@ -30,7 +30,7 @@ export const createMetadata = (models: Model[] = []): Metadata => { }, ...model.attributes, }, - lifecycles: model.lifecycles || {}, + lifecycles: model.lifecycles ?? ({} as Meta['lifecycles']), indexes: model.indexes || [], }); } diff --git a/packages/core/database/src/metadata/types.ts b/packages/core/database/src/metadata/types.ts index 5cc237aa61..a601384038 100644 --- a/packages/core/database/src/metadata/types.ts +++ b/packages/core/database/src/metadata/types.ts @@ -107,15 +107,10 @@ export namespace Relation { export type OneToOne = BaseBidirectional & { relation: 'oneToOne'; useJoinTable?: boolean; - } & ( - | { - joinTable: JoinTable; - } - | { - joinColumn: JoinColumn; - owner?: boolean; - } - ); + joinTable?: JoinTable; + joinColumn?: JoinColumn; + owner?: boolean; + }; export type OneToMany = BaseBidirectional & { relation: 'oneToMany'; @@ -124,17 +119,11 @@ export namespace Relation { export type ManyToOne = BaseBidirectional & { relation: 'manyToOne'; - useJoinTable?: boolean; - } & ( - | { - joinTable: JoinTable; - } - | { - joinColumn: JoinColumn; - owner?: boolean; - } - ); + joinTable?: JoinTable; + joinColumn?: JoinColumn; + owner?: boolean; + }; export type ManyToMany = BaseBidirectional & { relation: 'manyToMany'; @@ -240,7 +229,7 @@ export interface Model { tableName: string; singularName: string; attributes: Record; - lifecycles: Record; + lifecycles?: Record; indexes: Index[]; componentLink?: Meta; columnToAttribute?: Record; diff --git a/packages/core/database/src/schema/schema.ts b/packages/core/database/src/schema/schema.ts index bd6030a1fc..59c0b26f19 100644 --- a/packages/core/database/src/schema/schema.ts +++ b/packages/core/database/src/schema/schema.ts @@ -2,7 +2,6 @@ import * as types from '../types'; import type { Metadata, Meta, Attribute } from '../metadata/types'; import type { Column, Schema, Table } from './types'; -import { isRelationAttribute } from '../metadata/relations'; const createColumn = (name: string, attribute: Attribute): Column => { const { type, args = [], ...opts } = getColumnType(attribute); @@ -15,7 +14,7 @@ const createColumn = (name: string, attribute: Attribute): Column => { notNullable: false, unsigned: false, ...opts, - ...(attribute.column || {}), + ...('column' in attribute ? attribute.column ?? {} : {}), }; }; @@ -31,8 +30,8 @@ const createTable = (meta: Meta): Table => { const attribute = meta.attributes[key]; // if (types.isRelation(attribute.type)) { - if (isRelationAttribute(attribute)) { - if (attribute.morphColumn && attribute.owner) { + if (attribute.type === 'relation') { + if ('morphColumn' in attribute && attribute.morphColumn && attribute.owner) { const { idColumn, typeColumn } = attribute.morphColumn; table.columns.push( @@ -45,7 +44,12 @@ const createTable = (meta: Meta): Table => { ); table.columns.push(createColumn(typeColumn.name, { type: 'string' })); - } else if (attribute.joinColumn && attribute.owner && attribute.joinColumn.referencedTable) { + } else if ( + 'joinColumn' in attribute && + attribute.joinColumn && + attribute.owner && + attribute.joinColumn.referencedTable + ) { // NOTE: we could pass uniquness for oneToOne to avoid creating more than one to one const { name: columnName, referencedColumn, referencedTable } = attribute.joinColumn; @@ -73,7 +77,7 @@ const createTable = (meta: Meta): Table => { columns: [columnName], }); } - } else if (types.isScalar(attribute.type)) { + } else if (types.isScalarAttribute(attribute)) { const column = createColumn(attribute.columnName || key, attribute); if (column.unique) { @@ -100,7 +104,7 @@ const createTable = (meta: Meta): Table => { }; const getColumnType = (attribute: Attribute) => { - if (attribute.columnType) { + if ('columnType' in attribute && attribute.columnType) { return attribute.columnType; } diff --git a/packages/core/database/src/validations/relations/bidirectional.ts b/packages/core/database/src/validations/relations/bidirectional.ts index d9fe956090..9b37fd6bb8 100644 --- a/packages/core/database/src/validations/relations/bidirectional.ts +++ b/packages/core/database/src/validations/relations/bidirectional.ts @@ -1,32 +1,33 @@ -import { Attribute } from '@strapi/typings'; -import * as types from '../../types'; import { getJoinTableName } from '../../metadata/relations'; import type { Database } from '../..'; +import type { Relation } from '../../metadata/types'; type Link = { - relation: Attribute.Relation; - invRelation: Attribute.Relation; + relation: Relation.Bidirectional & { inversedBy: string }; + invRelation: Relation.Bidirectional & { inversedBy: string }; }; const getLinksWithoutMappedBy = (db: Database): Array => { - const relationsToUpdate = {}; + const relationsToUpdate: Record = {}; db.metadata.forEach((contentType) => { const attributes = contentType.attributes; // For each relation attribute, add the joinTable name to tablesToUpdate Object.values(attributes).forEach((attribute) => { - if (!types.isRelation(attribute.type)) return; + if (attribute.type !== 'relation') { + return; + } - if (attribute.inversedBy) { + if ('inversedBy' in attribute && attribute.inversedBy) { const invRelation = db.metadata.get(attribute.target).attributes[attribute.inversedBy]; // Both relations use inversedBy. - if (invRelation.inversedBy) { + if ('inversedBy' in invRelation && invRelation.inversedBy) { relationsToUpdate[attribute.joinTable.name] = { - relation: attribute, - invRelation, + relation: attribute as Relation.Bidirectional & { inversedBy: string }, + invRelation: invRelation as Relation.Bidirectional & { inversedBy: string }, }; } } @@ -36,12 +37,12 @@ const getLinksWithoutMappedBy = (db: Database): Array => { return Object.values(relationsToUpdate); }; -const isLinkTableEmpty = async (db, linkTableName) => { +const isLinkTableEmpty = async (db: Database, linkTableName: string) => { // If the table doesn't exist, it's empty const exists = await db.getSchemaConnection().hasTable(linkTableName); if (!exists) return true; - const result = await db.getConnection().count('* as count').from(linkTableName); + const result = await db.getConnection().from(linkTableName).count('* as count'); return Number(result[0].count) === 0; };