How to Use the Prisma Kysely Extension

Sep 11, 2023 | Programming

Writing and maintaining raw SQL queries for Prisma can often feel like trying to read a foreign language—tedious and filled with potential pitfalls. Thankfully, the Prisma Kysely Extension comes to the rescue! This powerful tool allows you to write raw SQL queries with the benefits of type-safety and autocompletion, making your development experience smoother and more efficient.

What’s the Prisma Kysely Extension?

The Prisma Kysely Extension is like a friendly translator that helps you communicate with your database without losing the context and structure that Prisma offers. It seamlessly integrates Kysely’s functionality with Prisma, allowing you to use all your favorite Kysely plugins while writing SQL queries confidently.

Let’s break down how to set up and use this cool extension!

Getting Started

Ready to dive in? Follow these steps to use prisma-extension-kysely:

  1. Create a new project or open an existing one.
  2. Install the necessary dependencies:
  3. npm install prisma-extension-kysely kysely
  4. Set up prisma-kysely:
  5. npm install -D prisma-kysely
  6. Add this to your schema.prisma:
  7. generator kysely {
      provider = "prisma-kysely"
    }
  8. Generate the types:
  9. npx prisma generate

Extending Your Prisma Client

Next, let’s extend your Prisma Client to include Kysely. Imagine your Prisma Client is a Swiss Army knife; we’re simply adding another handy tool to it.

import kyselyExtension from "prisma-extension-kysely";
import type DB from ".prisma/generated/types";

const prisma = new PrismaClient().$extends(
  kyselyExtension({
    kysely: (driver) =>
      new KyselyDB({
        dialect: "YourDatabaseDialect",
        createDriver: () => driver,
        createAdapter: () => new PostgresAdapter(),
        createIntrospector: (db) => new PostgresIntrospector(db),
        createQueryCompiler: () => new PostgresQueryCompiler(),
        plugins: [
          // Add your favorite plugins here!
        ],
      }),
  }),
);

Writing Raw SQL Queries

Now that your Prisma Client is extended, you can write raw SQL queries with type safety. Think of this like assembling a Lego set, where each piece fits perfectly and connects to the others.

const query = prisma.$kysely
  .selectFrom("User")
  .selectAll()
  .where("id", "=", id);

const result = await query.execute();

Handling Transactions

Prisma’s interactive transactions are fully supported! Use tx.$kysely instead of prisma.$kysely to ensure everything runs smoothly.

await prisma.$transaction(async (tx) => {
  await tx.$kysely
    .insertInto("User")
    .values({ id: 1, name: "John Doe" })
    .execute();

  await tx.$kysely
    .insertInto("User")
    .values({ id: 2, name: "Jane Doe" })
    .execute();
});

Troubleshooting

If you run into issues while using the Prisma Kysely Extension, here are some troubleshooting tips:

  • Ensure all dependencies are installed and up to date.
  • Check your schema.prisma for correct generator configuration.
  • Verify the database connection settings are correct.
  • If you receive errors about queries, double-check your Kysely code for accuracy.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox