• Stars
    star
    208
  • Rank 183,308 (Top 4 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Link models easily via a REST interface between Mongoose/Node-Express/Angular.js

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' }});

// Enable population for the given fields
angularBridge.addResource('projects', db.Project, { populate: "_ref1" });

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.

More Repositories

1

pm2

Node.js Production Process Manager with a built-in Load Balancer.
JavaScript
40,761
star
2

gridcontrol

Networked Process Manager to execute functions in a Computer Grid
JavaScript
578
star
3

pm2-deploy

Deploy part of PM2
JavaScript
178
star
4

node-logman

Log client side/server side messages and monitor them with a realtime interface
CSS
113
star
5

pm2-interface

Deprecated
JavaScript
90
star
6

multissh

SSH into multiple machine to execute a command in parallel, with a convenient terminal UX
JavaScript
51
star
7

Skytoop

A Nice Desktop in Cloud with a filemanager and a lot of features ! Fork it ! Web2Py
JavaScript
30
star
8

cryptoengine

Monitor and verify transactions from a local or remote bitcoin deamon.
JavaScript
20
star
9

pm2-axon

JavaScript
18
star
10

pm2-axon-rpc

Modified version of visionmedia/axon-rpc
JavaScript
14
star
11

express-repl

Interact with express internal data (routes, settings...) with an auto reconnect remote repl
JavaScript
11
star
12

yeoman-jade-angularjs

Yeoman template with nice jade integration (Haml like)
JavaScript
10
star
13

scaleway-commander

Manage a fleet of scaleway servers
JavaScript
10
star
14

WebGL-solar-system

WebGL - Three.js v53 - Realistic solar system in WebGL
JavaScript
9
star
15

spiderlink

Simple, yet optimized application intercommunication within the same machine. Based on WS.
JavaScript
8
star
16

pm2-openstack

Nginx load balancing and hot edit + load monitoring and create VM triggers
7
star
17

pm2-dev

Development dashboard for pm2
JavaScript
6
star
18

gc-refresh-mode

Google Chrome & Emacs & File Save = Automatic Refresh. This plugin permits you to refresh AUTOMATICALLY google chrome when saving a file ! - Fell free to fork it and enhance it. It worth (I do not have enough time to maitain it) -
Python
6
star
19

ProjectManager

J2EE - Spring (MVC, Secutiry, Autowiring), Hibernate, PostgreSQL, ExtJS. Project manager
JavaScript
5
star
20

Web2Py-application-template

Application template for Web2Py including JS/CSS minification, logger...
JavaScript
5
star
21

Speak.js

Make your software speak, naturally
JavaScript
4
star
22

afflux-server.js

Standalone log server, that routes and send message logs to different db/bucket/table/realtime interface
JavaScript
4
star
23

shelljs

๐Ÿš Portable Unix shell commands for Node.js
JavaScript
4
star
24

InterBenchmark

Benchmarks script : Node.Js vs Ruby Rails vs Django vs WebPy vs J2EE-Spring vs Express vs Php - Nginx FastCgi Passenger Tomcat
Shell
3
star
25

coinvest

coinvest
JavaScript
3
star
26

youtube-to-mp3-UI

Your own youtubetomp3
JavaScript
3
star
27

micro-dot-files

dot files for low resources systems (.bashrc/.emacs.el/nodejs)
Shell
3
star
28

Empathy

Baby steps with Raspberry Pi & Node.js
JavaScript
3
star
29

Css-Sprite-AutoGen

Transform multiple images in one and gives css position - Avoid multiple request for each images - Web2Py application
JavaScript
2
star
30

test-repo

JavaScript
2
star
31

easy-voice

JavaScript
2
star
32

GeonameLocal

GeonameLocal
Shell
2
star
33

lensesly

lensesly
Ruby
2
star
34

companystats

JavaScript
2
star
35

Eschool

LMS
JavaScript
2
star
36

afflux-logger.js

Logger client for afflux server - namespaced and custom logger for logging specific events
JavaScript
2
star
37

high-summary

Retrieve title, image and summary of a web page
JavaScript
2
star
38

synapsis

JavaScript
2
star
39

Speak

Speak
JavaScript
2
star
40

node-v8tools

V8 profiler bindings
C++
2
star
41

Sputnik

Library to control Drone over 4G (Hybrid System) / Drone as API / Expose the world as an API
Python
2
star
42

Gsplitter

Gsplitter extension allows you to have a big preview of Google search results !
JavaScript
2
star
43

SoundcloudCommentsHider

Chrome extension - Automatically hide comments on Soundcloud.
JavaScript
1
star
44

auto-ap

Shell
1
star
45

nurse-api

Interface for seccomp-nurse
JavaScript
1
star
46

DeportedChat

JavaScript
1
star
47

.i3

C
1
star
48

rails_template

rails new my_new_app -m rails_template/generate_template.rb
Ruby
1
star
49

storydata-htp

JavaScript
1
star
50

pm2-hub-rd

R&D on pub sub system
JavaScript
1
star
51

rails_snippets

Some rails snippets
1
star
52

Unification

Ruby
1
star
53

afflux-express.js

Middlewares for Express to log access and errors to afflux-server.js
1
star
54

Quizzy

Quizzy
JavaScript
1
star
55

carcass-auth-app

JavaScript
1
star
56

carcass-memoray

In memory storage with array.js
JavaScript
1
star
57

opencensus-website

opencensus.io website content
CSS
1
star
58

xlogin

Automatic X login service for systemd
Makefile
1
star
59

commander.js

node.js command-line interfaces made easy
JavaScript
1
star
60

Little-Books-of-R

1
star
61

tlsmail

Unpack of tlsmail, putting it on github to look at it. I want to remove some errors from when it's required.
Ruby
1
star
62

node

evented I/O for v8 javascript
JavaScript
1
star
63

TAH-project

JavaScript
1
star
64

Monetify

Monetify
Ruby
1
star
65

mavlinkjs

JavaScript
1
star
66

release-test-check

JavaScript
1
star
67

Arduino_LoRa_SX12XX_Demo_Sensor-heltec-ab01

C
1
star
68

thinblog

Thin Wordpress Theme for Wordpress - Example : http://blog.hemca.fr
PHP
1
star
69

reach

1
star
70

jquery.heat-progressbar

Jquery plugin for drawing easily a progressbar with heat color
JavaScript
1
star
71

clem-remote

JavaScript
1
star
72

afflux-listener.js

JavaScript
1
star
73

Gamification

Ruby
1
star
74

OpenStreetMap

OpenStreetMap
JavaScript
1
star
75

MDNotes

Live markdown editor with multiple accounts
JavaScript
1
star
76

AMNBS-boilerplate

Boilerplate/Structure App - Mongoose + Nodejs + Express + AngularJS + Boostrap + Compass
JavaScript
1
star
77

gc-refresh-mode-

Google Chrome & Emacs & File Save = Automatic Refresh. This plugin permits you to refresh AUTOMATICALLY google chrome when saving a file !
1
star
78

youtube-download

JavaScript
1
star