refactor: removing globals usages

This commit is contained in:
Umed Khudoiberdiev 2022-04-02 22:04:20 +05:00
parent 2205a1a6a5
commit ec27803f28
15 changed files with 178 additions and 184 deletions

View File

@ -16,18 +16,16 @@ import {
import { DataSource } from "../../../src/data-source/DataSource"
import { Repository } from "../../../src/repository/Repository"
import { TreeRepository } from "../../../src/repository/TreeRepository"
import { getConnectionManager } from "../../../src/index"
import { NoConnectionForRepositoryError } from "../../../src/error/NoConnectionForRepositoryError"
import { EntityManager } from "../../../src/entity-manager/EntityManager"
import { CannotGetEntityManagerNotConnectedError } from "../../../src/error/CannotGetEntityManagerNotConnectedError"
import { DataSourceOptions } from "../../../src/data-source/DataSourceOptions"
import { PostgresConnectionOptions } from "../../../src/driver/postgres/PostgresConnectionOptions"
describe("Connection", () => {
// const resourceDir = __dirname + "/../../../../../test/functional/connection/";
describe("before connection is established", function () {
let connection: DataSource
let dataSource: DataSource
before(async () => {
const options = setupSingleTestingConnection("mysql", {
name: "default",
@ -35,23 +33,23 @@ describe("Connection", () => {
})
if (!options) return
connection = getConnectionManager().create(options)
dataSource = new DataSource(options)
})
after(() => {
if (connection && connection.isInitialized)
return connection.close()
if (dataSource && dataSource.isInitialized)
return dataSource.destroy()
return Promise.resolve()
})
it("connection.isConnected should be false", () => {
if (!connection) return
if (!dataSource) return
connection.isInitialized.should.be.false
dataSource.isInitialized.should.be.false
})
it.skip("entity manager and reactive entity manager should not be accessible", () => {
expect(() => connection.manager).to.throw(
expect(() => dataSource.manager).to.throw(
CannotGetEntityManagerNotConnectedError,
)
// expect(() => connection.reactiveEntityManager).to.throw(CannotGetEntityManagerNotConnectedError);
@ -72,22 +70,22 @@ describe("Connection", () => {
});*/
it("should not be able to close", () => {
if (!connection) return
return connection.close().should.be.rejected // CannotCloseNotConnectedError
if (!dataSource) return
return dataSource.close().should.be.rejected // CannotCloseNotConnectedError
})
it("should not be able to sync a schema", () => {
if (!connection) return
return connection.synchronize().should.be.rejected // CannotCloseNotConnectedError
if (!dataSource) return
return dataSource.synchronize().should.be.rejected // CannotCloseNotConnectedError
})
it.skip("should not be able to use repositories", () => {
if (!connection) return
if (!dataSource) return
expect(() => connection.getRepository(Post)).to.throw(
expect(() => dataSource.getRepository(Post)).to.throw(
NoConnectionForRepositoryError,
)
expect(() => connection.getTreeRepository(Category)).to.throw(
expect(() => dataSource.getTreeRepository(Category)).to.throw(
NoConnectionForRepositoryError,
)
// expect(() => connection.getReactiveRepository(Post)).to.throw(NoConnectionForRepositoryError);
@ -95,23 +93,21 @@ describe("Connection", () => {
})
it("should be able to connect", () => {
if (!connection) return
return connection.connect().should.be.fulfilled
if (!dataSource) return
return dataSource.connect().should.be.fulfilled
})
})
describe.skip("establishing connection", function () {
it("should throw DriverOptionNotSetError when extra.socketPath and host is missing", function () {
expect(() => {
getConnectionManager().create(<DataSourceOptions>{
new DataSource({
type: "mysql",
username: "test",
password: "test",
database: "test",
entities: [],
dropSchema: false,
schemaCreate: false,
enabledDrivers: ["mysql"],
})
}).to.throw(Error)
})

View File

@ -1,6 +1,5 @@
import { expect } from "chai"
import "reflect-metadata"
import { getConnectionManager } from "../../../../src"
import { DataSource } from "../../../../src/data-source/DataSource"
import { Repository } from "../../../../src/repository/Repository"
import { setupSingleTestingConnection } from "../../../utils/test-utils"
@ -14,21 +13,21 @@ describe("persistence > custom-column-names", function () {
// -------------------------------------------------------------------------
// connect to db
let connection: DataSource
let dataSource: DataSource
before(async () => {
const options = setupSingleTestingConnection("mysql", {
entities: [Post, Category, CategoryMetadata],
})
if (!options) return
connection = getConnectionManager().create(options)
dataSource = new DataSource(options)
})
after(() => connection.close())
after(() => dataSource.close())
// clean up database before each test
function reloadDatabase() {
if (!connection) return
return connection.synchronize(true).catch((e) => {
if (!dataSource) return
return dataSource.synchronize(true).catch((e) => {
throw e
})
}
@ -37,10 +36,10 @@ describe("persistence > custom-column-names", function () {
let categoryRepository: Repository<Category>
let metadataRepository: Repository<CategoryMetadata>
before(function () {
if (!connection) return
postRepository = connection.getRepository(Post)
categoryRepository = connection.getRepository(Category)
metadataRepository = connection.getRepository(CategoryMetadata)
if (!dataSource) return
postRepository = dataSource.getRepository(Post)
categoryRepository = dataSource.getRepository(Category)
metadataRepository = dataSource.getRepository(CategoryMetadata)
})
// -------------------------------------------------------------------------
@ -48,7 +47,7 @@ describe("persistence > custom-column-names", function () {
// -------------------------------------------------------------------------
describe("attach exist entity to exist entity with many-to-one relation", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, newCategory: Category, loadedPost: Post
before(reloadDatabase)
@ -96,7 +95,7 @@ describe("persistence > custom-column-names", function () {
})
describe("attach new entity to exist entity with many-to-one relation", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, newCategory: Category, loadedPost: Post
before(reloadDatabase)
@ -139,7 +138,7 @@ describe("persistence > custom-column-names", function () {
})
describe("attach new entity to new entity with many-to-one relation", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, newCategory: Category, loadedPost: Post
before(reloadDatabase)
@ -177,7 +176,7 @@ describe("persistence > custom-column-names", function () {
})
describe("attach exist entity to exist entity with one-to-one relation", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post,
newCategory: Category,
newMetadata: CategoryMetadata,
@ -241,7 +240,7 @@ describe("persistence > custom-column-names", function () {
})
describe("attach new entity to exist entity with one-to-one relation", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post,
newCategory: Category,
newMetadata: CategoryMetadata,

View File

@ -1,6 +1,6 @@
import "reflect-metadata"
import { expect } from "chai"
import { createConnection } from "../../../src"
import { DataSource } from "../../../src"
import { getTypeOrmConfig } from "../../utils/test-utils"
import { MysqlConnectionOptions } from "../../../src/driver/mysql/MysqlConnectionOptions"
@ -19,15 +19,16 @@ describe("github issues > #2096 [mysql] Database name isn't read from url", () =
const url = `mysql://${username}:${password}@${host}:${port}/${database}`
const connection = await createConnection({
const dataSource = new DataSource({
name: "#2096",
url,
entities: [__dirname + "/entity/*{.js,.ts}"],
synchronize: true,
type: "mysql",
})
expect(connection.isInitialized).to.eq(true)
await connection.close()
await dataSource.initialize()
expect(dataSource.isInitialized).to.eq(true)
await dataSource.destroy()
}
})
})

View File

@ -1,19 +1,16 @@
import "reflect-metadata"
import { expect } from "chai"
import {
closeTestingConnections,
reloadTestingDatabases,
setupSingleTestingConnection,
} from "../../utils/test-utils"
import { DataSource } from "../../../src/data-source/DataSource"
import { createConnection, Repository } from "../../../src"
import { DataSource, Repository } from "../../../src"
import { Bar } from "./entity/Bar"
import { DocumentEnum } from "./documentEnum"
describe("github issues > #2871 Empty enum array is returned as array with single empty string", () => {
let connection: DataSource
let dataSource: DataSource
let repository: Repository<Bar>
before(async () => {
@ -25,17 +22,18 @@ describe("github issues > #2871 Empty enum array is returned as array with singl
if (!options) return
connection = await createConnection(options)
dataSource = new DataSource(options)
await dataSource.initialize()
})
beforeEach(async () => {
if (!connection) return
await reloadTestingDatabases([connection])
repository = connection.getRepository(Bar)
if (!dataSource) return
await reloadTestingDatabases([dataSource])
repository = dataSource.getRepository(Bar)
})
after(() => closeTestingConnections([connection]))
after(() => closeTestingConnections([dataSource]))
it("should extract array with values from enum array values from 'postgres'", async () => {
if (!connection) return
if (!dataSource) return
const documents: DocumentEnum[] = [
DocumentEnum.DOCUMENT_A,
DocumentEnum.DOCUMENT_B,
@ -51,7 +49,7 @@ describe("github issues > #2871 Empty enum array is returned as array with singl
})
it("should extract array with one value from enum array with one value from 'postgres'", async () => {
if (!connection) return
if (!dataSource) return
const documents: DocumentEnum[] = [DocumentEnum.DOCUMENT_D]
const barSaved = await repository.save({ documents })
@ -64,7 +62,7 @@ describe("github issues > #2871 Empty enum array is returned as array with singl
// This `it` test that issue #2871 is fixed
it("should extract empty array from empty enum array from 'postgres'", async () => {
if (!connection) return
if (!dataSource) return
const documents: DocumentEnum[] = []
const barSaved = await repository.save({ documents })

View File

@ -1,10 +1,10 @@
import "reflect-metadata"
import {
createTestingConnections,
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { DataSource } from "../../../src/data-source/index"
import { DataSource } from "../../../src"
import { expect } from "chai"
import { Content } from "./entity/Content"
import { Photo } from "./entity/Photo"
@ -19,6 +19,7 @@ describe("github issues > #2927 When using base class' custom repository, the di
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchema: true,
enabledDrivers: ["postgres"],
})),
)
beforeEach(() => reloadTestingDatabases(dataSources))

View File

@ -1,4 +1,3 @@
import { getConnectionManager } from "../../../src"
import { DataSource } from "../../../src/data-source/DataSource"
import { MysqlConnectionOptions } from "../../../src/driver/mysql/MysqlConnectionOptions"
import {
@ -13,8 +12,8 @@ function isMySql(v: TestingConnectionOptions): v is MysqlConnectionOptions {
}
describe("github issues > #4753 MySQL Replication Config broken", () => {
let connections: DataSource[] = []
after(() => closeTestingConnections(connections))
let dataSources: DataSource[] = []
after(() => closeTestingConnections(dataSources))
it("should connect without error when using replication", async () => {
const connectionOptions: MysqlConnectionOptions | undefined =
@ -26,8 +25,7 @@ describe("github issues > #4753 MySQL Replication Config broken", () => {
// Skip if MySQL tests aren't enabled at all
return
}
const connectionManager = getConnectionManager()
const connection = connectionManager.create({
const dataSource = new DataSource({
type: "mysql",
replication: {
master: {
@ -48,8 +46,8 @@ describe("github issues > #4753 MySQL Replication Config broken", () => {
entities: [User],
})
connections.push(connection)
await connection.connect()
connection.isInitialized.should.be.true
dataSources.push(dataSource)
await dataSource.connect()
dataSource.isInitialized.should.be.true
})
})

View File

@ -5,7 +5,7 @@ import {
reloadTestingDatabases,
setupSingleTestingConnection,
} from "../../utils/test-utils"
import { DataSource, createConnection } from "../../../src"
import { DataSource } from "../../../src"
import { fail } from "assert"
describe("github issues > #5119 migration with foreign key that changes target", () => {
@ -36,9 +36,10 @@ describe("github issues > #5119 migration with foreign key that changes target",
fail()
return
}
const connection = await createConnection(options)
const dataSource = new DataSource(options)
await dataSource.initialize()
try {
const sqlInMemory = await connection.driver
const sqlInMemory = await dataSource.driver
.createSchemaBuilder()
.log()
@ -61,7 +62,7 @@ describe("github issues > #5119 migration with foreign key that changes target",
`ALTER TABLE "post" DROP CONSTRAINT "FK_4490d00e1925ca046a1f52ddf04"`,
])
} finally {
connection.close()
dataSource.close()
}
}),
)

View File

@ -1,5 +1,5 @@
import "reflect-metadata"
import { DataSource, createConnection } from "../../../src"
import { DataSource } from "../../../src"
import {
closeTestingConnections,
createTestingConnections,
@ -38,10 +38,11 @@ describe("github issues > #6115 Down migration for enums with defaults are wrong
return
}
const connection = await createConnection(options)
const queryRunner = connection.createQueryRunner()
const dataSource = new DataSource(options)
await dataSource.initialize()
const queryRunner = dataSource.createQueryRunner()
const sqlInMemory = await connection.driver
const sqlInMemory = await dataSource.driver
.createSchemaBuilder()
.log()
@ -54,7 +55,7 @@ describe("github issues > #6115 Down migration for enums with defaults are wrong
// update entity
for (const query of upQueries) {
await connection.query(query)
await dataSource.query(query)
}
let table = await queryRunner.getTable("metric")
@ -83,7 +84,7 @@ describe("github issues > #6115 Down migration for enums with defaults are wrong
// revert update
for (const query of downQueries.reverse()) {
await connection.query(query)
await dataSource.query(query)
}
table = await queryRunner.getTable("metric")
@ -108,7 +109,7 @@ describe("github issues > #6115 Down migration for enums with defaults are wrong
expect(defaultOperator4!.default).to.equal(`'eq'`)
await queryRunner.release()
await connection.close()
await dataSource.close()
}),
))
})

View File

@ -5,7 +5,7 @@ import {
reloadTestingDatabases,
setupSingleTestingConnection,
} from "../../utils/test-utils"
import { DataSource, createConnection } from "../../../src"
import { DataSource } from "../../../src"
import { fail } from "assert"
import { Query } from "../../../src/driver/Query"
import { MysqlConnectionOptions } from "../../../src/driver/mysql/MysqlConnectionOptions"
@ -42,9 +42,10 @@ describe("github issues > #6442 JoinTable does not respect inverseJoinColumns re
fail()
}
const migrationConnection = await createConnection(options)
const migrationDataSource = new DataSource(options)
await migrationDataSource.initialize()
try {
const sqlInMemory = await migrationConnection.driver
const sqlInMemory = await migrationDataSource.driver
.createSchemaBuilder()
.log()
@ -59,7 +60,7 @@ describe("github issues > #6442 JoinTable does not respect inverseJoinColumns re
])
} finally {
await connection.close()
await migrationConnection.close()
await migrationDataSource.close()
}
}),
)

View File

@ -5,12 +5,7 @@ import {
setupTestingConnections,
} from "../../utils/test-utils"
import { MongoDriver } from "../../../src/driver/mongodb/MongoDriver"
import {
DataSource,
DataSourceOptions,
createConnection,
MongoClient,
} from "../../../src"
import { DataSource, DataSourceOptions, MongoClient } from "../../../src"
import { Warn } from "./entity/Warn"
import { MongoConnectionOptions } from "../../../src/driver/mongodb/MongoConnectionOptions"
@ -32,16 +27,17 @@ describe('github issues > #6900 MongoDB ConnectionManager doesn\'t select given
const host: string =
(options[0] as MongoConnectionOptions).host || "localhost"
const connection = await createConnection({
const dataSource = new DataSource({
...options[0],
url: `mongodb://${host}`,
database: "foo",
} as DataSourceOptions)
connections.push(connection)
await dataSource.initialize()
connections.push(dataSource)
await reloadTestingDatabases(connections)
const mongoDriver = connection.driver as MongoDriver
const mongoDriver = dataSource.driver as MongoDriver
const client = mongoDriver.queryRunner!
.databaseConnection as any as MongoClient
@ -60,17 +56,19 @@ describe('github issues > #6900 MongoDB ConnectionManager doesn\'t select given
const host: string =
(options[0] as MongoConnectionOptions).host || "localhost"
const connection = await createConnection({
const dataSource = new DataSource({
...options[0],
entities: [Warn],
url: `mongodb://${host}`,
database: "foo",
} as DataSourceOptions)
connections.push(connection)
await dataSource.initialize()
connections.push(dataSource)
await reloadTestingDatabases(connections)
const repo = connection.getRepository(Warn)
const repo = dataSource.getRepository(Warn)
await repo.insert({
id: Math.floor(Math.random() * 1000000),
@ -81,7 +79,7 @@ describe('github issues > #6900 MongoDB ConnectionManager doesn\'t select given
createdAt: new Date(),
})
const mongoDriver = connection.driver as MongoDriver
const mongoDriver = dataSource.driver as MongoDriver
const client = mongoDriver.queryRunner!
.databaseConnection as any as MongoClient

View File

@ -1,11 +1,10 @@
import "reflect-metadata"
import * as assert from "assert"
import { createConnection, getConnectionOptions } from "../../../src/index"
import { DataSource } from "../../../src/data-source/DataSource"
import { DataSource } from "../../../src"
import { getTypeOrmConfig } from "../../utils/test-utils"
describe("github issues > #798 sqlite: 'database' path in ormconfig.json is not relative", () => {
let connection: DataSource
let dataSource: DataSource
const oldCwd = process.cwd()
before(function () {
@ -17,34 +16,34 @@ describe("github issues > #798 sqlite: 'database' path in ormconfig.json is not
})
afterEach(() => {
if (connection && connection.isInitialized) {
connection.close()
if (dataSource && dataSource.isInitialized) {
dataSource.close()
}
})
it("should find the sqlite database if the cwd is changed", async function () {
// run test only if sqlite3 is enabled in ormconfig
const isEnabled = getTypeOrmConfig().some(
const config = getTypeOrmConfig().find(
(conf) => conf.type === "sqlite" && conf.skip === false,
)
if (!isEnabled) return
if (!config) return
const options = await getConnectionOptions("sqlite")
connection = await createConnection(options)
const dataSource = new DataSource(config)
await dataSource.initialize()
assert.strictEqual(connection.isInitialized, true)
assert.strictEqual(dataSource.isInitialized, true)
})
it("should find the sqlite database if the cwd is changed for better-sqlite3", async function () {
// run test only if sqlite3 is enabled in ormconfig
const isEnabled = getTypeOrmConfig().some(
const config = getTypeOrmConfig().find(
(conf) => conf.type === "better-sqlite3" && conf.skip === false,
)
if (!isEnabled) return
if (!config) return
const options = await getConnectionOptions("better-sqlite3")
connection = await createConnection(options)
const dataSource = new DataSource(config)
await dataSource.initialize()
assert.strictEqual(connection.isInitialized, true)
assert.strictEqual(dataSource.isInitialized, true)
})
})

View File

@ -1,13 +1,12 @@
import "reflect-metadata"
import * as assert from "assert"
import { createConnection } from "../../../src/index"
import rimraf from "rimraf"
import { dirname } from "path"
import { DataSource } from "../../../src/data-source/DataSource"
import { getTypeOrmConfig } from "../../utils/test-utils"
describe("github issues > #799 sqlite: 'database' path should be created", () => {
let connection: DataSource
let dataSource: DataSource
const path = `${__dirname}/tmp/sqlitedb.db`
const cleanup = (done: () => void) => {
@ -20,8 +19,8 @@ describe("github issues > #799 sqlite: 'database' path should be created", () =>
after(cleanup)
afterEach(() => {
if (connection && connection.isInitialized) {
connection.close()
if (dataSource && dataSource.isInitialized) {
dataSource.close()
}
})
@ -32,13 +31,14 @@ describe("github issues > #799 sqlite: 'database' path should be created", () =>
)
if (isEnabled === false) return
connection = await createConnection({
const dataSource = new DataSource({
name: "sqlite",
type: "sqlite",
database: path,
})
await dataSource.initialize()
assert.strictEqual(connection.isInitialized, true)
assert.strictEqual(dataSource.isInitialized, true)
})
it("should create the whole path to database file for better-sqlite3", async function () {
@ -48,12 +48,13 @@ describe("github issues > #799 sqlite: 'database' path should be created", () =>
)
if (isEnabled === false) return
connection = await createConnection({
const dataSource = new DataSource({
name: "better-sqlite3",
type: "better-sqlite3",
database: path,
})
await dataSource.initialize()
assert.strictEqual(connection.isInitialized, true)
assert.strictEqual(dataSource.isInitialized, true)
})
})

View File

@ -1,7 +1,6 @@
import "reflect-metadata"
import { expect } from "chai"
import { DataSource } from "../../src/data-source/DataSource"
import { createConnection } from "../../src/index"
import { Repository } from "../../src/repository/Repository"
import { PostDetails } from "../../sample/sample2-one-to-one/entity/PostDetails"
import { Post } from "../../sample/sample2-one-to-one/entity/Post"
@ -18,7 +17,7 @@ describe("one-to-one", function () {
// -------------------------------------------------------------------------
// connect to db
let connection: DataSource
let dataSource: DataSource
before(async function () {
const options = setupSingleTestingConnection("mysql", {
entities: [
@ -33,15 +32,16 @@ describe("one-to-one", function () {
})
if (!options) return
connection = await createConnection(options)
dataSource = new DataSource(options)
await dataSource.initialize()
})
after(() => connection.close())
after(() => dataSource.destroy())
// clean up database before each test
function reloadDatabase() {
if (!connection) return
return connection.synchronize(true)
if (!dataSource) return
return dataSource.synchronize(true)
}
let postRepository: Repository<Post>,
@ -50,13 +50,13 @@ describe("one-to-one", function () {
postImageRepository: Repository<PostImage>,
postMetadataRepository: Repository<PostMetadata>
before(function () {
if (!connection) return
if (!dataSource) return
postRepository = connection.getRepository(Post)
postDetailsRepository = connection.getRepository(PostDetails)
postCategoryRepository = connection.getRepository(PostCategory)
postImageRepository = connection.getRepository(PostImage)
postMetadataRepository = connection.getRepository(PostMetadata)
postRepository = dataSource.getRepository(Post)
postDetailsRepository = dataSource.getRepository(PostDetails)
postCategoryRepository = dataSource.getRepository(PostCategory)
postImageRepository = dataSource.getRepository(PostImage)
postMetadataRepository = dataSource.getRepository(PostMetadata)
})
// -------------------------------------------------------------------------
@ -64,7 +64,7 @@ describe("one-to-one", function () {
// -------------------------------------------------------------------------
describe("insert post and details (has inverse relation + full cascade options)", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, details: PostDetails, savedPost: Post
@ -99,7 +99,7 @@ describe("one-to-one", function () {
})
it("should have inserted post in the database", function () {
if (!connection) return
if (!dataSource) return
const expectedPost = new Post()
expectedPost.id = savedPost.id
expectedPost.text = savedPost.text
@ -113,7 +113,7 @@ describe("one-to-one", function () {
})
it("should have inserted post details in the database", async function () {
if (!connection) return
if (!dataSource) return
const expectedDetails = new PostDetails()
expectedDetails.id = savedPost.details!.id
expectedDetails.authorName = savedPost.details!.authorName
@ -127,7 +127,7 @@ describe("one-to-one", function () {
})
it("should load post and its details if left join used", async function () {
if (!connection) return
if (!dataSource) return
const expectedPost = new Post()
expectedPost.id = savedPost.id
expectedPost.text = savedPost.text
@ -150,7 +150,7 @@ describe("one-to-one", function () {
})
it("should load details and its post if left join used (from reverse side)", function () {
if (!connection) return
if (!dataSource) return
const expectedDetails = new PostDetails()
expectedDetails.id = savedPost.details!.id
@ -201,7 +201,7 @@ describe("one-to-one", function () {
})
describe("insert post and category (one-side relation)", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, category: PostCategory, savedPost: Post
@ -287,7 +287,7 @@ describe("one-to-one", function () {
})
describe("cascade updates should not be executed when cascadeUpdate option is not set", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, details: PostDetails
before(reloadDatabase)
@ -330,7 +330,7 @@ describe("one-to-one", function () {
})
describe("cascade remove should not be executed when cascadeRemove option is not set", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, details: PostDetails
before(reloadDatabase)
@ -350,7 +350,7 @@ describe("one-to-one", function () {
})
it("should ignore updates in the model and do not update the db when entity is updated", function () {
if (!connection) return
if (!dataSource) return
delete newPost.details
return postRepository
.save(newPost)
@ -372,7 +372,7 @@ describe("one-to-one", function () {
// todo: check why it generates extra query
describe("cascade updates should be executed when cascadeUpdate option is set", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, newImage: PostImage
before(reloadDatabase)
@ -419,7 +419,7 @@ describe("one-to-one", function () {
})
describe("cascade remove should be executed when cascadeRemove option is set", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, newMetadata: PostMetadata
before(reloadDatabase)

View File

@ -1,7 +1,6 @@
import "reflect-metadata"
import { expect } from "chai"
import { DataSource } from "../../src/data-source/DataSource"
import { createConnection } from "../../src/index"
import { Repository } from "../../src/repository/Repository"
import { PostDetails } from "../../sample/sample3-many-to-one/entity/PostDetails"
import { Post } from "../../sample/sample3-many-to-one/entity/Post"
@ -18,7 +17,7 @@ describe("many-to-one", function () {
// -------------------------------------------------------------------------
// connect to db
let connection: DataSource
let dataSource: DataSource
before(async function () {
const options = setupSingleTestingConnection("mysql", {
entities: [
@ -33,15 +32,16 @@ describe("many-to-one", function () {
})
if (!options) return
connection = await createConnection(options)
dataSource = new DataSource(options)
await dataSource.initialize()
})
after(() => connection.close())
after(() => dataSource.destroy())
// clean up database before each test
function reloadDatabase() {
if (!connection) return
return connection.synchronize(true)
if (!dataSource) return
return dataSource.synchronize(true)
}
let postRepository: Repository<Post>,
@ -50,12 +50,12 @@ describe("many-to-one", function () {
postImageRepository: Repository<PostImage>,
postMetadataRepository: Repository<PostMetadata>
before(function () {
if (!connection) return
postRepository = connection.getRepository(Post)
postDetailsRepository = connection.getRepository(PostDetails)
postCategoryRepository = connection.getRepository(PostCategory)
postImageRepository = connection.getRepository(PostImage)
postMetadataRepository = connection.getRepository(PostMetadata)
if (!dataSource) return
postRepository = dataSource.getRepository(Post)
postDetailsRepository = dataSource.getRepository(PostDetails)
postCategoryRepository = dataSource.getRepository(PostCategory)
postImageRepository = dataSource.getRepository(PostImage)
postMetadataRepository = dataSource.getRepository(PostMetadata)
})
// -------------------------------------------------------------------------
@ -63,7 +63,7 @@ describe("many-to-one", function () {
// -------------------------------------------------------------------------
describe("insert post and details (has inverse relation + full cascade options)", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, details: PostDetails, savedPost: Post
before(reloadDatabase)
@ -97,7 +97,7 @@ describe("many-to-one", function () {
})
it("should have inserted post in the database", function () {
if (!connection) return
if (!dataSource) return
const expectedPost = new Post()
expectedPost.id = savedPost.id
expectedPost.text = savedPost.text
@ -111,7 +111,7 @@ describe("many-to-one", function () {
})
it("should have inserted post details in the database", function () {
if (!connection) return
if (!dataSource) return
const expectedDetails = new PostDetails()
expectedDetails.id = savedPost.details!.id
expectedDetails.authorName = savedPost.details!.authorName
@ -126,7 +126,7 @@ describe("many-to-one", function () {
})
it("should load post and its details if left join used", function () {
if (!connection) return
if (!dataSource) return
const expectedPost = new Post()
expectedPost.id = savedPost.id
expectedPost.text = savedPost.text
@ -147,7 +147,7 @@ describe("many-to-one", function () {
})
it("should load details and its post if left join used (from reverse side)", function () {
if (!connection) return
if (!dataSource) return
const expectedDetails = new PostDetails()
expectedDetails.id = savedPost.details!.id
@ -173,7 +173,7 @@ describe("many-to-one", function () {
})
it("should load saved post without details if left joins are not specified", function () {
if (!connection) return
if (!dataSource) return
const expectedPost = new Post()
expectedPost.id = savedPost.id
expectedPost.text = savedPost.text
@ -187,7 +187,7 @@ describe("many-to-one", function () {
})
it("should load saved post without details if left joins are not specified", function () {
if (!connection) return
if (!dataSource) return
const expectedDetails = new PostDetails()
expectedDetails.id = savedPost.details!.id
expectedDetails.authorName = savedPost.details!.authorName
@ -203,7 +203,7 @@ describe("many-to-one", function () {
})
describe("insert post and category (one-side relation)", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, category: PostCategory, savedPost: Post
before(reloadDatabase)
@ -236,7 +236,7 @@ describe("many-to-one", function () {
})
it("should have inserted post in the database", function () {
if (!connection) return
if (!dataSource) return
const expectedPost = new Post()
expectedPost.id = savedPost.id
expectedPost.text = savedPost.text
@ -249,7 +249,7 @@ describe("many-to-one", function () {
})
it("should have inserted category in the database", function () {
if (!connection) return
if (!dataSource) return
const expectedPost = new PostCategory()
expectedPost.id = savedPost.category.id
expectedPost.name = "technology"
@ -261,7 +261,7 @@ describe("many-to-one", function () {
})
it("should load post and its category if left join used", function () {
if (!connection) return
if (!dataSource) return
const expectedPost = new Post()
expectedPost.id = savedPost.id
expectedPost.title = savedPost.title
@ -291,7 +291,7 @@ describe("many-to-one", function () {
})
describe("cascade updates should not be executed when cascadeUpdate option is not set", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, details: PostDetails
before(reloadDatabase)
@ -334,7 +334,7 @@ describe("many-to-one", function () {
})
describe("cascade remove should not be executed when cascadeRemove option is not set", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, details: PostDetails
before(reloadDatabase)
@ -374,7 +374,7 @@ describe("many-to-one", function () {
})
describe("cascade updates should be executed when cascadeUpdate option is set", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, newImage: PostImage
before(reloadDatabase)
@ -421,7 +421,7 @@ describe("many-to-one", function () {
})
describe("cascade remove should be executed when cascadeRemove option is set", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, newMetadata: PostMetadata
before(reloadDatabase)
@ -468,7 +468,7 @@ describe("many-to-one", function () {
})
describe("insert post details from reverse side", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, details: PostDetails, savedDetails: PostDetails
before(reloadDatabase)

View File

@ -1,7 +1,6 @@
import "reflect-metadata"
import { expect } from "chai"
import { DataSource } from "../../src/data-source/DataSource"
import { createConnection } from "../../src/index"
import { Repository } from "../../src/repository/Repository"
import { PostDetails } from "../../sample/sample4-many-to-many/entity/PostDetails"
import { Post } from "../../sample/sample4-many-to-many/entity/Post"
@ -16,7 +15,7 @@ describe("many-to-many", function () {
// -------------------------------------------------------------------------
// connect to db
let connection: DataSource
let dataSource: DataSource
before(async function () {
const options = setupSingleTestingConnection("mysql", {
entities: [
@ -25,15 +24,16 @@ describe("many-to-many", function () {
})
if (!options) return
connection = await createConnection(options)
dataSource = new DataSource(options)
await dataSource.initialize()
})
after(() => connection.close())
after(() => dataSource.destroy())
// clean up database before each test
function reloadDatabase() {
if (!connection) return
return connection.synchronize(true)
if (!dataSource) return
return dataSource.synchronize(true)
}
let postRepository: Repository<Post>,
@ -42,12 +42,12 @@ describe("many-to-many", function () {
postImageRepository: Repository<PostImage>,
postMetadataRepository: Repository<PostMetadata>
before(function () {
if (!connection) return
postRepository = connection.getRepository(Post)
postDetailsRepository = connection.getRepository(PostDetails)
postCategoryRepository = connection.getRepository(PostCategory)
postImageRepository = connection.getRepository(PostImage)
postMetadataRepository = connection.getRepository(PostMetadata)
if (!dataSource) return
postRepository = dataSource.getRepository(Post)
postDetailsRepository = dataSource.getRepository(PostDetails)
postCategoryRepository = dataSource.getRepository(PostCategory)
postImageRepository = dataSource.getRepository(PostImage)
postMetadataRepository = dataSource.getRepository(PostMetadata)
})
// -------------------------------------------------------------------------
@ -55,7 +55,7 @@ describe("many-to-many", function () {
// -------------------------------------------------------------------------
describe("insert post and details (has inverse relation + full cascade options)", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, details: PostDetails, savedPost: Post
before(reloadDatabase)
@ -191,7 +191,7 @@ describe("many-to-many", function () {
})
describe("insert post and category (one-side relation)", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, category: PostCategory, savedPost: Post
before(reloadDatabase)
@ -278,7 +278,7 @@ describe("many-to-many", function () {
})
describe("cascade updates should not be executed when cascadeUpdate option is not set", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, details: PostDetails
before(reloadDatabase)
@ -323,7 +323,7 @@ describe("many-to-many", function () {
})
describe("cascade remove should not be executed when cascadeRemove option is not set", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, details: PostDetails
before(reloadDatabase)
@ -373,7 +373,7 @@ describe("many-to-many", function () {
})
describe("cascade updates should be executed when cascadeUpdate option is set", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, newImage: PostImage
before(reloadDatabase)
@ -421,7 +421,7 @@ describe("many-to-many", function () {
})
describe("cascade remove should be executed when cascadeRemove option is set", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, newMetadata: PostMetadata
before(reloadDatabase)
@ -469,7 +469,7 @@ describe("many-to-many", function () {
})
describe("insert post details from reverse side", function () {
if (!connection) return
if (!dataSource) return
let newPost: Post, details: PostDetails, savedDetails: PostDetails
before(reloadDatabase)