{"id":19577473,"url":"https://github.com/yamigr/micronats","last_synced_at":"2025-02-26T11:42:56.258Z","repository":{"id":51700324,"uuid":"214856933","full_name":"yamigr/micronats","owner":"yamigr","description":"MICRONATS 🚀 - A nats.io micro-service-framework for nodejs.","archived":false,"fork":false,"pushed_at":"2023-05-10T08:28:36.000Z","size":26,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-09T15:02:13.218Z","etag":null,"topics":["framework","microservice","natsio","nodejs","service"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yamigr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-13T16:46:44.000Z","updated_at":"2023-05-10T08:28:34.000Z","dependencies_parsed_at":"2022-08-23T01:40:15.001Z","dependency_job_id":null,"html_url":"https://github.com/yamigr/micronats","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yamigr%2Fmicronats","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yamigr%2Fmicronats/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yamigr%2Fmicronats/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yamigr%2Fmicronats/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yamigr","download_url":"https://codeload.github.com/yamigr/micronats/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240848985,"owners_count":19867615,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["framework","microservice","natsio","nodejs","service"],"created_at":"2024-11-11T07:06:42.845Z","updated_at":"2025-02-26T11:42:56.021Z","avatar_url":"https://github.com/yamigr.png","language":"JavaScript","readme":"# Micronats\n\n\u003e MICRONATS :rocket: - A nats.io micro-service-framework for nodejs.\n\nWith this framework you can easily and very quickly create a micro-service for any requirement :rainbow:\n\nWhat's inside?\n* Publish / Subscribe\n* Request / Response\n* Fast \u0026 simple data storage\n* Database-Events\n\nDependencies\n* NATS.io server [https://nats.io/download/](https://nats.io/download/)\n\n## Content\n* [Installing](#installing)\n* [Use](#use)\n* [Options](#options)\n* [Service](#service)\n* [$call](#call)\n* [$data](#data)\n* [$storage](#storage)\n* [authors](#authors)\n* [license](#license)\n\n\u003ca name=\"installing\"\u003e\u003c/a\u003e\n\n## Installing\n```sh\nnpm i micronats\n```\n\n\u003ca name=\"use\"\u003e\u003c/a\u003e\n\n## Use\n* Servicename - name of the service\n* Hooks - event callbacks\n* Methods - Middleware for the service\n* Funcs - local functions\n* Data - local data\n\nSubject **servicename.methodname**, example **user-service-example.addUser**\n\n```js\nconst Micronats = require('micronats')\nconst mn = new Micronats(/* options */)\n\nmn.create({\n    servicename : 'user-service-example',\n    beforeCreate(){\n        console.log('HOOK: before create. No data or storage set')\n    },\n    created(){\n        console.log('HOOK: created')\n    },\n    mounted(){\n        console.log('HOOK: mounted. All done. Service is ready')\n    },\n    destroyed(){\n        console.log('HOOK service destroyed')\n    },\n    methods : {\n    },\n    funcs : {\n    },\n    data(){\n        // Local variables\n        return {\n        }\n    }\n})\n\n// Start the services\nmn.listen()\n```\n\n```js\nconst Micronats = require('micronats')\nconst mn = new Micronats(/* options */)\n\nmn.create({\n    servicename : 'user-service-example',\n    beforeCreate(){\n        console.log('HOOK: before create. No data or storage set')\n    },\n    created(){\n        console.log('HOOK: created')\n    },\n    mounted(){\n        // Make some serivce registry here\n        // Or get some data from another services\n        // Read the storage, ... init some service-components\n        // Start some other stuff\n        console.log('HOOK: mounted. All done. Service is ready')\n   \n        // Call local function\n        this.$call.setTimestamp()\n        \n        // Talk to services\n        mn.service.publish('user-service-example.addScore', \n            { score : 3 }\n        )\n\n        mn.service.publish('dashboard-sevice.scores', \n            { score : this.$data.score }\n        )\n\n        mn.service.request('user-service-example.addUser', \n            { name : 'yamigr' }, \n            { max : 1}, \n            function(msg){\n                console.log(msg)\n        })\n\n        mn.service.request('user-service-example.getAll', \n            {}, \n            { max : 1}, \n            function(msg){\n                console.log(msg)\n            })\n\n        mn.service.request('user-service-example.delUser',\n            {_id: '6oYDedYd'},\n            { max : 1},\n            function(msg){\n                console.log(msg.err)\n            })\n\n        // Subscribe database events\n        mn.service.subscribe('user-service-example.$storage.\u003e', \n            function(msg, _, subject){\n                console.log(subject, msg)\n            })\n    },\n    destroyed(){\n        console.log('HOOK service destroyed')\n    },\n    methods : {\n        addUser(req, res){\n            // add some service-methods\n            // use this.$storage to store some data\n            this.$storage.put(req, function(err, doc){\n                res({message : 'User added', doc : doc, err : err})\n            })\n        },\n        getAll(req, res){\n            this.$storage.find({}, function(err, users){\n                res({users : users})\n            })\n        },\n        getUser(req, res){\n            this.$storage.findOne(req._id, function(err, user){\n                res({user : user})\n            })\n        },\n        delUser(req, res){\n            this.$storage.del(req._id, function(err){\n                res({err : err})\n            })\n        },\n        addScore(req){\n            this.$data.score += req.score\n            console.log(this.$data.score)\n        }\n        /*\n            create more methods\n        */\n    },\n    funcs : {\n        setTimestamp(){\n           // call local-functions with this.$call\n           // access local-variables with this.$data\n           this.$data.timestamp = Date() \n        }\n    },\n    data(){\n        // Local variables\n        return {\n            timestamp : Date(),\n            score : 42\n        }\n    }\n})\n\n///////////////////////////////////////////\n// Listen runs the instances\nmn.listen()\n\n///////////////////////////////////////////\n// Destroy a service by name\nsetTimeout(function(){\n    mn.destroy('user-service-example')\n}, 5000)\n\n///////////////////////////////////////////\n// Events\nmn.on('err', function(err){\n    console.log('error:', err)\n})\nmn.on('disconnect', function(err){\n    console.log('disconnect')\n})\nmn.on('reconnecting', function(){\n    console.log('reconnecting')\n})\nmn.on('reconnect', function(){\n    console.log('reconnect')\n})\n```\n\n\u003ca name=\"options\"\u003e\u003c/a\u003e\n\n## Options\n\n**Important** - Keep the json-options.\n```js\nlet options = {\n    connection : {\n        json : true,\n        port : 4222,\n        servers : ['nats://nats.io:4222']\n    },\n    db : {\n        path : './db',\n        options : {\n            valueEncoding: 'json'\n        }\n    }\n}\nconst mn = new Micronats(options)\n\n```\n\u003ca name=\"service\"\u003e\u003c/a\u003e\n\n## Service\nCommunicate with services.\n```js\nmn.service.publish('servicename.methodname', {/* data */})\n\nmn.service.subscribe('servicename.$storage.eventname._id', function(msg, _, subject){\n        console.log(subject, msg)\n})\n\n// Wildcard\nmn.service.subscribe('servicename.$storage.eventname.\u003e', function(msg, _, subject){\n        console.log(subject, msg)\n})\n\nmn.service.request('servicename.methodname', function(msg){\n        console.log(msg)\n})\n\nmn.service.requestOne('servicename.methodname', function(msg){\n        console.log(msg)\n})\n```\n\n\u003ca name=\"methods\"\u003e\u003c/a\u003e\n\n## Methods\nCall a method from another service use the subject **servicename.methodname** and send the json-data.\n```js\naddUser(req, res){\n    // add some service-methods and handle the request and send a response\n    this.$storage.put(req, function(err){\n        res({message : 'User added'})\n    })\n}\n```\n\n\u003ca name=\"call\"\u003e\u003c/a\u003e\n\n## $call\nCall local functions.\n```js\nfuncs : {\n    setTimestamp(){\n        // call local-functions with this.$call in other methods\n        // access the variables in data with this.$data\n        this.$data.timestamp = Date() \n    }\n}\n```\n\nAccess the functions in methods or other functions.\n```js\nthis.$call.setTimestamp()\n```\n\n\u003ca name=\"data\"\u003e\u003c/a\u003e\n\n## $data\nCreate the data object to set some local variables.\n```js\ndata(){\n    // Use local variables with this.$data in other methods\n    return {\n        timestamp : Date()\n    }\n}\n```\n\nAccess the variables in methods or functions.\n```js\nthis.$data.timestamp\n```\n\n\u003ca name=\"storage\"\u003e\u003c/a\u003e\n\n## $storage\nStore data with **this.$storage** in the methods. \n\nInsert single data\n```js\n// Object without _id\nthis.$storage.put({...}, function(err, doc){\n})\n\n// Event from storage\nmn.service.subscribe('servicename.$storage.put._id', function(msg, _, subject){\n        console.log('Database event:', subject, msg)\n})\n```\n\nInsert multiple data\n```js\n// Handles a batch\nvar ops = [\n  { type: 'put',  value: {}},\n  { type: 'put',  value: {}},\n  { type: 'put',  value: {}},\n  { type: 'del',  key: _id},\n]\n\nthis.$storage.batch( ops, function(err){\n})\n\n// Event from storage for each bach-entry\nmn.service.subscribe('servicename.$storage.put | del._id', function(msg, _, subject){\n        console.log(subject, msg)\n})\n```\nUpdate a entry\n```js\n// Object needs to have the _id-prop\nthis.$storage.update({ _id : 'T4gbDiXx3', ...}, function(err){\n})\n\n// Event from storage\nmn.service.subscribe('servicename.$storage.update._id', function(msg, _, subject){\n        console.log(subject, msg)\n})\n```\n\nFind one entry\n```js\nthis.$storage.findOne(_id, function(err, doc){\n    // returns a object\n})\n```\n\nFind data\n```js\n// Mongodb-like find-filter\nthis.$storage.find({}, function(err, docs){\n    // returns a array of docs\n})\n\nthis.$storage.find( { name: { $in: ['yamigr', 'yanosh'] }}, function(err, docs){\n    // returns a array of docs\n})\n```\n\nDelete a entry by id\n```js\nthis.$storage.del(_id, function(err){\n})\n\n// Event from storage\nmn.service.subscribe('servicename.$storage.del._id', function(msg, _, subject){\n        console.log(subject, msg)\n})\n```\n\n\u003ca name=\"authors\"\u003e\u003c/a\u003e\n\n## Authors\n* **Yannick Grund** - *Initial work* - [yamigr](https://github.com/yamigr)\n\n\u003ca name=\"license\"\u003e\u003c/a\u003e\n\n## License\n\nThis project is licensed under the MIT License\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyamigr%2Fmicronats","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyamigr%2Fmicronats","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyamigr%2Fmicronats/lists"}