Code Monkey home page Code Monkey logo

marsdb-sync-server's Introduction

Build Status npm version Coverage Status Dependency Status

It's a Meteor compatible DDP server, based on MarsDB, but with major improvements. It supports methods, pub/sub and collection operations. It have very similar to the original Meteor interface, so, you really knows how to use it, if you are familiar with Meteor. But it also highly customizable, because it can be used with any server, that you passed to a configure function. Check out a basic example with express and webpack.

Features

  • Asynchonous server – no Fibers
  • Reactive joins in publish – out of the box
  • Framework agnostic – configured upon node.js's http server
  • Easy to test and play – no MongoDB needed, MarsDB works with memory by default

WARNING

It's only a concept until 1.0. Use it for your own risk. It does not support scaling, handled only changes happened on one instance. Scaling will be handled by MarsDB-Mongo module.

Examples

Basic Express/Webpack example

The client repository comes with a simple example. To try it out:

git clone https://github.com/c58/marsdb-sync-client.git
cd marsdb-sync-client/example && npm install
npm start

Then, just point your browser at http://localhost:3000.

Configure a server

import http from 'http';
import MarsSync from 'marsdb-sync-server';
import requireDir from 'require-dir';

// Some server configuration
// ...
const server = http.createServer();

// Setup marsdb-sync-server
MarsSync.configure({ server });

// You must require all your models, publishers and methods.
// They will be registered in MarsSync server.
requireDir('./js/models');
requireDir('./js/publishers');
requireDir('./js/methods');

Auto-publish

// ...
// Setup marsdb-sync-server with autoPublish enabled
MarsSync.configure({ server, autoPublish: true });

// With {autoPublish: true} all documents in each
// collection will be sent to each newly connected client

Publising

import UserModel from '../models/User.model';
import TodoModel from '../models/Todo.model';
import { publish } from 'marsdb-sync-server';

// Publish all todos in a collection and all users,
// who have created todos
publish('allTodos', (ctx, arg1, arg2) => {
  // Publisher always receive first `ctx` arguments,
  // that contains `connection` field for now.
  // In next versions it would be extended by other modules
  return TodoModel.find().join((todo) => [
    // Define joins for each todo, it's not reactive
    // (changed user will not be changed in clients)
    UserModel.find(todo.authorId),

    // ...and this reactive. All changed users will be
    // sended the clients.
    UserModel.find(todo.authorId).observe(),
  ]);

  // Must return a cursor or array of cursors.
});

Methods

import { method } from 'marsdb-sync-server';

// Defines a method named 'seyHello'
method('sayHello', (ctx, name = 'unknown') => {
  // First argument the same as in `publish`
  const msg = 'Hello, ' + name;
  return msg;

  // Might return something.
  // If returned promise or array of promises, then an
  // `updated` message sended to a client when all promises
  // will be resolved.
});

Using with MongoDB (and other storages)

By default MarsDB uses memory to store collections. You can easily configure it for using MongoDB (or other storages) as a backend. Just configure MarsDB to use MongoDB before any instance of a Collection class created

// server.js
import MarsMongo from 'marsdb-mongo';
// DO NOT import Colllection releted modules here
// ...other imports

// ...some server configuration
MarsMongo.configure({ url: 'mongodb://127.0.0.1:27017' });
MarsSync.configure({ server: server });

// Require all Collection releted modules
requireDir('./js/models');
requireDir('./js/publishers');
requireDir('./js/methods');

Roadmap

  • Context customization by other modules
  • Documentation

Contributing

I'm waiting for your pull requests and issues. Don't forget to execute gulp lint before requesting. Accepted only requests without errors.

License

See License

marsdb-sync-server's People

Contributors

c58 avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

rongfengliang

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.