Skip to content

Commit

Permalink
Merge pull request #118 from GP4cK/upgrade-typeorm-code
Browse files Browse the repository at this point in the history
chore(typeorm): migrate deprecated code
  • Loading branch information
TriPSs committed Apr 13, 2023
2 parents ae2d65d + a530d47 commit 1bad18f
Show file tree
Hide file tree
Showing 9 changed files with 2,403 additions and 2,767 deletions.
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
"@m8a/nestjs-typegoose": "11.0.0",
"@nestjs/common": "9.3.12",
"@nestjs/core": "9.3.12",
"@nestjs/graphql": "^10.1.3",
"@nestjs/graphql": "10.1.3",
"@nestjs/jwt": "10.0.3",
"@nestjs/mongoose": "9.2.0",
"@nestjs/passport": "9.0.0",
"@nestjs/platform-express": "9.1.4",
"@nestjs/sequelize": "9.0.0",
"@nestjs/typeorm": "^9.0.1",
"apollo-server-core": "3.11.1",
"apollo-server-express": "3.11.1",
"apollo-server-plugin-base": "3.7.1",
Expand Down Expand Up @@ -56,11 +57,8 @@
"@jscutlery/semver": "2.26.0",
"@nestjs/apollo": "^10.0.22",
"@nestjs/cli": "8.2.8",
"@nestjs/common": "8.4.7",
"@nestjs/core": "8.4.7",
"@nestjs/schematics": "8.0.11",
"@nestjs/testing": "8.4.7",
"@nestjs/typeorm": "8.0.4",
"@nestjs/testing": "^9.3.12",
"@nrwl/eslint-plugin-nx": "15.3.3",
"@nrwl/jest": "15.3.3",
"@nrwl/js": "15.3.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// this is needed to create a query builder in typeorm :(
import { Connection, ConnectionOptions, createConnection, getConnection } from 'typeorm'
import { DataSource, DataSourceOptions } from 'typeorm'

import { RelationOfTestRelationEntity } from './relation-of-test-relation.entity'
import { seed } from './seeds'
Expand All @@ -9,7 +9,7 @@ import { TestRelation } from './test-relation.entity'
import { TestSoftDeleteEntity } from './test-soft-delete.entity'
import { TestSoftDeleteRelation } from './test-soft-delete.relation'

