From cf58d1ed56c671d0ee077dfecc286002b4bae5ed Mon Sep 17 00:00:00 2001 From: Chris Swithinbank Date: Fri, 26 Apr 2024 16:07:55 +0200 Subject: [PATCH] Improve `asDrizzleTable()` types (#10882) --- .changeset/angry-lemons-tie.md | 7 +++++++ packages/db/src/runtime/index.ts | 2 +- packages/db/src/utils.ts | 11 ++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 .changeset/angry-lemons-tie.md diff --git a/.changeset/angry-lemons-tie.md b/.changeset/angry-lemons-tie.md new file mode 100644 index 0000000000..91e0978f49 --- /dev/null +++ b/.changeset/angry-lemons-tie.md @@ -0,0 +1,7 @@ +--- +"@astrojs/db": patch +--- + +Improves the typing of the `asDrizzleTable()` utility + +Fixes a type error when passing the output of `defineTable()` to the utility and returns a more detailed type inferred from the columns of the passed table config. diff --git a/packages/db/src/runtime/index.ts b/packages/db/src/runtime/index.ts index 67dea935af..06d08a8793 100644 --- a/packages/db/src/runtime/index.ts +++ b/packages/db/src/runtime/index.ts @@ -8,7 +8,7 @@ import { sqliteTable, text, } from 'drizzle-orm/sqlite-core'; -import { type DBColumn, type DBTable } from '../core/types.js'; +import type { DBColumn, DBTable } from '../core/types.js'; import { type SerializedSQL, isSerializedSQL } from './types.js'; import { pathToFileURL } from './utils.js'; diff --git a/packages/db/src/utils.ts b/packages/db/src/utils.ts index 4e1a18685e..7a98cca35c 100644 --- a/packages/db/src/utils.ts +++ b/packages/db/src/utils.ts @@ -1,2 +1,11 @@ export { defineDbIntegration } from './core/utils.js'; -export { asDrizzleTable } from './runtime/index.js'; +import { tableSchema } from './core/schemas.js'; +import type { ColumnsConfig, TableConfig } from './core/types.js'; +import { type Table, asDrizzleTable as internal_asDrizzleTable } from './runtime/index.js'; + +export function asDrizzleTable< + TableName extends string = string, + TColumns extends ColumnsConfig = ColumnsConfig, +>(name: TableName, tableConfig: TableConfig) { + return internal_asDrizzleTable(name, tableSchema.parse(tableConfig)) as Table; +}