GithubHelp home page GithubHelp logo

mongofb's Introduction

MongoFB

Table of Contents

General Information

What is MongoFB

MongoFB is a combination of MongoDB and Firebase. You can run your MongoDB anywhere you like, and sign up for your own Firebase account. MongoFB provides an API so you can query, index, and aggregate your data (all the things you wish your firebase could do). MongoFB uses Firebase as its master source of data, so you get built in Security Rules, Authentication, and can listen for updates on any document of any collection, or any field of any document (all the things you wish your mongodb would do).

  • Firebase
    • Security/Authentication
    • WebSockets
  • MongoDB
    • querying
    • indexing
    • aggregation

Diagram

Diagram

Usage

Sever Configuration

MongoFB can be included in your express or zappajs server, using middleware.

mongofb = require 'mongofb'

app.use mongofb {
  root: '/api/1'      # the root url to host mongofb on
  cache:
    max: 100          # the max number of results to store in a local LRU cache
    maxAge: 1000*60*5 # the max age of any result in the LRU cache
  firebase:
    url: ''           # the url of your firebase
    secret: ''        # your firebase secret - only needed if your firebase has security rules
  mongodb:
    db: 'test'        # the mongodb to connect to
    host: 'localhost' # the host of your mongodb
    pass: ''          # the password to connect with
    port: 27017       # the port to connect to
    user: 'admin'     # the user to connect with
    options: {}       # other connection options [ref](#https://github.com/mongodb/node-mongodb-native/blob/master/docs/articles/MongoClient.md#basic-parts-of-the-url)
}

Server API

/API-ROOT/mongofb.js

Serves the javascript client

/API-ROOT/Firebase

Let's the client look up the public url of your Firebase

/API-ROOT/ObjectId

The client calls here to get a new ObjectID before writing to Firebase

/API-ROOT/sync/:collection/:id

After an insert, update, or remove, a client will tell the server it needs to
update data in Firebase. The server will then pull the most up-to-date data
directly from Firebase and write it to MongoDB for querying.

/API-ROOT/:collection/find

Perform a db.collection.find on your MongoDB. Pass your query as query
parameters to this endpoint. The result is returned as an array.

special options
 - limit: limits the number of results in the response

/API-ROOT/:collection/findOne

Perform a db.collection.findOne on your MongoDB. Pass your query as query
parameters to this endpoint. The result is returned as an object

/API-ROOT/:collection/:id*

Perform a db.collection.findOne by {_id: ObjectID()} on your MongoDB. Pass your
query as query parameters to this endpoint. The result is returned as an object.

This endpoint functions more like a standard resource url as no query parameters
are used.  This method also lets you query for specific fields of a document.

example: /API-ROOT/posts/510b56c221168da296f27bd5/author/name

The above might be a posts collection for my blog. With this I could directly
access the author's name of post 510b56c221168da296f27bd5.

The corresponding Firebase URL for that data would be
https://my-firebase.firebaseio.com/posts/510b56c221168da296f27bd5/author/name

Server Hooks

Sometimes you may want to modify the response from your api, or set default values for parameters, or do something special if the user is authenticated. This is all possible with MongoFB Hooks.

You can define your hooks in your server configuration. The current hooks available are...

new_query = collection.before.find(query)

new_doc = collection.after.find(doc)

Example Usage

app.use mongofb {
  firebase: config.firebase
  mongofb: config.mongodb
  root: '/api/1'
  hooks:
    users:
      after:
        find: (doc) ->
          # hide private user information to other users
          return doc if @user?.auth?.id == doc.id
          {_id: doc._id, public: doc.public}
    posts:
      before:
        find: (query) ->
          # an author changed their name
          if query.author?.name == 'joe'
            query.author.name = 'joey'

          # if we search for football or baseball, also search all sports
          if query.tag in ['football', 'baseball']
            query.tag = [query.tag, 'sports']

          # force a small limit
          query.limit = 10
}

Authentication

authenticate any request by passing a token query parameter with the value being the users' firebase token.

The @user can then be referenced in your hooks

Client SDK

How to use the Javascript SDK

Classes

mongofb.Database
mongofb.Collection
mongofb.Document
mongofb.DocumentRef

mongofb.Database

# This is the equivalent to a MongoDB Database

# Connect to our MongoFB server
db = new mongofb.Database 'http://localhost:3000/API-ROOT'

