From 36465387454cdb797f886f732d40a70faa92cbad Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 14 Nov 2020 16:31:59 +0300 Subject: refactor: reuse Mongoose connection in Agenda --- app.js | 23 --------------------- index.js | 30 ++++++--------------------- services/events/event.service.js | 8 -------- services/index.js | 10 --------- services/users/user.model.js | 6 ------ services/users/user.schema.js | 14 ------------- services/users/user.service.js | 7 ------- src/app.js | 23 +++++++++++++++++++++ src/connectDb.js | 40 ++++++++++++++++++++++++++++++++++++ src/services/events/event.service.js | 35 +++++++++++++++++++++++++++++++ src/services/index.js | 10 +++++++++ src/services/users/user.model.js | 6 ++++++ src/services/users/user.schema.js | 14 +++++++++++++ src/services/users/user.service.js | 7 +++++++ 14 files changed, 141 insertions(+), 92 deletions(-) delete mode 100644 app.js delete mode 100644 services/events/event.service.js delete mode 100644 services/index.js delete mode 100644 services/users/user.model.js delete mode 100644 services/users/user.schema.js delete mode 100644 services/users/user.service.js create mode 100644 src/app.js create mode 100644 src/connectDb.js create mode 100644 src/services/events/event.service.js create mode 100644 src/services/index.js create mode 100644 src/services/users/user.model.js create mode 100644 src/services/users/user.schema.js create mode 100644 src/services/users/user.service.js diff --git a/app.js b/app.js deleted file mode 100644 index 45f9e61..0000000 --- a/app.js +++ /dev/null @@ -1,23 +0,0 @@ -const feathers = require('@feathersjs/feathers'); -const express = require('@feathersjs/express'); -const socketio = require('@feathersjs/socketio'); -const cors = require('cors') - -const services = require('./services'); - - -const app = express(feathers()); - -app.use(express.json()); -app.use(express.urlencoded({ extended: true })); -app.use(express.static(__dirname)); -app.use(express.errorHandler()); -app.use(cors()); - -app.configure(express.rest()); -app.configure(socketio()); -app.configure(services); - - -module.exports = app; - 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/services/index.js b/services/index.js deleted file mode 100644 index f2d65d0..0000000 --- a/services/index.js +++ /dev/null @@ -1,10 +0,0 @@ -const Users = require('./users/user.service.js'); -const Events = require('./events/event.service.js'); - -module.exports = app => { - app.configure(Users); - app.configure(Events); - - app.get('/ping', (req, res) => res.send('pong')); -}; - diff --git a/services/users/user.model.js b/services/users/user.model.js deleted file mode 100644 index 74a83ae..0000000 --- a/services/users/user.model.js +++ /dev/null @@ -1,6 +0,0 @@ -const { model } = require('mongoose'); -const userSchema = require('./user.schema.js'); - -module.exports = model('User', userSchema); - - diff --git a/services/users/user.schema.js b/services/users/user.schema.js deleted file mode 100644 index f2c98ef..0000000 --- a/services/users/user.schema.js +++ /dev/null @@ -1,14 +0,0 @@ -const { Schema } = require('mongoose'); - -module.exports = new Schema({ - username: { - type: String, - unique: true, - required: true - }, - password: { - type: String, - required: true - } -}, { timestamps: true }); - diff --git a/services/users/user.service.js b/services/users/user.service.js deleted file mode 100644 index 67f2ae6..0000000 --- a/services/users/user.service.js +++ /dev/null @@ -1,7 +0,0 @@ -const service = require('feathers-mongoose'); -const Model = require('./user.model.js'); - -const UserService = service({ Model }) - -module.exports = app => app.use('/users', UserService); - diff --git a/src/app.js b/src/app.js new file mode 100644 index 0000000..45f9e61 --- /dev/null +++ b/src/app.js @@ -0,0 +1,23 @@ +const feathers = require('@feathersjs/feathers'); +const express = require('@feathersjs/express'); +const socketio = require('@feathersjs/socketio'); +const cors = require('cors') + +const services = require('./services'); + + +const app = express(feathers()); + +app.use(express.json()); +app.use(express.urlencoded({ extended: true })); +app.use(express.static(__dirname)); +app.use(express.errorHandler()); +app.use(cors()); + +app.configure(express.rest()); +app.configure(socketio()); +app.configure(services); + + +module.exports = app; + 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/src/services/index.js b/src/services/index.js new file mode 100644 index 0000000..f2d65d0 --- /dev/null +++ b/src/services/index.js @@ -0,0 +1,10 @@ +const Users = require('./users/user.service.js'); +const Events = require('./events/event.service.js'); + +module.exports = app => { + app.configure(Users); + app.configure(Events); + + app.get('/ping', (req, res) => res.send('pong')); +}; + diff --git a/src/services/users/user.model.js b/src/services/users/user.model.js new file mode 100644 index 0000000..74a83ae --- /dev/null +++ b/src/services/users/user.model.js @@ -0,0 +1,6 @@ +const { model } = require('mongoose'); +const userSchema = require('./user.schema.js'); + +module.exports = model('User', userSchema); + + diff --git a/src/services/users/user.schema.js b/src/services/users/user.schema.js new file mode 100644 index 0000000..f2c98ef --- /dev/null +++ b/src/services/users/user.schema.js @@ -0,0 +1,14 @@ +const { Schema } = require('mongoose'); + +module.exports = new Schema({ + username: { + type: String, + unique: true, + required: true + }, + password: { + type: String, + required: true + } +}, { timestamps: true }); + diff --git a/src/services/users/user.service.js b/src/services/users/user.service.js new file mode 100644 index 0000000..67f2ae6 --- /dev/null +++ b/src/services/users/user.service.js @@ -0,0 +1,7 @@ +const service = require('feathers-mongoose'); +const Model = require('./user.model.js'); + +const UserService = service({ Model }) + +module.exports = app => app.use('/users', UserService); + -- cgit v1.2.3