Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP/POC support prisma 5.4 serverless driver adapters #8847

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/assets-local/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
},
"dependencies": {
"@keystone-6/core": "^5.0.0",
"@prisma/client": "^4.16.2"
"@prisma/client": "^5.4.0"
},
"devDependencies": {
"prisma": "^4.16.2",
"prisma": "^5.4.0",
"typescript": "~5.0.0"
}
}
4 changes: 2 additions & 2 deletions examples/assets-s3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
},
"dependencies": {
"@keystone-6/core": "^5.0.0",
"@prisma/client": "^4.16.2",
"@prisma/client": "^5.4.0",
"dotenv": "^16.0.0"
},
"devDependencies": {
"prisma": "^4.16.2",
"prisma": "^5.4.0",
"typescript": "~5.0.0"
}
}
4 changes: 2 additions & 2 deletions examples/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"dependencies": {
"@keystone-6/auth": "^7.0.0",
"@keystone-6/core": "^5.0.0",
"@prisma/client": "^4.16.2"
"@prisma/client": "^5.4.0"
},
"devDependencies": {
"prisma": "^4.16.2",
"prisma": "^5.4.0",
"typescript": "~5.0.0"
}
}
4 changes: 2 additions & 2 deletions examples/cloudinary/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
"dependencies": {
"@keystone-6/cloudinary": "workspace:^",
"@keystone-6/core": "^5.7.0",
"@prisma/client": "^4.16.2",
"@prisma/client": "^5.4.0",
"dotenv": "^16.0.0"
},
"devDependencies": {
"prisma": "^4.16.2",
"prisma": "^5.4.0",
"typescript": "~5.0.0"
}
}
4 changes: 2 additions & 2 deletions examples/custom-admin-ui-logo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
"dependencies": {
"@keystone-6/core": "^5.0.0",
"@keystone-ui/core": "^5.0.1",
"@prisma/client": "^4.16.2",
"@prisma/client": "^5.4.0",
"next": "^13.3.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"prisma": "^4.16.2",
"prisma": "^5.4.0",
"typescript": "~5.0.0"
}
}
4 changes: 2 additions & 2 deletions examples/custom-admin-ui-navigation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
},
"dependencies": {
"@keystone-6/core": "^5.0.0",
"@prisma/client": "^4.16.2",
"@prisma/client": "^5.4.0",
"react": "^18.2.0"
},
"devDependencies": {
"prisma": "^4.16.2",
"prisma": "^5.4.0",
"typescript": "~5.0.0"
}
}
4 changes: 2 additions & 2 deletions examples/custom-admin-ui-pages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
"dependencies": {
"@keystone-6/core": "^5.0.0",
"@keystone-ui/core": "^5.0.1",
"@prisma/client": "^4.16.2",
"@prisma/client": "^5.4.0",
"next": "^13.3.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"prisma": "^4.16.2",
"prisma": "^5.4.0",
"typescript": "~5.0.0"
}
}
4 changes: 2 additions & 2 deletions examples/custom-field-view/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
"@keystone-ui/core": "^5.0.1",
"@keystone-ui/fields": "^7.1.1",
"@keystone-ui/icons": "^6.0.1",
"@prisma/client": "^4.16.2",
"@prisma/client": "^5.4.0",
"next": "^13.3.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"prisma": "^4.16.2",
"prisma": "^5.4.0",
"typescript": "~5.0.0"
}
}
4 changes: 2 additions & 2 deletions examples/custom-field/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"dependencies": {
"@keystone-6/core": "^5.0.0",
"@keystone-ui/fields": "^7.1.1",
"@prisma/client": "^4.16.2"
"@prisma/client": "^5.4.0"
},
"devDependencies": {
"prisma": "^4.16.2",
"prisma": "^5.4.0",
"typescript": "~5.0.0"
}
}
4 changes: 2 additions & 2 deletions examples/custom-id/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"dependencies": {
"@keystone-6/core": "^5.0.0",
"@paralleldrive/cuid2": "^2.2.1",
"@prisma/client": "^4.16.2"
"@prisma/client": "^5.4.0"
},
"devDependencies": {
"prisma": "^4.16.2",
"prisma": "^5.4.0",
"tsx": "^3.9.0",
"typescript": "~5.0.0"
}
Expand Down
4 changes: 2 additions & 2 deletions examples/custom-output-paths/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
},
"dependencies": {
"@keystone-6/core": "^5.0.0",
"@prisma/client": "^4.16.2",
"@prisma/client": "^5.4.0",
"next": "^13.3.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"prisma": "^4.16.2",
"prisma": "^5.4.0",
"typescript": "~5.0.0"
}
}
28 changes: 28 additions & 0 deletions examples/custom-prisma-driver-adapter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## Example Project - Extend Prisma Schema

