diff --git a/packages/core/database/src/entity-manager/entity-repository.ts b/packages/core/database/src/entity-manager/entity-repository.ts index 885841f150..2f1d130c51 100644 --- a/packages/core/database/src/entity-manager/entity-repository.ts +++ b/packages/core/database/src/entity-manager/entity-repository.ts @@ -2,6 +2,8 @@ import { isString } from 'lodash/fp'; import type { Database } from '..'; import type { ID } from '../types'; +export type Repository = ReturnType; + type Params = Record; type Data = Record; type Entity = { diff --git a/packages/core/database/src/entity-manager/index.ts b/packages/core/database/src/entity-manager/index.ts index 439444ea6f..d91f0e2511 100644 --- a/packages/core/database/src/entity-manager/index.ts +++ b/packages/core/database/src/entity-manager/index.ts @@ -29,7 +29,7 @@ import { mapAsync } from '@strapi/utils'; import * as types from '../utils/types'; import { createField } from '../fields'; import { createQueryBuilder } from '../query'; -import { createRepository } from './entity-repository'; +import { createRepository, Repository } from './entity-repository'; import { deleteRelatedMorphOneRelationsAfterMorphToManyUpdate } from './morph-relations'; import { isPolymorphic, @@ -55,6 +55,8 @@ import type { Database } from '..'; import type { Meta } from '../metadata'; import type { ID } from '../types'; +export type EntityManager = ReturnType; + type Params = { where?: any; filters?: any; @@ -257,7 +259,7 @@ const processData = ( }; export const createEntityManager = (db: Database) => { - const repoMap: Record = {}; + const repoMap: Record = {}; return { async findOne(uid: string, params: Params) { @@ -1502,9 +1504,5 @@ export const createEntityManager = (db: Database) => { return repoMap[uid]; }, - - clearRepositories() { - repoMap.clear(); - }, }; }; diff --git a/packages/core/database/src/index.ts b/packages/core/database/src/index.ts index cfb91c1b73..d7964aca75 100644 --- a/packages/core/database/src/index.ts +++ b/packages/core/database/src/index.ts @@ -1,11 +1,11 @@ import type { Knex } from 'knex'; import { Dialect, getDialect } from './dialects'; -import { createSchemaProvider } from './schema'; +import { createSchemaProvider, SchemaProvider } from './schema'; import { createMetadata, Metadata } from './metadata'; -import { createEntityManager } from './entity-manager'; -import { createMigrationsProvider } from './migrations'; -import { createLifecyclesProvider } from './lifecycles'; +import { createEntityManager, EntityManager } from './entity-manager'; +import { createMigrationsProvider, MigrationProvider } from './migrations'; +import { createLifecyclesProvider, LifecycleProvider } from './lifecycles'; import { createConnection } from './connection'; import * as errors from './errors'; import { Callback, transactionCtx } from './transaction-context'; @@ -36,17 +36,21 @@ class Database { metadata: Metadata; - schema: ReturnType; + schema: SchemaProvider; - migrations: ReturnType; + migrations: MigrationProvider; - lifecycles: ReturnType; + lifecycles: LifecycleProvider; - entityManager: ReturnType; + entityManager: EntityManager; - static transformContentTypes: typeof transformContentTypes; + static transformContentTypes = transformContentTypes; - static init: (config: DatabaseConfig) => Promise; + static async init(config: DatabaseConfig) { + const db = new Database(config); + await validateDatabase(db); + return db; + } constructor(config: DatabaseConfig) { this.metadata = createMetadata(config.models); @@ -160,12 +164,4 @@ class Database { } } -// TODO: move into strapi -Database.transformContentTypes = transformContentTypes; -Database.init = async (config: DatabaseConfig) => { - const db = new Database(config); - await validateDatabase(db); - return db; -}; - export { Database, errors }; diff --git a/packages/core/database/src/utils/content-types.ts b/packages/core/database/src/utils/content-types.ts index b1b398dfb9..80b1cebcb8 100644 --- a/packages/core/database/src/utils/content-types.ts +++ b/packages/core/database/src/utils/content-types.ts @@ -1,9 +1,12 @@ -interface Attribute { +import type { Model } from '../types'; + +export interface Attribute { type: string; multiple?: boolean; } -interface ContentType { +export interface ContentType { + uid: string; modelName: string; collectionName: string; attributes: Record; @@ -26,7 +29,7 @@ const transformAttribute = (attribute: Attribute) => { }; // TODO: model logic outside DB -export const transformContentTypes = (contentTypes: ContentType[]) => { +export const transformContentTypes = (contentTypes: ContentType[]): Model[] => { return contentTypes.map((contentType) => { const model = { ...contentType,