Skip to content

Commit

Permalink
Add a Knex adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed Feb 11, 2019
1 parent d1fdbb8 commit dc53492
Show file tree
Hide file tree
Showing 51 changed files with 1,470 additions and 104 deletions.
19 changes: 19 additions & 0 deletions .changeset/ade8480b/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"releases": [
{ "name": "@voussoir/fields", "type": "minor" },
{ "name": "@voussoir/test-utils", "type": "minor" },
{ "name": "@voussoir/cypress-project-basic", "type": "minor" }
],
"dependents": [
{
"name": "@voussoir/cypress-project-access-control",
"type": "patch",
"dependencies": ["@voussoir/fields", "@voussoir/test-utils"]
},
{
"name": "@voussoir/cypress-project-login",
"type": "patch",
"dependencies": ["@voussoir/fields", "@voussoir/test-utils"]
}
]
}
1 change: 1 addition & 0 deletions .changeset/ade8480b/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add support for the Knex adapter
11 changes: 10 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ x-job-setup: &job-setup
docker:
# specify the version you desire here
- image: circleci/node:carbon-browsers
environment:
TEST_DATABASE_URL: postgresql://root@localhost/circle_test
KNEX_URI: 'postgres://keystone5:k3yst0n3@localhost:5432/circle_test'
- image: mongo:3.6.7
- image: circleci/postgres:9.6.5-alpine-ram

working_directory: /home/circleci/repo

Expand Down Expand Up @@ -62,6 +66,11 @@ jobs:
steps:
- checkout
- restore_cache: *restore-cache
- run: sudo apt-get update
- run: sudo apt-get install postgresql-client-9.6
- run: psql -d $TEST_DATABASE_URL -c "CREATE SCHEMA keystone;"
- run: psql -d $TEST_DATABASE_URL -c "CREATE USER keystone5 PASSWORD 'k3yst0n3';"
- run: psql -d $TEST_DATABASE_URL -c "GRANT ALL ON SCHEMA keystone TO keystone5;"
- run: sudo yarn global add bolt
# Set Cypress cache dir here, in case this job runs/completes first, and
# hence clobbers the `node_modules` cache with a non-cypress version,
Expand All @@ -74,7 +83,7 @@ jobs:
- run: yarn lint:flow
- run:
name: Unit tests
command: yarn jest --ci --testResultsProcessor="jest-junit" --maxWorkers=2
command: yarn jest --ci --testResultsProcessor="jest-junit" --maxWorkers=1
environment:
JEST_JUNIT_OUTPUT: 'reports/junit/js-test-results.xml'
- store_test_results:
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"website:build": "cd website && yarn build",
"arch": "cd packages/arch/www && yarn && cd .. && yarn start",
"test": "yarn lint && yarn test:unit && yarn cypress:run",
"test:unit": "DISABLE_LOGGING=true NODE_ENV=test jest",
"test:unit": "DISABLE_LOGGING=true NODE_ENV=test jest --maxWorkers=1",
"test:unit:debug": "NODE_ENV=test node --inspect-brk `which jest` --runInBand",
"changeset": "build-releases changeset",
"version-packages": "build-releases version",
Expand Down Expand Up @@ -117,6 +117,7 @@
"jest-junit": "^5.2.0",
"kalendaryo": "^1.10.0",
"keystone-email": "^1.1.0",
"knex": "^0.16.3",
"loader-utils": "^1.1.0",
"lodash.debounce": "^4.0.8",
"lodash.get": "^4.4.2",
Expand Down Expand Up @@ -144,6 +145,7 @@
"passport": "^0.4.0",
"passport-twitter": "^1.0.4",
"pify": "^4.0.1",
"pg": "^7.8.0",
"pino": "5.0.0-rc.4",
"pluralize": "^7.0.0",
"popper.js": "^1.14.6",
Expand Down
2 changes: 2 additions & 0 deletions packages/adapter-knex/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/*.md
**/*.test.js
69 changes: 69 additions & 0 deletions packages/adapter-knex/README/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Keystone 5 Knex Database Adapter

The [Knex](https://knexjs.org/#changelog) adapter is a general purpose adapter which can be used to connect to a range of different database backends.
At present, the only fully supported backend is `Postgres`, however knex gives the potential for `MSSQL`, `MySQL`, `MariaDB`, `SQLite3`, `Oracle`, and `Amazon Redshift` to be supported.

## Setting Up Your Database

Before running Keystone, you must set up a database, a schema, and a user.

```shell
createdb -U postgres ks5_dev
psql ks5_dev -U postgres -c "CREATE SCHEMA keystone;"
psql ks5_dev -U postgres -c "CREATE USER keystone5 PASSWORD 'k3yst0n3'"
psql ks5_dev -U postgres -c "GRANT ALL ON SCHEMA keystone TO keystone5;"
```

## Usage

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

const keystone = new Keystone({
name: 'My Awesome Project',
adapter: new KnexAdapter(),
});

const client = 'postgres';
const connection = 'postgres://keystone5:[email protected]:5432/ks5_dev';
const schemaName = 'keystone';

const knexOptions = { ... };

keystone.connect(
client,
connection,
schemaName,
...knexOptions,
);
```

## API

### `client`

_**Default:**_ `'postgres'`

Defines the type of backend to use. Current `postgres` is supported, but any value supported by Knex may be supported in the future.

### `connection`

_**Default:**_ `'postgres://keystone5:[email protected]:5432/ks5_dev'`

Either a connection string, or a connection object, as accepted by knex.
See [knex docs](https://knexjs.org/#Installation-client) for more details.
If the environment variable `KNEX_URI` is set, its value will be used as the default.

### `schemaName`

_**Default:**_ `'keystone'`

All keystone tables are grouped within a schema. This value should match the name of the schema used in the `CREATE SCHEMA` step above.

### `knexOptions`

Any extra options provided will be passed through to the knex configuration function. See the [Knex docs](https://knexjs.org/#Installation-client) for possible values.

## Debugging

To log all knex queries, run the server with the environment variable `DEBUG=knex:query`.
Loading

0 comments on commit dc53492

Please sign in to comment.