Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sqlite): fix writing outbox #1248

Merged
merged 1 commit into from
Jun 25, 2023
Merged
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
5 changes: 0 additions & 5 deletions apps/backend/src/core/table/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { NestAggregateSqliteQueryModel } from './sqlite/record-sqlite.aggregate-
import { NestRecordSqliteQueryModel } from './sqlite/record-sqlite.query-model.js'
import { NestRecordSqliteRepository } from './sqlite/record-sqlite.repository.js'
import { NestRecordSqliteTreeQueryModel, RECORD_TREE_QUERY_MODEL } from './sqlite/record-sqlite.tree-query-model.js'
import { NestSqliteUnitOfWork, UNIT_OF_WORK } from './sqlite/sqlite.uow.js'
import { NestTableKVCache, STORAGE } from './sqlite/table-kv.cache.js'
import { NestTableSqliteQueryModel } from './sqlite/table-sqlite.query-model.js'
import { NestTableSqliteRepository, TABLE_KV_CACHE } from './sqlite/table-sqlite.repository.js'
Expand Down Expand Up @@ -76,8 +75,4 @@ export const dbAdapters: Provider[] = [
useFactory: cacheStorageFactory,
inject: [PinoLogger, cacheStorageConfig.KEY],
},
{
provide: UNIT_OF_WORK,
useClass: NestSqliteUnitOfWork,
},
]
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { RecordSqliteRepository } from '@undb/sqlite'
import { ClsService } from 'nestjs-cls'
import { Option } from 'oxide.ts'
import { NestOutboxService } from '../../../../outbox/outbox.service.js'
import { InjectUnitOrWork } from './sqlite.uow.js'
import { InjectUnitOfWork } from '../../../../uow/uow.service.js'

