Skip to content

Commit

Permalink
Revert "Updated to latest mongodb driver. Added reference to @types/m…
Browse files Browse the repository at this point in the history
…ongodb."

This reverts commit b0df68a.
  • Loading branch information
meirgottlieb committed Dec 29, 2016
1 parent b0df68a commit 264282e
Show file tree
Hide file tree
Showing 15 changed files with 3,145 additions and 424 deletions.
16 changes: 3 additions & 13 deletions benchmarks/sessionImpl.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,8 @@ suite("SessionImpl", () => {
config.createSessionFactory(connection, (err: Error, sessionFactory: SessionFactory) => {
if (err) return done(err);

sessionFactory.createIndexes((err) => {
if (err) return done(err);

session = sessionFactory.createSession();
session.query(Cat).removeAll({}, done);
});
session = sessionFactory.createSession();
session.query(Cat).removeAll({}, done);
});
});

Expand All @@ -74,13 +70,7 @@ suite("SessionImpl", () => {
});

test("find", (done) => {
session.query(Cat).findOne({ name: 'cat' + (i++)}, (err, result) => {
if (err) return done(err);
if (!result) {
return done(new Error("ran out of cats at " + i));
}
done();
});
session.query(Cat).findOne({ name: 'cat' + (i++)}, done);
});

