Skip to content

Commit

Permalink
Merge pull request #155 from eoin-obrien/duplicate-extension-error
Browse files Browse the repository at this point in the history
feat: throw error if Prisma client is already extended with Kysely
  • Loading branch information
eoin-obrien authored Oct 23, 2024
2 parents 72f8c56 + f826055 commit e05b789
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,29 @@ export type PrismaKyselyExtensionArgs<Database> = {
* The Kysely instance to provide to the Prisma client
*/
// kysely: Kysely<Database>;
kysely: (driver: PrismaDriver<any>) => Kysely<Database>;
kysely: (driver: PrismaDriver<unknown>) => Kysely<Database>;
};

export class PrismaKyselyExtensionError extends Error {
constructor(message: string) {
super(message);
this.name = "PrismaKyselyExtensionError";
}
}

/**
* Define a Prisma extension that adds Kysely query builder methods to the Prisma client
* @param extensionArgs The extension configuration object
*/
export default <Database>(extensionArgs: PrismaKyselyExtensionArgs<Database>) =>
Prisma.defineExtension((client) => {
// Check if the client is already extended
if ("$kysely" in client) {
throw new PrismaKyselyExtensionError(
"The Prisma client is already extended with Kysely",
);
}

const driver = new PrismaDriver(client);
const kysely = extensionArgs.kysely(driver);

Expand Down
18 changes: 18 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,24 @@ describe("prisma-extension-kysely", () => {
);
});

it("should throw an error if the Prisma client is already extended with Kysely", async () => {
expect(() =>
xprisma.$extends(
kyselyExtension({
kysely: (driver) =>
new Kysely<DB>({
dialect: {
createAdapter: () => new SqliteAdapter(),
createDriver: () => driver,
createIntrospector: (db) => new SqliteIntrospector(db),
createQueryCompiler: () => new SqliteQueryCompiler(),
},
}),
}),
),
).toThrow("The Prisma client is already extended with Kysely");
});

describe("@prisma/extension-read-replicas", () => {
const replica = new PrismaClient().$extends(
kyselyExtension({
Expand Down

0 comments on commit e05b789

Please sign in to comment.