Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/publish-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Publish Preview

on:
pull_request:
workflow_dispatch:

env:
NX_BRANCH: ${{ github.event.number }}
NX_RUN_GROUP: ${{ github.run_id }}

jobs:
publish-preview:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: 22.x

- name: Setup
uses: ./.github/actions/setup-step

- name: Build
run: yarn nx run-many --target=build --all

- name: Publish Preview
run: npx pkg-pr-new publish './dist/packages/*'
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
"@docusaurus/module-type-aliases": "3.9.1",
"@docusaurus/preset-classic": "3.9.1",
"@jscutlery/semver": "5.7.1",
"@mikro-orm/core": "^6.6.2",
"@mikro-orm/nestjs": "^6.1.1",
"@mikro-orm/sqlite": "^6.6.2",
"@nestjs/apollo": "^13.2.1",
"@nestjs/cli": "11.0.10",
"@nestjs/schematics": "11.0.8",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { MikroORM, Options, EntityManager } from '@mikro-orm/core'
import { SqliteDriver } from '@mikro-orm/sqlite'

import { TestEntity } from './test.entity'
import { TestRelation } from './test-relation.entity'
import { TestSoftDeleteEntity } from './test-soft-delete.entity'
import { seed } from './seeds'

export const CONNECTION_OPTIONS: Options<SqliteDriver> = {
driver: SqliteDriver,
dbName: ':memory:',
entities: [TestEntity, TestRelation, TestSoftDeleteEntity],
allowGlobalContext: true
}

export async function createTestConnection(): Promise<MikroORM<SqliteDriver>> {
const orm = await MikroORM.init(CONNECTION_OPTIONS)
const generator = orm.getSchemaGenerator()
await generator.createSchema()
return orm
}

export async function truncate(em: EntityManager): Promise<void> {
const connection = em.getConnection()
await connection.execute('DELETE FROM test_relation')
await connection.execute('DELETE FROM test_entity_many_test_relations')
await connection.execute('DELETE FROM test_entity')
await connection.execute('DELETE FROM test_soft_delete_entity')
}

export async function refresh(em: EntityManager): Promise<void> {
await truncate(em)
await seed(em.fork())
}
73 changes: 73 additions & 0 deletions packages/query-mikroorm/__tests__/__fixtures__/seeds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { MikroORM, EntityManager } from '@mikro-orm/core'

import { TestEntity } from './test.entity'
import { TestRelation } from './test-relation.entity'
import { TestSoftDeleteEntity } from './test-soft-delete.entity'

export const TEST_ENTITIES: TestEntity[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map((i) => {
const testEntity = new TestEntity()
testEntity.testEntityPk = `test-entity-${i}`
testEntity.stringType = `foo${i}`
testEntity.boolType = i % 2 === 0
testEntity.numberType = i
testEntity.dateType = new Date(`2020-02-${i.toString().padStart(2, '0')}`)
return testEntity
})

export const TEST_RELATIONS: TestRelation[] = TEST_ENTITIES.reduce((relations, te) => {
return [
...relations,
...[1, 2, 3].map((i) => {
const relation = new TestRelation()
relation.testRelationPk = `test-relations-${te.testEntityPk}-${i}`
relation.relationName = `${te.stringType}-relation-${i}`
relation.testEntityId = te.testEntityPk
return relation
})
]
}, [] as TestRelation[])

export const TEST_SOFT_DELETE_ENTITIES: TestSoftDeleteEntity[] = [1, 2, 3, 4, 5].map((i) => {
const entity = new TestSoftDeleteEntity()
entity.testEntityPk = `test-soft-delete-entity-${i}`
entity.stringType = `foo${i}`
return entity
})

export async function seed(em: EntityManager): Promise<void> {
for (const entity of TEST_ENTITIES) {
const newEntity = em.create(TestEntity, {
testEntityPk: entity.testEntityPk,
stringType: entity.stringType,
boolType: entity.boolType,
numberType: entity.numberType,
dateType: entity.dateType
})
em.persist(newEntity)
}

await em.flush()

for (const relation of TEST_RELATIONS) {
const testEntity = await em.findOne(TestEntity, { testEntityPk: relation.testEntityId })
const newRelation = em.create(TestRelation, {
testRelationPk: relation.testRelationPk,
relationName: relation.relationName,
testEntityId: relation.testEntityId,
testEntity
})
em.persist(newRelation)
}

await em.flush()

for (const entity of TEST_SOFT_DELETE_ENTITIES) {
const newEntity = em.create(TestSoftDeleteEntity, {
testEntityPk: entity.testEntityPk,
stringType: entity.stringType
})
em.persist(newEntity)
}

await em.flush()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Entity, PrimaryKey, Property, ManyToOne, ManyToMany, OneToOne, Collection } from '@mikro-orm/core'

import { TestEntity } from './test.entity'

@Entity()
export class TestRelation {
@PrimaryKey()
testRelationPk!: string

@Property()
relationName!: string

@Property({ nullable: true })
testEntityId?: string

@ManyToOne(() => TestEntity, { nullable: true })
testEntity?: TestEntity

@ManyToMany(() => TestEntity, (entity) => entity.manyTestRelations)
manyTestEntities = new Collection<TestEntity>(this)

@OneToOne(() => TestEntity, (entity) => entity.oneTestRelation, { nullable: true })
oneTestEntity?: TestEntity
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Entity, PrimaryKey, Property } from '@mikro-orm/core'

@Entity()
export class TestSoftDeleteEntity {
@PrimaryKey()
testEntityPk!: string

@Property()
stringType!: string

@Property({ nullable: true })
deletedAt?: Date
}
33 changes: 33 additions & 0 deletions packages/query-mikroorm/__tests__/__fixtures__/test.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Entity, PrimaryKey, Property, ManyToOne, OneToMany, ManyToMany, OneToOne, Collection } from '@mikro-orm/core'

