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 --- 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 +++++++ 5 files changed, 72 insertions(+) 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 (limited to 'src/services') 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