unitech / angular-bridge Goto Github PK
View Code? Open in Web Editor NEWLink models easily via a REST interface between Mongoose/Node-Express/Angular.js
Link models easily via a REST interface between Mongoose/Node-Express/Angular.js
If I have a user-model, I'd like to be able to do this:
angularBridge.addResource('user', User, {hide: ['__v', 'password']})
So no secret info is shared on most endpoints, but I also want to allow the user to set their own password (which is encrypted in the model's pre('save')
):
middleware.post('/user', (req, res) => {
const user = new User({
email: req.body.email,
username: req.body.username,
password: req.body.password
})
user.save()
.then(user => {
return sendEmail('register', {user: user})
.then(() => {
delete user._id
delete user.password
res.json(user)
})
})
.catch(e => {
delete e.op
res.status(400).json(e)
})
})
As it is, I get a Path 'password' is required.
message, because it's hidden.
Maybe angular-bridge could check if the route exists before it overwrites it?
This appears to be an implementation of the Rails Resource pattern. I like it. It saves a lot of boilerplate code at the beginning of a project. I just downloaded mean.io and replaced the articles controller with a one-liner. This is awesome!
Specifically, I edited mean/config/routes.js. I removed this:
var articles = require('../app/controllers/articles');
app.get('/articles', articles.all);
app.post('/articles', auth.requiresLogin, articles.create);
app.get('/articles/:articleId', articles.show);
app.put('/articles/:articleId', auth.requiresLogin, auth.article.hasAuthorization, articles.update);
app.del('/articles/:articleId', auth.requiresLogin, auth.article.hasAuthorization, articles.destroy);
//Finish with setting up the articleId param
app.param('articleId', articles.article);
and I added this:
var bridge = new (require('angular-bridge'))(app, {
urlPrefix : '/'
});
var mongoose = require('mongoose')
var Article = mongoose.model('Article')
bridge.addResource('articles', Article)
This allowed me to throw away this file and the application still worked fine.
This API could be consumed by anything. There's nothing angular-specific about it. Perhaps the project should be renamed, unless you intend to do some tighter integration with Angular. Perhaps you already have plans for this?
One kind of integration I'd love to see would be to allow you to re-use Mongoose schemas in the browser to automatically generate your $resources. It would be nice if validations were available and introspectable in the browser so they could be integrated with angular's form validation.
angularBridge.addResource('projects', db.Project, { query: '{_user: String(req.user._id)}'});
https://github.com/Unitech/angular-bridge/blob/master/lib/index.js#L99
I have a resource called users
which has the following hidden fields:
[
"hash",
"salt",
"connectedAccounts.facebook.token",
"connectedAccounts.facebook.secret",
"connectedAccounts.google.token",
"connectedAccounts.google.secret",
"connectedAccounts.twitter.token",
"connectedAccounts.twitter.secret"
]
It looks like when I update the model (specifically at connectedAccounts.facebook.rules
) the token
and secret
for all connectedAccounts
gets deleted. Am I using the hide
property correctly? If not, what do I need to do?
The reason I suspect hide
being related to the issue is that if I remove those fields from the hide
property, the token
and secret
fields are left alone on an update.
I'm guessing it has to do with the fact that in the first scenario, I'm not sending those fields because they're hidden and the client doesn't even have them, so somehow it's interpreted that those fields should be removed. However, hash
and salt
don't experience the same issue, which is why I'm at a loss... Thanks!
Great project. What are the options of using this in Angular 2.0, also in typescript? Thanks.
Can we see an example using nested resources, I can't seem to figure out how that would work. I have a mongoose Schema like so:
var mongoose = require('mongoose');
module.exports = function Schemas() {
this.schema = mongoose.Schema;
this.EmployeeSchema = new this.schema({
'firstname': {
type: String,
required: true,
trim: true
},
'lastname': {
type: String,
required: true,
trim: true
},
'email': {
type: String,
required: true,
trim: true,
index: true,
validate: /\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/
},
'departmentId': {
type: this.schema.ObjectId,
trim: true,
required: true
},
'enddate': {
type: String,
trim: true
},
'active': {
type: Boolean,
"default": true
}
});
this.EmployeeSchemaModel = mongoose.model('employees', this.EmployeeSchema);
this.DepartmentSchema = new this.schema({
'name': {
type: String,
required: true,
trim: true,
index: {
unique: true
}
},
'employees': [this.EmployeeSchema]
});
this.DepartmentSchemaModel = mongoose.model('departments', this.DepartmentSchema);
mongoose.connect('mongodb://localhost:8120/staff');
};
So I have Employee
documents nested within Departments
. How do I configure angular-bridge to give me URL's like:
/departments/{some department id}/employees/{some employee id}
express deprecated res.send(status, body):
Use res.status(status).send(body) instead node_modules/angular-bridge/lib/index.js:64:7
Please Fix !
Ending up with error 400
Angular-bridge looks really interesting.
There seems to be something wrong in the error handling for posting a new document:
AngularBridge.prototype.collectionPost = function() {
return _.bind(function(req, res, next) {
if (!req.resource) { next(); return; }
var self = this;
if (!req.body) throw new Error('Nothing submitted.');
var epured_body = this.epureRequest(req, req.resource);
var doc = new req.resource.model(epured_body);
doc.save(function(err) {
if (err) { error(err); return; } // <-- error not defined
if(doc.schema.methods.post) doc.schema.methods.post();
res.send(doc);
});
}, this);
};
The error handling seems to be a little inconsistent through out the library. Some places an exception is thrown, others uses the response object to send an error object. Some places the actual error is returned, in other places only success = false is sent.
And as far as I can see, all errors are returned with an 200 OK status. Is that right?
Hi
Do you intend to licence this under an open source licence? If so which?
TIA
Could you republish this to npm? I need to get the requestPrehandler in there, and I noticed it's not in the module when I ask for version 0.3.6.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.