test("edit x 1000", (done) => {
Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "An Object Document Mapper (ODM) for MongoDB.",
"version": "1.1.2",
"author": {
"name": "Artifact Health, Inc",
"name": "Artifact Health, LLC",
"url": "http://www.artifacthealth.com"
},
"repository": {
Expand All @@ -20,12 +20,10 @@
"test": "gulp"
},
"dependencies": {
"@types/mongodb": "^2.1.36",
"@types/node": "^6.0.54",
"@types/rx": "^2.5.34",
"async": "^2.0.1",
"change-case": "^2.2.0",
"mongodb": "^2.2.16",
"mongodb": "^2.1.19",
"reflect-helper": "^1.0.2",
"rx": "^4.1.0"
},
Expand Down
2 changes: 1 addition & 1 deletion src/core/timerUtil.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @hidden
*/
export function getDuration(start: [number, number]): number {
export function getDuration(start: number[]): number {

var stop = process.hrtime(start);
return (stop[0] * 1000) + stop[1] / 1000000;
Expand Down
4 changes: 1 addition & 3 deletions src/mapping/bufferMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ export class BufferMapping extends MappingBase {
context.addError("Expected Binary.");
return;
}

var binary = (<Binary>value);
return binary.read(0, binary.length());
return (<Binary>value).value(true);
}

write(context: WriteContext, value: any): any {
Expand Down
119 changes: 50 additions & 69 deletions src/persister.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as mongodb from "mongodb";
import {FindAndModifyWriteOpResultObject, DeleteWriteOpResultObject, UpdateWriteOpResult, MongoCountPreferences} from "mongodb";
import * as async from "async";
import {onlyOnce, chain} from "./core/callback";
import {EntityMapping} from "./mapping/entityMapping";
Expand Down Expand Up @@ -57,6 +56,21 @@ interface FindAndModifyOptions extends WriteOptions {
new?: boolean;
}

interface RemoveOptions extends WriteOptions {
single?: boolean;
}

interface UpdateOptions extends WriteOptions {
multi?: boolean;
upsert?: boolean;
}

interface CountOptions {
limit: number;
skip: number;
}


/**
* @hidden
*/
Expand Down Expand Up @@ -234,7 +248,7 @@ export class PersisterImpl implements Persister {
handleCallback = this._createTraceableCallback({ kind: QueryKind[QueryKind.FindOne], criteria, fields }, callback);
}

this._collection.findOne(criteria, {fields:fields}, (err, document) => {
this._collection.findOne(criteria, fields, (err, document) => {
if (err) return callback(err);

// check to make sure the version has not changed
Expand Down Expand Up @@ -332,7 +346,7 @@ export class PersisterImpl implements Persister {

private _findOne(query: FindOneQuery, callback: ResultCallback<any>): void {

this._collection.findOne(query.criteria, {fields:query.fields}, (err, document) => {
this._collection.findOne(query.criteria, query.fields, (err, document) => {
if (err) return callback(err);
this._loadOne(document, callback);
});
Expand Down Expand Up @@ -366,7 +380,7 @@ export class PersisterImpl implements Persister {
function next(err?: Error) {
if (err) return error(err);

cursor.next((err: Error, item: any) => {
cursor.nextObject((err: Error, item: any) => {
if (err) return error(err);

// null item indicates the cursor is finished
Expand Down Expand Up @@ -480,19 +494,13 @@ export class PersisterImpl implements Persister {
this._findOneAndModify(query, this._fetchOne(query, handleCallback));
break;
case QueryKind.RemoveOne:
this._removeOne(query, handleCallback);
break;
case QueryKind.RemoveAll:
this._removeAll(query, handleCallback);
this._remove(query, handleCallback);
break;
case QueryKind.UpdateOne:
this._updateOne(query, handleCallback);
break;
case QueryKind.UpdateAll:
this._updateAll(query, handleCallback);
break;
case QueryKind.Upsert:
this._upsert(query, handleCallback);
this._update(query, handleCallback);
break;
case QueryKind.Distinct:
this._distinct(query, handleCallback);
Expand Down Expand Up @@ -558,7 +566,7 @@ export class PersisterImpl implements Persister {

function replenish() {
// try to retrieve a document from the cursor
cursor.next((err: Error, item: any) => {
cursor.nextObject((err: Error, item: any) => {
if(err) return error(err);

// if the document is null then the cursor is finished
Expand All @@ -576,7 +584,7 @@ export class PersisterImpl implements Persister {
process(err, item);

while((<any>cursor).bufferedCount() > 0) {
cursor.next(process);
cursor.nextObject(process);
}
});
}
Expand Down Expand Up @@ -631,7 +639,7 @@ export class PersisterImpl implements Persister {
(function next(err?: Error) {
if (err) return error(err);

cursor.next((err: Error, item: any) => {
cursor.nextObject((err: Error, item: any) => {
if (err) return error(err);

if (item == null) {
Expand Down Expand Up @@ -678,7 +686,7 @@ export class PersisterImpl implements Persister {

private _prepareFind(query: FindAllQuery): mongodb.Cursor {

var cursor = this._collection.find(query.criteria).project(query.fields);
var cursor = this._collection.find(query.criteria, query.fields);

if(query.orderDocument !== undefined) {
cursor.sort(query.orderDocument);
Expand All @@ -701,33 +709,20 @@ export class PersisterImpl implements Persister {

private _findOneAndModify(query: QueryDefinition, callback: ResultCallback<Object>): void {

var self = this;

switch (query.kind) {
case QueryKind.FindOneAndRemove:
this._collection.findOneAndDelete(query.criteria, findAndModifyCallback);
break;
default:
this._collection.findOneAndUpdate(
query.criteria,
query.updateDocument,
{
returnOriginal: !query.wantsUpdated,
upsert: query.kind == QueryKind.FindOneAndUpsert,
sort: <any>query.orderDocument
},
findAndModifyCallback);
break;
}
var options: FindAndModifyOptions = {
remove: query.kind == QueryKind.FindOneAndRemove,
new: query.wantsUpdated,
upsert: query.kind == QueryKind.FindOneAndUpsert
};

function findAndModifyCallback(err: Error, response: FindAndModifyWriteOpResultObject): void {
this._collection.findAndModify(query.criteria, query.orderDocument, query.updateDocument, options, (err, response) => {
if (err) return callback(err);

var document = response.value;
if (!document) return callback(null); // no match for criteria

// check if the entity is already in the session
var entity = self._session.getObject(document["_id"]);
var entity = this._session.getObject(document["_id"]);
if(entity !== undefined) {
// We check to see if the entity is already in the session so we know if we need to refresh the
// entity or not.
Expand All @@ -737,26 +732,27 @@ export class PersisterImpl implements Persister {
else {
// If the entity is not in the session, then it will be loaded and added to the session as managed.
// The state may be changed to Removed below.
self._loadOne(document, (err, value) => {
this._loadOne(document, (err, value) => {
if(err) return callback(err);
entity = value;
handleCallback();
});
}

var self = this;
function handleCallback() {
// If the entity is not pending deletion, then see if we need to refresh the entity from the updated
// document or notify the session that the entity is now removed.

// Note that if the entity is pending deletion in the session then the callback will be called with null
// for the result. This is a little funny but seems most consistent with other methods such as findOne.
if (entity) {
if (query.kind == QueryKind.FindOneAndRemove) {
if (options.remove) {
// If entity was removed then notify the session that the entity has been removed from the
// database. Note that the remove operation is not cascaded.
self._session.notifyRemoved(entity);
}
else if (!query.wantsUpdated && alreadyLoaded) {
else if (options.new && alreadyLoaded) {
// If findAndModify returned the updated document and the entity was already part of the session
// before findAndModify was executed, then refresh the entity from the returned document. Note
// that the refresh operation is not cascaded.
Expand All @@ -776,44 +772,29 @@ export class PersisterImpl implements Persister {
// after findOneAndUpdate.
callback(null, entity);
}
}
}

private _removeOne(query: QueryDefinition, callback: ResultCallback<number>): void {

this._collection.deleteOne(query.criteria, (err: Error, response: DeleteWriteOpResultObject) => {
if(err) return callback(err);
callback(null, response.result.n);
});
}

private _removeAll(query: QueryDefinition, callback: ResultCallback<number>): void {

this._collection.deleteMany(query.criteria, (err: Error, response: DeleteWriteOpResultObject) => {
if(err) return callback(err);
callback(null, response.result.n);
});
}
private _remove(query: QueryDefinition, callback: ResultCallback<number>): void {

private _updateOne(query: QueryDefinition, callback: ResultCallback<number>): void {
var options: RemoveOptions = {
single: query.kind == QueryKind.RemoveOne
};

this._collection.updateOne(query.criteria, query.updateDocument, (err: Error, response: UpdateWriteOpResult) => {
this._collection.remove(query.criteria, options, (err: Error, response: any) => {
if(err) return callback(err);
callback(null, response.result.nModified);
callback(null, response.result.n);
});
}

private _updateAll(query: QueryDefinition, callback: ResultCallback<number>): void {
private _update(query: QueryDefinition, callback: ResultCallback<number>): void {

this._collection.updateMany(query.criteria, query.updateDocument, (err: Error, response: UpdateWriteOpResult) => {
if(err) return callback(err);
callback(null, response.result.nModified);
});
}

private _upsert(query: QueryDefinition, callback: ResultCallback<number>): void {
var options: UpdateOptions = {
multi: query.kind == QueryKind.UpdateAll,
upsert: query.kind == QueryKind.Upsert
};

this._collection.updateOne(query.criteria, query.updateDocument, { upsert: true }, (err: Error, response: UpdateWriteOpResult) => {
this._collection.update(query.criteria, query.updateDocument, options, (err: Error, response: any) => {
if(err) return callback(err);
callback(null, response.result.nModified);
});
Expand Down Expand Up @@ -850,9 +831,9 @@ export class PersisterImpl implements Persister {

// TODO: add options for readpreference

var options: MongoCountPreferences = {
var options: CountOptions = {
limit: query.limitCount,
skip: <any>query.skipCount
skip: query.skipCount
};

this._collection.count(query.criteria, options, (err: Error, result: number) => {
Expand Down Expand Up @@ -1184,7 +1165,7 @@ class CursorImpl<T> {

next(callback: ResultCallback<T>): void {

this._cursor.next((err: Error, item: any) => {
this._cursor.nextObject((err: Error, item: any) => {
if (err) return handleError(err);

if (item == null) {
Expand Down
4 changes: 2 additions & 2 deletions src/sessionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class SessionFactoryImpl implements InternalSessionFactory {
}
}

collection.createIndex(keys, indexOptions, (err, indexName) => {
collection.ensureIndex(keys, indexOptions, (err, indexName) => {
if (err) return indexDone(err);

if (this.logger) {
Expand Down Expand Up @@ -211,7 +211,7 @@ export class SessionFactoryImpl implements InternalSessionFactory {

let collection = this._collections[mapping.id];
if (collection) {
collection.dropIndexes((err: Error) => {
collection.dropAllIndexes((err: Error) => {
if (err) return done(err);

if (this.logger) {
Expand Down
Loading

0 comments on commit 264282e

Please sign in to comment.