diff --git a/package-lock.json b/package-lock.json index 210f9a5..c72166e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -385,14 +385,6 @@ "integrity": "sha1-n7OjzzEyMoFR81PeRjLgHlIQK+o=", "dev": true }, - "@szmarczak/http-timer": { - "version": "4.0.5", - "resolved": "https://r.cnpmjs.org/@szmarczak/http-timer/download/@szmarczak/http-timer-4.0.5.tgz", - "integrity": "sha1-v71QIR6d+lG6B9pYoUzf0zMgUVI=", - "requires": { - "defer-to-connect": "^2.0.0" - } - }, "@types/bcrypt": { "version": "3.0.0", "resolved": "https://r.cnpmjs.org/@types/bcrypt/download/@types/bcrypt-3.0.0.tgz", @@ -1297,11 +1289,6 @@ "strip-bom": "^4.0.0" } }, - "defer-to-connect": { - "version": "2.0.0", - "resolved": "https://r.cnpmjs.org/defer-to-connect/download/defer-to-connect-2.0.0.tgz", - "integrity": "sha1-g9axmdsEFZOshNeBtSIjCMz0wsE=" - }, "define-properties": { "version": "1.1.3", "resolved": "https://r.cnpmjs.org/define-properties/download/define-properties-1.1.3.tgz", @@ -2603,6 +2590,15 @@ "resolved": "https://r.cnpmjs.org/nocache/download/nocache-2.1.0.tgz", "integrity": "sha1-Egyf/sQ7Vymx1d6IzXGqdaC6SR8=" }, + "node-cron": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-2.0.3.tgz", + "integrity": "sha512-eJI+QitXlwcgiZwNNSRbqsjeZMp5shyajMR81RZCqeW0ZDEj4zU9tpd4nTh/1JsBiKbF8d08FCewiipDmVIYjg==", + "requires": { + "opencollective-postinstall": "^2.0.0", + "tz-offset": "0.0.1" + } + }, "node-environment-flags": { "version": "1.0.6", "resolved": "https://r.cnpmjs.org/node-environment-flags/download/node-environment-flags-1.0.6.tgz", @@ -2958,6 +2954,11 @@ "resolved": "https://r.cnpmjs.org/openapi-types/download/openapi-types-1.3.5.tgz", "integrity": "sha1-ZxjPvIV/5sbxRx9lsyveu5wQzkA=" }, + "opencollective-postinstall": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/opencollective-postinstall/-/opencollective-postinstall-2.0.3.tgz", + "integrity": "sha512-8AV/sCtuzUeTo8gQK5qDZzARrulB3egtLzFgteqB2tcT4Mw7B8Kt7JcDHmltjz6FOAHsvTevk70gZEbhM4ZS9Q==" + }, "os-tmpdir": { "version": "1.0.2", "resolved": "https://r.cnpmjs.org/os-tmpdir/download/os-tmpdir-1.0.2.tgz", @@ -3936,6 +3937,11 @@ "integrity": "sha1-mNYApevcOPQMsndSLxLcgA6eJfo=", "dev": true }, + "tz-offset": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/tz-offset/-/tz-offset-0.0.1.tgz", + "integrity": "sha512-kMBmblijHJXyOpKzgDhKx9INYU4u4E1RPMB0HqmKSgWG8vEcf3exEfLh4FFfzd3xdQOw9EuIy/cP0akY6rHopQ==" + }, "undefsafe": { "version": "2.0.3", "resolved": "https://r.cnpmjs.org/undefsafe/download/undefsafe-2.0.3.tgz", diff --git a/package.json b/package.json index 2097e66..baed92e 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "json2csv": "^5.0.1", "lodash": "^4.17.15", "mongoose": "^5.8.7", + "node-cron": "^2.0.3", "swagger-ui-dist": "^3.30.2" }, "devDependencies": { diff --git a/src/db.ts b/src/db.ts index 5f4face..2e8592d 100644 --- a/src/db.ts +++ b/src/db.ts @@ -2,12 +2,14 @@ import mongoose from 'mongoose'; import cfenv from 'cfenv'; import _ from 'lodash'; import ChangelogModel from './models/changelog'; +import cron from 'node-cron'; // database urls, prod db url is retrieved automatically const TESTING_URL = 'mongodb://localhost/dfopdb_test'; const DEV_URL = 'mongodb://localhost/dfopdb'; const debugging = true; +const changelogKeepDays = 30; // days to keep the changelog if (process.env.NODE_ENV !== 'production' && debugging) { mongoose.set('debug', true); // enable mongoose debug @@ -78,6 +80,16 @@ export default class db { this.state.db = mongoose.connection; done(); }); + + if (mode !== 'test') { // clear old changelog regularly + cron.schedule('0 0 * * *', () => { + ChangelogModel.deleteMany({_id: {$lt: // id from time + Math.floor(new Date().getTime() / 1000 - changelogKeepDays * 24 * 60 * 60).toString(16) + '0000000000000000' + }}).log({method: 'scheduled changelog delete', url: '', authDetails: {}}).lean().exec(err => { + if (err) console.error(err); + }); + }); + } } static disconnect (done) {