Skip to content

Commit

Permalink
Add a Prisma adapter (keystonejs#3298)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Sep 27, 2020
1 parent 22bfcca commit f70c9f1
Show file tree
Hide file tree
Showing 71 changed files with 1,942 additions and 74 deletions.
15 changes: 15 additions & 0 deletions .changeset/shy-cameras-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@keystonejs/adapter-prisma': major
'@keystonejs/fields': major
'@keystonejs/fields-auto-increment': major
'@keystonejs/fields-cloudinary-image': major
'@keystonejs/fields-content': major
'@keystonejs/fields-location-google': major
'@keystonejs/fields-mongoid': major
'@keystonejs/fields-oembed': major
'@keystonejs/fields-unsplash': major
'@keystonejs/test-utils': major
'@keystonejs/api-tests': major
---

Added support for a Prisma adapter to Keystone.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,6 @@ tags
# End of https://www.gitignore.io/api/vim

projects/
temp/
temp/

.api-test-prisma-clients
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ packages/arch/www/public/**/*
**/dist
**/.next
**/.keystone
.api-test-prisma-clients
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"repository": "https://github.com/keystonejs/keystone",
"homepage": "https://github.com/keystonejs/keystone",
"scripts": {
"prisma": "prisma",
"dev": "yarn demo todo dev",
"demo": "yarn --cwd ./examples",
"website": "manypkg run @keystonejs/website",
Expand All @@ -25,7 +26,7 @@
"lint:types": "tsc",
"lint": "yarn lint:prettier && yarn lint:eslint && yarn lint:markdown && yarn lint:types",
"test": "yarn lint && yarn test:unit && yarn cypress:run",
"test:unit": "cross-env DISABLE_LOGGING=true NODE_ENV=test jest --maxWorkers=1 --logHeapUsage",
"test:unit": "cross-env DISABLE_LOGGING=true NODE_ENV=test jest --no-watchman --maxWorkers=1 --logHeapUsage",
"test:unit:debug": "cross-env NODE_ENV=test node --inspect-brk `which jest` --runInBand",
"benchmark": "yarn workspace @keystonejs/benchmarks go",
"changeset": "changeset",
Expand Down
2 changes: 2 additions & 0 deletions packages/adapter-prisma/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/*.md
**/*.test.js
1 change: 1 addition & 0 deletions packages/adapter-prisma/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @keystonejs/adapter-prisma
83 changes: 83 additions & 0 deletions packages/adapter-prisma/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<!--[meta]
section: api
subSection: database-adapters
title: Prisma adapter
[meta]-->

# Prisma database adapter

[![View changelog](https://img.shields.io/badge/changelogs.xyz-Explore%20Changelog-brightgreen)](https://changelogs.xyz/@keystonejs/adapter-prisma)

> The Keystone Prisma adapter is not currently production-ready. It depends on the [`prisma-migrate`](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-migrate) system which is currently flagged as `EXPERIMENTAL`. Once `prisma-migrate` is out of experimental mode we will release a production-ready version of `@keystonejs/adapter-prisma`.
The [Prisma](https://www.prisma.io/) adapter allows Keystone to connect a database using a client generated by Prisma.

> This adapter currently only supports `PostgreSQL`. Future releases will enable support for all database backends which are [supported by Prisma](https://www.prisma.io/docs/reference/database-connectors/database-features).
## Usage

```javascript
const { PrismaAdapter } = require('@keystonejs/adapter-prisma');

const keystone = new Keystone({
adapter: new PrismaAdapter({ url: 'postgres://...' }),
});
```

## Config

### `url`

_**Default:**_ `DATABASE_URL`

The connection string for your database, in the form `postgres://<user>:<password>@<host>:<port>/<dbname>`.
By default it will use the value of the environment variable `DATABASE_URL`.

### `getPrismaPath`

_**Default:**_ `({ prismaSchema }) => '.prisma'`

A function which returns a directory name for storing the generated Prisma schema and client.

### `getDbSchemaName`

_**Default:**_ `({ prismaSchema }) => 'public'`

A function which returns a database schema name to use for storage of all Keystone tables in your database.

> You can also set the schema name by including the suffix `?schema=...` in your `DATABASE_URL` or `url`. In this case you should set this value to `() => null`.
### `enableLogging`

_**Default:**_ `false`

Enables logging at the [`query`](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/logging#overview) level in the Prisma client.

### `dropDatabase`

_**Default:**_ `false`

Allow the adapter to drop the entire database and recreate the tables / foreign keys based on the list schema in your application. This option is ignored in production, i.e. when the environment variable NODE_ENV === 'production'.

## Setup

Before running Keystone with the Prisma adapter you will need to have a PostgreSQL database to connect to.

If you already have a database then you can use its connection string in the `url` config option.
If you don't have a database already then you can create one locally with the following commands.

```shell allowCopy=false showLanguage=false
createdb -U postgres keystone
psql keystone -U postgres -c "CREATE USER keystone5 PASSWORD 'k3yst0n3'"
psql keystone -U postgres -c "GRANT ALL ON DATABASE keystone TO keystone5;"
```

If using the above, you will want to set a connection string of:

```javascript
const keystone = new Keystone({
adapter: new PrismaAdapter({ url: `postgres://keystone5:k3yst0n3@localhost:5432/keystone` }),
});
```

See the [adapters setup](/docs/quick-start/adapters.md) guide for more details on how to setup a database.
3 changes: 3 additions & 0 deletions packages/adapter-prisma/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { PrismaAdapter, PrismaListAdapter, PrismaFieldAdapter } = require('./lib/adapter-prisma');

module.exports = { PrismaAdapter, PrismaListAdapter, PrismaFieldAdapter };
Loading

0 comments on commit f70c9f1

Please sign in to comment.