Skip to content

Commit

Permalink
Merge branch 'master' into 8.5
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jun 17, 2024
2 parents 90fa8af + 332f7f5 commit 174d8be
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ const noPasswordAuthMechanisms = [
* @event `close`: Emitted after we `disconnected` and `onClose` executed on all of this connection's models.
* @event `reconnected`: Emitted after we `connected` and subsequently `disconnected`, followed by successfully another successful connection.
* @event `error`: Emitted when an error occurs on this connection.
* @event `fullsetup`: Emitted after the driver has connected to primary and all secondaries if specified in the connection string.
* @event `operation-start`: Emitted when a call to the MongoDB Node.js driver, like a `find()` or `insertOne()`, happens on any collection tied to this connection.
* @event `operation-end`: Emitted when a call to the MongoDB Node.js driver, like a `find()` or `insertOne()`, either succeeds or errors.
* @api public
*/

Expand Down
23 changes: 15 additions & 8 deletions lib/drivers/node-mongodb-native/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,23 +236,30 @@ function iter(i) {
}

if (syncCollectionMethods[i] && typeof lastArg === 'function') {
const ret = collection[i].apply(collection, _args.slice(0, _args.length - 1));
return lastArg.call(this, null, ret);
const result = collection[i].apply(collection, _args.slice(0, _args.length - 1));
this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: this.name, method: i, result });
return lastArg.call(this, null, result);
}

const ret = collection[i].apply(collection, _args);
if (ret != null && typeof ret.then === 'function') {
return ret.then(
res => {
typeof lastArg === 'function' && lastArg(null, res);
return res;
result => {
if (typeof lastArg === 'function') {
lastArg(null, result);
} else {
this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: this.name, method: i, result });
}
return result;
},
err => {
error => {
if (typeof lastArg === 'function') {
lastArg(err);
lastArg(error);
return;
} else {
this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: this.name, method: i, error });
}
throw err;
throw error;
}
);
}
Expand Down
50 changes: 50 additions & 0 deletions test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,56 @@ describe('connections:', function() {
});
});

describe('events', function() {
let conn;

before(async function() {
conn = mongoose.createConnection(start.uri2);
await conn.asPromise();
await conn.collection('test').deleteMany({});
return conn;
});

after(function() {
return conn.close();
});

it('operation-start', async function() {
const events = [];
conn.on('operation-start', ev => events.push(ev));

await conn.collection('test').findOne({ answer: 42 });
assert.equal(events.length, 1);
assert.equal(events[0].collectionName, 'test');
assert.equal(events[0].method, 'findOne');
assert.deepStrictEqual(events[0].params, [{ answer: 42 }]);

await conn.collection('test').insertOne({ _id: 12, answer: 99 });
assert.equal(events.length, 2);
assert.equal(events[1].collectionName, 'test');
assert.equal(events[1].method, 'insertOne');
assert.deepStrictEqual(events[1].params, [{ _id: 12, answer: 99 }]);
});

it('operation-end', async function() {
const events = [];
conn.on('operation-end', ev => {
events.push(ev);
});

await conn.collection('test').insertOne({ _id: 17, answer: 42 });
assert.equal(events.length, 1);
assert.equal(events[0].collectionName, 'test');
assert.equal(events[0].method, 'insertOne');

await conn.collection('test').findOne({ answer: 42 });
assert.equal(events.length, 2);
assert.equal(events[1].collectionName, 'test');
assert.equal(events[1].method, 'findOne');
assert.deepStrictEqual(events[1].result, { _id: 17, answer: 42 });
});
});

it('should allow closing a closed connection', async function() {
const db = mongoose.createConnection();

Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ declare module 'mongoose' {
export type NumberSchemaDefinition = typeof Number | 'number' | 'Number' | typeof Schema.Types.Number;
export type StringSchemaDefinition = typeof String | 'string' | 'String' | typeof Schema.Types.String;
export type BooleanSchemaDefinition = typeof Boolean | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean;
export type DateSchemaDefinition = typeof NativeDate | 'date' | 'Date' | typeof Schema.Types.Date;
export type DateSchemaDefinition = DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date;
export type ObjectIdSchemaDefinition = 'ObjectId' | 'ObjectID' | typeof Schema.Types.ObjectId;

export type SchemaDefinitionWithBuiltInClass<T> = T extends number
Expand Down

0 comments on commit 174d8be

Please sign in to comment.