@Injectable()
export class NestRecordSqliteRepository extends RecordSqliteRepository {
constructor(
@InjectUnitOrWork()
@InjectUnitOfWork()
protected readonly uow: IUnitOfWork<EntityManager>,
protected readonly orm: MikroORM,
protected readonly cls: ClsService<ClsStore>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import type { ITableCache, ITableSpec, Table } from '@undb/core'
import { type IUnitOfWork } from '@undb/domain'
import { TableSqliteRepository } from '@undb/sqlite'
import { Option } from 'oxide.ts'
import { InjectUnitOrWork } from './sqlite.uow.js'
import { InjectUnitOfWork } from '../../../../uow/uow.service.js'

export const TABLE_KV_CACHE = Symbol('TABLE_KV_CACHE')
export const InjectTableKVCache = () => Inject(TABLE_KV_CACHE)

@Injectable()
export class NestTableSqliteRepository extends TableSqliteRepository {
constructor(
@InjectUnitOrWork()
@InjectUnitOfWork()
protected readonly uow: IUnitOfWork<EntityManager>,
@InjectTableKVCache()
protected readonly cache: ITableCache,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { CommandHandler } from '@nestjs/cqrs'
import { TableSpecHandler, type ITableRepository } from '@undb/core'
import { CreateFieldCommand, CreateFieldCommandHandler as DomainHandler } from '@undb/cqrs'
import { type IUnitOfWork } from '@undb/domain'
import { InjectUnitOfWork } from '../../../uow/uow.service.js'
import { InjectTableRepository } from '../adapters/index.js'
import { InjectUnitOrWork } from '../adapters/sqlite/sqlite.uow.js'

@CommandHandler(CreateFieldCommand)
export class CreateFieldCommandHandler extends DomainHandler implements ICommandHandler<CreateFieldCommand, void> {
constructor(
@InjectUnitOrWork()
@InjectUnitOfWork()
protected readonly uow: IUnitOfWork,
@InjectTableRepository()
protected readonly tableRepo: ITableRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { ClsStore, TableSpecHandler, type IRecordRepository, type ITableReposito
import { CreateTableCommand, CreateTableCommandHandler as DomainHandler } from '@undb/cqrs'
import { type IUnitOfWork } from '@undb/domain'
import { ClsService } from 'nestjs-cls'
import { InjectUnitOfWork } from '../../../uow/uow.service.js'
import { InjectRecordRepository, InjectTableRepository } from '../adapters/index.js'
import { InjectUnitOrWork } from '../adapters/sqlite/sqlite.uow.js'

@CommandHandler(CreateTableCommand)
export class CreateTableCommandHandler extends DomainHandler implements ICommandHandler<CreateTableCommand> {
constructor(
@InjectUnitOrWork()
@InjectUnitOfWork()
protected readonly uow: IUnitOfWork,
@InjectTableRepository()
protected readonly repo: ITableRepository,
Expand Down
3 changes: 2 additions & 1 deletion apps/backend/src/core/table/table.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Module } from '@nestjs/common'
import { CqrsModule } from '@nestjs/cqrs'
import { OutboxModule } from '../../outbox/outbox.module.js'
import { UnitOrWorkModule } from '../../uow/uow.module.js'
import { dbAdapters } from './adapters/index.js'
import { commandHandlers } from './commands/index.js'
import { queryHandlers } from './queries/index.js'
Expand All @@ -9,7 +10,7 @@ import { tableSpecHandler } from './services/table-spec.handler.js'

@Module({
controllers: [RecordController],
imports: [CqrsModule, OutboxModule],
imports: [CqrsModule, OutboxModule, UnitOrWorkModule],
providers: [...commandHandlers, ...queryHandlers, ...dbAdapters, tableSpecHandler],
exports: [...dbAdapters],
})
Expand Down
2 changes: 2 additions & 0 deletions apps/backend/src/outbox/outbox.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Module } from '@nestjs/common'
import { UnitOrWorkModule } from '../uow/uow.module.js'
import { NestOutboxService } from './outbox.service.js'

@Module({
imports: [UnitOrWorkModule],
providers: [NestOutboxService],
exports: [NestOutboxService],
})
Expand Down
10 changes: 8 additions & 2 deletions apps/backend/src/outbox/outbox.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { MikroORM, UseRequestContext } from '@mikro-orm/core'
import { Injectable } from '@nestjs/common'
import { type IUnitOfWork } from '@undb/domain'
import { EntityManager, Outbox, OutboxService } from '@undb/sqlite'
import { InjectUnitOfWork } from '../uow/uow.service.js'

@Injectable()
export class NestOutboxService extends OutboxService {
constructor(protected readonly orm: MikroORM) {
super(orm.em as EntityManager)
constructor(
@InjectUnitOfWork()
protected readonly uow: IUnitOfWork<EntityManager>,
public readonly orm: MikroORM,
) {
super(uow)
}

@UseRequestContext()
Expand Down
13 changes: 13 additions & 0 deletions apps/backend/src/uow/uow.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common'
import { NestSqliteUnitOfWork, UNIT_OF_WORK } from './uow.service.js'

@Module({
providers: [
{
provide: UNIT_OF_WORK,
useClass: NestSqliteUnitOfWork,
},
],
exports: [UNIT_OF_WORK],
})
export class UnitOrWorkModule {}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { EntityManager } from '@mikro-orm/better-sqlite'
import { MikroORM } from '@mikro-orm/core'
import { Inject, Injectable } from '@nestjs/common'
import { SqliteUnitOfWork } from '@undb/sqlite'
import { EntityManager, SqliteUnitOfWork } from '@undb/sqlite'

export const UNIT_OF_WORK = Symbol('UNIT_OF_WORK')
export const InjectUnitOrWork = () => Inject(UNIT_OF_WORK)

export const InjectUnitOfWork = () => Inject(UNIT_OF_WORK)

@Injectable()
export class NestSqliteUnitOfWork extends SqliteUnitOfWork {
Expand Down
8 changes: 6 additions & 2 deletions packages/database/sqlite/src/services/outbox.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { EntityManager } from '@mikro-orm/better-sqlite'
import { LockMode } from '@mikro-orm/core'
import type { IEvent } from '@undb/domain'
import type { IEvent, IUnitOfWork } from '@undb/domain'
import { Outbox } from '../entity/outbox.js'

export interface IOutboxService {
Expand All @@ -9,7 +9,11 @@ export interface IOutboxService {
}

export class OutboxService implements IOutboxService {
constructor(protected readonly em: EntityManager) {}
constructor(protected readonly uow: IUnitOfWork<EntityManager>) {}

private get em() {
return this.uow.conn()
}

persist(event: IEvent): Outbox {
const outbox = new Outbox(event)
Expand Down