[next] MongoDB Indexes (#2142)

* feat: added mongo indexing support

* fix: fixed typescript issue

* chore: better types

* fix: revert debug stuff

* fix: addressed ts error

* feat: added config option to disable auto-indexing

* chore: reordered imports

* refactor: cleaned up some filepaths
This commit is contained in:
Wyatt Johnson
2019-02-06 17:53:34 +00:00
committed by GitHub
parent 7e8ef2189d
commit 9b0e6ed53b
26 changed files with 397 additions and 126 deletions
+54 -2
View File
@@ -19,8 +19,11 @@ import {
nodesToEdges,
NodeToCursorTransformer,
OrderedConnectionInput,
} from "talk-server/models/connection";
import Query from "talk-server/models/query";
} from "talk-server/models/helpers/connection";
import Query, {
createConnectionOrderVariants,
createIndexFactory,
} from "talk-server/models/helpers/query";
import { TenantResource } from "talk-server/models/tenant";
function collection(mongo: Db) {
@@ -137,6 +140,54 @@ export interface Comment extends TenantResource {
metadata?: Record<string, any>;
}
export async function createCommentIndexes(mongo: Db) {
const createIndex = createIndexFactory(collection(mongo));
// UNIQUE { id }
await createIndex({ tenantID: 1, id: 1 }, { unique: true });
const variants = createConnectionOrderVariants<Readonly<Comment>>([
{ createdAt: -1 },
{ createdAt: 1 },
{ replyCount: -1, createdAt: -1 },
{ "actionCounts.REACTION": -1, createdAt: -1 },
]);
// Story based Comment Connection pagination.
// { storyID, ...connectionParams }
await variants(createIndex, {
tenantID: 1,
storyID: 1,
status: 1,
});
// Story based Comment Connection pagination that are flagged.
// { storyID, ...connectionParams }
await variants(createIndex, {
tenantID: 1,
storyID: 1,
status: 1,
"actionCounts.FLAG": 1,
});
// Story + Reply based Comment Connection pagination.
// { storyID, ...connectionParams }
await variants(createIndex, {
tenantID: 1,
storyID: 1,
parentID: 1,
status: 1,
});
// Author based Comment Connection pagination.
// { authorID, ...connectionParams }
await variants(createIndex, {
tenantID: 1,
authorID: 1,
status: 1,
});
}
export type CreateCommentInput = Omit<
Comment,
| "id"
@@ -670,6 +721,7 @@ function applyInputToQuery(
input: CommentConnectionInput,
query: Query<Comment>
) {
// NOTE: (wyattjoh) if we ever extend these, ensure that the new order variant is added as an index into the `createCommentConnectionOrderVariants` function.
switch (input.orderBy) {
case GQLCOMMENT_SORT.CREATED_AT_DESC:
query.orderBy({ createdAt: -1 });