diff --git a/lib/strategies/array.js b/lib/strategies/array.js index f9a71e2..65d3bb9 100644 --- a/lib/strategies/array.js +++ b/lib/strategies/array.js @@ -1,23 +1,30 @@ -var cloneSchema = require('../clone-schema'); +var cloneSchema = require("../clone-schema"); -module.exports = function(schema, options) { +module.exports = function (schema, options) { var clonedSchema = cloneSchema(schema); - clonedSchema.add({ refVersion : Number }); + clonedSchema.add({ refVersion: Number }); var mongoose = options.mongoose; var Schema = mongoose.Schema; var ObjectId = Schema.Types.ObjectId; - var versionedSchema = new Schema({ refId : ObjectId, created : Date, modified : Date, versions : [clonedSchema] }); + var versionedSchema = new Schema({ + refId: ObjectId, + created: Date, + modified: Date, + versions: [clonedSchema], + }); if (options.documentProperty) { var documentPropertyField = {}; - documentPropertyField[options.documentProperty] = clonedSchema.path(options.documentProperty).options; + documentPropertyField[options.documentProperty] = clonedSchema.path( + options.documentProperty + ).options; versionedSchema.add(documentPropertyField); } - - versionedSchema.pre('save', function(next) { + + versionedSchema.pre("save", function (next) { if (!this.created) { this.created = new Date(); } @@ -27,20 +34,25 @@ module.exports = function(schema, options) { next(); }); - versionedSchema.statics.latest = function(count, cb) { - if (typeof(count) == 'function') { + versionedSchema.statics.latest = async function (count, cb) { + if (typeof count == "function") { cb = count; count = 10; } - return this - .find({}) - .limit(count) - .sort('-created') - .exec(cb); + try { + const result = await this.find({}) + .limit(count) + .sort("-created") + .exec(); + + return cb(null, result); + } catch (err) { + return cb(err); + } }; - - for(var key in options) { + + for (var key in options) { if (options.hasOwnProperty(key)) { versionedSchema.set(key, options[key]); } @@ -50,17 +62,17 @@ module.exports = function(schema, options) { var VersionedModel = mongoose.model(options.collection, versionedSchema); schema.statics.VersionedModel = VersionedModel; - schema.pre('save', function(next) { + schema.pre("save", async function (next) { var self = this; if (!options.suppressVersionIncrement) { - this.increment(); // Increment origins version + this.increment(); // Increment origins version } var modifiedPaths = this.modifiedPaths(); if (modifiedPaths.length) { - var onlyIgnoredPathModified = modifiedPaths.every(function(path) { + var onlyIgnoredPathModified = modifiedPaths.every(function (path) { return options.ignorePaths.indexOf(path) >= 0; }); @@ -69,14 +81,22 @@ module.exports = function(schema, options) { } } - VersionedModel.findOne({ refId : this._id }, function(err, versionedModel) { + try { + const versionedModel = await VersionedModel.findOne({ + refId: this._id, + }); + if (!versionedModel) { - versionedModel = new VersionedModel({ refId : self._id, versions : [] }); + versionedModel = new VersionedModel({ + refId: self._id, + versions: [], + }); } // Set a document identifier in case it was specified in options if (options.documentProperty) { - versionedModel[options.documentProperty] = self[options.documentProperty]; + versionedModel[options.documentProperty] = + self[options.documentProperty]; } // copy but don't deep clone @@ -96,28 +116,33 @@ module.exports = function(schema, options) { versionedModel.versions.shift(); } - versionedModel.save(function(err) { - if (options.logError && err) { - console.log(err); - } + await versionedModel.save(); - next(); - }); - }); - }); + return next(); + } catch (err) { + if (options.logError && err) { + console.log(err); + } + return next(); + } + }); - schema.pre('remove', function(next) { + schema.pre("remove", async function (next) { if (!options.removeVersions) { return next(); } - VersionedModel.remove({ refId : this._id }, function(err) { + try { + await VersionedModel.remove({ refId: this._id }).exec(); + + return next(); + } catch (err) { if (options.logError && err) { console.log(err); } next(); - }); + } }); -} \ No newline at end of file +}; diff --git a/lib/strategies/collection.js b/lib/strategies/collection.js index 4e631f3..ada95c5 100644 --- a/lib/strategies/collection.js +++ b/lib/strategies/collection.js @@ -26,21 +26,25 @@ module.exports = function(schema, options) { next(); }); - schema.pre('remove', function(next) { + schema.pre('remove', async function(next) { if (!options.removeVersions) { return next(); } - schema.statics.VersionedModel.remove({ refId : this._id }, function(err) { + try { + await schema.statics.VersionedModel.remove({ refId : this._id }).exec(); + + return next(); + } catch (err) { if (options.logError && err) { console.log(err); } - next(); - }); + return next(); + } }); - schema.post('save', function () { + schema.post('save', async function () { if (!this.modifiedFields.length) { return; @@ -62,11 +66,13 @@ module.exports = function(schema, options) { versionedModel.refId = this._id; // Sets origins document id as a reference versionedModel.dateCreated = new Date(); - versionedModel.save(function(err) { + try { + await versionedModel.save(); + } catch (err) { if (options.logError && err) { console.log(err); } - }); + } }); } diff --git a/package.json b/package.json index edcbeb1..a2241ee 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "version" ], "dependencies": { - "mongoose": "4" + "mongoose": ">=8" }, "devDependencies": { "mocha": "1.9.x", diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..64af0b0 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,214 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@mongodb-js/saslprep@^1.1.0": + version "1.1.4" + resolved "https://registry.yarnpkg.com/@mongodb-js/saslprep/-/saslprep-1.1.4.tgz#24ec1c4915a65f5c506bb88c081731450d91bb1c" + integrity sha512-8zJ8N1x51xo9hwPh6AWnKdLGEC5N3lDa6kms1YHmFBoRhTpJR6HG8wWk0td1MVCu9cD4YBrvjZEtd5Obw0Fbnw== + dependencies: + sparse-bitfield "^3.0.3" + +"@types/webidl-conversions@*": + version "7.0.3" + resolved "https://registry.yarnpkg.com/@types/webidl-conversions/-/webidl-conversions-7.0.3.tgz#1306dbfa53768bcbcfc95a1c8cde367975581859" + integrity sha512-CiJJvcRtIgzadHCYXw7dqEnMNRjhGZlYK05Mj9OyktqV8uVT8fD2BFOB7S1uwBE3Kj2Z+4UyPmFw/Ixgw/LAlA== + +"@types/whatwg-url@^11.0.2": + version "11.0.4" + resolved "https://registry.yarnpkg.com/@types/whatwg-url/-/whatwg-url-11.0.4.tgz#ffed0dc8d89d91f62e3f368fcbda222a487c4f63" + integrity sha512-lXCmTWSHJvf0TRSO58nm978b8HJ/EdsSsEKLd3ODHFjo+3VGAyyTp4v50nWvwtzBxSMQrVOK7tcuN0zGPLICMw== + dependencies: + "@types/webidl-conversions" "*" + +assertion-error@1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/assertion-error/-/assertion-error-1.0.0.tgz#c7f85438fdd466bc7ca16ab90c81513797a5d23b" + integrity sha512-g/gZV+G476cnmtYI+Ko9d5khxSoCSoom/EaNmmCfwpOvBXEJ18qwFrxfP1/CsIqk2no1sAKKwxndV0tP7ROOFQ== + +bson@^6.2.0: + version "6.3.0" + resolved "https://registry.yarnpkg.com/bson/-/bson-6.3.0.tgz#d47acba525ba7d7eb0e816c10538bce26a337fe0" + integrity sha512-balJfqwwTBddxfnidJZagCBPP/f48zj9Sdp3OJswREOgsJzHiQSaOIAtApSgDQFYgHqAvFkp53AFSqjMDZoTFw== + +chai@~1.9.0: + version "1.9.2" + resolved "https://registry.yarnpkg.com/chai/-/chai-1.9.2.tgz#3f1a20f82b0b9d7437577d24d6f12b1a69d3b590" + integrity sha512-olRoaitftnzWHFEAza6MXR4w+FfZrOVyV7r7U/Z8ObJefCgL8IuWkAuASJjSXrpP9wvgoL8+1dB9RbMLc2FkNg== + dependencies: + assertion-error "1.0.0" + deep-eql "0.1.3" + +commander@0.6.1: + version "0.6.1" + resolved "https://registry.yarnpkg.com/commander/-/commander-0.6.1.tgz#fa68a14f6a945d54dbbe50d8cdb3320e9e3b1a06" + integrity sha512-0fLycpl1UMTGX257hRsu/arL/cUbcvQM4zMKwvLvzXtfdezIV4yotPS2dYtknF+NmEfWSoCEF6+hj9XLm/6hEw== + +debug@*, debug@4.x: + version "4.3.4" + resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865" + integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ== + dependencies: + ms "2.1.2" + +deep-eql@0.1.3: + version "0.1.3" + resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-0.1.3.tgz#ef558acab8de25206cd713906d74e56930eb69f2" + integrity sha512-6sEotTRGBFiNcqVoeHwnfopbSpi5NbH1VWJmYCVkmxMmaVTT0bUTrNaGyBwhgP4MZL012W/mkzIn3Da+iDYweg== + dependencies: + type-detect "0.1.1" + +diff@1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/diff/-/diff-1.0.2.tgz#4ae73f1aee8d6fcf484f1a1ce77ce651d9b7f0c9" + integrity sha512-BOZXenW4qYFnn8GhH24O4xfjF5CxT01uSYOfF/hGpTGFcs/50zc5nnF1AtV1ePP/ok4hGC9ZENrLtm5jjj16GA== + +growl@1.7.x: + version "1.7.0" + resolved "https://registry.yarnpkg.com/growl/-/growl-1.7.0.tgz#de2d66136d002e112ba70f3f10c31cf7c350b2da" + integrity sha512-VWv7s1EI41AG2LiCr7uAuxWikLDN1SQOuEUc37d/P34NAIIYgkvWYngNw0d9d9iCrDFL0SYCE9UQpxhIjjtuLg== + +jade@0.26.3: + version "0.26.3" + resolved "https://registry.yarnpkg.com/jade/-/jade-0.26.3.tgz#8f10d7977d8d79f2f6ff862a81b0513ccb25686c" + integrity sha512-mkk3vzUHFjzKjpCXeu+IjXeZD+QOTjUUdubgmHtHTDwvAO2ZTkMTTVrapts5CWz3JvJryh/4KWZpjeZrCepZ3A== + dependencies: + commander "0.6.1" + mkdirp "0.3.0" + +kareem@2.5.1: + version "2.5.1" + resolved "https://registry.yarnpkg.com/kareem/-/kareem-2.5.1.tgz#7b8203e11819a8e77a34b3517d3ead206764d15d" + integrity sha512-7jFxRVm+jD+rkq3kY0iZDJfsO2/t4BBPeEb2qKn2lR/9KhuksYk5hxzfRYWMPV8P/x2d0kHD306YyWLzjjH+uA== + +memory-pager@^1.0.2: + version "1.5.0" + resolved "https://registry.yarnpkg.com/memory-pager/-/memory-pager-1.5.0.tgz#d8751655d22d384682741c972f2c3d6dfa3e66b5" + integrity sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg== + +mkdirp@0.3.0: + version "0.3.0" + resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.3.0.tgz#1bbf5ab1ba827af23575143490426455f481fe1e" + integrity sha512-OHsdUcVAQ6pOtg5JYWpCBo9W/GySVuwvP9hueRMW7UqshC0tbfzLv8wjySTPm3tfUZ/21CE9E1pJagOA91Pxew== + +mkdirp@0.3.3: + version "0.3.3" + resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.3.3.tgz#595e251c1370c3a68bab2136d0e348b8105adf13" + integrity sha512-Oamd41MnZw/yuxtarGf3MFbHzFqQY4S17DcN+rATh2t5MKuCtG7vVVRG+RUT6g9+hr47DIVucIHGOUlwmJRvDA== + +mocha@1.9.x: + version "1.9.0" + resolved "https://registry.yarnpkg.com/mocha/-/mocha-1.9.0.tgz#141054b13cb03ce5ce59aece3d65d5ca01b8df0a" + integrity sha512-Jl+ayJV6248+dwVqmWfz46GNv2wUMeKUorcwDaYiGUnCzSXO0cT3sxri3he8YvUHXfwnE9et+sibGOxvded1GQ== + dependencies: + commander "0.6.1" + debug "*" + diff "1.0.2" + growl "1.7.x" + jade "0.26.3" + mkdirp "0.3.3" + ms "0.3.0" + +mongodb-connection-string-url@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/mongodb-connection-string-url/-/mongodb-connection-string-url-3.0.0.tgz#b4f87f92fd8593f3b9365f592515a06d304a1e9c" + integrity sha512-t1Vf+m1I5hC2M5RJx/7AtxgABy1cZmIPQRMXw+gEIPn/cZNF3Oiy+l0UIypUwVB5trcWHq3crg2g3uAR9aAwsQ== + dependencies: + "@types/whatwg-url" "^11.0.2" + whatwg-url "^13.0.0" + +mongodb@6.3.0: + version "6.3.0" + resolved "https://registry.yarnpkg.com/mongodb/-/mongodb-6.3.0.tgz#ec9993b19f7ed2ea715b903fcac6171c9d1d38ca" + integrity sha512-tt0KuGjGtLUhLoU263+xvQmPHEGTw5LbcNC73EoFRYgSHwZt5tsoJC110hDyO1kjQzpgNrpdcSza9PknWN4LrA== + dependencies: + "@mongodb-js/saslprep" "^1.1.0" + bson "^6.2.0" + mongodb-connection-string-url "^3.0.0" + +mongoose-text-search@0.0.2: + version "0.0.2" + resolved "https://registry.yarnpkg.com/mongoose-text-search/-/mongoose-text-search-0.0.2.tgz#0bd54954584a011d25655de7a70674ad50bce19e" + integrity sha512-aB5l4IU45SZIn99S/X3qpsePXacWYreG/P20sIQXgSvg3wkn4jgo08A1iFv2sjRLKWDctJE/Kx72Lp8q90AGLQ== + +mongoose@>=8: + version "8.1.1" + resolved "https://registry.yarnpkg.com/mongoose/-/mongoose-8.1.1.tgz#2ea2dcdcf4943196f585b3915f63001e79214e1b" + integrity sha512-DbLb0NsiEXmaqLOpEz+AtAsgwhRw6f25gwa1dF5R7jj6lS1D8X6uTdhBSC8GDVtOwe5Tfw2EL7nTn6hiJT3Bgg== + dependencies: + bson "^6.2.0" + kareem "2.5.1" + mongodb "6.3.0" + mpath "0.9.0" + mquery "5.0.0" + ms "2.1.3" + sift "16.0.1" + +mpath@0.9.0: + version "0.9.0" + resolved "https://registry.yarnpkg.com/mpath/-/mpath-0.9.0.tgz#0c122fe107846e31fc58c75b09c35514b3871904" + integrity sha512-ikJRQTk8hw5DEoFVxHG1Gn9T/xcjtdnOKIU1JTmGjZZlg9LST2mBLmcX3/ICIbgJydT2GOc15RnNy5mHmzfSew== + +mquery@5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/mquery/-/mquery-5.0.0.tgz#a95be5dfc610b23862df34a47d3e5d60e110695d" + integrity sha512-iQMncpmEK8R8ncT8HJGsGc9Dsp8xcgYMVSbs5jgnm1lFHTZqMJTUWTDx1LBO8+mK3tPNZWFLBghQEIOULSTHZg== + dependencies: + debug "4.x" + +ms@0.3.0: + version "0.3.0" + resolved "https://registry.yarnpkg.com/ms/-/ms-0.3.0.tgz#03edc348d613e66a56486cfdac53bcbe899cbd61" + integrity sha512-25BVmSAdN4KRX7XeI6/gwQ9ewx6t9QB9/8X2fVJUUDpPc03qTRaEPgt5bTMZQ5T2l+XT+haSfqIkysOupDsSVQ== + +ms@2.1.2: + version "2.1.2" + resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009" + integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w== + +ms@2.1.3: + version "2.1.3" + resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2" + integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA== + +punycode@^2.3.0: + version "2.3.1" + resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.3.1.tgz#027422e2faec0b25e1549c3e1bd8309b9133b6e5" + integrity sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg== + +sift@16.0.1: + version "16.0.1" + resolved "https://registry.yarnpkg.com/sift/-/sift-16.0.1.tgz#e9c2ccc72191585008cf3e36fc447b2d2633a053" + integrity sha512-Wv6BjQ5zbhW7VFefWusVP33T/EM0vYikCaQ2qR8yULbsilAT8/wQaXvuQ3ptGLpoKx+lihJE3y2UTgKDyyNHZQ== + +sparse-bitfield@^3.0.3: + version "3.0.3" + resolved "https://registry.yarnpkg.com/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz#ff4ae6e68656056ba4b3e792ab3334d38273ca11" + integrity sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ== + dependencies: + memory-pager "^1.0.2" + +tr46@^4.1.1: + version "4.1.1" + resolved "https://registry.yarnpkg.com/tr46/-/tr46-4.1.1.tgz#281a758dcc82aeb4fe38c7dfe4d11a395aac8469" + integrity sha512-2lv/66T7e5yNyhAAC4NaKe5nVavzuGJQVVtRYLyQ2OI8tsJ61PMLlelehb0wi2Hx6+hT/OJUWZcw8MjlSRnxvw== + dependencies: + punycode "^2.3.0" + +type-detect@0.1.1: + version "0.1.1" + resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-0.1.1.tgz#0ba5ec2a885640e470ea4e8505971900dac58822" + integrity sha512-5rqszGVwYgBoDkIm2oUtvkfZMQ0vk29iDMU0W2qCa3rG0vPDNczCMT4hV/bLBgLg8k8ri6+u3Zbt+S/14eMzlA== + +webidl-conversions@^7.0.0: + version "7.0.0" + resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-7.0.0.tgz#256b4e1882be7debbf01d05f0aa2039778ea080a" + integrity sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g== + +whatwg-url@^13.0.0: + version "13.0.0" + resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-13.0.0.tgz#b7b536aca48306394a34e44bda8e99f332410f8f" + integrity sha512-9WWbymnqj57+XEuqADHrCJ2eSXzn8WXIW/YSGaZtb2WKAInQ6CHfaUUcTyyver0p8BDg5StLQq8h1vtZuwmOig== + dependencies: + tr46 "^4.1.1" + webidl-conversions "^7.0.0"