K
Kysely11mo ago
bvkimball

Feature/Question: Would like to execute query with Prisma.$queryRaw or $executeRaw

I use prisma-kysely to generate my kysely types and i find this to be a great workflow. I still like using prisma for most of my usecase because I have other implementations to enforce security and business logic. I find it unnecessary to include pg module if i already have a query-executor and a connection with prisma. It would be nice to do something like the following: instead of:
const db = new Kysely<DB>({
dialect: new PostgresDialect({
pool: new Pool({
connectionString: DATABASE_URL,
}),
}),
});
const result = await db.selectFrom('person').selectAll().execute()
const db = new Kysely<DB>({
dialect: new PostgresDialect({
pool: new Pool({
connectionString: DATABASE_URL,
}),
}),
});
const result = await db.selectFrom('person').selectAll().execute()
Use the dialect without a connection or pool:
const db = new Kysely<DB>({
dialect: new PostgresDialect(),
}),
const result = await prisma.$queryRaw(db.selectFrom('person').selectAll().compile())
const db = new Kysely<DB>({
dialect: new PostgresDialect(),
}),
const result = await prisma.$queryRaw(db.selectFrom('person').selectAll().compile())
Use the dialect passing a custom runner:
const db = new Kysely<DB>({
dialect: new PostgresDialect({ runner: prisma }),
}),
const result = await db.selectFrom('person').selectAll().execute()
const db = new Kysely<DB>({
dialect: new PostgresDialect({ runner: prisma }),
}),
const result = await db.selectFrom('person').selectAll().execute()
9 Replies
koskimas
koskimas11mo ago
You could also implement a custom Driver that uses the prisma instance. The Driver interface is pretty simple. You can get inspiration from the built-in and 3rd party dialects.
Igal
Igal11mo ago
A prisma dialect is actually a great idea, since many of our users use Kysely with prisma-kysely, and if cold starts are not an issue, could call prisma directly from kysely executes.
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
Igal
Igal11mo ago
Like this
prisma.$queryRawUnsafe(
compiledQuery.sql,
...compiledQuery.parameters
)
prisma.$queryRawUnsafe(
compiledQuery.sql,
...compiledQuery.parameters
)
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
koskimas
koskimas4mo ago
Every query builder not written by complete morons take care of parameterizing inputs. If you doubt we haven't considered SQL injection, or don't know what it is, you should stay far far away from us. However, if you explicitly use unsafe SQL functions like sql.lit or sql.raw, everything's possible. Another way to think about this: Kysely uses similar code internally to execute the queries. 1. The query is compiled into a CompiledQuery instance 2. The compiled sql and parameters are given to the underlying DB driver You can easily try stuff and see the generated SQL here https://kyse.link
koskimas
koskimas4mo ago
Unsafe functions only exist under the sql object. Each unsafe function has this warning when you hover over it
No description
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server
More Posts