npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

prisma-extension-kysely2

v2.1.0-5

Published

Prisma extension for Kysely

Downloads

1,340

Readme

Prisma Kysely Extension 2

This is forked version of — prisma-extension-kysely

My target just want to use native kysely transaction and extend client without any args

npm version npm downloads GitHub license Node.js CI Node.js Package codecov Maintainability

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-kysely2 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-kysely2 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

Features

  • Type-safe — Write raw SQL queries in a type-safe manner with kysely
  • Seamless integration — Use kysely queries with Prisma as if they were native
  • Autocompletion — Get autocompletion for your queries in your IDE
  • Type inference — Get type inference for your queries in your IDE

Get started

Click the Use this template button and provide details for your Client extension

Install the dependencies:

npm install prisma-extension-kysely2 kysely

Set up the excellent prisma-kysely library to automatically generate types for your database:

npm install -D prisma-kysely

Add prisma-kysely as a generator to your schema.prisma:

generator kysely {
  provider = "prisma-kysely"
}

Generate the types:

npx prisma generate

Extend your Prisma Client:

import prismaKysely from "prisma-extension-kysely2";
import type { DB } from "./prisma/generated/types";

// Default with types
const prisma = new PrismaClient().$extends(prismaKysely<DB>());

// Custom dialect,plugins,options
const prisma = new PrismaClient().$extends(
  prismaKysely({
    dialect: {
      // This is where the magic happens!
      createDriver: () => driver,
      // Don't forget to customize these to match your database!
      createAdapter: () => new PostgresAdapter(),
      createIntrospector: (db) => new PostgresIntrospector(db),
      createQueryCompiler: () => new PostgresQueryCompiler(),
    },
    plugins: [
      // Add your favorite plugins here!
    ],
  }),
);

It's that simple! Now you can write raw SQL queries with kysely and use them with Prisma:

// Replace this...
const result = prisma.$queryRaw`SELECT * FROM User WHERE id = ${id}`;

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

// Thanks to kysely's magic, everything is type-safe!
const result = await query.execute();

// You can also execute queries without fetching the results
await prisma.$kysely.deleteFrom("User").where("id", "=", id).execute();

Transactions

Prisma's interactive transactions are fully supported by prisma-extension-kysely2! Just remeber to use tx.$kysely instead of prisma.$kysely, and you're good to go:

await prisma.$kysely.transaction().execute(async (trx) => {});

// Can use both

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();
});

Plugins

Do you love Kysely's plugins? So do we! You can use them with prisma-extension-kysely2 as well:

const prisma = new PrismaClient().$extends(
  prismaKysely<DB>({
    dialect: {
      createDriver: () => driver,
      createAdapter: () => new PostgresAdapter(),
      createIntrospector: (db) => new PostgresIntrospector(db),
      createQueryCompiler: () => new PostgresQueryCompiler(),
    },
    // Use your favorite plugins!
    plugins: [new CamelCasePlugin()],
  }),
);

If you're using the CamelCasePlugin, don't forget to add the camelCase option to your Prisma schema too:

generator kysely {
  provider = "prisma-kysely"
  camelCase = true
}

Take a look at the camel case example to see it in action! Check out the Kysely documentation for more information about plugins.

Examples

Check out the examples directory for a sample project!

cd examples/basic
npm install
npx prisma db push
npm run dev

Learn more

License

This project is licensed under the MIT License - see the LICENSE file for details.