export const CONNECTION_OPTIONS: ConnectionOptions = {
export const CONNECTION_OPTIONS: DataSourceOptions = {
type: 'sqlite',
database: ':memory:',
dropSchema: true,
Expand All @@ -25,16 +25,10 @@ export const CONNECTION_OPTIONS: ConnectionOptions = {
logging: false
}

export function createTestConnection(): Promise<Connection> {
return createConnection(CONNECTION_OPTIONS)
}

export function closeTestConnection(): Promise<void> {
return getConnection().close()
}

export function getTestConnection(): Connection {
return getConnection()
export async function createTestConnection(): Promise<DataSource> {
const dataSource = new DataSource(CONNECTION_OPTIONS)
await dataSource.initialize()
return dataSource
}

const tables = [
Expand All @@ -46,15 +40,14 @@ const tables = [
'test_soft_delete_relation',
'test_entity_many_test_relations_test_relation'
]
export const truncate = async (connection: Connection): Promise<void> => {
export const truncate = async (connection: DataSource): Promise<void> => {
await tables.reduce(async (prev, table) => {
await prev
await connection.query(`DELETE
FROM ${table}`)
await connection.query(`DELETE FROM ${table}`)
}, Promise.resolve())
}

export const refresh = async (connection: Connection = getConnection()): Promise<void> => {
export const refresh = async (connection: DataSource): Promise<void> => {
await truncate(connection)
return seed(connection)
}
4 changes: 2 additions & 2 deletions packages/query-typeorm/__tests__/__fixtures__/seeds.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Connection, getConnection, In } from 'typeorm'
import { DataSource, In } from 'typeorm'

import { RelationOfTestRelationEntity } from './relation-of-test-relation.entity'
import { TestEntity } from './test.entity'
Expand Down Expand Up @@ -69,7 +69,7 @@ export const TEST_RELATIONS_OF_RELATION = TEST_RELATIONS.map<Partial<RelationOfT
testRelationId: testRelation.testRelationPk
})) as RelationOfTestRelationEntity[]

export const seed = async (connection: Connection = getConnection()): Promise<void> => {
export const seed = async (connection: DataSource): Promise<void> => {
const testEntityRepo = connection.getRepository(TestEntity)
const testRelationRepo = connection.getRepository(TestRelation)
const relationOfTestRelationRepo = connection.getRepository(RelationOfTestRelationEntity)
Expand Down
12 changes: 8 additions & 4 deletions packages/query-typeorm/__tests__/query/aggregate.builder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { AggregateQuery, GroupBy } from '@ptc-org/nestjs-query-core'
import { format as formatSql } from 'sql-formatter'
import { DataSource } from 'typeorm'

import { AggregateBuilder } from '../../src/query'
import { closeTestConnection, createTestConnection, getTestConnection } from '../__fixtures__/connection.fixture'
import { createTestConnection } from '../__fixtures__/connection.fixture'
import { TestEntity } from '../__fixtures__/test.entity'

describe('AggregateBuilder', (): void => {
beforeEach(createTestConnection)
afterEach(closeTestConnection)
let dataSource: DataSource
beforeEach(async () => {
dataSource = await createTestConnection()
})
afterEach(() => dataSource.destroy())

const getRepo = () => getTestConnection().getRepository(TestEntity)
const getRepo = () => dataSource.getRepository(TestEntity)
const getQueryBuilder = () => getRepo().createQueryBuilder()
const createAggregateBuilder = () => new AggregateBuilder<TestEntity>(getRepo())

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { Class, Filter, Query, SortDirection, SortNulls } from '@ptc-org/nestjs-query-core'
import { format as formatSql } from 'sql-formatter'
import { anything, deepEqual, instance, mock, verify, when } from 'ts-mockito'
import { QueryBuilder, WhereExpressionBuilder } from 'typeorm'
import { DataSource, QueryBuilder, WhereExpressionBuilder } from 'typeorm'

import { FilterQueryBuilder, WhereBuilder } from '../../src/query'
import { closeTestConnection, createTestConnection, getTestConnection } from '../__fixtures__/connection.fixture'
import { createTestConnection } from '../__fixtures__/connection.fixture'
import { TestEntity } from '../__fixtures__/test.entity'
import { TestSoftDeleteEntity } from '../__fixtures__/test-soft-delete.entity'

describe('FilterQueryBuilder', (): void => {
beforeEach(createTestConnection)
afterEach(closeTestConnection)
let connection: DataSource
beforeEach(async () => {
connection = await createTestConnection()
})
afterEach(() => connection.destroy())

const getEntityQueryBuilder = <Entity>(entity: Class<Entity>, whereBuilder: WhereBuilder<Entity>): FilterQueryBuilder<Entity> =>
new FilterQueryBuilder(getTestConnection().getRepository(entity), whereBuilder)
new FilterQueryBuilder(connection.getRepository(entity), whereBuilder)

const expectSQLSnapshot = <Entity>(query: QueryBuilder<Entity>): void => {
const [sql, params] = query.getQueryAndParameters()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { Class, Query, SortDirection, SortNulls } from '@ptc-org/nestjs-query-core'
import { format as formatSql } from 'sql-formatter'
import { DataSource } from 'typeorm'

import { RelationQueryBuilder } from '../../src/query'
import { closeTestConnection, createTestConnection, getTestConnection } from '../__fixtures__/connection.fixture'
import { createTestConnection } from '../__fixtures__/connection.fixture'
import { TEST_ENTITIES } from '../__fixtures__/seeds'
import { TestEntity } from '../__fixtures__/test.entity'
import { TestRelation } from '../__fixtures__/test-relation.entity'

describe('RelationQueryBuilder', (): void => {
beforeEach(createTestConnection)
afterEach(closeTestConnection)
let dataSource: DataSource
beforeEach(async () => {
dataSource = await createTestConnection()
})
afterEach(() => dataSource.destroy())

const getRelationQueryBuilder = <Entity, Relation>(
EntityClass: Class<Entity>,
relationName: string
): RelationQueryBuilder<Entity, Relation> =>
new RelationQueryBuilder(getTestConnection().getRepository(EntityClass), relationName)
): RelationQueryBuilder<Entity, Relation> => new RelationQueryBuilder(dataSource.getRepository(EntityClass), relationName)

const expectSQLSnapshot = <Entity, Relation>(
EntityClass: Class<Entity>,
Expand Down
12 changes: 8 additions & 4 deletions packages/query-typeorm/__tests__/query/where.builder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { Filter } from '@ptc-org/nestjs-query-core'
import { format as formatSql } from 'sql-formatter'
import { DataSource } from 'typeorm'

import { WhereBuilder } from '../../src/query'
import { closeTestConnection, createTestConnection, getTestConnection } from '../__fixtures__/connection.fixture'
import { createTestConnection } from '../__fixtures__/connection.fixture'
import { TestEntity } from '../__fixtures__/test.entity'

describe('WhereBuilder', (): void => {
beforeEach(createTestConnection)
afterEach(closeTestConnection)
let dataSource: DataSource
beforeEach(async () => {
dataSource = await createTestConnection()
})
afterEach(() => dataSource.destroy())

const getRepo = () => getTestConnection().getRepository(TestEntity)
const getRepo = () => dataSource.getRepository(TestEntity)
const getQueryBuilder = () => getRepo().createQueryBuilder()
const createWhereBuilder = () => new WhereBuilder<TestEntity>()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Test, TestingModule } from '@nestjs/testing'
import { InjectRepository, TypeOrmModule } from '@nestjs/typeorm'
import { getDataSourceToken, InjectRepository, TypeOrmModule } from '@nestjs/typeorm'
import { Filter, SortDirection } from '@ptc-org/nestjs-query-core'
import { plainToClass } from 'class-transformer'
import { Repository } from 'typeorm'

import { TypeOrmQueryService } from '../../src'
import { FilterQueryBuilder } from '../../src/query'
import { closeTestConnection, CONNECTION_OPTIONS, getTestConnection, refresh, truncate } from '../__fixtures__/connection.fixture'
import { CONNECTION_OPTIONS, refresh, truncate } from '../__fixtures__/connection.fixture'
import {
TEST_ENTITIES,
TEST_RELATIONS,
Expand Down Expand Up @@ -40,7 +40,10 @@ describe('TypeOrmQueryService', (): void => {
}
}

afterEach(closeTestConnection)
afterEach(() => {
const dataSource = moduleRef.get(getDataSourceToken())
return dataSource.destroy()
})

beforeEach(async () => {
moduleRef = await Test.createTestingModule({
Expand All @@ -51,7 +54,9 @@ describe('TypeOrmQueryService', (): void => {
providers: [TestEntityService, TestRelationService, TestSoftDeleteEntityService]
}).compile()

await refresh()
const dataSource = moduleRef.get(getDataSourceToken())

await refresh(dataSource)
})

it('should create a filterQueryBuilder and assemblerService based on the repo passed in if not provided', () => {
Expand Down Expand Up @@ -1945,14 +1950,14 @@ describe('TypeOrmQueryService', (): void => {

describe('#createMany', () => {
it('call save on the repo with instances of entities when passed plain objects', async () => {
await truncate(getTestConnection())
await truncate(moduleRef.get(getDataSourceToken()))
const queryService = moduleRef.get(TestEntityService)
const created = await queryService.createMany(TEST_ENTITIES)
expect(created).toEqual(TEST_ENTITIES)
})

it('call save on the repo with instances of entities when passed instances', async () => {
await truncate(getTestConnection())
await truncate(moduleRef.get(getDataSourceToken()))
const instances = TEST_ENTITIES.map((e) => plainToClass(TestEntity, e))
const queryService = moduleRef.get(TestEntityService)
const created = await queryService.createMany(instances)
Expand All @@ -1967,15 +1972,15 @@ describe('TypeOrmQueryService', (): void => {

describe('#createOne', () => {
it('call save on the repo with an instance of the entity when passed a plain object', async () => {
await truncate(getTestConnection())
await truncate(moduleRef.get(getDataSourceToken()))
const entity = TEST_ENTITIES[0]
const queryService = moduleRef.get(TestEntityService)
const created = await queryService.createOne(entity)
expect(created).toEqual(entity)
})

it('call save on the repo with an instance of the entity when passed an instance', async () => {
await truncate(getTestConnection())
await truncate(moduleRef.get(getDataSourceToken()))
const entity = plainToClass(TestEntity, TEST_ENTITIES[0])
const queryService = moduleRef.get(TestEntityService)
const created = await queryService.createOne(entity)
Expand Down
Loading

0 comments on commit 1bad18f

Please sign in to comment.