This project implements an example of mutating the Prisma Schema to:

- Set the Prisma Binary Target;
- Add a multi-column column unique constraint to a list; and
- Add a NOT NULL relationship field

These show three separate ways of mutating the Prisma Schema see `keystone.ts` and `schema.ts`.

## Instructions

To run this project, clone the Keystone repository locally, run `pnpm` at the root of this repository, then navigate to this directory and run:

```shell
pnpm dev
```

This will start the Admin UI at [localhost:3000](http://localhost:3000).
You can use the Admin UI to create items in your database.

You can also access a GraphQL Playground at [localhost:3000/api/graphql](http://localhost:3000/api/graphql), which allows you to directly run GraphQL queries and mutations.

Congratulations, you're now up and running with Keystone! 🚀

## Try it out in CodeSandbox 🧪

You can play with this example online in a web browser using the free [codesandbox.io](https://codesandbox.io/) service. To launch this example, open the URL <https://githubbox.com/keystonejs/keystone/tree/main/examples/extend-prisma-schema>. You can also fork this sandbox to make your own changes.
Comment on lines +1 to +28
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole directory (custom-prisma-driver-adapter) is the copy of the extend-prisma-schema example.

This could also be the start of showing an example with prisma client extensions like https://github.com/prisma/extension-read-replicas

would be super beneficial for users

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking we might use Prisma extensions for hooks in the near future, so it does have me thinking about how this functionality might impact the order of operations there.

As is, if we supported the custom PrismaClient, Keystone would be .extending the custom client, which I think is ok?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as far as I know and my testing has gone you call $extend() on the prisma client and get a new instance of the prisma client back which extends the "old" prisma client instance but doesn't modify it.

The only issue with the current setup of keystone is, that when you call context.prisma.xxxx you won't have access to the user or keystone defined extensions as the infered type of the prisma client comes from what ever @prisma/client resolves to.

Copy link
Contributor Author

@mmachatschek mmachatschek Oct 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking we might use Prisma extensions for hooks in the near future, so it does have me thinking about how this functionality might impact the order of operations there.

@dcousens I love this idea and would make the usage of context.prisma over context.db much easier if you have custom logic in list hooks

36 changes: 36 additions & 0 deletions examples/custom-prisma-driver-adapter/keystone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { config } from '@keystone-6/core';
import { Pool, neonConfig } from '@neondatabase/serverless';
import { PrismaNeon } from '@prisma/adapter-neon';
import { WebSocket } from 'undici';
import { fixPrismaPath } from '../example-utils';
import { lists } from './schema';
import { PrismaClient } from '.myprisma/client';

export default config({
db: {
provider: 'sqlite',
url: process.env.DATABASE_URL || 'file:./keystone-example.db',

prismaClient: config => {
neonConfig.webSocketConstructor = WebSocket;

const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const adapter = new PrismaNeon(pool);
return new PrismaClient({
...config,
adapter,
});
},

extendPrismaSchema: (schema: any) => {
return schema.replace(
/(generator [^}]+)}/g,
['$1previewFeatures = ["driverAdapters"]', '}'].join('\n')
);
},

// WARNING: this is only needed for our monorepo examples, dont do this
...fixPrismaPath,
},
lists,
});
24 changes: 24 additions & 0 deletions examples/custom-prisma-driver-adapter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@keystone-6/example-custom-prisma-driver-adapter",
"version": "0.0.0",
"private": true,
"license": "MIT",
"scripts": {
"dev": "keystone dev",
"start": "keystone start",
"build": "keystone build",
"postinstall": "keystone postinstall --fix"
},
"dependencies": {
"@keystone-6/core": "^5.2.0",
"@keystone-6/fields-document": "^8.0.0",
"@neondatabase/serverless": "^0.6.0",
"@prisma/adapter-neon": "^5.4.0",
"@prisma/client": "^5.4.0",
"undici": "^5.25.0"
},
"devDependencies": {
"prisma": "^5.4.0",
"typescript": "~5.0.0"
}
}
7 changes: 7 additions & 0 deletions examples/custom-prisma-driver-adapter/sandbox.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"template": "node",
"container": {
"startScript": "keystone dev",
"node": "18"
}
}
Loading