# Get a collection
posts = db.collection 'posts'
posts = db.get 'posts' 

# Get a document directly
post = db.collection('posts').get('510b56c221168da296f27bd5')
post = db.get('posts/510b56c221168da296f27bd5')
post = db.get('posts.510b56c221168da296f27bd5')

# Get a field from a document directly
name = db.get('posts/510b56c221168da296f27bd5/author/name')
name = db.get('posts.510b56c221168da296f27bd5.author.name')

mongofb.Collection

# This is the equivalent to a MongoDB Collection

# Get a collection
users = db.collection 'users'
users = db.get 'users'

# Insert a document (this method must be asynchronous)
users.insert {foo: 'bar'}, (err, user) ->
  throw err if err
  console.log user.val()

# Run a find query (synchronous)
docs = users.find {foo: 'bar'}

# Run a find query (asynchronous)
users.find {foo: 'bar'}, (err, docs) ->
  throw err if err
  console.log docs

# Run a findById (synchronous)
user = users.findById '510b56c221168da296f27bd5'

# Run a findById (asynchronous)
users.findById '510b56c221168da296f27bd5', (err, user) ->
  throw err if err
  console.log user

# Run a findOne (synchronous)
user = users.findOne {foo: 'bar'}

# Run a findOne (asynchonous)
users.findOne {foo: 'bar'}, (err, user) ->
  throw err if err
  console.log user

# Remove a document (this method must be asynchronous)
# only allowed to remove by id
users.remove '510b56c221168da296f27bd5', (err) ->
  throw err if err

mongofb.Document

# This is the equivalent of a MongoDB Document
post = posts.findById '510b56c221168da296f27bd5'

# Update a field in a Document
post.get('author.name').set('new author')

# update an entire Document
post.set {author: {name: 'the author'}, content: 'long post'}

# get json for a document
post.val()

# listeners
post.on 'update', (val) ->
  # called when this document is updated

post.on 'value', (val) ->
  # called immediately, and when the document is updated

post.on 'remove', (val) ->
  # called when the post is removed from the database

mongofb.DocumentRef

# A DocumentRef is a reference to a field of a Document

# Get a ref
ref = post.get('author.name')

# add listeners
ref.on 'update', (val) ->
ref.on 'value', (val) ->

# remove listeners
ref.off 'update'
ref.off 'value'
ref.off()

# get the parent ref
ref.parent()

# change the value of this property
ref.set('new author')

# get the json value for this ref
ref.val()


Examples

Server

express = require 'express'
mongofb = require '../lib/server'

app = express()
app.use mongofb {
  firebase:
    url: 'https://vn42xl9zsez.firebaseio-demo.com/'
  mongodb:
    host: 'localhost'
    port: 27017
  root: '/api/v2'
}
app.get '/', (req, res) ->
  res.send """
  <html>
    <body>
      <script type='text/javascript' src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js'></script>
      <script type='text/javascript' src='https://cdn.firebase.com/v0/firebase.js'></script>
      <script type='text/javascript' src='/api/v2/mongofb.js'></script>
    </body>
  </html>
  """

app.listen 3000
console.log "listening: 3000"

Javascript Client

window.db = new mongofb.Database 'http://localhost:3000/api/v2'
window.cookies = db.collection 'cookies'

cookies.insert {type: 'chocolate'}, (err, cookie) ->
  throw err if err
  window.cookie = cookie

  cookie.on 'update', (val) ->
    console.log 'cookie updated to', val

  ref = cookie.get 'type'
  ref.on 'update', (val) ->
    console.log 'cookie.type updated to', val

  ref.set 'peanut butter'

iOS Client

Coming Soon!

Android Client

Coming Soon!

mongofb's People

Contributors

scien avatar sjanderson avatar

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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mongofb's Issues

Use an object clone instead of data object from emit

Firebase returns a snapshot in their event functions, I find it more convenient to return the data object since you already have the DocumentRef you're listening on. Right now the @DaTa object is emitted, which means you are editing the DocumentRef data, without access to the DocumentRef object itself.

It would make more sense to either return a cloned object so people don't accidentally update it and try to re-save to the database or return the DocumentRef itself as Firebase does.

Going to make this change, but open to discussion if anyone has some use cases where the current model makes more sense.

npm install error

log

> [email protected] install /Users/pirsquare/git/mongofb/node_modules/mongodb/node_modules/kerberos
> (node-gyp rebuild 2> builderror.log) || (exit 0)

  CXX(target) Release/obj.target/kerberos/lib/kerberos.o
