|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- /*
- * 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}})
- .sort({tag:1, user:1, message:1, subject:1})
- .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({ "$**":'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;
|