/* * Main database access functions */ var common, model = {}; const dbs = { /* Method API route * <- to server * -> to client * Description */ routes: { "msg/ids": { /* GET /api/msg/ids [no args] * -> Array of message schema object ids * Get ALL known message ids */ get: function(req, res) { model.Messages.find({},{_id: true}).exec() .then(results => { //selects id from message: var parsed = []; for (var i in results) { parsed.push (results[i]._id); } res.send(parsed); } ) .catch(err => { console.log (err); res.status(500).json(err); }); }, }, "msg/id": { params: ":id", /* GET /api/msg/id/[massage-id] * -> Message schema * Get a particular message */ get: function(req, res) { model.Messages.findById(req.params.id) .exec(function(err, result) { if (err) { console.log (err); res.status(404).json(err); } else { //console.log(JSON.stringify(result)); res.json(result); } }); }, }, "msg/search": { params: ":phrase", /* GET /api/msg/id/[massage-id] * -> Message schema * Get a particular message */ get: function(req, res) { model.Messages.find({$text: {$search: req.params.phrase}}) .exec().then(results => { //selects id from message: var parsed = []; for (var i in results) { parsed.push (results[i]._id); } res.send(parsed); } ) .catch(err => { console.log (err); res.status(500).json(err); }); }, }, "msg": { /* POST /api/msg * <- Message schema * -> Message schema * Create a new message */ post: function(req, res) { /* if ( !(req.body.tags instanceof Array) ) { return res.status(400).json({ error: "bad request" }); }*/ //console.log("Subject: "+JSON.stringify(req.body)); model.Messages.create({ subject: req.body.subject, message: req.body.message, user: req.body.user, tag: req.body.tag }, function(err, result) { if (err) { return res.status(401).json(err.message); } else { res.json({message: "Message created!!"}); } if (result == null) { return res.status(500).json("Can not create message.") } }); }, /* PUT /api/msg * <- * -> * Update a message */ //put: function(req, res) {}, }, "tag/ids": { /* GET /api/tag/ids [no args] * -> Array of tag schema object ids * Get ALL known tag ids */ get: function(req, res) { model.Tags.find({},{_id: true}).exec() .then(results => { //selects id from tag: var parsed = []; for (var i in results) { parsed.push (results[i]._id); } res.send(parsed); } ) .catch(err => { console.log (err); res.status(500).json(err); }); }, }, "tag": { params: ":id", /* GET /api/tag/[tag-id] * -> Tag schema * Get a particular tag */ get: function(req, res) { model.Tags.findById(req.params.id) .exec(function(err, result) { if (err) { console.log (err); res.status(404).json(err); } else { //console.log(JSON.stringify(result)); res.json(result); } }); }, }, }, /* Initialize requirements * - DB connection * - DB schemata */ init: function (_common) { common = _common; /* DB Connection * Local db: common.config.dbLocalConn * TH db: common.config.dbConn */ common.mongoose.connect (common.config.dbLocalConn, { useNewUrlParser: true }).then(() => { console.log("Database connected successfully."); }).catch(err => { console.log('Database connection error.'); process.exit(); }); /* DB Schemata * Privat fields: * - per model: _list: Elements that are included in list fetch * - per entry: _comment: Comment for Admin UI - TODO: not working yet */ var messageSchema = common.mongoose.Schema({ subject: { type: String, required: true, _comment: "" }, message: { type: String, required: true, _comment: "" }, user: { type: String, required: true, _comment: "" }, tag: { type: [String], _comment: "" }, //createtime: { type: Date, default: Date.now }, }); messageSchema.index({ tag:'text', subject:'text', message:'text', user:'text' }); model.Messages = common.mongoose.model('messages', messageSchema); model.Messages._list = [ "" ]; var tagSchema = common.mongoose.Schema({ name: { type: String, required: true, _comment: "" }, //unique }); model.Tags = common.mongoose.model('tags', messageSchema); model.Tags._list = [ "" ]; var userSchema = common.mongoose.Schema({ //_id: { type: String }, name: { type: String, required: true, _comment: "" }, pwd: { type: String, _comment: "" }, //hash: { type: String }, //salt: { type: String }, //type: { type: String }, roles: { type: [String], required: true, _comment: "" }, tags: { type: [String], _comment: "" }, //deactivated: { type: Boolean }, //participating: { type: [String] }, //host: { type: Boolean }, bookmarks: { type: [String], _comment: "" }, }); model.Users = common.mongoose.model('users', userSchema); model.Users._list = [ "" ]; }, models: model, }; /* app.get ("/api/msg/search/:phrase", function (req, res) { Message.find ({$text: {$search: req.params.phrase}) .then (function (err, results){ if (err) { console.log (err); res .status(404) .json (err); } else { console.log(JSON.stringify(results)); res.json(results); } }); }); */ module.exports = dbs;