npm WARN excluding symbolic link index.js -> lib/sass.js
npm WARN excluding symbolic link lib/index.js -> sass.js

> [email protected] install /Users/pirsquare/git/mongofb/node_modules/mongodb/node_modules/bson
> (node-gyp rebuild 2> builderror.log) || (exit 0)

  CXX(target) Release/obj.target/bson/ext/bson.o
\
> [email protected] install /Users/pirsquare/git/mongofb/node_modules/asset-wrap/node_modules/chokidar/node_modules/fsevents
> node-gyp rebuild

  CXX(target) Release/obj.target/fse/fsevents.o
In file included from ../fsevents.cc:6:
../node_modules/nan/nan.h:339:13: error: no member named 'New' in 'v8::String'
    return  _NAN_ERROR(v8::Exception::Error, errmsg);
.
.
.
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make: *** [Release/obj.target/fse/fsevents.o] Error 1

I think we need to update dependencies version?

consider removing all data from DocumentRefs

problem state i just discovered...

  • data is updated
  • ref catches update and changes its @data
  • ref emits event
  • event handler used ref.get('property').val() // ISSUE: property was out of date
  • ref updates @document.data

ref.get passes the document and the path, it ignores local data, so the document data had to be updated before emitting events. I can't think of any benefit to duplicating the data in the DocumentRef at all now. I think the val() call should just always ask the document what the value at its path is.

only tricky thing i can think of is if you have a ref listener it would have to tell the document to update that part of its data. going to sit on this for a little bit and think it over.

$.or does not get caught properly

" if use_objectid, it needs to turn the string you're passing in into an ObjectId
[Aug-20 6:22 PM] Bryant Williams: with $in it's easy, because the key is still _id
[Aug-20 6:23 PM] Bryant Williams: with $or, you'd have to dig a little further

[Aug-20 6:23 PM] Bryant Williams: if criteria.$or, check each object for _id
can you file a bug?"

Upsert logic

Would be great to have upsert logic. I'll fork this project and make a PR for the author's approval.

validate input to remove

to make sure people don't accidentally do...

cookie = db.get('cookies').findOne({type: 'chocolate'})
cookie.remove('type')

when they meant to do

cookie.get('type').set(null)

Remove only allows ID right now change to removeById

[12:22 PM] Jason Gornall: db.objects.remove({site_id:'533efe9a4be147a107014871'})โ€จ Error: Firebase.child failed: First argument was an invalid path: "objects/[object Object]". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
having some difficulty with a db call
any ideas?
[12:23 PM] Bryant Williams: i think remove only allows an _id right now
[12:23 PM] Jason Gornall: ahhh that must be it
[12:23 PM] Bryant Williams: db.objects.remove(_id)
[12:23 PM] Jason Gornall: k
[12:23 PM] Bryant Williams: should probably change that to removeById
and add support for a real remove
file issues on the mongofb github project
[12:24 PM] Jason Gornall: alrighty

update firebase

current version is out of date with docs and would like to take advantage of some newer features

project status

Hi, I have a question, this project is still functional?

Please reconsider continuing the project development.

Thanks you.

-Carlos

updateData needs to watch out for 'created' field

and created should only be checked for if the ref is on the document level

lt3.site.get('users').get('facebook-7200529').set('admin')
undefined
Uncaught TypeError: Cannot read property 'created' of undefined mongofb.js:773
exports.DocumentRef.DocumentRef.updateData mongofb.js:773
(anonymous function) mongofb.js:765
success mongofb.js:18
j main.js:1
k.fireWith main.js:1
x main.js:1
b

more efficient ref.on and ref.off

current refs keep an array with each event

events:
  type: [handler1, handler2]

ref.off 'value' isn't called until events[type] is empty. when we could do ref.off 'value', handler1 and stop receiving those events immediately.

note this only affects refs with two listeners.

i think all of my code avoids this anyway by using multiple refs

document.get('property').on 'value', handler1
document.get('property').on 'value', handler2

which would then use separate refs and this would be a non-issue

add ref.remove()

#14

cookie.get('type').remove()

which is a shortcut for

cookie.get('type').set(null)

better error messages

current error (err)

{
name: "MongoError"
}

better error would be - err.toString()

MongoError: bad skip value in query

or - err.message

bad skip value in query

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.