drizzle-team / drizzle-team/drizzle-orm
[FEATURE]: Drizzle Prisma integration with no restrictions (Solution proposal)
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Describe what you want
Hello. recently I have seen your latest work `drizzle-prisma-generator` and `drizzle-orm/prisma/pg`, so much appreciate these, they are gorgeous.
The issue is not a really feature request, but sharing a solution, that I found out. The solution allows to use Drizzle inside Prisma without no restrictions described in the current documentation.
So here is the solution (PostgreSQL only, did not investigate for others):
There is experimental Prisma feature [node-postgres adapter for prisma](https://github.com/prisma/prisma/tree/main/packages/adapter-pg) which allows to use `pg` pool instead of native Prisma PostgreSQL adapter:
```prisma
// schema.prisma
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}
generator drizzle {
provider = "drizzle-prisma-generator"
output = "./drizzle"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
```
Once we have added the feature flag to our schema, re-generate Prisma Client:
```
npx prisma generate
```
Than we need to install required dependencies
```
npm install pg @prisma/adapter-pg drizzle-orm
npm install -D drizzle-prisma-generator @types/pg
```
And the TS code:
```typescript
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "@prisma/client";
import { drizzle as pgDrizzle } from "drizzle-orm/node-postgres";
import { NodePgDatabase } from "drizzle-orm/node-postgres/driver";
import { Pool } from "pg";
import { Prisma } from ".prisma/client";
export class PrismaDrizzleClient<
ClientOptions extends Prisma.PrismaClientOptions = Prisma.PrismaClientOptions,
> extends PrismaClient {
private readonly pgPool: Pool;
public readonly $drizzle: NodePgDatabase;
constructor(
options?: Prisma.Subset,
) {
const connectionString = `${process.env.DATABASE_URL}`;
const pool = new Pool({ connectionString });
const adapter = new PrismaPg(pool);
super({
...options,
adapter,
});
this.pgPool = pool;
this.$drizzle = pgDrizzle(pool);
}
getDrizzleWithSchema>(
schema: Schema,
): NodePgDatabase {
return pgDrizzle(this.pgPool, { schema });
}
}
```
So now we are sharing the same connection pool between Prisma and Drizzle, and are able to use Drizzle without any restrictions that native Prisma driver made before.
Contributor guide
Assessment
This issue has not been assessed yet.