Giter Site home page Giter Site logo

capaj / moonridge Goto Github PK

View Code? Open in Web Editor NEW
66.0 7.0 8.0 6.65 MB

Mongo live query framework bootstrapped on socket.io-rpc and mongoosejs. Takes your mongoose models and allows for easy and elegant consumption over the network in your frontend app or in remote node process.

Home Page: http://capaj.github.io/Moonridge

License: MIT License

JavaScript 96.05% HTML 3.95%
mongoosejs realtime-database socket-io

moonridge's Introduction

Deprecated notice

I don't maintain this anymore, so use only if you're willing to fix bugs yourself.

Moonridge Build Status Dependency Status

NPM badge

isomorphic client side library and server framework, which brings Mongoose model to the browser(or over the network to other node process). Based on socket.io-rpc. Framework agnostic-usable with anything-let it be Angular, Aurelia, React or any other.

Probably the coolest feature is live queries. These are performance hungry, but Moonridge is caching live queries in memory, so that one query is being live checked only once. If 10000 users run the same query, the DB performance performs the same amount of operations as if one user was accessing it. So your DB should be under the same load no matter how many people use your web app(presuming they are not writing into the DB).

Install

npm i moonridge -S

How to use it?

Serverside API is quite straightforward, if still not sufficent, read source code of examples.

in smoke test folder(Angular|React|Aurelia),

Basic usage serverside

    var MR = require('moonridge')

	MR.connect("mongodb://localhost/moonridge_showcase") //MongoDB address is optional-you can connect as always with mongoose
	var mongoose = MR.mongoose
    var bookModel = MR.model('book', {  //mongoose schema definition
            name: String,
            author: String
        }, {
             schemaInit: function (schema) {
                // makes sure only one book per nameXauthor exists
                schema.index({ name: 1, author: 1 }, { unique: true, dropDups: true });
            }
        })
    ...
    var app = require('express')()
    var server = require('http').Server(app)
    //bookModel is an extended mongoose model, so if you know how to work with mongoose models, you'll be right at home
    MR.bootstrap(server) 
    app.use('/api', MR.baucis()) // gives your REST api for your DB in case you need it alongside to socket.io API
    server.listen(port, () => {
          app.emit('listening')
          console.log('started listening on ' + port, ' in ', env, new Date())
    })

On the CLIENT:

   	var Moonridge = require('moonridge-client')
	//Moonridge backend
	var mr = Moonridge({url: 'http://localhost:8080', hs: {query: 'nick=testUser'}})
	var fighterModel = mr.model('fighter')
	//live query
	var LQ = fighterModel.liveQuery().sort('health').exec()

	LQ.promise.then(function(){
	  LQ.result //has a result of the query-array or a number
	  //query is live now
	});
	//create a new entity
	fighterModel.create({name: 'Arya', health: 50}).then(function(created){
	  console.log('created a fighter: ', created)
	  //LQ.result now also contains Arya
	  created.health = 49
	  //update an entity
	  fighterModel.update(created).then(function () {
  	    //remove it from DB
  	    fighterModel.remove(created)
	  });
	});

Also you need to connect to your backend-just pass a simple object with url property like HERE.

The whole client side api for queries shadows the Mongoose query API.

Errorhandling

All server-client communication is done with socket.io-rpc-another project of mine, so errors are propagated for all server-side calls which return an error(or reject their promise). This is especially helpful with schema validation errors, where backend tells the frontend exactly what failed.

Supported browsers

Desktop

Internet Explorer 8+ - though it needs es5shim
Safari 4+
Google Chrome 4+
Firefox 4+
Opera 10.61+

Mobile

iPhone Safari
iPad Safari
Android WebKit
WebOs WebKit

Why not just a ported mongoosejs on the client side?

One could ask why not just port mongoosejs to the client side and let clients talk to mongo directly. While this would surely be an interesting project, Moonridge has features which would not be possible without a server instance(live querying, custom authorization/authentication). I think these features are worth it introducing a new framework to the backend.

How does live querying work in one paragraph

