diff --git a/docs/custom-repository.md b/docs/custom-repository.md index c51332e6c5..96dcaee23b 100644 --- a/docs/custom-repository.md +++ b/docs/custom-repository.md @@ -34,7 +34,7 @@ In order to extend `UserRepository` functionality you can use `.extend` method o ```typescript // user.repository.ts export const UserRepository = dataSource.getRepository(User).extend({ - findByName(firstName: string, lastName: string) { + findOneByName(firstName: string, lastName: string) { return this.createQueryBuilder("user") .where("user.firstName = :firstName", { firstName }) .andWhere("user.lastName = :lastName", { lastName }) diff --git a/src/connection/options-reader/ConnectionOptionsEnvReader.ts b/src/connection/options-reader/ConnectionOptionsEnvReader.ts index 9ec94b693c..d724080134 100644 --- a/src/connection/options-reader/ConnectionOptionsEnvReader.ts +++ b/src/connection/options-reader/ConnectionOptionsEnvReader.ts @@ -77,17 +77,6 @@ export class ConnectionOptionsEnvReader { "TYPEORM_MAX_QUERY_EXECUTION_TIME", ), debug: PlatformTools.getEnvVariable("TYPEORM_DEBUG"), - cli: { - entitiesDir: PlatformTools.getEnvVariable( - "TYPEORM_ENTITIES_DIR", - ), - migrationsDir: PlatformTools.getEnvVariable( - "TYPEORM_MIGRATIONS_DIR", - ), - subscribersDir: PlatformTools.getEnvVariable( - "TYPEORM_SUBSCRIBERS_DIR", - ), - }, cache: this.transformCaching(), uuidExtension: PlatformTools.getEnvVariable( "TYPEORM_UUID_EXTENSION", diff --git a/src/data-source/BaseDataSourceOptions.ts b/src/data-source/BaseDataSourceOptions.ts index fbd860ea76..4685f700fa 100644 --- a/src/data-source/BaseDataSourceOptions.ts +++ b/src/data-source/BaseDataSourceOptions.ts @@ -204,24 +204,4 @@ export interface BaseDataSourceOptions { */ readonly ignoreErrors?: boolean } - - /** - * CLI settings. - */ - readonly cli?: { - /** - * Directory where entities should be created by default. - */ - readonly entitiesDir?: string - - /** - * Directory where migrations should be created by default. - */ - readonly migrationsDir?: string - - /** - * Directory where subscribers should be created by default. - */ - readonly subscribersDir?: string - } } diff --git a/src/data-source/DataSource.ts b/src/data-source/DataSource.ts index b9271c7aed..ed8b2d9162 100644 --- a/src/data-source/DataSource.ts +++ b/src/data-source/DataSource.ts @@ -196,20 +196,6 @@ export class DataSource { */ setOptions(options: Partial): this { Object.assign(this.options, options) - - this.logger = new LoggerFactory().create( - this.options.logger, - this.options.logging, - ) - this.driver = new DriverFactory().create(this) - this.namingStrategy = - options.namingStrategy || new DefaultNamingStrategy() - this.queryResultCache = options.cache - ? new QueryResultCacheFactory(this).create() - : undefined - - // build all metadatas to make sure options are valid - // await this.buildMetadatas(); return this } @@ -681,5 +667,13 @@ export class DataSource { ), this.driver, ) + + // set current data source to the entities + for (let entityKey in flattenedEntities) { + const entity = flattenedEntities[entityKey] + if (InstanceChecker.isBaseEntityConstructor(entity)) { + entity.useDataSource(this) + } + } } } diff --git a/src/entity-manager/EntityManager.ts b/src/entity-manager/EntityManager.ts index a4719d8fbe..cd97824e0a 100644 --- a/src/entity-manager/EntityManager.ts +++ b/src/entity-manager/EntityManager.ts @@ -1347,7 +1347,14 @@ export class EntityManager { withRepository>(repository: R): R { const repositoryConstructor = repository.constructor as typeof Repository - return new repositoryConstructor(repository.target, this) as R + const { target, manager, queryRunner, ...otherRepositoryProperties } = + repository + return Object.assign( + new repositoryConstructor(repository.target, this) as R, + { + ...otherRepositoryProperties, + }, + ) } /** diff --git a/src/repository/BaseEntity.ts b/src/repository/BaseEntity.ts index e4d4eb5f2f..fa810b3cf9 100644 --- a/src/repository/BaseEntity.ts +++ b/src/repository/BaseEntity.ts @@ -1,13 +1,11 @@ import { Repository } from "./Repository" -import { getConnection } from "../globals" import { FindOptionsWhere } from "../find-options/FindOptionsWhere" import { DeepPartial } from "../common/DeepPartial" import { SaveOptions } from "./SaveOptions" import { FindOneOptions } from "../find-options/FindOneOptions" import { RemoveOptions } from "./RemoveOptions" import { FindManyOptions } from "../find-options/FindManyOptions" -import { DataSource } from "../data-source/DataSource" -import { ObjectType } from "../common/ObjectType" +import { DataSource } from "../data-source" import { SelectQueryBuilder } from "../query-builder/SelectQueryBuilder" import { InsertResult } from "../query-builder/result/InsertResult" import { UpdateResult } from "../query-builder/result/UpdateResult" @@ -27,10 +25,10 @@ export class BaseEntity { // ------------------------------------------------------------------------- /** - * Connection used in all static methods of the BaseEntity. + * DataSource used in all static methods of the BaseEntity. */ // @ts-ignore: Unused variable which is actually used - private static usedConnection?: DataSource + private static dataSource?: DataSource // ------------------------------------------------------------------------- // Public Methods @@ -41,7 +39,8 @@ export class BaseEntity { * If entity composite compose ids, it will check them all. */ hasId(): boolean { - return (this.constructor as any).getRepository().hasId(this) + const baseEntity = this.constructor as typeof BaseEntity + return baseEntity.getRepository().hasId(this) } /** @@ -49,40 +48,42 @@ export class BaseEntity { * If entity does not exist in the database then inserts, otherwise updates. */ save(options?: SaveOptions): Promise { - return (this.constructor as any).getRepository().save(this, options) + const baseEntity = this.constructor as typeof BaseEntity + return baseEntity.getRepository().save(this, options) } /** * Removes current entity from the database. */ remove(options?: RemoveOptions): Promise { - return (this.constructor as any).getRepository().remove(this, options) + const baseEntity = this.constructor as typeof BaseEntity + return baseEntity.getRepository().remove(this, options) as Promise } /** * Records the delete date of current entity. */ softRemove(options?: SaveOptions): Promise { - return (this.constructor as any) - .getRepository() - .softRemove(this, options) + const baseEntity = this.constructor as typeof BaseEntity + return baseEntity.getRepository().softRemove(this, options) } /** * Recovers a given entity in the database. */ recover(options?: SaveOptions): Promise { - return (this.constructor as any).getRepository().recover(this, options) + const baseEntity = this.constructor as typeof BaseEntity + return baseEntity.getRepository().recover(this, options) } /** * Reloads entity data from the database. */ async reload(): Promise { - const base: any = this.constructor - const newestEntity: BaseEntity = await base + const baseEntity = this.constructor as typeof BaseEntity + const newestEntity: BaseEntity = await baseEntity .getRepository() - .findOneOrFail(base.getId(this)) + .findOneOrFail(baseEntity.getId(this)) ObjectUtils.assign(this, newestEntity) } @@ -92,21 +93,22 @@ export class BaseEntity { // ------------------------------------------------------------------------- /** - * Sets connection to be used by entity. + * Sets DataSource to be used by entity. */ - static useConnection(connection: DataSource) { - this.usedConnection = connection + static useDataSource(dataSource: DataSource) { + this.dataSource = dataSource } /** * Gets current entity's Repository. */ static getRepository( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, ): Repository { - const connection: DataSource = - (this as any).usedConnection || getConnection() - return connection.getRepository(this) + const dataSource = (this as typeof BaseEntity).dataSource + if (!dataSource) + throw new Error(`DataSource is not set for this entity.`) + return dataSource.getRepository(this) } /** @@ -129,25 +131,28 @@ export class BaseEntity { /** * Gets entity mixed id. */ - static getId(this: ObjectType, entity: T): any { - return (this as any).getRepository().getId(entity) + static getId( + this: { new (): T } & typeof BaseEntity, + entity: T, + ): any { + return this.getRepository().getId(entity) } /** * Creates a new query builder that can be used to build a SQL query. */ static createQueryBuilder( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, alias?: string, ): SelectQueryBuilder { - return (this as any).getRepository().createQueryBuilder(alias) + return this.getRepository().createQueryBuilder(alias) } /** * Creates a new entity instance. */ static create( - this: ObjectType & typeof BaseEntity, + this: { new (): T } & typeof BaseEntity, ): T /** @@ -155,7 +160,7 @@ export class BaseEntity { * Note that it copies only properties that present in entity schema. */ static create( - this: ObjectType & typeof BaseEntity, + this: { new (): T } & typeof BaseEntity, entityLikeArray: DeepPartial[], ): T[] @@ -164,31 +169,33 @@ export class BaseEntity { * Note that it copies only properties that present in entity schema. */ static create( - this: ObjectType & typeof BaseEntity, + this: { new (): T } & typeof BaseEntity, entityLike: DeepPartial, ): T + /** * Creates a new entity instance and copies all entity properties from this object into a new entity. * Note that it copies only properties that present in entity schema. */ static create( - this: ObjectType & typeof BaseEntity, + this: { new (): T } & typeof BaseEntity, entityOrEntities?: any, - ): T { - return (this as any).getRepository().create(entityOrEntities) + ) { + return this.getRepository().create(entityOrEntities) } /** * Merges multiple entities (or entity-like objects) into a given entity. */ static merge( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, mergeIntoEntity: T, ...entityLikes: DeepPartial[] ): T { - return (this as any) - .getRepository() - .merge(mergeIntoEntity, ...entityLikes) + return this.getRepository().merge( + mergeIntoEntity, + ...entityLikes, + ) as T } /** @@ -201,10 +208,10 @@ export class BaseEntity { * Returns undefined if entity with given id was not found. */ static preload( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, entityLike: DeepPartial, ): Promise { - const thisRepository = (this as any).getRepository() as Repository + const thisRepository = this.getRepository() as Repository return thisRepository.preload(entityLike) } @@ -213,8 +220,8 @@ export class BaseEntity { * If entities do not exist in the database then inserts, otherwise updates. */ static save( - this: ObjectType, - entities: T[], + this: { new (): T } & typeof BaseEntity, + entities: DeepPartial[], options?: SaveOptions, ): Promise @@ -223,8 +230,8 @@ export class BaseEntity { * If entity does not exist in the database then inserts, otherwise updates. */ static save( - this: ObjectType, - entity: T, + this: { new (): T } & typeof BaseEntity, + entity: DeepPartial, options?: SaveOptions, ): Promise @@ -232,20 +239,18 @@ export class BaseEntity { * Saves one or many given entities. */ static save( - this: ObjectType, - entityOrEntities: T | T[], + this: { new (): T } & typeof BaseEntity, + entityOrEntities: DeepPartial | DeepPartial[], options?: SaveOptions, - ): Promise { - return (this as any) - .getRepository() - .save(entityOrEntities as any, options) + ) { + return this.getRepository().save(entityOrEntities as any, options) } /** * Removes a given entities from the database. */ static remove( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, entities: T[], options?: RemoveOptions, ): Promise @@ -254,7 +259,7 @@ export class BaseEntity { * Removes a given entity from the database. */ static remove( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, entity: T, options?: RemoveOptions, ): Promise @@ -263,20 +268,18 @@ export class BaseEntity { * Removes one or many given entities. */ static remove( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, entityOrEntities: T | T[], options?: RemoveOptions, - ): Promise { - return (this as any) - .getRepository() - .remove(entityOrEntities as any, options) + ) { + return this.getRepository().remove(entityOrEntities as any, options) } /** * Records the delete date of all given entities. */ static softRemove( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, entities: T[], options?: SaveOptions, ): Promise @@ -285,7 +288,7 @@ export class BaseEntity { * Records the delete date of a given entity. */ static softRemove( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, entity: T, options?: SaveOptions, ): Promise @@ -294,13 +297,14 @@ export class BaseEntity { * Records the delete date of one or many given entities. */ static softRemove( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, entityOrEntities: T | T[], options?: SaveOptions, - ): Promise { - return (this as any) - .getRepository() - .softRemove(entityOrEntities as any, options) + ) { + return this.getRepository().softRemove( + entityOrEntities as any, + options, + ) } /** @@ -310,11 +314,10 @@ export class BaseEntity { * Does not check if entity exist in the database, so query will fail if duplicate entity is being inserted. */ static insert( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, entity: QueryDeepPartialEntity | QueryDeepPartialEntity[], - options?: SaveOptions, ): Promise { - return (this as any).getRepository().insert(entity, options) + return this.getRepository().insert(entity) } /** @@ -324,7 +327,7 @@ export class BaseEntity { * Does not check if entity exist in the database. */ static update( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, criteria: | string | string[] @@ -336,11 +339,8 @@ export class BaseEntity { | ObjectID[] | FindOptionsWhere, partialEntity: QueryDeepPartialEntity, - options?: SaveOptions, ): Promise { - return (this as any) - .getRepository() - .update(criteria, partialEntity, options) + return this.getRepository().update(criteria, partialEntity) } /** @@ -349,7 +349,7 @@ export class BaseEntity { * Executes fast and efficient INSERT ... ON CONFLICT DO UPDATE/ON DUPLICATE KEY UPDATE query. */ static upsert( - this: ObjectType & typeof BaseEntity, + this: { new (): T } & typeof BaseEntity, entityOrEntities: | QueryDeepPartialEntity | QueryDeepPartialEntity[], @@ -368,7 +368,7 @@ export class BaseEntity { * Does not check if entity exist in the database. */ static delete( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, criteria: | string | string[] @@ -379,49 +379,48 @@ export class BaseEntity { | ObjectID | ObjectID[] | FindOptionsWhere, - options?: RemoveOptions, ): Promise { - return (this as any).getRepository().delete(criteria, options) + return this.getRepository().delete(criteria) } /** * Counts entities that match given options. */ static count( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, options?: FindManyOptions, ): Promise { - return (this as any).getRepository().count(options) + return this.getRepository().count(options) } /** * Counts entities that match given WHERE conditions. */ static countBy( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, where: FindOptionsWhere, ): Promise { - return (this as any).getRepository().count(where) + return this.getRepository().countBy(where) } /** * Finds entities that match given options. */ static find( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, options?: FindManyOptions, ): Promise { - return (this as any).getRepository().find(options) + return this.getRepository().find(options) } /** * Finds entities that match given WHERE conditions. */ static findBy( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, where: FindOptionsWhere, ): Promise { - return (this as any).getRepository().find(where) + return this.getRepository().findBy(where) } /** @@ -430,10 +429,10 @@ export class BaseEntity { * but ignores pagination settings (from and take options). */ static findAndCount( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, options?: FindManyOptions, ): Promise<[T[], number]> { - return (this as any).getRepository().findAndCount(options) + return this.getRepository().findAndCount(options) } /** @@ -442,10 +441,10 @@ export class BaseEntity { * but ignores pagination settings (from and take options). */ static findAndCountBy( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, where: FindOptionsWhere, ): Promise<[T[], number]> { - return (this as any).getRepository().findAndCount(where) + return this.getRepository().findAndCountBy(where) } /** @@ -459,30 +458,30 @@ export class BaseEntity { * }) */ static findByIds( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, ids: any[], ): Promise { - return (this as any).getRepository().findByIdsBy(ids) + return this.getRepository().findByIds(ids) } /** * Finds first entity that matches given conditions. */ static findOne( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, options: FindOneOptions, ): Promise { - return (this as any).getRepository().findOne(options) + return this.getRepository().findOne(options) } /** * Finds first entity that matches given conditions. */ static findOneBy( - this: ObjectType, - where?: FindOptionsWhere, + this: { new (): T } & typeof BaseEntity, + where: FindOptionsWhere, ): Promise { - return (this as any).getRepository().findOneBy(where) + return this.getRepository().findOneBy(where) } /** @@ -495,30 +494,30 @@ export class BaseEntity { * }) */ static findOneById( - this: ObjectType, - id?: string | number | Date | ObjectID, + this: { new (): T } & typeof BaseEntity, + id: string | number | Date | ObjectID, ): Promise { - return (this as any).getRepository().findOneById(id) + return this.getRepository().findOneById(id) } /** * Finds first entity that matches given conditions. */ static findOneOrFail( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, options: FindOneOptions, ): Promise { - return (this as any).getRepository().findOneOrFail(options) + return this.getRepository().findOneOrFail(options) } /** * Finds first entity that matches given conditions. */ static findOneByOrFail( - this: ObjectType, - where?: FindOptionsWhere, + this: { new (): T } & typeof BaseEntity, + where: FindOptionsWhere, ): Promise { - return (this as any).getRepository().findOneByOrFail(where) + return this.getRepository().findOneByOrFail(where) } /** @@ -526,17 +525,19 @@ export class BaseEntity { * Raw query execution is supported only by relational databases (MongoDB is not supported). */ static query( - this: ObjectType, + this: { new (): T } & typeof BaseEntity, query: string, parameters?: any[], ): Promise { - return (this as any).getRepository().query(query, parameters) + return this.getRepository().query(query, parameters) } /** * Clears all the data from the given table/collection (truncates/drops it). */ - static clear(this: ObjectType): Promise { - return (this as any).getRepository().clear() + static clear( + this: { new (): T } & typeof BaseEntity, + ): Promise { + return this.getRepository().clear() } } diff --git a/src/util/InstanceChecker.ts b/src/util/InstanceChecker.ts index 889975d515..8a25f3c155 100644 --- a/src/util/InstanceChecker.ts +++ b/src/util/InstanceChecker.ts @@ -27,6 +27,7 @@ import type { EntityMetadata } from "../metadata/EntityMetadata" import type { ColumnMetadata } from "../metadata/ColumnMetadata" import type { MssqlParameter } from "../driver/sqlserver/MssqlParameter" import { DataSource } from "../data-source" +import { BaseEntity } from "../repository/BaseEntity" export class InstanceChecker { static isMssqlParameter(obj: unknown): obj is MssqlParameter { @@ -92,6 +93,14 @@ export class InstanceChecker { static isEntitySchema(obj: unknown): obj is EntitySchema { return this.check(obj, "EntitySchema") } + static isBaseEntityConstructor(obj: unknown): obj is typeof BaseEntity { + return ( + typeof obj === "function" && + typeof (obj as typeof BaseEntity).hasId === "function" && + typeof (obj as typeof BaseEntity).save === "function" && + typeof (obj as typeof BaseEntity).useDataSource === "function" + ) + } static isFindOperator(obj: unknown): obj is FindOperator { return ( this.check(obj, "FindOperator") || this.check(obj, "EqualOperator") diff --git a/test/functional/base-entity/base-entity.test.ts b/test/functional/base-entity/base-entity.test.ts new file mode 100644 index 0000000000..896bec4e18 --- /dev/null +++ b/test/functional/base-entity/base-entity.test.ts @@ -0,0 +1,26 @@ +import "../../utils/test-setup" +import { setupTestingConnections } from "../../utils/test-utils" +import { User } from "./entity/User" +import { expect } from "chai" +import { DataSource } from "../../../src" + +describe("base entity", () => { + it("test if DataSource calls `useDataSource` of the provided entities", async () => { + const dataSourceOptions = setupTestingConnections({ + entities: [User], + enabledDrivers: ["sqlite"], + }) + if (!dataSourceOptions.length) return + + const dataSource = new DataSource(dataSourceOptions[0]) + await dataSource.initialize() + await dataSource.synchronize(true) + + await User.save({ name: "Timber Saw" }) + const timber = await User.findOneByOrFail({ name: "Timber Saw" }) + expect(timber).to.be.eql({ + id: 1, + name: "Timber Saw", + }) + }) +}) diff --git a/test/functional/base-entity/entity/User.ts b/test/functional/base-entity/entity/User.ts new file mode 100644 index 0000000000..cc1ad10b8b --- /dev/null +++ b/test/functional/base-entity/entity/User.ts @@ -0,0 +1,13 @@ +import { Entity } from "../../../../src/decorator/entity/Entity" +import { PrimaryGeneratedColumn } from "../../../../src/decorator/columns/PrimaryGeneratedColumn" +import { Column } from "../../../../src/decorator/columns/Column" +import { BaseEntity } from "../../../../src" + +@Entity() +export class User extends BaseEntity { + @PrimaryGeneratedColumn() + id: number + + @Column() + name: string +} diff --git a/test/functional/custom-repository/custom-repository.test.ts b/test/functional/custom-repository/custom-repository.test.ts new file mode 100644 index 0000000000..45fc212078 --- /dev/null +++ b/test/functional/custom-repository/custom-repository.test.ts @@ -0,0 +1,59 @@ +import "../../utils/test-setup" +import { + closeTestingConnections, + createTestingConnections, + reloadTestingDatabases, +} from "../../utils/test-utils" +import { User } from "./entity/User" +import { DataSource } from "../../../src" +import { expect } from "chai" + +describe("custom repository", () => { + let dataSources: DataSource[] + before( + async () => + (dataSources = await createTestingConnections({ + entities: [User], + })), + ) + beforeEach(() => reloadTestingDatabases(dataSources)) + after(() => closeTestingConnections(dataSources)) + + it("withRepository must work properly in transactions", () => + Promise.all( + dataSources.map(async (dataSource) => { + const CustomRepository = dataSource.getRepository(User).extend({ + findOneByName(name: string) { + return this.findOneBy({ name }) + }, + }) + + // check if custom repository function works + await CustomRepository.save({ name: "Timber Saw" }) + const user = await CustomRepository.findOneByName("Timber Saw") + expect(user).to.be.eql({ + id: 1, + name: "Timber Saw", + }) + + // now check it in the transaction + await dataSource.manager.transaction( + async (transactionalManager) => { + const transactionalCustomRepository = + await transactionalManager.withRepository( + CustomRepository, + ) + await CustomRepository.save({ name: "Natures Prophet" }) + const user = + await transactionalCustomRepository.findOneByName( + "Natures Prophet", + ) + expect(user).to.be.eql({ + id: 2, + name: "Natures Prophet", + }) + }, + ) + }), + )) +}) diff --git a/test/functional/custom-repository/entity/User.ts b/test/functional/custom-repository/entity/User.ts new file mode 100644 index 0000000000..88d2d61cae --- /dev/null +++ b/test/functional/custom-repository/entity/User.ts @@ -0,0 +1,12 @@ +import { Entity } from "../../../../src/decorator/entity/Entity" +import { PrimaryGeneratedColumn } from "../../../../src/decorator/columns/PrimaryGeneratedColumn" +import { Column } from "../../../../src/decorator/columns/Column" + +@Entity() +export class User { + @PrimaryGeneratedColumn() + id: number + + @Column() + name: string +} diff --git a/test/functional/entity-model/entity-model.ts b/test/functional/entity-model/entity-model.ts index 33fe1a7ac6..4adb7e719b 100644 --- a/test/functional/entity-model/entity-model.ts +++ b/test/functional/entity-model/entity-model.ts @@ -22,7 +22,7 @@ describe("entity-model", () => { it("should save successfully and use static methods successfully", async () => { // These must run sequentially as we have the global context of the `Post` ActiveRecord class for (const connection of connections) { - Post.useConnection(connection) // change connection each time because of AR specifics + Post.useDataSource(connection) // change connection each time because of AR specifics const post = Post.create() post.title = "About ActiveRecord" @@ -48,7 +48,7 @@ describe("entity-model", () => { for (const connection of connections.filter( (c) => c.driver.supportedUpsertType != null, )) { - Post.useConnection(connection) // change connection each time because of AR specifics + Post.useDataSource(connection) // change connection each time because of AR specifics const externalId = "external-entity" @@ -80,8 +80,8 @@ describe("entity-model", () => { // These must run sequentially as we have the global context of the `Post` ActiveRecord class for (const connection of connections) { await connection.synchronize(true) - Post.useConnection(connection) - Category.useConnection(connection) + Post.useDataSource(connection) + Category.useDataSource(connection) const category = Category.create() category.id = 1 diff --git a/test/functional/relations/lazy-relations/lazy-relations-loading-via-base-entitiy-finders/index.ts b/test/functional/relations/lazy-relations/lazy-relations-loading-via-base-entitiy-finders/index.ts index 086eb1fd2f..a002c7d2b2 100644 --- a/test/functional/relations/lazy-relations/lazy-relations-loading-via-base-entitiy-finders/index.ts +++ b/test/functional/relations/lazy-relations/lazy-relations-loading-via-base-entitiy-finders/index.ts @@ -25,8 +25,8 @@ describe("lazy-relations-loading-via-base-entity-finders", () => { it("works", async () => { for (let connection of connections) { - Category.useConnection(connection) - Post.useConnection(connection) + Category.useDataSource(connection) + Post.useDataSource(connection) const category = new Category() category.name = "hello" await category.save() diff --git a/test/functional/repository/find-options-operators/repository-find-operators.ts b/test/functional/repository/find-options-operators/repository-find-operators.ts index fb870c3692..7a45152a61 100644 --- a/test/functional/repository/find-options-operators/repository-find-operators.ts +++ b/test/functional/repository/find-options-operators/repository-find-operators.ts @@ -837,7 +837,7 @@ describe("repository > find options > operators", () => { it("should work with ActiveRecord model", async () => { // These must run sequentially as we have the global context of the `PersonAR` ActiveRecord class for (const connection of connections) { - PersonAR.useConnection(connection) + PersonAR.useDataSource(connection) const person = new PersonAR() person.name = "Timber" diff --git a/test/github-issues/2201/issue-2201.ts b/test/github-issues/2201/issue-2201.ts index 629d6aeaf7..1e743c1265 100644 --- a/test/github-issues/2201/issue-2201.ts +++ b/test/github-issues/2201/issue-2201.ts @@ -39,9 +39,9 @@ describe("github issues > #2201 - Create a select query when using a (custom) ju }) if (!connections.length) return - User.useConnection(connections[0]) - Record.useConnection(connections[0]) - RecordContext.useConnection(connections[0]) + User.useDataSource(connections[0]) + Record.useDataSource(connections[0]) + RecordContext.useDataSource(connections[0]) const user = User.create({ id: "user1" }) await user.save() diff --git a/test/github-issues/2313/issue-2313.ts b/test/github-issues/2313/issue-2313.ts index fb2a193ad9..d95234353d 100644 --- a/test/github-issues/2313/issue-2313.ts +++ b/test/github-issues/2313/issue-2313.ts @@ -24,7 +24,7 @@ describe("github issues > #2313 - BaseEntity has no findOneOrFail() method", () it("should find the appropriate record when one exists", async () => { // These must run sequentially as we have the global context of the `Post` ActiveRecord class for (const connection of connections) { - Post.useConnection(connection) // change connection each time because of AR specifics + Post.useDataSource(connection) // change connection each time because of AR specifics const post1 = new Post() post1.data = 123 @@ -55,7 +55,7 @@ describe("github issues > #2313 - BaseEntity has no findOneOrFail() method", () it("should throw no matching record exists", async () => { // These must run sequentially as we have the global context of the `Post` ActiveRecord class for (const connection of connections) { - Post.useConnection(connection) // change connection each time because of AR specifics + Post.useDataSource(connection) // change connection each time because of AR specifics try { await Post.findOneByOrFail({ id: 100 })