summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-11-14 16:31:59 +0300
committereug-vs <eug-vs@keemail.me>2020-11-14 16:31:59 +0300
commit36465387454cdb797f886f732d40a70faa92cbad (patch)
tree28616bd04e3e98c5bd40e82b0c674a0ea9437357
parent2a791b8999dc08824baf6787021d45df676b4088 (diff)
downloadbsu-fantom-36465387454cdb797f886f732d40a70faa92cbad.tar.gz
refactor: reuse Mongoose connection in Agenda
-rw-r--r--index.js30
-rw-r--r--services/events/event.service.js8
-rw-r--r--src/app.js (renamed from app.js)0
-rw-r--r--src/connectDb.js40
-rw-r--r--src/services/events/event.service.js35
-rw-r--r--src/services/index.js (renamed from services/index.js)0
-rw-r--r--src/services/users/user.model.js (renamed from services/users/user.model.js)0
-rw-r--r--src/services/users/user.schema.js (renamed from services/users/user.schema.js)0
-rw-r--r--src/services/users/user.service.js (renamed from services/users/user.service.js)0
9 files changed, 81 insertions, 32 deletions
diff --git a/index.js b/index.js
index 28eefa4..f0778fd 100644
--- a/index.js
+++ b/index.js
@@ -1,30 +1,12 @@
-const mongoose = require('mongoose');
-const Promise = require('bluebird');
require('dotenv').config();
-const app = require('./app.js');
-
-mongoose.Promise = Promise;
+const app = require('./src/app.js');
+const { connectDb } = require('./src/connectDb.js');
const PORT = process.env.PORT || 3030;
-const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/bsu-fantom';
-const { MONGODB_USER, MONGODB_PASSWORD } = process.env;
-
-mongoose.connect(MONGODB_URL, {
- user: MONGODB_USER,
- pass: MONGODB_PASSWORD,
- useNewUrlParser: true,
- useUnifiedTopology: true,
- useCreateIndex: true,
- useFindAndModify: false,
- family: 4 // Use IPv4, skip trying IPv6
-});
-
-const db = mongoose.connection;
-db.on('error', console.error.bind(console, 'connection error:'));
-db.once('open', () => {
- console.log('Connection to MongoDB successful');
-});
-app.listen(PORT).on('listening', () => console.log(`Feathers server listening on localhost:${PORT}`));
+connectDb().then(() => app
+ .listen(PORT)
+ .on('listening', () => console.log(`Feathers server listening on localhost:${PORT}`))
+);
diff --git a/services/events/event.service.js b/services/events/event.service.js
deleted file mode 100644
index e96ceda..0000000
--- a/services/events/event.service.js
+++ /dev/null
@@ -1,8 +0,0 @@
-class Events {
- async create(data, params) {
- return data;
- };
-}
-
-module.exports = app => app.use('/events', new Events());
-
diff --git a/app.js b/src/app.js
index 45f9e61..45f9e61 100644
--- a/app.js
+++ b/src/app.js
diff --git a/src/connectDb.js b/src/connectDb.js
new file mode 100644
index 0000000..c7ad13b
--- /dev/null
+++ b/src/connectDb.js
@@ -0,0 +1,40 @@
+const mongoose = require('mongoose');
+const Promise = require('bluebird');
+
+const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/bsu-fantom';
+const { MONGODB_USER, MONGODB_PASSWORD } = process.env;
+
+let connection;
+
+const connectDb = () => new Promise ((resolve, reject) => {
+ mongoose.Promise = Promise;
+
+ mongoose.connect(MONGODB_URL, {
+ user: MONGODB_USER,
+ pass: MONGODB_PASSWORD,
+ useNewUrlParser: true,
+ useUnifiedTopology: true,
+ useCreateIndex: true,
+ useFindAndModify: false,
+ family: 4 // Use IPv4, skip trying IPv6
+ });
+
+ connection = mongoose.connection;
+
+ connection.on('error', error => {
+ console.log(error);
+ reject(error);
+ });
+
+ connection.once('open', () => {
+ console.log('Connection to MongoDB successfull')
+ resolve();
+ });
+});
+
+const getConnection = () => connection;
+
+module.exports = {
+ connectDb,
+ getConnection
+};
diff --git a/src/services/events/event.service.js b/src/services/events/event.service.js
new file mode 100644
index 0000000..5c5bcff
--- /dev/null
+++ b/src/services/events/event.service.js
@@ -0,0 +1,35 @@
+const Agenda = require('agenda');
+const { getConnection } = require('../../connectDb.js');
+
+
+class Events {
+ setup(app) {
+ this.collectionName = 'events';
+
+ const connection = getConnection();
+ this.agenda = new Agenda();
+ this.agenda.mongo(
+ connection.collection(this.collectionName).conn.db,
+ this.collectionName
+ );
+
+ this.defineJobs();
+
+ return this.agenda.start();
+ };
+
+ defineJobs() {
+ this.agenda.define('attend class', async job => {
+ console.log('Running attend class job with attrs:');
+ console.log(job.attrs);
+ });
+ };
+
+ async create(data, params) {
+ return this.agenda.schedule(data.date, 'attend class', { data });
+ };
+}
+
+
+module.exports = app => app.use('/events', new Events());
+
diff --git a/services/index.js b/src/services/index.js
index f2d65d0..f2d65d0 100644
--- a/services/index.js
+++ b/src/services/index.js
diff --git a/services/users/user.model.js b/src/services/users/user.model.js
index 74a83ae..74a83ae 100644
--- a/services/users/user.model.js
+++ b/src/services/users/user.model.js
diff --git a/services/users/user.schema.js b/src/services/users/user.schema.js
index f2c98ef..f2c98ef 100644
--- a/services/users/user.schema.js
+++ b/src/services/users/user.schema.js
diff --git a/services/users/user.service.js b/src/services/users/user.service.js
index 67f2ae6..67f2ae6 100644
--- a/services/users/user.service.js
+++ b/src/services/users/user.service.js