Skip to content
KeystoneJS LogoKeystoneJS (α)

Knex Database Adapter

WARNING: This adapter is under active development and is not production ready. It will drop your entire database every time you connect!

The Knex adapter is a general purpose adapter which can be used to connect to a range of different database backends. At present, the only fully tested 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. Assuming you're on MacOS and have Postgres installed the build-test-db.sh does this for you:

./packages/adapter-knex/build-test-db.sh

Otherwise, you can run the steps manually:

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

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

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

const uri = 'postgres://keystone5:k3yst0n3@127.0.0.1:5432/ks5_dev';
const client = 'postgres';
const schemaName = 'keystone';

const knexOptions = { ... };

keystone.connect(uri, {
  client,
  schemaName,
  ...knexOptions,
});

API

uri

Default: 'postgres://keystone5:k3yst0n3@127.0.0.1:5432/ks5_dev'

Either a connection string, or a connection object, as accepted by Knex. See knex docs for more details. If the environment variable KNEX_URI is set, its value will be used as the default.

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.

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 for possible values.

Debugging

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

Have you found a mistake, something that is missing, or could be improved on this page? Please edit the Markdown file on GitHub and submit a PR with your changes.

Edit Page