summaryrefslogtreecommitdiff
path: root/src/services
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 /src/services
parent2a791b8999dc08824baf6787021d45df676b4088 (diff)
downloadbsu-fantom-36465387454cdb797f886f732d40a70faa92cbad.tar.gz
refactor: reuse Mongoose connection in Agenda
Diffstat (limited to 'src/services')
-rw-r--r--src/services/events/event.service.js35
-rw-r--r--src/services/index.js10
-rw-r--r--src/services/users/user.model.js6
-rw-r--r--src/services/users/user.schema.js14
-rw-r--r--src/services/users/user.service.js7
5 files changed, 72 insertions, 0 deletions
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);
+