import { notionRequest, type JsonRecord } from './request.ts'
import { inputRecord, optionalString, requiredString } from './validation.ts'
type DatabaseResponse = {
id?: string
title?: unknown[]
data_sources?: Array<{ id?: string; name?: string }>
parent?: JsonRecord
is_inline?: boolean
in_trash?: boolean
url?: string
[key: string]: unknown
}
/**
* Read a Notion database container by database id: title and its data sources.
* The schema (properties) lives on the data source — follow up with
* get-data-source using one of the returned data source ids.
*/
export default async function getDatabase(params: Record<string, unknown>) {
const input = inputRecord(params)
const databaseId = requiredString(input, 'databaseId')
const result = await notionRequest<DatabaseResponse>(
'/databases/' + encodeURIComponent(databaseId),
{
account: optionalString(input, 'account'),
integration: optionalString(input, 'integration'),
},
)
return {
id: result.id ?? null,
title: result.title ?? [],
dataSources: (result.data_sources ?? []).map((source) => ({
id: source.id ?? null,
name: source.name ?? null,
})),
parent: result.parent ?? null,
isInline: result.is_inline === true,
inTrash: result.in_trash === true,
url: result.url ?? null,
}
}