Skip to content

Commit

Permalink
fixed some mongodb method wrong signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
Umed Khudoiberdiev committed May 17, 2018
1 parent f45ccf4 commit 394c7c1
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 50 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ however since API is already quite stable we don't expect too much breaking chan
If we missed a note on some change or you have a questions on migrating from old version,
feel free to ask us and community.

## 0.2.6

* fixed wrong aggregate and count methods signature in mongodb

## 0.2.5

* added support for enum arrays in postgres
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,18 @@ await timber.remove();

`npm install sql.js --save`

* for **Oracle** (experimental)
* for **Oracle**

`npm install oracledb --save`

Install only *one* of them, depending on which database you use.

To make the Oracle driver work, you need to follow the installation instructions from
[their](https://github.com/oracle/node-oracledb) site.
Oracle support is experimental at the moment and isn't bug-free.
Expect to see more stable Oracle support in the near future.

* for **MongoDB** (experimental)

`npm install mongodb --save`

##### TypeScript configuration

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "typeorm",
"private": true,
"version": "0.2.5",
"version": "0.2.6",
"description": "Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, MongoDB databases.",
"license": "MIT",
"readmeFilename": "README.md",
Expand Down
101 changes: 59 additions & 42 deletions src/entity-manager/MongoEntityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ 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>|EntitySchema<Entity>|string, query?: ObjectLiteral): Cursor<Entity> {
createCursor<Entity, T = any>(entityClassOrName: ObjectType<Entity>|EntitySchema<Entity>|string, query?: ObjectLiteral): Cursor<T> {
const metadata = this.connection.getMetadata(entityClassOrName);
return this.queryRunner.cursor(metadata.tableName, query);
}
Expand All @@ -256,58 +256,31 @@ export class MongoEntityManager extends EntityManager {
* This returns modified version of cursor that transforms each result into Entity model.
*/
createEntityCursor<Entity>(entityClassOrName: ObjectType<Entity>|EntitySchema<Entity>|string, query?: ObjectLiteral): Cursor<Entity> {

const metadata = this.connection.getMetadata(entityClassOrName);
const cursor = this.createCursor(entityClassOrName, query);
const ParentCursor = PlatformTools.load("mongodb").Cursor;
cursor.toArray = function (callback?: MongoCallback<Entity[]>) {
if (callback) {
ParentCursor.prototype.toArray.call(this, (error: MongoError, results: Entity[]): void => {
if (error) {
callback(error, results);
return;
}

const transformer = new DocumentToEntityTransformer();
return callback(error, transformer.transformAll(results, metadata));
});
} else {
return ParentCursor.prototype.toArray.call(this).then((results: Entity[]) => {
const transformer = new DocumentToEntityTransformer();
return transformer.transformAll(results, metadata);
});
}
};
cursor.next = function (callback?: MongoCallback<CursorResult>) {
if (callback) {
ParentCursor.prototype.next.call(this, (error: MongoError, result: CursorResult): void => {
if (error || !result) {
callback(error, result);
return;
}

const transformer = new DocumentToEntityTransformer();
return callback(error, transformer.transform(result, metadata));
});
} else {
return ParentCursor.prototype.next.call(this).then((result: Entity) => {
if (!result) return result;
const transformer = new DocumentToEntityTransformer();
return transformer.transform(result, metadata);
});
}
};
this.applyEntityTransformationToCursor(metadata, cursor);
return cursor;
}

/**
* Execute an aggregation framework pipeline against the collection.
*/
aggregate<Entity>(entityClassOrName: ObjectType<Entity>|EntitySchema<Entity>|string, pipeline: ObjectLiteral[], options?: CollectionAggregationOptions): AggregationCursor<Entity> {
aggregate<Entity, R = any>(entityClassOrName: ObjectType<Entity>|EntitySchema<Entity>|string, pipeline: ObjectLiteral[], options?: CollectionAggregationOptions): AggregationCursor<R> {
const metadata = this.connection.getMetadata(entityClassOrName);
return this.queryRunner.aggregate(metadata.tableName, pipeline, options);
}

/**
* Execute an aggregation framework pipeline against the collection.
* This returns modified version of cursor that transforms each result into Entity model.
*/
aggregateEntity<Entity>(entityClassOrName: ObjectType<Entity>|EntitySchema<Entity>|string, pipeline: ObjectLiteral[], options?: CollectionAggregationOptions): AggregationCursor<Entity> {
const metadata = this.connection.getMetadata(entityClassOrName);
const cursor = this.queryRunner.aggregate(metadata.tableName, pipeline, options);
this.applyEntityTransformationToCursor(metadata, cursor);
return cursor;
}

/**
* Perform a bulkWrite operation without a fluent API.
*/
Expand All @@ -319,7 +292,7 @@ export class MongoEntityManager extends EntityManager {
/**
* Count number of matching documents in the db to a query.
*/
count<Entity>(entityClassOrName: ObjectType<Entity>|EntitySchema<Entity>|string, query?: ObjectLiteral, options?: MongoCountPreferences): Promise<any> {
count<Entity>(entityClassOrName: ObjectType<Entity>|EntitySchema<Entity>|string, query?: ObjectLiteral, options?: MongoCountPreferences): Promise<number> {
const metadata = this.connection.getMetadata(entityClassOrName);
return this.queryRunner.count(metadata.tableName, query, options);
}
Expand Down Expand Up @@ -644,4 +617,48 @@ export class MongoEntityManager extends EntityManager {
};
}

/**
* Overrides cursor's toArray and next methods to convert results to entity automatically.
*/
protected applyEntityTransformationToCursor<Entity>(metadata: EntityMetadata, cursor: Cursor<Entity>|AggregationCursor<Entity>) {
const ParentCursor = PlatformTools.load("mongodb").Cursor;
cursor.toArray = function (callback?: MongoCallback<Entity[]>) {
if (callback) {
ParentCursor.prototype.toArray.call(this, (error: MongoError, results: Entity[]): void => {
if (error) {
callback(error, results);
return;
}

const transformer = new DocumentToEntityTransformer();
return callback(error, transformer.transformAll(results, metadata));
});
} else {
return ParentCursor.prototype.toArray.call(this).then((results: Entity[]) => {
const transformer = new DocumentToEntityTransformer();
return transformer.transformAll(results, metadata);
});
}
};
cursor.next = function (callback?: MongoCallback<CursorResult>) {
if (callback) {
ParentCursor.prototype.next.call(this, (error: MongoError, result: CursorResult): void => {
if (error || !result) {
callback(error, result);
return;
}

const transformer = new DocumentToEntityTransformer();
return callback(error, transformer.transform(result, metadata));
});
} else {
return ParentCursor.prototype.next.call(this).then((result: Entity) => {
if (!result) return result;
const transformer = new DocumentToEntityTransformer();
return transformer.transform(result, metadata);
});
}
};
}

}
15 changes: 11 additions & 4 deletions src/repository/MongoRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,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> {
createCursor<T = any>(query?: ObjectLiteral): Cursor<T> {
return this.manager.createCursor(this.metadata.target, query);
}

Expand All @@ -119,10 +119,17 @@ export class MongoRepository<Entity extends ObjectLiteral> extends Repository<En
/**
* Execute an aggregation framework pipeline against the collection.
*/
aggregate(pipeline: ObjectLiteral[], options?: CollectionAggregationOptions): AggregationCursor<Entity> {
return this.manager.aggregate(this.metadata.target, pipeline, options);
aggregate<R = any>(pipeline: ObjectLiteral[], options?: CollectionAggregationOptions): AggregationCursor<R> {
return this.manager.aggregate<R>(this.metadata.target, pipeline, options);
}

/**
* Execute an aggregation framework pipeline against the collection.
* This returns modified version of cursor that transforms each result into Entity model.
*/
aggregateEntity(pipeline: ObjectLiteral[], options?: CollectionAggregationOptions): AggregationCursor<Entity> {
return this.manager.aggregateEntity(this.metadata.target, pipeline, options);
}
/**
* Perform a bulkWrite operation without a fluent API.
*/
Expand All @@ -133,7 +140,7 @@ export class MongoRepository<Entity extends ObjectLiteral> extends Repository<En
/**
* Count number of matching documents in the db to a query.
*/
count(query?: ObjectLiteral, options?: MongoCountPreferences): Promise<any> {
count(query?: ObjectLiteral, options?: MongoCountPreferences): Promise<number> {
return this.manager.count(this.metadata.target, query || {}, options);
}

Expand Down

0 comments on commit 394c7c1

Please sign in to comment.