diff --git a/models/Histories.js b/models/Histories.js new file mode 100644 index 0000000..873e6c5 --- /dev/null +++ b/models/Histories.js @@ -0,0 +1,15 @@ +module.exports = mongoose => { + const schema = new mongoose.Schema({ + radio: String, + artist: String, + title: String, + createdAt: { + type: Date, + default: Date.now + } + }) + + const Histories = mongoose.model('Histories', schema) + + return Histories +} diff --git a/models/Notifications.js b/models/Notifications.js new file mode 100644 index 0000000..bb20680 --- /dev/null +++ b/models/Notifications.js @@ -0,0 +1,14 @@ +module.exports = mongoose => { + const schema = new mongoose.Schema({ + user: String, + property: { + type: String, + enum: ['artist', 'title'] + }, + value: String + }) + + const Notifications = mongoose.model('Notifications', schema) + + return Notifications +} diff --git a/models/index.js b/models/index.js new file mode 100644 index 0000000..b59cba8 --- /dev/null +++ b/models/index.js @@ -0,0 +1,41 @@ +const fs = require('fs') +const path = require('path') +const mongoose = require('mongoose') + +const basename = path.basename(__filename) + +mongoose.set('useNewUrlParser', true) +mongoose.set('useUnifiedTopology', true) +mongoose.set('useFindAndModify', false) +mongoose.set('useCreateIndex', true) +mongoose.set('debug', true) + +mongoose.connect(process.env.MONGO_URL) + +const db = mongoose.connection + +const getSchemas = () => { + const m = {} + + fs.readdirSync(__dirname) + .filter(file => { + return ( + file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js' + ) + }) + .forEach(file => { + const model = require(path.resolve(__dirname, file))(mongoose) + m[model.modelName] = model + }) + + return m +} + +db.on('error', console.error.bind(console, 'Mongodb connection error:')) + +const models = getSchemas() + +module.exports = { + models: models, + mongoose: mongoose +}