Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

Introduction


The following is an example of a daemon that uses the composer to function.

Similar to with services, we adhere to this folder schema to maintain code standardization.



Folder Schema


Code Samples


config.js
"use strict";

var daemon = {
   "type": "daemon",
   "dbs": [
      {
         prefix: "",
         name: "company",
         multitenant: false,
         mongo: true
      }
   ],
   prerequisites: {
      cpu: '',
      memory: ''
   },
   serviceName: "composerDaemon",
   serviceGroup: "Composer",
   servicePort: 4211,
   "errors": {},
   "schema": {
      "insertRecords": {
         "mw": __dirname + "/lib/mw/add.daemon.js",
         "_apiInfo": {
            "l": "Insert Records"
         }
      }
   }
};

module.exports = daemon;


index.js
"use strict";
var composer = require("soajs.composer");
composer.deploy(__dirname +"/config.js", function(error){
   console.log( (error)? error : "Daemon started ...");
});


Middleware:

buildMasterIndex job
"use strict";

var mongo = require("../model/mongo");

module.exports = {
   
   "checkIfExists": function (soajs, next) {
      
      var condition = {
         "id": 1
      };
      
      var combo = {
         condition: condition
      };
      if(!soajs.data){
         soajs.data = {}
      }
      mongo.findEntry(soajs, combo, function (error, oneRecord) {
         if (error) {
            return next(error);
         }
         if (oneRecord) {
            console.log("next()")
            next();
         }
         else {
            var combo2 = {
               "record": {
                  "name": "Joe",
                  "id": 1,
                  "position": "supervisor"
               }
            };
            
            mongo.insertEntries(soajs, combo2, function(error){
               if(error){
                  return next(error);
               }
               else{
                  console.log("insertEntries")
                  soajs.data.new= true;
                  next();
               }
            });
         }
      });
      
   },
   "update": function (soajs, end) {
      if (!soajs.data.new){
         var condition = {
            "id": 1
         };
         
         var combo = {
            condition: condition
         };
         
         mongo.findEntry(soajs, combo, function(error, oneRecord){
            if (error){
               return end(error);
            }
            else{
               var opt = {
                  ts: new Date().getTime()
               };
               
               var updated = {
                  "$set": opt
               };
               
               var combo1 = {
                  updatedFields: updated,
                  options: {
                     'upsert': false,
                     'multi': true
                  },
                  versioning: true,
                  condition: condition
               };
               mongo.updateEntries(soajs, combo1, function (error) {
                  if (error) {
                     return end(error);
                  }
                  
                  console.log("timestamp updated");
                  return end(null, true);
               });
            }
         });
      }
      else {
         return end(null, true);
      }
      
   }
};

Model:

mongo.js
"use strict";
var database = 'company';
var collection = 'employees';

var lib = {
   "defaults": function (combo) {
      if (!combo.database) {
         combo.database = database
      }
      if (!combo.collection) {
         combo.collection = collection
      }
      if (!Object.hasOwnProperty.call(combo, 'versioning')) {
         combo.versioning = false;
      }
   },
   
   "findEntry": function (soajs, combo, cb) {
      lib.defaults(combo);
      soajs.mongo[combo.database].findOne(combo.collection, combo.condition || {}, combo.fields || {}, cb);
   },
   "insertEntries": function (soajs, combo, cb) {
      lib.defaults(combo);
      soajs.mongo[combo.database].insert(combo.collection, combo.record, combo.versioning || false, cb);
   },
   "updateEntries": function (soajs, combo, cb) {
      lib.defaults(combo);
      soajs.mongo[combo.database].update(combo.collection, combo.condition, combo.updatedFields, combo.options || {}, combo.versioning || false, cb);
   }
};

module.exports = lib;
  • No labels