Skip to content

Commit

Permalink
fix: fix openapi readable values
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Aug 17, 2024
1 parent 3a90e84 commit 46113d0
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { singleton } from "@undb/di"
import { env } from "@undb/env"
import { IObjectStorage, IPresign, IPutObject } from "@undb/table"
import { nanoid } from "nanoid"
import * as path from "node:path"
Expand All @@ -22,7 +23,7 @@ export class LocalObjectStorage implements IObjectStorage {
}
}
async getPreviewUrl(fileName: string): Promise<string> {
return "/public/" + fileName
return env.UNDB_BASE_URL + "/public/" + fileName
}
async put(buffer: Buffer, path: string, originalname: string, mimeType: string): Promise<IPutObject> {
await Bun.write(`./.undb/storage/${path ? path + "/" : ""}${originalname}`, buffer)
Expand Down
17 changes: 11 additions & 6 deletions apps/backend/src/modules/openapi/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import {
import { executionContext, getCurrentUserId, setContextValue } from "@undb/context/server"
import { CommandBus, QueryBus } from "@undb/cqrs"
import { inject, singleton } from "@undb/di"
import { type ICommandBus, type IQueryBus, None, PaginatedDTO, Some } from "@undb/domain"
import { type ICommandBus, type IQueryBus, PaginatedDTO, Some } from "@undb/domain"
import { createLogger } from "@undb/logger"
import { API_TOKEN_HEADER_NAME, createOpenApiSpec, type IApiTokenService, injectApiTokenService } from "@undb/openapi"
import { injectQueryBuilder, type IQueryBuilder } from "@undb/persistence"
import { GetReadableRecordByIdQuery, GetReadableRecordsQuery } from "@undb/queries"
import { injectSpaceService, type ISpaceService } from "@undb/space"
import {
injectRecordRepository,
injectRecordsQueryService,
injectTableRepository,
type IRecordReadableValueDTO,
type IRecordRepository,
type IRecordsQueryService,
type ITableRepository,
withUniqueTable,
} from "@undb/table"
Expand All @@ -40,8 +40,8 @@ export class OpenAPI {
private readonly baseRepo: IBaseRepository,
@injectTableRepository()
private readonly repo: ITableRepository,
@injectRecordRepository()
private readonly recordRepo: IRecordRepository,
@injectRecordsQueryService()
private readonly recordsQueryService: IRecordsQueryService,

@inject(QueryBus)
private readonly queryBus: IQueryBus,
Expand Down Expand Up @@ -82,7 +82,12 @@ export class OpenAPI {
const ts = withUniqueTable({ baseName, tableName }).unwrap()
const table = (await this.repo.findOne(Some(ts))).expect("Table not found")
const base = (await this.baseRepo.findOneById(table.baseId)).expect("Base not found")
const record = (await this.recordRepo.findOne(table, None)).into(undefined)
const record = (
await this.recordsQueryService.getReadableRecords({
tableId: table.id.value,
pagination: { limit: 1 },
})
).values.at(0)

const spec = createOpenApiSpec(base, table, record)

Expand Down
4 changes: 2 additions & 2 deletions packages/openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { z } from "@undb/zod"
extendZodWithOpenApi(z)

import type { Base } from "@undb/base"
import type { RecordDO, TableDo } from "@undb/table"
import type { IReadableRecordDTO, TableDo } from "@undb/table"
import {
RECORD_COMPONENT,
bulkDeleteRecords,
Expand All @@ -28,7 +28,7 @@ import {

export const API_TOKEN_HEADER_NAME = "x-undb-api-token"

export const createOpenApiSpec = (base: Base, table: TableDo, record?: RecordDO) => {
export const createOpenApiSpec = (base: Base, table: TableDo, record?: IReadableRecordDTO) => {
const registry = new OpenAPIRegistry()

const recordSchema = createRecordComponent(table, record)
Expand Down
25 changes: 6 additions & 19 deletions packages/openapi/src/openapi/record.openapi.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
import type { RouteConfig } from "@asteasolutions/zod-to-openapi"
import type { Base } from "@undb/base"
import { RecordDO, recordId, type TableDo } from "@undb/table"
import { recordId, type IReadableRecordDTO, type TableDo } from "@undb/table"
import { z, type ZodTypeAny } from "@undb/zod"
import { objectify } from "radash"

export const RECORD_ID_COMPONENT = "RecordId"
export const RECORD_COMPONENT = "Record"
export const RECORD_VALUES_COMPONENT = "RecordValues"
export const RECORD_DISPLAY_VALUES_COMPONENT = "RecordDisplayValues"

export const createRecordComponent = (table: TableDo, record?: RecordDO) => {
const fields = table.schema.fields
const displayFields = table.schema.getFieldsHasDisplayValue()
export const createRecordComponent = (table: TableDo, record?: IReadableRecordDTO) => {
const schema = table.schema.readableSchema
const displayScheam = table.schema.displayValuesSchema

const example = record
? objectify(
fields,
(f) => f.name.value,
(f) => record?.getValue(f.id).into(undefined)?.value,
)
: undefined
const example = record?.values
const displayExample = record?.displayValues

const displayExample = record
? objectify(
displayFields,
(f) => f.name.value,
(f) => record.getDisplayValueByField(f.id)?.into(undefined),
)
: undefined
return z
.object({
id: recordId.openapi(RECORD_ID_COMPONENT, { example: record?.id }),
values: schema.openapi(RECORD_VALUES_COMPONENT, { example }),
displayValues: displayScheam.openapi(RECORD_DISPLAY_VALUES_COMPONENT, { example: displayExample }),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const recordReadableDisplayValueDTO = z.record(fieldName, z.any())
export type IRecordReadableDisplayValueDTO = z.infer<typeof recordReadableDisplayValueDTO>

export const readableRecordDTO = z.object({
id: recordId,
values: recordReadableValueDTO,
displayValues: recordReadableDisplayValueDTO.optional(),
})
Expand Down
1 change: 1 addition & 0 deletions packages/table/src/modules/records/record/record.do.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class RecordDO extends AggregateRoot<IRecordEvent> {
const values = this.values.toReadable(table, fields)
const displayValues = this.displayValues?.toReadable(table, fields)
return {
id: this.id.value,
values,
displayValues,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ import { None, Some, type PaginatedDTO } from "@undb/domain"
import { withUniqueTable } from "../../../../specifications/table-name.specification"
import { ViewIdVo } from "../../../views"
import type { IGetRecordsDTO } from "../../dto"
import { buildQuery, type IRecordReadableValueDTO } from "../../record"
import { buildQuery, type IReadableRecordDTO } from "../../record"
import { recordsToReadable } from "../../record.util"
import type { RecordsQueryService } from "../records.query-service"

export async function getReadableRecords(
this: RecordsQueryService,
dto: IGetRecordsDTO,
): Promise<PaginatedDTO<IRecordReadableValueDTO>> {
): Promise<PaginatedDTO<IReadableRecordDTO>> {
const spec = withUniqueTable(dto).expect("Invalid unique table specification")
const table = (await this.tableRepository.findOne(Some(spec))).expect("Table not found")
const viewId = dto.viewId ? Some(new ViewIdVo(dto.viewId)) : None

const query = buildQuery(table, dto)
const data = await this.repo.find(table, viewId, query)
const readable = recordsToReadable(table, data.values)
const values = await this.populateAttachments({}, table, data.values)
const readable = recordsToReadable(table, values)
return {
total: data.total,
values: readable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { injectObjectStorage, type IObjectStorage } from "../../storage"
import type { AggregateResult, ICountRecordsDTO, IGetAggregatesDTO, IGetRecordByIdDTO, IGetRecordsDTO } from "../dto"
import {
injectRecordQueryRepository,
type IReadableRecordDTO,
type IRecordDTO,
type IRecordQueryRepository,
type IRecordReadableValueDTO,
Expand All @@ -24,7 +25,7 @@ export interface IRecordsQueryService {
getRecords(query: IGetRecordsDTO): Promise<PaginatedDTO<IRecordDTO>>
countRecords(query: ICountRecordsDTO): Promise<number>
getRecordById(query: IGetRecordByIdDTO): Promise<Option<IRecordDTO>>
getReadableRecords(query: IGetRecordsDTO): Promise<PaginatedDTO<IRecordReadableValueDTO>>
getReadableRecords(query: IGetRecordsDTO): Promise<PaginatedDTO<IReadableRecordDTO>>
getReadableRecordById(query: IGetRecordByIdDTO): Promise<Option<IRecordReadableValueDTO>>
getAggregates(query: IGetAggregatesDTO): Promise<Record<string, AggregateResult>>
populateAttachments(dto: IGetRecordsDTO, table: TableDo, records: IRecordDTO[]): Promise<IRecordDTO[]>
Expand Down

0 comments on commit 46113d0

Please sign in to comment.