crul:meteor-external-mongo-db

v0.3.6Published 9 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

meteor external mongo db Build Status

external / remote mongo db connector for meteor apps

meteor add crul:meteor-external-mongo-db

quick start

1import { ExternalDbPublisher } from 'meteor/crul:meteor-external-mongo-db';
2
3if (Meteor.isServer) {
4    var externalDbPublisher = new ExternalDbPublisher();
5    externalDbPublisher.connect('mongodb://127.0.0.1:27017/dbName');
6    // externalDbPublisher.disconnect('mongodb://127.0.0.1:27017/dbName'); // not working :(
7}

dependencies

  • ecmascript
  • underscore
  • random

api

this packages exports:

  • ExternalDbPublisher class (server side) which has 2 methods:

    • connect(dbUrl): it publish:

      • a collection named 'dbName-collections' with the collections of the remote DB
      • all the server collections with plain names, find function signature: find(where, options)
    • disconnect(dbUrl): disabled, disconnect works but you cannot re-connect again so I've disabled the disconnect feature

  • ExternalDb class, use: new ExternalDb(dbUrl);

  • externalDbFactory instance, use: externalDbFactory.create(dbUrl);

! because collections are published with plain names, remote DB should have no collection with same name than collections in local DB

roadmap

  • add count support (tmeasday:publish-counts?)
  • add configuration/settings (optional prefix for collections)
  • disconnect / reconnect

example project instructions

  • meteor configuration

    meteor create meteorTestApp
    cd meteorTestApp
    meteor add crul:meteor-external-mongo-db
    
  • add main.js code to server:

    1import { Meteor } from 'meteor/meteor';
    2import ExternalDbPublisher from 'meteor/crul:meteor-external-mongo-db';
    3
    4Meteor.startup(() => {
    5    var externalDbPublisher = new ExternalDbPublisher();
    6    externalDbPublisher.connect('mongodb://127.0.0.1:27017/dbName');
    7});
  • add main.js code to client:

    1import { Meteor } from 'meteor/meteor';
    2import './main.html';
    3
    4Meteor.subscribe('dbName-collections');
    5let Collections = new Meteor.Collection('dbName-collections');
    6let Collection;
    7let collections = {};
    8
    9Template.body.helpers({
    10    collections() {
    11        return Collections.find({});
    12    },
    13    collection() {
    14        return Collection.find({}, { limit: 10 });
    15    },
    16    collectionLoaded() {
    17        return Session.get('collectionLoaded');
    18    }
    19});
    20
    21Template.body.events({
    22    'click .collection'(event, instance) {
    23        let collectionName = $(arguments[0].currentTarget).html();
    24
    25        Meteor.subscribe(collectionName);
    26        collections[collectionName] = collections[collectionName] || new Meteor.Collection(collectionName) 
    27        Collection = collections[collectionName];
    28        Session.set('collectionLoaded', true);
    29    }
    30});
  • add main.html code to client:

    1<body>        
    2    <div style="border: solid 1px #000">
    3    {{#each item in collections}}
    4        <button class="collection">{{item.name}}</button>
    5    {{/each}}
    6    </div>
    7
    8    {{#if collectionLoaded}}
    9        <div style="border: solid 1px #666">
    10        {{#each item in collection}}
    11            <div class="feed">{{item._id}}</div>
    12        {{/each}}
    13        </div>
    14    {{/if}}
    15</body>