Skip to content

Commit

Permalink
Updates to utilize a callbackify wrapper to get around a node issue n…
Browse files Browse the repository at this point in the history
  • Loading branch information
owenallenaz committed May 17, 2019
1 parent 1f4a536 commit f482f2c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
} = require("mongodb");

const {
callbackify,
errors,
getMyHooks,
getMyFields,
Expand Down Expand Up @@ -676,7 +677,7 @@ async function aggregate(pipeline, options = {}) {
return args.docs;
}

Model.prototype.aggregate = util.callbackify(aggregate);
Model.prototype.aggregate = callbackify(aggregate);

async function findById(id, options = {}) {
var self = this;
Expand All @@ -685,7 +686,7 @@ async function findById(id, options = {}) {
return docs.length === 0 ? null : docs[0];
}

Model.prototype.findById = util.callbackify(findById);
Model.prototype.findById = callbackify(findById);

async function find(filter, options = {}) {
var self = this;
Expand Down Expand Up @@ -786,7 +787,7 @@ async function find(filter, options = {}) {
}
}

Model.prototype.find = util.callbackify(find);
Model.prototype.find = callbackify(find);

Model.prototype.count = function(filter, options, cb) {
var self = this;
Expand Down Expand Up @@ -1155,7 +1156,7 @@ async function _executeHooks(args) {

Model.prototype._executeHooksP = _executeHooks;

Model.prototype._executeHooks = util.callbackify(_executeHooks);
Model.prototype._executeHooks = callbackify(_executeHooks);

var _getMyFindFields_regex = /^(\w+?)\./;
Model.prototype._getMyFindFields = function(fields) {
Expand Down
9 changes: 9 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { ObjectID } = require("mongodb");
const typecaster = require("typecaster");
const extend = require("extend");
const async = require("async");
const util = require("util");

const objectLib = require("./lib/objectLib.js");
const arrayLib = require("./lib/arrayLib.js");
Expand Down Expand Up @@ -457,8 +458,16 @@ function _newErrorType(name) {
return CustomErrorType;
}

function callbackify(fn) {
const newFn = util.callbackify(fn);
return function(...args) {
return newFn.call(this, ...args);
}
}

module.exports = {
_newErrorType,
callbackify,
convertValue,
errors : {
ValidationError : _newErrorType("ValidationError")
Expand Down
13 changes: 13 additions & 0 deletions testing/Model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,19 @@ describe(__filename, function() {
});
});

it("should have valid callbackified functions", function() {
var model = new mongolayer.Model({
collection : "foo"
});

var functions = ["find", "findById", "insert", "update", "save", "aggregate", "remove"];

functions.forEach(function(val, i) {
assert.strictEqual(model[val].constructor.name, "Function");
assert.notStrictEqual(model[val][Symbol.toStringTag], "AsyncFunction");
});
});

describe("conversion", function() {
var model;

Expand Down

0 comments on commit f482f2c

Please sign in to comment.