Giter Site home page Giter Site logo

angular-bridge's Introduction

Angular Bridge

Build Status NPM version

Create, read, update, delete MongoDB collections via AngularJS.

It uses :

  • Mongoose for accessing to Mongodb
  • Express for the routing
  • AngularJS (with the $resource method)

Btw don't forget to include angular-resource.js

Sample code

Backend code

In you db.js backend :

var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/pizza');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;

var PizzaSchema = new Schema({
    author : {
	    type : String,
    },
    color : {
	    type : String
    },
    size : {
            type : Number
    },  
    password : {   // You can hide it from read and write ! (cf after)
            type : String 
    }
});

// You can optionally add a method to schema.methods that is executed based
// on the type of HTTP request with the names "query", "get", "put", "post", and "delete"
// The callback receives the affected entities as it's parameter.
PizzaSchema.methods.query = function(entities) {
  console.log("Queried:");
  console.log(entities);
};

PizzaSchema.methods.get = function(entity) {
  console.log("Got:")
  console.log(entity);
};

PizzaSchema.methods.put = function(entity) {
  console.log("Put:")
  console.log(entity);
};

PizzaSchema.methods.post = function(entity) {
  console.log("Posted:")
  console.log(entity);
};

PizzaSchema.methods.delete = function(entity) {
  console.log("Deleted:")
  console.log(entity);
};

exports.Pizza = mongoose.model('pizzas', PizzaSchema);

In your backend app.js :

var db = require('./db.js');

// Mount all the resource on /api prefix
var angularBridge = new (require('angular-bridge'))(app, {
    urlPrefix : '/api/'
});

// With express you can password protect a url prefix :
app.use('/api', express.basicAuth('admin', 'my_password'));

// Expose the pizzas collection via REST
angularBridge.addResource('pizzas', db.Pizza);

// Hiding fields
angularBridge.addResource('toppings', db.Toppings, { hide : ['_id', 'password']});

// Read-only fields (sent to client, but will not write to database)
angularBridge.addResource('jaboodies', db.Jaboody, { readOnly: ['_id', 'cantChangeMe']});

// Force a mongoose query (to restrict access to certain items only)
angularBridge.addResource('projects', db.Project, { query: '{_user: String(req.user._id)}'});
// Note:  This can be passed as an object, but you can also pass it as a string
//        in cases where the object you're looking for is only accessible within
//        the HTTP-verb callback (in this example, 'req' will give an error if it
//        is not passed as a string)

// Force a particular value regardless of what the client sends
angularBridge.addResource('clients', db.Client, { force: {_user: 'req.user._id' }});

BE CAREFUL! force AND query BOTH USE eval()

Front end code

That's all for the backend, now in Angular :

var HomeCtrl = function($scope, $routeParams, $location, $resource) {
    var PizzaDb = $resource('/api/pizzas/:id', { id: '@_id' }); 
   
   // Magic, you are ready now !
   
   var new_pizza = new PizzaDb({
     author : 'agoodpizayolo',
     color : 'blue',
     size : 999
   });
   
   new_pizza.$save(function(save_the_pizza) {
      console.log('Success pizza - ', save_the_pizza);
   });
   
   PizzaDb.get({id : '50b40dd6ed3f055a27000001'}, function(pizza) {
    	pizza.color = 'UV';
    	pizza.$save();
    });
};

License

(The MIT License)

Copyright (c) 2013 Alexandre Strzelewicz [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

angular-bridge's People

Contributors

tompi avatar unitech avatar vegar avatar

Watchers

 avatar  avatar  avatar

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.