fixing mongodb issues

This commit is contained in:
Umed Khudoiberdiev 2017-05-20 14:53:40 +05:00
parent bf0c3f0bb5
commit 8f5827b550
3 changed files with 30 additions and 13 deletions

View File

@ -34,6 +34,8 @@ import {
CollStats
} from "../driver/mongodb/typings";
import {ObjectLiteral} from "../common/ObjectLiteral";
import {MongoQueryRunner} from "../driver/mongodb/MongoQueryRunner";
import {MongoDriver} from "../driver/mongodb/MongoDriver";
/**
* Entity manager supposed to work with any entity, automatically find its repository and call its methods,
@ -86,7 +88,8 @@ export class MongoEntityManager extends EntityManager {
* Creates a cursor for a query that can be used to iterate over results from MongoDB.
*/
createCursor<Entity>(entityClassOrName: ObjectType<Entity>|string, query?: ObjectLiteral): Cursor<Entity> {
return this.getMongoRepository(entityClassOrName as any).createCursor(query);
const metadata = this.connection.getMetadata(entityClassOrName);
return this.queryRunner.cursor(metadata.tableName, query);
}
/**
@ -331,4 +334,12 @@ export class MongoEntityManager extends EntityManager {
return this.getMongoRepository(entityClassOrName as any).updateOne(query, update, options);
}
// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
protected get queryRunner(): MongoQueryRunner {
return (this.connection.driver as MongoDriver).queryRunner;
}
}

View File

@ -40,13 +40,21 @@ import {
UnorderedBulkOperation,
UpdateWriteOpResult
} from "../driver/mongodb/typings";
import {MongoEntityManager} from "../entity-manager/MongoEntityManager";
/**
* Repository used to manage mongodb documents of a single entity type.
*/
export class MongoRepository<Entity extends ObjectLiteral> extends Repository<Entity> {
// todo: implement join from find options too
// -------------------------------------------------------------------------
// Protected Methods Set Dynamically
// -------------------------------------------------------------------------
/**
* Entity Manager used by this repository.
*/
protected manager: MongoEntityManager;
// -------------------------------------------------------------------------
// Overridden Methods
@ -60,14 +68,6 @@ export class MongoRepository<Entity extends ObjectLiteral> extends Repository<En
throw new Error(`Queries aren't supported by MongoDB.`);
}
/**
* Transactions are not supported by MongoDB.
* Calling this method will return an error.
*/
transaction(runInTransaction: (repository: Repository<Entity>) => Promise<any>|any): Promise<any> {
throw new Error(`Transactions aren't supported by MongoDB.`);
}
/**
* Using Query Builder with MongoDB is not supported yet.
* Calling this method will return an error.
@ -122,7 +122,13 @@ export class MongoRepository<Entity extends ObjectLiteral> extends Repository<En
*/
async findByIds(ids: any[], optionsOrConditions?: FindManyOptions<Entity>|Partial<Entity>): Promise<Entity[]> {
const query = this.convertFindManyOptionsOrConditionsToMongodbQuery(optionsOrConditions) || {};
query["_id"] = { $in: ids.map(id => id[this.metadata.objectIdColumn!.propertyName]) };
const objectIdInstance = require("mongodb").ObjectID;
query["_id"] = { $in: ids.map(id => {
if (id instanceof objectIdInstance)
return id;
return id[this.metadata.objectIdColumn!.propertyName];
}) };
const cursor = await this.createEntityCursor(query);
if (FindOptionsUtils.isFindManyOptions(optionsOrConditions)) {
@ -174,7 +180,7 @@ export class MongoRepository<Entity extends ObjectLiteral> extends Repository<En
* Creates a cursor for a query that can be used to iterate over results from MongoDB.
*/
createCursor(query?: ObjectLiteral): Cursor<Entity> {
return this.queryRunner.cursor(this.metadata.tableName, query);
return this.manager.createCursor(this.metadata.target, query);
}
/**

View File

@ -19,7 +19,7 @@ export class Repository<Entity extends ObjectLiteral> {
// -------------------------------------------------------------------------
/**
* Connection used by this repository.
* Entity Manager used by this repository.
*/
protected manager: EntityManager;