Every client liveQuery is serialized and sent via socket.io to backend. Backend parses it and constructs real mongoose query, which is immediately run(if it doesn't exist already in server memory). The return is sent back to client. Any change to a certain document (creation, deletion, update) is checked again for all in-memory queries. MongoDB checks just one recently changed document, not the whole query, so it should be pretty quick. If query is satisfied, the changed document is propagated to listening clients. And that is basically it.

But mongoDB doesn't have JOINs!

Yes I know and if you need joins, you better look for something else. Moonridge is tailored for web apps which do a lot of small and custom queries. Basically you would want to save any bit of bandwith and show pieces of data in your app as soon as possible, you are best served using Moonridge.

Production samples

I have few production apps running on moonrige, feel free to take a peek how moonridge is used:

Pull requests are welcome and same goes for issues! js-standard-style

moonridge's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

moonridge's Issues

add exists event

this event should be emitted whenever existence of doc changes from false to true.
So not only on creation, but also when server starts up.
Useful if you want to hook up cron job to a document or something like that.

Help in integrating with mean.js stack

Currently using the Yeoman https://github.com/meanjs/generator-meanjs to generate my app. A typical schema model.js looks like:

var mongoose = require('mongoose'),
  Schema = mongoose.Schema;

/**
 * Configuration Schema
 */
var ConfigurationSchema = new Schema({
    serialNumber: { type: String, trim: true}) },
    tz:           { type: String, trim: true, enum: ['EST','CST','MST','PST'] },
    user:         { type: Schema.ObjectId, ref: 'User' },
    updated_at:   { type: Date },
    created_at:   { type: Date, default: Date.now }
});

mongoose.model('Configuration', ConfigurationSchema);

ConfigurationSchema.pre('save', function(next){
  this.updated_at = Date.now;
  if ( !this.created_at ) {
    this.created_at = Date.now;
  }
  next();
});

Can you please explain how to translate this scaffolded system to using MR?

I don't quite see how to go from:

var ConfigurationSchema = new Schema({...});
mongoose.model('Configuration', ConfigurationSchema);

with controller:

exports.list = function(req, res) {
    Configuration.find({'user':req.user}).sort('-created').populate('user', 'displayName').exec(function(err, configurations) {
...
}

to

var configurationModel = MR.model('configuration', {...});

and controller.

Does the controller change, or can it use the Mongoose schema as before (for the existing routes)?

Simple example

Hi.
I am trying to merge your framework with a simple MEAN stack, but I never used jspm, so I'm a bit lost on the client side.
Using jspm install Moonridge=npm:moonridge-client i get

err  No version match found for github:rase-/node-XMLHttpRequest@a6b6f2

Can you provide a simple MEAN stack for your framework, or explain how to configure the client part?

Thanks

Update with populated field

I just encountered an issue that I think turned out to be two different issues.

using this (partial) Schema
var MrQuotes = this.MR.model('Mrquotes', {
quote_number: Number,
quote_version: Number,
broker_id: { type: Schema.Types.ObjectId, ref: 'Brokers' },
mrClient_ids: [{type: Schema.Types.ObjectId, ref: 'Mrclients' }],
stat: String,
create_date: Date,....

I am able to retrieve records with all manner of queries against the model client side. No problems, in fact it works incredibly well. When I add a new object to the mrClient_ids array and call update that also works. However I found that if I sliced an item out of the array and called update the previous state was serialized to the database without the desired change. I tracked this down to mr-rpc-methods and compared toUpdate with doc after the _.merge(doc, toUpdate) and found that underscore was not merging the changes. I noticed that since I had been using populate that the objects looked very different and I guessed that lodash was probably getting confused. As you know the original document consists of the array of object ID's and not the fully populated document. So I tried an experiment. Before calling update I did a foreach on the collection, extracted just the _id's into an array, assigned them to the model and then ran the update. I actually expected this to work but it also didn't. I still don't know why. So I thought about replacing lodash for this, just to simplify things and used plain JS as described here: https://plainjs.com/javascript/utilities/merge-two-javascript-objects-19/

Using that technique I was then able to update both adding and removing objects. In either case I have retained the approach of updating the model with only the id's as that seems to be a cleaner solution rather than hoping that everything in between handles the nested objects correctly.

Any thoughts on this?

Thanks and by the way great work on this project. It's incredibly useful.

/W

`npm test` fails because missing dependency `morgan`

After running npm test I get the following error.

npm test

> [email protected] test /Users/keheliya/dev/Moonridge
> mocha

  basic CRUD including working liveQueries
module.js:338
    throw err;
    ^
Error: Cannot find module 'morgan'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/Users/keheliya/dev/Moonridge/test/e2e-smoketest/server.js:16:9)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

I think morgan is used by server.js

Updating records on a filtered result.

Open 2 browser tabs. Then on tab 1, filter the query to show only 1 result.
On the second tab update a record that does not exists in the first tab.
Sometimes the record updated on the second tab is shown on the first tab. But it shouldn't become visible because of the declared filter.
This problem should pose problems to pagination.

Maybe this problem is because the server treats both browser tabs as the same client?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.