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', () => {})
})
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 }
}
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 }
}
No description
5 Replies
DiamondDragon
DiamondDragon12mo ago
Ah @a_sherman the error came from mocking the database in the schema folder as described in your blog post
No description
Angelelz
Angelelz12mo ago
You cannot run queries against a mocked database 🤦‍♂️
DiamondDragon
DiamondDragon12mo ago
I’m not importing ‘mDb’ anywhere in my project. But once removing it the error goes away. It felt like from the post this was a way of testing your queries but seems perhaps the drizzle function is being executed when your schema file is imported elsewhere in the project and erroring since no no client is passed to it
Andrii Sherman
Andrii Sherman12mo ago
no, in the post I was explaning how to get typed database instance, just to prototype your database calls and decide on a schema design before testing it on a real database with a real client connection I thought I explained it there, maybe I need to edit my post, so it will be more clear
DiamondDragon
DiamondDragon12mo ago
Yeah it could be more clear that the typed instance needs to be deleted when the file is being used by real connections or else it will error out. Although I assume you could just have the mock instance in a separate file and leave it there
Want results from more Discord servers?
Add your server