Skip to content

Commit

Permalink
Create indexes for relation adjacency table fields (#4768)
Browse files Browse the repository at this point in the history
  • Loading branch information
kschingiz committed Feb 8, 2021
1 parent 45b047a commit a093185
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/friendly-countries-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/adapter-mongoose': patch
---

Created indexes for relation adjacency collection fields
7 changes: 6 additions & 1 deletion packages/adapter-mongoose/lib/adapter-mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ class MongooseAdapter extends BaseKeystoneAdapter {

schema.add({ [leftFkPath]: {} });
schema.add({ [rightFkPath]: {} });

// create 2 way indexes for reference fields
schema.index({ [leftFkPath]: 1 });
schema.index({ [rightFkPath]: 1 });

// 4th param is 'skipInit' which avoids calling `model.init()`.
// We call model.init() later, after we have a connection up and running to
// avoid issues with Mongoose's lazy queue and setting up the indexes.
Expand Down Expand Up @@ -630,7 +635,7 @@ class MongooseFieldAdapter extends BaseFieldAdapter {
// drop and recreate all indexes.
schemaOptions.unique = true;
}
if (this.isIndexed) {
if (this.isIndexed || this.isRelationship) {
schemaOptions.index = true;
}
return { ...schemaOptions, ...mongooseOptions };
Expand Down

0 comments on commit a093185

Please sign in to comment.