TypeError: client.unsafe is not a function

I was attempting to run tests in a different monorepo folder (my trpc folder) than my core folder where drizzle is and ran into this . I am spinning up temp docker containers.

It works in my core folder just fine but I can't figure out why i am getting this issue. Perhaps a drizzle bug?

describe('Tests', () => {
    let container: StartedPostgreSqlContainer
    let db: any
    beforeAll(async () => {
        const setup = await setupDockerTestDb({ logger: false }) // <--- ERROR HERE
        container = setup.container
        db = setup.db
    })
    afterAll(async () => {
        await container.stop()
    })
    it('should work', () => {})
})


export async function setupDockerTestDb({ logger = false } = {}) {
    const container = await new PostgreSqlContainer('pg_uuidv7')
        .withEnvironment({
            POSTGRES_USER: POSTGRES_USER,
            POSTGRES_PASSWORD: POSTGRES_PASSWORD,
            POSTGRES_DB: POSTGRES_DB,
        })
        .start()

    const connectionString = ...
    const client = postgres(connectionString)
    const db: PostgresJsDatabase<typeof schema> = drizzle(client, {
        logger: logger,
    })

    await db.execute(sql`CREATE EXTENSION IF NOT EXISTS pg_uuidv7`)
    const migrationPath = path.join(process.cwd(), 'src/db/migrations')

    try {
        // Set client_min_messages to WARNING to suppress NOTICE messages
        await db.execute(sql`SET client_min_messages TO WARNING`)
        await migrate(db, {
            migrationsFolder: migrationPath,
        })
    } catch (error) {
        console.error('An error occurred during migration:', error)
        // Handle the error appropriately, e.g., by re-throwing it or exiting the process
        throw error
    } finally {
        // Reset client_min_messages to its default value
        await db.execute(sql`RESET client_min_messages`)
    }

    // Populate Database
    await up(db)

    return { container, db, confirmDatabaseReady, client }
}
image.png
Was this page helpful?