Skip to content

Instantly share code, notes, and snippets.

@askucher
Last active November 11, 2015 17:26
Show Gist options
  • Save askucher/9e1c63b1010eff83d409 to your computer and use it in GitHub Desktop.
Save askucher/9e1c63b1010eff83d409 to your computer and use it in GitHub Desktop.
egregorjs.md

Egregorjs is data-binding framework

#####It privide immediate update of the data in one place when changed the data on another place located on different computer or application.

####Example

//server.js
Egregor = require("egregor")

chat = Egregor("chat")

chat.plug("mongodb", {  connectionString: "user:[email protected]" })
chat.plug("frontend", {  port: 3000 });

chat.rooms = [  {title: "General", messages: []}, { title: "Random", messages:[] } ]
//client.js
chat = Egregor("chat")

chat.plug("backend", {  port: 3000 })
chat.plug("localStorage");

someViewEngine.render("chat-rooms", chat.rooms)

####Documentation

//client.js


egregor =  Egregor("unique-id")
egregor.plug("backend", { secretKey: "XXX", url: "https://..." })


egregor.prop = 123

here the Egregor should send the socket message to the server with delta of JSON in order to synchronize the data

//server.js


egregor = Egregor("unique-id")
egregor.plug("frontend", { secretKey: "XXX" })

egregor.then(function(delta) {

  console.log(egregor.prop) //=> 123
  egregor.prop = "321"

});

here the Egregor should send the socket message to the client with delta of JSON in order to synchronize the data

//client.js

egregor = Egregor("unique-id")

egregor.then(function(delta) {
  console.log(egregor.prop) //=> 321
});

Then we can bind the egregor with database in order to save data from user input.

//server.js
egregor.plug("mongodb", { connectionString: "XXX" })

It is very useful for rapid prototyping of application and show to investors

Then we can bind the egregor with localStorage in order to keep application working even when connection is interrupted

//client.js
egregor.plug("localStorage")

Then we can bind the egregor with deltaLog.log file in order to data the data flow

//client.js
egregor.plug("deltaLog", { file: "./deltaLog.log" })

Then we can bind the sharedObject with workflow in order to fire serverside actions like sendmail or another curstomActivity

//client.js
egregor.plug("workflow", { fromFile: "./workflow.js" })

And It would be good to think about transaction to handle errors

function success() {

}

function error(delta) {

//your delta has not been processed correctly. Your data is restored 
//Please process your delta again

  this.wait(1000).tryProcess(delta);

}

egregor.then(success, error)

And finally how to write the plugin:

//server.js

module.exports = function(Egregor) {

Egregor.plugin("frontend", function(options, delta) {
//..
})

Egregor.plugin("mongodb", function(options, delta) {
//..
})


Egregor.plugin("deltaLog", function(options, delta) {
//..
})

Egregor.plugin("workflow", function(options, delta) {
//..
})

}

//when user does the require("egregor") it can parse "node_modules/egregor-plugin-*" and find all plugins
//client.js

//Egregor is global object

Egregor.plugin("backend", function(options, delta) {
//..
})

Egregor.plugin("localStorage", function(options, delta) {
//..
})



How to install

#install backend
npm i --save egregor
npm i --save egregor-plugin-frontend
npm i --save egregor-plugin-mongodb
#install frontend  (should not use browserify for ease)

bower i --save egregor
bower i --save egregor-plugin-backend
bower i --save egregor-plugin-localstorage

#where egregor.js is same for frontend and backend.

Egregor transformations

//client.js

chat = Egregor("chat")

function getUserById(id) {

//...

}

//transformation will be applied when 'chat.message' is changed
function transform(message) {

  message.user = getUserById(message.userId)

  return message;

}

//filter will be applied when 'chat.message' is changed
function activeFilter(message) {

  return message.status === "active"

}


messages =  chat.message.map(transform).filter(activeFilter)

someViewEngine.render("messages", messages)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment