Giter Site home page Giter Site logo

victoronl / prisma-extension-kysely Goto Github PK

View Code? Open in Web Editor NEW

This project forked from eoin-obrien/prisma-extension-kysely

0.0 0.0 0.0 584 KB

Drop down to raw SQL in Prisma without sacrificing type safety!

Home Page: https://www.npmjs.com/package/prisma-extension-kysely

License: MIT License

Shell 0.59% JavaScript 6.75% TypeScript 92.66%

prisma-extension-kysely's Introduction

Prisma Kysely Extension

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

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-kysely 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 kyselyExtension from "prisma-extension-kysely";
import type { DB } from "./prisma/generated/types";

const prisma = new PrismaClient().$extends(
  kyselyExtension({
    kysely: (driver) =>
      new Kysely<DB>({
        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-kysely! Just remeber to use tx.$kysely instead of prisma.$kysely, and you're good to go:

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

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.

// Don't do this! Prefer prisma.$transaction instead.
await prisma.$kysely.transaction().execute(async (trx) => {});

Plugins

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

const prisma = new PrismaClient().$extends(
  kyselyExtension({
    kysely: (driver) =>
      new Kysely<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.

prisma-extension-kysely's People

Contributors

eoin-obrien avatar release-please[bot] avatar renovate[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.