-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1106 from undb-xyz/release/0.5.0
- Loading branch information
Showing
64 changed files
with
1,392 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
apps/backend/src/modules/table/commands/create-table.command.handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
import type { ICommandHandler } from '@nestjs/cqrs' | ||
import { CommandHandler } from '@nestjs/cqrs' | ||
import { ClsStore, TableSpecHandler, type ITableRepository } from '@undb/core' | ||
import { ClsStore, TableSpecHandler, type IRecordRepository, type ITableRepository } from '@undb/core' | ||
import { CreateTableCommand, CreateTableCommandHandler as DomainHandler } from '@undb/cqrs' | ||
import { ClsService } from 'nestjs-cls' | ||
import { InjectTableReposiory } from '../adapters/index.js' | ||
import { InjectRecordReposiory, InjectTableReposiory } from '../adapters/index.js' | ||
|
||
@CommandHandler(CreateTableCommand) | ||
export class CreateTableCommandHandler extends DomainHandler implements ICommandHandler<CreateTableCommand> { | ||
constructor( | ||
@InjectTableReposiory() | ||
protected readonly repo: ITableRepository, | ||
@InjectRecordReposiory() | ||
protected readonly recordRepo: IRecordRepository, | ||
protected readonly handler: TableSpecHandler, | ||
protected readonly cls: ClsService<ClsStore>, | ||
) { | ||
super(repo, handler, cls) | ||
super(repo, recordRepo, handler, cls) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Controller, Get, Param, Res } from '@nestjs/common' | ||
import { type Response } from 'express' | ||
import { OpenAPIDocService } from './openapi-doc.service.js' | ||
|
||
@Controller('openapi') | ||
export class OpenAPIDocController { | ||
constructor(private readonly openAPIService: OpenAPIDocService) {} | ||
|
||
@Get('docs/tables/:tableId') | ||
public async doc(@Res() res: Response, @Param('tableId') tableId: string) { | ||
const html = await this.openAPIService.generateDoc(tableId) | ||
|
||
res.type('html') | ||
res.send(html) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { type IRecordQueryModel, type ITableRepository } from '@undb/core' | ||
import { createRedocHTML, createTableSchema } from '@undb/openapi' | ||
import { InjectRecordQueryModel, InjectTableReposiory } from '../modules/table/adapters/index.js' | ||
|
||
@Injectable() | ||
export class OpenAPIDocService { | ||
constructor( | ||
@InjectTableReposiory() | ||
private readonly repo: ITableRepository, | ||
@InjectRecordQueryModel() | ||
private readonly recordRepo: IRecordQueryModel, | ||
) {} | ||
public async generateDoc(tableId: string): Promise<string> { | ||
const table = (await this.repo.findOneById(tableId)).unwrap() | ||
const record = await this.recordRepo.findOne(tableId, null) | ||
|
||
const spec = createTableSchema(table, record.into()) | ||
const html = createRedocHTML(table, spec) | ||
|
||
return html | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Body, Controller, Delete, Get, Param, Query, UseGuards, Version } from '@nestjs/common' | ||
import { CommandBus, QueryBus } from '@nestjs/cqrs' | ||
import { BulkDeleteRecordsCommand, DeleteRecordCommand, GetRecordQuery, GetRecordsQuery } from '@undb/cqrs' | ||
import { JwtAuthGuard } from '../auth/jwt-auth.guard.js' | ||
import { OpenAPIService } from './openapi.service.js' | ||
|
||
@Controller({ | ||
path: 'openapi', | ||
version: '1', | ||
}) | ||
@UseGuards(JwtAuthGuard) | ||
export class OpenAPIController { | ||
constructor( | ||
private queryBus: QueryBus, | ||
private readonly commandBus: CommandBus, | ||
private readonly service: OpenAPIService, | ||
) {} | ||
|
||
@Version('1') | ||
@Get('tables/:tableId/records') | ||
public async getRecords(@Param('tableId') tableId: string, @Query('viewId') viewId?: string) { | ||
const result = await this.queryBus.execute(new GetRecordsQuery({ tableId, viewId })) | ||
const records = await this.service.mapMany(tableId, result.records ?? []) | ||
return { records } | ||
} | ||
|
||
@Version('1') | ||
@Get('tables/:tableId/records/:id') | ||
public async getRecordById(@Param('tableId') tableId: string, @Param('id') id: string) { | ||
const result = await this.queryBus.execute(new GetRecordQuery({ tableId, id })) | ||
const record = result ? await this.service.mapMany(tableId, result) : null | ||
return { record } | ||
} | ||
|
||
@Version('1') | ||
@Delete('tables/:tableId/records/:id') | ||
public async deleteRecord(@Param('tableId') tableId: string, @Param('id') id: string) { | ||
await this.commandBus.execute(new DeleteRecordCommand({ tableId, id })) | ||
} | ||
|
||
@Version('1') | ||
@Delete('tables/:tableId/records') | ||
public async deleteRecordsByIds(@Param('tableId') tableId: string, @Body('ids') ids: [string, ...string[]]) { | ||
await this.commandBus.execute(new BulkDeleteRecordsCommand({ tableId, ids })) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Inject, Provider } from '@nestjs/common' | ||
import { openApiRecordMapper } from '@undb/openapi' | ||
|
||
const OPEN_API_MAPPER = Symbol('OPEN_API_MAPPER') | ||
|
||
export const InjectOpenAPIMapper = () => Inject(OPEN_API_MAPPER) | ||
|
||
export const provider: Provider = { | ||
provide: OPEN_API_MAPPER, | ||
useValue: openApiRecordMapper, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Module } from '@nestjs/common' | ||
import { CqrsModule } from '@nestjs/cqrs' | ||
import { TableModule } from '../modules/table/table.module.js' | ||
import { OpenAPIDocController } from './openapi-doc.controller.js' | ||
import { OpenAPIDocService } from './openapi-doc.service.js' | ||
import { OpenAPIController } from './openapi.controller.js' | ||
import { provider as mapper } from './openapi.mapper.js' | ||
import { OpenAPIService } from './openapi.service.js' | ||
|
||
@Module({ | ||
imports: [TableModule, CqrsModule], | ||
controllers: [OpenAPIDocController, OpenAPIController], | ||
providers: [OpenAPIDocService, OpenAPIService, mapper], | ||
}) | ||
export class OpenAPIModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import type { IQueryRecordSchema, ITableRepository } from '@undb/core' | ||
import type { IOpenApiRecordMapper } from '@undb/openapi' | ||
import { InjectTableReposiory } from '../modules/table/adapters/index.js' | ||
import { InjectOpenAPIMapper } from './openapi.mapper.js' | ||
|
||
@Injectable() | ||
export class OpenAPIService { | ||
constructor( | ||
@InjectOpenAPIMapper() | ||
private readonly mapper: IOpenApiRecordMapper, | ||
@InjectTableReposiory() | ||
private readonly repo: ITableRepository, | ||
) {} | ||
|
||
public async mapMany(tableId: string, records: IQueryRecordSchema[]) { | ||
const table = (await this.repo.findOneById(tableId)).unwrap() | ||
const fields = table.schema.fields | ||
|
||
return records.map((record) => this.mapper(fields, record)) | ||
} | ||
|
||
public async map(tableId: string, record: IQueryRecordSchema) { | ||
const table = (await this.repo.findOneById(tableId)).unwrap() | ||
const fields = table.schema.fields | ||
|
||
return this.mapper(fields, record) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.