Skip to content

Commit

Permalink
Add rels data structure to adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed Mar 18, 2020
1 parent b6a555c commit 94ec087
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
12 changes: 9 additions & 3 deletions packages/adapter-knex/lib/adapter-knex.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ class KnexAdapter extends BaseKeystoneAdapter {
return result;
}

async postConnect() {
async postConnect({ rels }) {
Object.values(this.listAdapters).forEach(listAdapter => {
listAdapter._postConnect();
listAdapter._postConnect({ rels });
});

// Run this only if explicity configured and still never in production
Expand Down Expand Up @@ -254,12 +254,18 @@ class KnexListAdapter extends BaseListAdapter {
this.getListAdapterByKey = parentAdapter.getListAdapterByKey.bind(parentAdapter);
this.realKeys = [];
this.tableName = this.key;
this.rels = undefined;
}

prepareFieldAdapter() {}

_postConnect() {
_postConnect({ rels }) {
this.rels = rels;
this.fieldAdapters.forEach(fieldAdapter => {
fieldAdapter.rel = rels.find(
({ left, right }) =>
left.adapter === fieldAdapter || (right && right.adapter === fieldAdapter)
);
if (fieldAdapter._hasRealKeys()) {
this.realKeys.push(
...(fieldAdapter.realKeys ? fieldAdapter.realKeys : [fieldAdapter.path])
Expand Down
16 changes: 13 additions & 3 deletions packages/adapter-mongoose/lib/adapter-mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ class MongooseAdapter extends BaseKeystoneAdapter {
...mongooseConfig,
});
}
async postConnect() {

async postConnect({ rels }) {
return await pSettle(
Object.values(this.listAdapters).map(listAdapter => listAdapter.postConnect())
Object.values(this.listAdapters).map(listAdapter => listAdapter.postConnect({ rels }))
);
}

Expand Down Expand Up @@ -129,6 +130,8 @@ class MongooseListAdapter extends BaseListAdapter {

// Need to call postConnect() once all fields have registered and the database is connected to.
this.model = null;

this.rels = undefined;
}

prepareFieldAdapter(fieldAdapter) {
Expand All @@ -142,7 +145,14 @@ class MongooseListAdapter extends BaseListAdapter {
*
* @return Promise<>
*/
async postConnect() {
async postConnect({ rels }) {
this.rels = rels;
this.fieldAdapters.forEach(fieldAdapter => {
fieldAdapter.rel = rels.find(
({ left, right }) =>
left.adapter === fieldAdapter || (right && right.adapter === fieldAdapter)
);
});
if (this.configureMongooseSchema) {
this.configureMongooseSchema(this.schema, { mongoose: this.mongoose });
}
Expand Down
14 changes: 9 additions & 5 deletions packages/keystone/lib/Keystone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,18 +374,23 @@ module.exports = class Keystone {
`${left.listKey}.${left.path} refers to a non-existant field, ${left.config.ref}`
);
}

return Object.values(rels);
}

/**
* @return Promise<null>
*/
connect() {
const { adapters, name } = this;
return resolveAllKeys(mapKeys(adapters, adapter => adapter.connect({ name }))).then(() => {
if (this.eventHandlers.onConnect) {
return this.eventHandlers.onConnect(this);
const rels = this._consolidateRelationships();
return resolveAllKeys(mapKeys(adapters, adapter => adapter.connect({ name, rels }))).then(
() => {
if (this.eventHandlers.onConnect) {
return this.eventHandlers.onConnect(this);
}
}
});
);
}

/**
Expand Down Expand Up @@ -555,7 +560,6 @@ module.exports = class Keystone {
pinoOptions,
cors = { origin: true, credentials: true },
} = {}) {
this._consolidateRelationships();
const middlewares = await this._prepareMiddlewares({ dev, apps, distDir, pinoOptions, cors });

// Now that the middlewares are done, it's safe to assume all the schemas
Expand Down
4 changes: 2 additions & 2 deletions packages/keystone/lib/adapters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class BaseKeystoneAdapter {
return this.listAdapters[key];
}

async connect({ name }) {
async connect({ name, rels }) {
// Connect to the database
await this._connect({ name }, this.config);

Expand All @@ -26,7 +26,7 @@ class BaseKeystoneAdapter {
// Validate the minimum database version requirements are met.
await this.checkDatabaseVersion();

const taskResults = await this.postConnect();
const taskResults = await this.postConnect({ rels });
const errors = taskResults.filter(({ isRejected }) => isRejected).map(({ reason }) => reason);

if (errors.length) {
Expand Down

0 comments on commit 94ec087

Please sign in to comment.