drizzle-team / drizzle-team/drizzle-orm
[BUG]: TypeScript Error when using sqlite in development and D1 in production
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### What version of `drizzle-orm` are you using?
0.29.0
### What version of `drizzle-kit` are you using?
0.20.0
### Describe the Bug
When initilizing drizzle the following way:
```ts
import { drizzle as drizzleD1, DrizzleD1Database } from 'drizzle-orm/d1'
import { drizzle, BetterSQLite3Database } from 'drizzle-orm/better-sqlite3'
// @ts-ignore
import Database from 'better-sqlite3'
import { join } from 'pathe'
export * as tables from '~/server/db/schema'
import * as schema from '~/server/db/schema'
let _db: DrizzleD1Database | BetterSQLite3Database | null = null
export const db = new Database(join(process.cwd(), '/db.sqlite'))
export const useDB = () => {
if(!_db) {
if (process.env.DB) {
// d1 in production
_db = drizzleD1(process.env.DB, {schema})
return _db
} else if (process.dev) {
_db = drizzle(db, {schema})
return _db
} else {
throw new Error('No database configured for production')
}
} else return _db
}
```
and then using drizzle with partial select like the following:
```ts
return useDB().select({
id: job.id,
slug: job.slug,
title: job.title,
type: job.type,
location: job.location,
company: company.name,
closedAt: job.closedAt,
}).from(job)
.innerJoin(savedJobsForUser, eq(savedJobsForUser.jobId, job.id))
.innerJoin(company, eq(company.id, job.company_id))
.where(eq(savedJobsForUser.userId, userId))
.all()
```
i get the following TS Error: `Expected 0 arguments, but got 1.` for the select part of the query.
This is the type of the select:
```
BaseSQLiteDatabase = Record, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations>.select(): SQLiteSelectBuilder | SQLiteSelectBuilder
```
### Expected behavior
That there should be no TS error and the types can be inferred from the query.
### Environment & setup
Nuxt 3.7.3 with Nitro 2.6.3
better-sqlite3: 8.4.0,
Contributor guide
Research direction
Reproduce the TypeScript error from the useDB() setup and partial select example, using the listed drizzle-orm and drizzle-kit versions. Start by tracing the BaseSQLiteDatabase.select() types for drizzle-orm/d1 and better-sqlite3, then compile the example to verify that the query accepts its selected fields and infers the result type without the error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- sqlite, typescript
- Domain
- database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100