Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongodb upgrade #3

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 59 additions & 34 deletions lib/strategies/array.js
Original file line number Diff line number Diff line change
@@ -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();
}
Expand All @@ -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]);
}
Expand All @@ -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;
});

Expand All @@ -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
Expand All @@ -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();
});
}
});
}
};
20 changes: 13 additions & 7 deletions lib/strategies/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
});
}
});

}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"version"
],
"dependencies": {
"mongoose": "4"
"mongoose": ">=8"
},
"devDependencies": {
"mocha": "1.9.x",
Expand Down
Loading