import { TestRelation } from './test-relation.entity'

@Entity()
export class TestEntity {
@PrimaryKey()
testEntityPk!: string

@Property()
stringType!: string

@Property()
boolType!: boolean

@Property()
numberType!: number

@Property()
dateType!: Date

@OneToMany(() => TestRelation, (relation) => relation.testEntity)
testRelations = new Collection<TestRelation>(this)

@ManyToOne(() => TestRelation, { nullable: true })
manyToOneRelation?: TestRelation

@ManyToMany(() => TestRelation, (relation) => relation.manyTestEntities, { owner: true })
manyTestRelations = new Collection<TestRelation>(this)

@OneToOne(() => TestRelation, (relation) => relation.oneTestEntity, { owner: true, nullable: true })
oneTestRelation?: TestRelation
}
20 changes: 20 additions & 0 deletions packages/query-mikroorm/__tests__/module.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NestjsQueryMikroOrmModule } from '../src'
import { TestEntity } from './__fixtures__/test.entity'

describe('NestjsQueryMikroOrmModule', () => {
it('should create a module', () => {
const mikroOrmModule = NestjsQueryMikroOrmModule.forFeature([TestEntity])
expect(mikroOrmModule.imports).toHaveLength(1)
expect(mikroOrmModule.module).toBe(NestjsQueryMikroOrmModule)
expect(mikroOrmModule.providers).toHaveLength(1)
expect(mikroOrmModule.exports).toHaveLength(2)
})

it('should support contextName parameter', () => {
const mikroOrmModule = NestjsQueryMikroOrmModule.forFeature([TestEntity], 'connection2')
expect(mikroOrmModule.imports).toHaveLength(1)
expect(mikroOrmModule.module).toBe(NestjsQueryMikroOrmModule)
expect(mikroOrmModule.providers).toHaveLength(1)
expect(mikroOrmModule.exports).toHaveLength(2)
})
})
28 changes: 28 additions & 0 deletions packages/query-mikroorm/__tests__/providers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { getQueryServiceToken } from '@ptc-org/nestjs-query-core'

import { createMikroOrmQueryServiceProviders } from '../src/providers'
import { TestEntity } from './__fixtures__/test.entity'
import { TestRelation } from './__fixtures__/test-relation.entity'

describe('createMikroOrmQueryServiceProviders', () => {
it('should create a provider for a single entity', () => {
const providers = createMikroOrmQueryServiceProviders([TestEntity])
expect(providers).toHaveLength(1)
expect(providers[0].provide).toBe(getQueryServiceToken(TestEntity))
expect(providers[0].useFactory).toBeDefined()
expect(providers[0].inject).toHaveLength(1)
})

it('should create providers for multiple entities', () => {
const providers = createMikroOrmQueryServiceProviders([TestEntity, TestRelation])
expect(providers).toHaveLength(2)
expect(providers[0].provide).toBe(getQueryServiceToken(TestEntity))
expect(providers[1].provide).toBe(getQueryServiceToken(TestRelation))
})

it('should support contextName parameter', () => {
const providers = createMikroOrmQueryServiceProviders([TestEntity], 'customConnection')
expect(providers).toHaveLength(1)
expect(providers[0].provide).toBe(getQueryServiceToken(TestEntity))
})
})
Loading
Loading