Skip to content

Commit 5a0e932

Browse files
authored
Add missing indexes to MongoDB (#3189)
1 parent 4dc2411 commit 5a0e932

5 files changed

Lines changed: 27 additions & 8 deletions

File tree

src/RealtimeServer/common/models/user.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import { obj } from '../utils/obj-path';
12
import { Site } from './site';
23

34
export const USER_PROFILES_COLLECTION = 'user_profiles';
45
export const USER_PROFILE_INDEX_PATHS: string[] = [];
56

67
export const USERS_COLLECTION = 'users';
7-
export const USER_INDEX_PATHS: string[] = USER_PROFILE_INDEX_PATHS;
8+
export const USER_INDEX_PATHS = [
9+
// Index for ParatextService.GetBiblicalTermsAsync() in .NET
10+
obj<User>().pathStr(u => u.email),
11+
// Index for SFProjectService.InviteAsync() in .NET
12+
obj<User>().pathStr(u => u.paratextId)
13+
];
814

915
export enum AuthType {
1016
Unknown,

src/RealtimeServer/common/services/doc-service.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Db, IndexSpecification } from 'mongodb';
1+
import { CreateIndexesOptions, Db, IndexSpecification } from 'mongodb';
22
import { ConnectSession } from '../connect-session';
33
import { Migration, MigrationConstructor } from '../migration';
44
import { ValidationSchema } from '../models/validation-schema';
@@ -70,7 +70,7 @@ export abstract class DocService<T = any> {
7070
}
7171

7272
abstract get collection(): string;
73-
protected abstract get indexPaths(): (string | IndexSpecification)[];
73+
protected abstract get indexPaths(): (string | IndexSpecification | [string, CreateIndexesOptions])[];
7474
validationSchema: ValidationSchema | undefined = undefined;
7575

7676
init(server: RealtimeServer): void {
@@ -96,6 +96,8 @@ export abstract class DocService<T = any> {
9696
const collection = db.collection(this.collection);
9797
if (typeof path === 'string') {
9898
await collection.createIndex({ [path]: 1 });
99+
} else if (Array.isArray(path)) {
100+
await collection.createIndex({ [path[0]]: 1 }, path[1] as CreateIndexesOptions);
99101
} else {
100102
await collection.createIndex(path);
101103
}

src/RealtimeServer/scriptureforge/models/sf-project.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { CreateIndexesOptions } from 'mongodb';
12
import { Project } from '../../common/models/project';
23
import { WritingSystem } from '../../common/models/writing-system';
34
import { obj } from '../../common/utils/obj-path';
@@ -13,9 +14,16 @@ export const SF_PROJECT_PROFILES_COLLECTION = 'sf_projects_profile';
1314
export const SF_PROJECT_PROFILES_INDEX_PATHS: string[] = [];
1415

1516
export const SF_PROJECTS_COLLECTION = 'sf_projects';
16-
export const SF_PROJECT_INDEX_PATHS: string[] = [
17-
obj<SFProject>().pathStr(q => q.name),
18-
obj<SFProject>().pathStr(q => q.paratextId)
17+
export const SF_PROJECT_INDEX_PATHS: (string | [string, CreateIndexesOptions])[] = [
18+
obj<SFProject>().pathStr(p => p.name),
19+
obj<SFProject>().pathStr(p => p.paratextId),
20+
// Index for ParatextService.GetBiblicalTermsAsync() in .NET
21+
obj<SFProject>().pathStr(p => p.shortName),
22+
// Indexes for SFProjectService.IsSourceProject() in .NET
23+
[obj<SFProject>().pathStr(p => p.translateConfig.source!.projectRef), { sparse: true }],
24+
[obj<SFProject>().pathStr(p => p.translateConfig.draftConfig.additionalTrainingSource!.projectRef), { sparse: true }],
25+
[obj<SFProject>().pathStr(p => p.translateConfig.draftConfig.alternateSource!.projectRef), { sparse: true }],
26+
[obj<SFProject>().pathStr(p => p.translateConfig.draftConfig.alternateTrainingSource!.projectRef), { sparse: true }]
1927
];
2028

2129
/** Length of id for a DBL resource. */

src/SIL.XForge.Scripture/ClientApp/src/xforge-common/indexeddb-offline-store.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ function getKeyRange(filter: PropertyFilter): IDBKeyRange | undefined {
3535
function createObjectStore(
3636
db: IDBDatabase,
3737
collection: string,
38-
indexPaths?: (string | { [x: string]: number | string })[]
38+
indexPaths?: (string | { [x: string]: number | string } | [string, unknown])[]
3939
): void {
4040
const objectStore = db.createObjectStore(collection, { keyPath: 'id' });
4141
if (indexPaths != null) {
4242
for (const path of indexPaths) {
4343
if (typeof path === 'string') {
4444
objectStore.createIndex(path, `data.${path}`);
45+
} else if (Array.isArray(path)) {
46+
// Index options are not supported in IndexedDB, so we ignore them.
47+
objectStore.createIndex(path[0], `data.${path[0]}`);
4548
} else {
4649
objectStore.createIndex(
4750
Object.keys(path).join('_'),

src/SIL.XForge.Scripture/ClientApp/src/xforge-common/models/realtime-doc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface RealtimeDocConstructor {
1010
readonly COLLECTION: string;
1111

1212
// string is the legacy one column index format, the associative array corresponds to MongoDB's IndexSpecification
13-
readonly INDEX_PATHS: (string | { [x: string]: number | string })[];
13+
readonly INDEX_PATHS: (string | { [x: string]: number | string } | [string, unknown])[];
1414

1515
new (realtimeService: RealtimeService, adapter: RealtimeDocAdapter): RealtimeDoc;
1616
}

0 commit comments

Comments
 (0)