Skip to content

Commit

Permalink
Add the ability to customise the Generated Prisma Schema (#8241)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Cousens <413395+dcousens@users.noreply.github.com>
Co-authored-by: Daniel Cousens <dcousens@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 24, 2023
1 parent eb25107 commit 25a1bf4
Show file tree
Hide file tree
Showing 41 changed files with 1,038 additions and 101 deletions.
6 changes: 6 additions & 0 deletions .changeset/pretty-books-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@keystone-6/fields-document': minor
'@keystone-6/core': minor
---

Adds an experimental `extendPrismaSchema` configuration option for lists and fields to mutate the prisma schema
28 changes: 28 additions & 0 deletions examples/extend-prisma-schema/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 `yarn` at the root of this repository, then navigate to this directory and run:

```shell
yarn 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.
14 changes: 14 additions & 0 deletions examples/extend-prisma-schema/keystone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { config } from '@keystone-6/core';
import { lists } from './schema';

export default config({
db: {
provider: 'sqlite',
url: process.env.DATABASE_URL || 'file:./keystone-example.db',
extendPrismaSchema: schema => {
// as an example, we will change the Prisma binary to be linux-musl
return schema.replace(/(generator [^}]+)}/g, '$1binaryTargets = ["native", "linux-musl"]\n}');
},
},
lists,
});
19 changes: 19 additions & 0 deletions examples/extend-prisma-schema/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@keystone-6/example-extend-prisma-schema",
"version": "0.0.8",
"private": true,
"license": "MIT",
"scripts": {
"dev": "keystone dev",
"start": "keystone start",
"build": "keystone build"
},
"dependencies": {
"@keystone-6/core": "^4.0.0",
"@keystone-6/fields-document": "^6.0.0"
},
"devDependencies": {
"typescript": "~4.9.4"
},
"repository": "https://github.com/keystonejs/keystone/tree/main/examples/extend-prisma-schema"
}
7 changes: 7 additions & 0 deletions examples/extend-prisma-schema/sandbox.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"template": "node",
"container": {
"startScript": "keystone dev",
"node": "16"
}
}
Loading

1 comment on commit 25a1bf4

@vercel
Copy link

@vercel vercel bot commented on 25a1bf4 Jan 24, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.