Skip to content

Commit

Permalink
feat(mongo): allow creating any kind of index in mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Aug 9, 2020
1 parent 990655f commit 8fbe48a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
5 changes: 5 additions & 0 deletions docs/docs/usage-with-mongo.md
Expand Up @@ -125,6 +125,11 @@ await orm.em.getDriver().ensureIndexes();
>
> `@Index({ properties: ['name', 'caption'], type: 'text' })`
> If you provide only `options` in the index definition, it will be used as is,
> this allows to define any kind of index:
>
> `@Index({ options: { point: '2dsphere', title: -1 } })`
## Native collection methods

Sometimes you need to perform some bulk operation, or you just want to populate your
Expand Down
13 changes: 9 additions & 4 deletions packages/mongodb/src/MongoDriver.ts
Expand Up @@ -129,18 +129,23 @@ export class MongoDriver extends DatabaseDriver<MongoConnection> {
private createIndexes(meta: EntityMetadata) {
const promises: Promise<string>[] = [];
meta.indexes.forEach(index => {
let fieldOrSpec: string | any;
let fieldOrSpec: string | Dictionary;
const properties = Utils.flatten(Utils.asArray(index.properties).map(prop => meta.properties[prop].fieldNames));
const collection = this.getConnection('write').getCollection(meta.name);

if (index.type === 'text') {
if (index.options && properties.length === 0) {
return promises.push(collection.createIndex(index.options));
}

if (index.type) {
const spec: Dictionary<string> = {};
properties.forEach(prop => spec[prop] = 'text');
properties.forEach(prop => spec[prop] = index.type!);
fieldOrSpec = spec;
} else {
fieldOrSpec = properties;
}

promises.push(this.getConnection('write').getCollection(meta.name).createIndex(fieldOrSpec, {
promises.push(collection.createIndex(fieldOrSpec, {
name: index.name,
unique: false,
...(index.options || {}),
Expand Down
10 changes: 6 additions & 4 deletions tests/EntityManager.mongo.test.ts
Expand Up @@ -498,10 +498,12 @@ describe('EntityManagerMongo', () => {
email_1: { key: { email: 1 }, name: 'email_1', unique: true },
});
expect(bookInfo.reduce((o: any, i: any) => { o[i.name] = i; return o; }, {} as any)).toMatchObject({
_id_: { key: { _id: 1 }, name: '_id_' },
publisher_idx: { key: { publisher: 1 }, name: 'publisher_idx' },
title_text: { key: { _fts: 'text', _ftsx: 1 }, weights: { title: 1 } },
title_1_author_1: { key: { title: 1, author: 1 }, name: 'title_1_author_1', unique: true },
'_id_': { key: { _id: 1 }, name: '_id_' },
'publisher_idx': { key: { publisher: 1 }, name: 'publisher_idx' },
'title_text': { key: { _fts: 'text', _ftsx: 1 }, weights: { title: 1 } },
'title_1_author_1': { key: { title: 1, author: 1 }, name: 'title_1_author_1', unique: true },
'point_2dsphere': { key: { point: '2dsphere' }, name: 'point_2dsphere' },
'point_2dsphere_title_-1': { key: { point: '2dsphere', title: -1 }, name: 'point_2dsphere_title_-1' },
});
});

Expand Down
5 changes: 5 additions & 0 deletions tests/entities/Book.ts
Expand Up @@ -8,6 +8,7 @@ import { BaseEntity3 } from './BaseEntity3';
@Entity({ tableName: 'books-table' })
@Unique({ properties: ['title', 'author'] })
@Index({ properties: 'title', type: 'text' })
@Index({ options: { point: '2dsphere', title: -1 } })
export class Book extends BaseEntity3 {

@PrimaryKey()
Expand Down Expand Up @@ -35,6 +36,10 @@ export class Book extends BaseEntity3 {
@Property()
metaArrayOfStrings?: string[];

@Property()
@Index({ type: '2dsphere' })
point?: [number, number];

constructor(title: string, author?: Author) {
super();
this.title = title;
Expand Down

0 comments on commit 8fbe48a

Please sign in to comment.