Type for schema when creating db client

import { BetterSQLite3Database, drizzle } from 'drizzle-orm/better-sqlite3'

import { drizzle as drizzleLibSQL, LibSQLDatabase } from 'drizzle-orm/libsql'

import * as schema from '~/server/database/schema'

let _db: BetterSQLite3Database | LibSQLDatabase | null = null

export const useDB = () => {
if (!_db) {
_db = drizzleLibSQL(
createLibSQLClient({
url: process.env.TURSO_DB_URL,
authToken: process.env.TURSO_DB_TOKEN
}),
{ schema }
)
import { BetterSQLite3Database, drizzle } from 'drizzle-orm/better-sqlite3'

import { drizzle as drizzleLibSQL, LibSQLDatabase } from 'drizzle-orm/libsql'

import * as schema from '~/server/database/schema'

let _db: BetterSQLite3Database | LibSQLDatabase | null = null

export const useDB = () => {
if (!_db) {
_db = drizzleLibSQL(
createLibSQLClient({
url: process.env.TURSO_DB_URL,
authToken: process.env.TURSO_DB_TOKEN
}),
{ schema }
)
I get the following typescript error whenever I try to include { schema } into my db creation: ``` Type 'LibSQLDatabase<typeof import("server/database/schema")>' is not assignable to type 'BetterSQLite3Database | LibSQLDatabase<Record<string, never>> | null'. Type 'LibSQLDatabase<typeof import("server/database/schema")>' is not assignable to type 'LibSQLDatabase<Record<string, never>>'. The types of '.schema' are incompatible between these types. Type 'ExtractTablesWithRelations<typeof import("server/database/schema")> | undefined' is not assignable to type 'ExtractTablesWithRelations<Record<string, never>> | undefined'. Type 'ExtractTablesWithRelations<typeof import("server/database/schema")>' is not assignable to type 'ExtractTablesWithRelations<Record<string, never>>'. Property 'todos' is incompatible with index signature. Type '{ tsName: "todos"; dbName: "todos"; columns: { id: SQLiteColumn<{ name: "id"; tableName: "todos"; dataType: "number"; columnType: "SQLiteInteger"; data: number; driverParam: number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, object>; userId: SQLiteColumn<...>; title: SQLiteColumn<....' is not assignable to type '{ tsName: string; dbName: never; columns: never; relations: Record<string, Relation<string>>; primaryKey: AnyColumn[]; }'. Types of property 'dbName' are incompatible. Type 'string' is not assignable to type 'never'.```
5 Replies
Titan
TitanOP10mo ago
My schema file matches the documentation and is a list of variable exports:
export const user = sqliteTable(
'app-user',
{
export const user = sqliteTable(
'app-user',
{
francis
francis10mo ago
@Titan you need to add the generic param to your let _db: BetterSQLite3Database | LibSQLDatabase definition, that's the actual problem your db variable is being defined without generics which means you're ending up with the default generic param of never effectively I believe it's DbType<typeof schema> but you'll have to look for yourself to confirm
Titan
TitanOP10mo ago
thanks for the pointers, no joy but I'll keep digging
francis
francis10mo ago
? did adding a generic to your declaration not solve it? since that's the actual problem the error is reporting that your runtime value can't be assigned to your variable because your variable type has no schema associated with it
Want results from more Discord servers?
Add your server