Gathering detailed insights and metrics for prisma-extension-kysely
Gathering detailed insights and metrics for prisma-extension-kysely
Gathering detailed insights and metrics for prisma-extension-kysely
Gathering detailed insights and metrics for prisma-extension-kysely
Drop down to raw SQL in Prisma without sacrificing type safety!
npm install prisma-extension-kysely
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
210 Stars
279 Commits
5 Forks
2 Watching
1 Branches
1 Contributors
Updated on 26 Nov 2024
Minified
Minified + Gzipped
TypeScript (89.19%)
JavaScript (10.81%)
Cumulative downloads
Total Downloads
Last day
-20.3%
1,722
Compared to previous day
Last week
0.4%
10,836
Compared to previous week
Last month
18.2%
49,557
Compared to previous month
Last year
257,936.6%
415,439
Compared to previous year
1
23
Writing and maintaining raw SQL queries for Prisma can be a tedious and error-prone task. The moment you need to write a query that is not supported out-of-the-box by Prisma, you lose all of that type-safety and autocompletion. This is where prisma-extension-kysely
comes in! It allows you to easily write raw SQL queries in a type-safe manner with kysely
and integrate them seamlessly with Prisma.
And the best part? You can use all of your favorite kysely
plugins with prisma-extension-kysely
too!
You don't have to take our word for it, though:
I have to say, this is BY FAR the most amazing community package I've seen in the Prisma ecosystem!
It makes it so much more convenient to drop down to raw SQL when needed without sacrificing DX — best of both worlds! 🚀
— Nikolas Burk, DevRel @ Prisma
kysely
kysely
queries with Prisma as if they were nativeClick the Use this template button and provide details for your Client extension
Install the dependencies:
1npm install prisma-extension-kysely kysely
Set up the excellent prisma-kysely
library to automatically generate types for your database:
1npm install -D prisma-kysely
Add prisma-kysely
as a generator to your schema.prisma
:
1generator kysely { 2 provider = "prisma-kysely" 3}
Generate the types:
1npx prisma generate
Extend your Prisma Client:
1import kyselyExtension from "prisma-extension-kysely"; 2import type { DB } from "./prisma/generated/types"; 3 4const prisma = new PrismaClient().$extends( 5 kyselyExtension({ 6 kysely: (driver) => 7 new Kysely<DB>({ 8 dialect: { 9 // This is where the magic happens! 10 createDriver: () => driver, 11 // Don't forget to customize these to match your database! 12 createAdapter: () => new PostgresAdapter(), 13 createIntrospector: (db) => new PostgresIntrospector(db), 14 createQueryCompiler: () => new PostgresQueryCompiler(), 15 }, 16 plugins: [ 17 // Add your favorite plugins here! 18 ], 19 }), 20 }), 21);
It's that simple! Now you can write raw SQL queries with kysely
and use them with Prisma:
1// Replace this... 2const result = prisma.$queryRaw`SELECT * FROM User WHERE id = ${id}`; 3 4// With this! 5const query = prisma.$kysely 6 .selectFrom("User") 7 .selectAll() 8 .where("id", "=", id); 9 10// Thanks to kysely's magic, everything is type-safe! 11const result = await query.execute(); 12 13// You can also execute queries without fetching the results 14await prisma.$kysely.deleteFrom("User").where("id", "=", id).execute();
Prisma's interactive transactions are fully supported by prisma-extension-kysely
! Just remeber to use tx.$kysely
instead of prisma.$kysely
, and you're good to go:
1await prisma.$transaction(async (tx) => { 2 await tx.$kysely 3 .insertInto("User") 4 .values({ id: 1, name: "John Doe" }) 5 .execute(); 6 7 await tx.$kysely 8 .insertInto("User") 9 .values({ id: 2, name: "Jane Doe" }) 10 .execute(); 11});
Don't try to use Kysely's transaction
method directly, though. It's not supported by prisma-extension-kysely
, and it will throw an error if you try to use it.
1// Don't do this! Prefer prisma.$transaction instead. 2await prisma.$kysely.transaction().execute(async (trx) => {});
Do you love Kysely's plugins? So do we! You can use them with prisma-extension-kysely
as well:
1const prisma = new PrismaClient().$extends( 2 kyselyExtension({ 3 kysely: (driver) => 4 new Kysely<DB>({ 5 dialect: { 6 createDriver: () => driver, 7 createAdapter: () => new PostgresAdapter(), 8 createIntrospector: (db) => new PostgresIntrospector(db), 9 createQueryCompiler: () => new PostgresQueryCompiler(), 10 }, 11 // Use your favorite plugins! 12 plugins: [new CamelCasePlugin()], 13 }), 14 }), 15);
If you're using the CamelCasePlugin
, don't forget to add the camelCase
option to your Prisma schema too:
1generator kysely { 2 provider = "prisma-kysely" 3 camelCase = true 4}
Take a look at the camel case example to see it in action! Check out the Kysely documentation for more information about plugins.
Using read replicas with prisma-extension-kysely
is a breeze!
Just use the excellent @prisma/extension-read-replicas
extension as normal.
Pay attention to how it's configured, though:
1// Use a common config for primary and replica clients (or different configs) 2const kyselyExtensionArgs: PrismaKyselyExtensionArgs<DB> = { 3 kysely: (driver) => 4 new Kysely<DB>({ 5 dialect: { 6 createAdapter: () => new SqliteAdapter(), 7 createDriver: () => driver, 8 createIntrospector: (db) => new SqliteIntrospector(db), 9 createQueryCompiler: () => new SqliteQueryCompiler(), 10 }, 11 }), 12}; 13 14// Initialize the replica client(s) and add the Kysely extension 15const replicaClient = new PrismaClient({ 16 datasourceUrl: "YOUR_REPLICA_URL", // Replace this with your replica's URL! 17 log: [{ level: "query", emit: "event" }], 18}).$extends(kyselyExtension(kyselyExtensionArgs)); 19 20// Initialize the primary client and add the Kysely extension and the read replicas extension 21const prisma = new PrismaClient() 22 .$extends(kyselyExtension(kyselyExtensionArgs)) // Apply the Kysely extension before the read replicas extension! 23 .$extends( 24 readReplicas({ 25 replicas: [replicaClient], 26 }), 27 ); // Apply the read replicas extension after the Kysely extension!
See how we're setting up the replica client as a fully-fledged Prisma client and extending it separately? That's the secret sauce! It make sure that the replica client has a separate Kysely instance. If you try to use bare URLs, you'll run into trouble; it'll share the same Kysely instance as the primary client, and you'll get unpleasant surprises!
1// Don't do this! It won't work as expected. 2readReplicas({ 3 url: "postgresql://user:password@localhost:5432/dbname", 4});
Also, note that we're applying the Kysely extension before the read replicas extension. This is important! If you apply the read replicas extension first, you won't get .$kysely
on the primary client.
Check out the read replicas example for a runnable example!
Check out the examples directory for a sample project!
1cd examples/basic 2npm install 3npx prisma db push 4npm run dev
This project is licensed under the MIT License - see the LICENSE file for details.
No vulnerabilities found.
No security vulnerabilities found.