diff options
-rw-r--r-- | .changeset/angry-lemons-tie.md | 7 | ||||
-rw-r--r-- | packages/db/src/runtime/index.ts | 2 | ||||
-rw-r--r-- | packages/db/src/utils.ts | 11 |
3 files changed, 18 insertions, 2 deletions
diff --git a/.changeset/angry-lemons-tie.md b/.changeset/angry-lemons-tie.md new file mode 100644 index 000000000..91e0978f4 --- /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 67dea935a..06d08a879 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 4e1a18685..7a98cca35 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<TColumns>) { + return internal_asDrizzleTable(name, tableSchema.parse(tableConfig)) as Table<TableName, TColumns>; +} |