K
Kysely2y ago
ohmi

Running database agnostic queries (MySQL)

As part of my test suite, I drop/create databases programmatically. Is it possible to have a Kysely connection that isn't attached to a specific database, so that I could run queries like DROP DATABASE a or CREATE DATABASE a? Other similar use cases include: RESET QUERY CACHE, SELECT 1 (for latency check), etc
4 Replies
Gaspero
Gaspero2y ago
Take a look at migration section of docs https://kysely.dev/docs/migrations Seems like you could use such approach as a workaround to write database structure agnostic queries . async function functionName(db: Kysely<any>): Promise<void> { // database agnostic query } But you would still need to initialize driver for each specific database connection.
Migrations | Kysely
Migration files should look like this:
ohmi
ohmiOP2y ago
is there a specific part you're hinting at? i think db.schema relies on a connection to an existing database, so CREATE/DROP DATABASE wouldnt work and i dont see any methods on the Kysely instance that seems to allow for arbitrary sql statements?
Gaspero
Gaspero2y ago
Ah. I'm sorry, I thought the question was about create/drop table. If database connection has sufficient permissions may be you could solve this by running raw sql? https://kysely-org.github.io/kysely/interfaces/Sql.html E.g.
const result = await sql<void>`create database db_name'.execute(db)
const result = await sql<void>`create database db_name'.execute(db)
Sql | kysely
Documentation for kysely
ohmi
ohmiOP2y ago
ah awesome thanks! i had looked over a similar page i had thought the sql template stings could only be passed into builder functions
Want results from more Discord servers?
Add your server