Skip to content

Commit

Permalink
onConnect support (keystonejs#1200)
Browse files Browse the repository at this point in the history
  • Loading branch information
JedWatson authored and timleslie committed May 20, 2019
1 parent dfcabe6 commit a98bce0
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 50 deletions.
7 changes: 7 additions & 0 deletions .changeset/92b6b934/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"releases": [
{ "name": "@keystone-alpha/demo-project-meetup", "type": "patch" },
{ "name": "@keystone-alpha/keystone", "type": "minor" }
],
"dependents": []
}
1 change: 1 addition & 0 deletions .changeset/92b6b934/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for an `onConnect` function to be passed to the Keystone constructor, which is called when all adapters have connected.
5 changes: 4 additions & 1 deletion demo-projects/meetup/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//imports for Keystone app core
require('dotenv').config();

const { Keystone, PasswordAuthStrategy } = require('@keystone-alpha/keystone');
const { MongooseAdapter } = require('@keystone-alpha/adapter-mongoose');
const { GraphQLApp } = require('@keystone-alpha/app-graphql');
Expand All @@ -9,10 +10,12 @@ const routes = require('./routes');
const { Event, Talk, User, Rsvp, Organiser, Sponsor } = require('./schema');

const MEETUP = require('./meetupConfig');
const initialiseData = require('./initialData');

const keystone = new Keystone({
name: MEETUP.name,
adapter: new MongooseAdapter(),
onConnect: initialiseData,
});

const authStrategy = keystone.createAuthStrategy({
Expand Down
39 changes: 31 additions & 8 deletions demo-projects/meetup/initialData.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,38 @@ require('dotenv').config();
const password = process.env.INITIAL_DATA_PASSWORD;
const PASSWORD_MIN_LENGTH = 8;

if (!password) {
throw new Error(`To seed initial data, set the 'INITIAL_DATA_PASSWORD' environment variable`);
} else if (password.length < PASSWORD_MIN_LENGTH) {
throw new Error(
`To seed initial data, the 'INITIAL_DATA_PASSWORD' environment variable must be at least ${PASSWORD_MIN_LENGTH} characters`
);
}
// You can force a re-init in development with the RECREATE_DATABASE
// environment variable.
const shouldRecreateDatabase = () =>
process.env.NODE_ENV !== 'production' && process.env.RECREATE_DATABASE;

module.exports = {
const validatePassword = () => {
if (!password) {
throw new Error(`To seed initial data, set the 'INITIAL_DATA_PASSWORD' environment variable`);
} else if (password.length < PASSWORD_MIN_LENGTH) {
throw new Error(
`To seed initial data, the 'INITIAL_DATA_PASSWORD' environment variable must be at least ${PASSWORD_MIN_LENGTH} characters`
);
}
};

module.exports = async keystone => {
// Check the users list to see if there are any; if we find none, assume
// it's a new database and initialise the demo data set.
const users = await keystone.lists.User.adapter.findAll();
if (!users.length || shouldRecreateDatabase()) {
// Ensure a valid initial password is available to be used
validatePassword();
// Drop the connected database to ensure no existing collections remain
Object.values(keystone.adapters).forEach(async adapter => {
await adapter.dropDatabase();
});
console.log('💾 Creating initial data...');
await keystone.createItems(initialData);
}
};

const initialData = {
User: [
{ name: 'Admin User', email: 'admin@keystonejs.com', isAdmin: true, password },
{
Expand Down
2 changes: 1 addition & 1 deletion demo-projects/meetup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"node": ">=8.4.0"
},
"scripts": {
"start": "NODE_ENV=development DISABLE_LOGGING=true node server.js",
"start": "NODE_ENV=development DISABLE_LOGGING=true keystone",
"build": "NODE_ENV=production next build"
},
"dependencies": {
Expand Down
37 changes: 0 additions & 37 deletions demo-projects/meetup/server.js

This file was deleted.

10 changes: 7 additions & 3 deletions packages/keystone/lib/Keystone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = class Keystone {
defaultAdapter,
name,
adapterConnectOptions = {},
onConnect,
}) {
this.name = name;
this.adapterConnectOptions = adapterConnectOptions;
Expand All @@ -46,6 +47,7 @@ module.exports = class Keystone {
this.getListByKey = key => this.lists[key];
this._graphQLQuery = {};
this.registeredTypes = new Set();
this.eventHandlers = { onConnect };

if (adapters) {
this.adapters = adapters;
Expand Down Expand Up @@ -108,8 +110,9 @@ module.exports = class Keystone {
...options,
})
)
// Don't unnecessarily leak any connection info
).then(() => {});
).then(() => {
if (this.eventHandlers.onConnect) this.eventHandlers.onConnect(this);
});
}

/**
Expand All @@ -118,7 +121,8 @@ module.exports = class Keystone {
disconnect() {
return resolveAllKeys(
mapKeys(this.adapters, adapter => adapter.disconnect())
// Don't unnecessarily leak any connection info
// Chain an empty function so that the result of this promise
// isn't unintentionally leaked to the caller
).then(() => {});
}

Expand Down

0 comments on commit a98bce0

Please sign in to comment.