K
Kysely

help

Running database agnostic queries (MySQL)

Oohmi5/19/2023
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
GGaspero5/21/2023
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:
Oohmi5/21/2023
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?
GGaspero5/21/2023
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
Oohmi5/21/2023
ah awesome thanks! i had looked over a similar page i had thought the sql template stings could only be passed into builder functions

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Asserting type of .countAll() (MySQL)I've noticed that the return type from the result of aggregation functions like `countAll()` and `suCoalesce return empty arrayHey is there a way to return an empty array rather than `null` when an array column is empty? I alrkysely-codegen for multiple databasesI have two MySQL 'databases' within the same instance. From the documentation, it doesn't look like Using MySQL functions in SELECT statementHi! Just trying to migrate over from Knex. Skimmed over the documentation but still unsure of how tExtract OrderBy TS KeysHey I have the following query ```ts const getBaseQuery = ({ offset, pageSize, slug }: GetBaseQuerinsert into with mix of static and table valuesHow might I execute an insert into that combines JS-side values with a select, like: ``` INSERT INTOMigration error "TypeError: Cannot read properties of undefined (reading 'getExecutor')"I am trying to run a migration using Kysely, and its returning this error: ``` file:///Users/brunocrwhere clause with lengthHow do I have a where clause with a length, e.g. `select * from data where length(prodcode) = 3`show generated sqlhow can i see what the generated sql is for a kysely query? e.g. ``` result = await db.selectFrom("Property does not exist on typeHello. I'm trying to run the following query: ```javascript result = await db.selectFrom("data") Correct type definitions for function receiving builderI have something like this and I was wondering if it's the correct way to type my helper function. Transform Postgres array into JS arrayHey, I was wondering if there's a helper or something to transform Postgres arrays `{one, two}` intoNoob Question: SQL INSERT that combines static values with a SELECT statementHey Kysely community! I'm just getting into Kysely and I have a question about how I could run the fIs there a way to execute an ExpressionBuilder?Using the expression hasDogNamed example from the docs ``` const eb = expressionBuilder<DB, 'personArgument of type 'string' is not assignable to parameter of type 'DynamicReferenceBuilder<never>'.The following statement, is giving me the error on the title ```ts fn("round", [fn.min("ld.price_btcHow do Date columns works?I am learning Kysely from the website, at the getting started section I can see the following line: