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

Removed deprecated methods and modify schemas for mongoose 5.9.7 #24

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
40 changes: 10 additions & 30 deletions src/MongodbStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ const mongoose = require('mongoose');
const Store = require('./Store.js');

async function findOrCreate({ where, defaults }) {
return this.collection.findAndModify(
return this.findOneAndUpdate(
where,
[],
{ $setOnInsert: defaults },
{ upsert: true, new: true }, // return new doc if one is upserted
);
).exec();
}

const abuseSchema = new mongoose.Schema({
Expand All @@ -26,7 +25,7 @@ const abuseSchema = new mongoose.Schema({
type: Date,
required: true,
},
});
}, { timestamps: true });
abuseSchema.statics.findOrCreate = findOrCreate;

const abuseHistorySchema = new mongoose.Schema({
Expand All @@ -52,7 +51,7 @@ const abuseHistorySchema = new mongoose.Schema({
default: 0,
},
userId: {
type: Number,
type: String,
required: false,
},
ip: {
Expand All @@ -63,26 +62,9 @@ const abuseHistorySchema = new mongoose.Schema({
type: Date,
required: true,
},
createdAt: {
type: Date,
required: true,
default: Date.now,
},
updatedAt: {
type: Date,
required: true,
default: Date.now,
},
});
}, { timestamps: true });
abuseHistorySchema.index({ key: 1, dateEnd: 1 }, { unique: true });

function beforSave(next) {
this.updatedAt = Date.now();
next();
}
abuseHistorySchema.pre('save', beforSave);
abuseHistorySchema.pre('update', beforSave);
abuseHistorySchema.pre('findOneAndUpdate', beforSave);
abuseHistorySchema.statics.findOrCreate = findOrCreate;

class MongodbStore extends Store {
Expand All @@ -100,7 +82,7 @@ class MongodbStore extends Store {

// remove all if time is passed
async _removeAll() {
await this.Ratelimits.remove({ dateEnd: { $lte: Date.now() } });
await this.Ratelimits.deleteMany({ dateEnd: { $lte: Date.now() } });
}

async incr(key, options, weight) {
Expand All @@ -116,8 +98,8 @@ class MongodbStore extends Store {
});
await this._increment(this.Ratelimits, { key }, weight, 'counter');
return {
counter: data.value.counter + weight,
dateEnd: data.value.dateEnd,
counter: data.counter + weight,
dateEnd: data.dateEnd,
};
}

Expand All @@ -131,8 +113,7 @@ class MongodbStore extends Store {
}).exec();

if (ratelimit) {
// eslint-disable-next-line
const dateEnd = ratelimit.dateEnd;
const { dateEnd } = ratelimit;
// create if not exist
await this.Abuse.findOrCreate({
where: { key: options.key, dateEnd },
Expand All @@ -146,8 +127,7 @@ class MongodbStore extends Store {
ip: options.ip,
dateEnd,
},
}).catch(() => {});

});
await this._increment(this.Abuse, { key: options.key, dateEnd }, 1, 'nbHit');
}
}
Expand Down