AngularJs & MongooseJS
If MongoDB, then use MongooseJS (http://mongoosejs.com/)
Advantages of Mongoose over usual MongoDB wrapper:
1. MongoDB uses named collections of arbitrary objects, and a Mongoose JS Model abstracts away this layer. Because of this, we don t have to deal with tasks such as asynchronously telling MongoDB to switch to that collection, or work with the annoying createFromHexString function.
2. Mongoose Models handle the grunt work of setting default values and validating data.
3. Mongoose lets us attach functions to our models
4. Mongoose handles limited sub-document population using manual references (i.e. no MongoDB DBRefs), which gives us the ability to mimic a familiar SQL join.
Ways to use Mongoose correctly:
1) 1 Schema = 1 file
2) Mongoose can t handle multi-level population yet, and populated fields are not Documents. Nesting schemas is helpful but it s an incomplete solution. Design your schemas accordingly.
3) Declare your models exactly once and use dependency injection; never declare them in a routes file.
4) Unit tests catch mistakes, encourage you to write modular code, and allow you to easily make sure your logic works. They are your friend.
Example Use:
var UserSchema = new Mongoose.Schema({?username : { type : String, validate: /\S+/, index : { unique : true } }, profile : {
name : {?first : { type : String, default : } last : { type : String, default : }
} }
});
var User = db.model('users', UserSchema);
=========================================
MongooseJS with Angular
http://thecodebarbarian.wordpress.com/2013/05/12/how-to-easily-validate-any-form-ever-using-angularjs/
If you ve ever tried to build any kind of website, odds are you ve had to create some way of validating and saving input from a form.
Pain Point #1. How to avoid writing a wall of if-statements for validating each data item
Pain Point #2. How to handle adding and deleting data items as your code base evolves, i.e. how to avoid having to make changes in several different locations when you want to add/remove a data item
Pain Point #3. How to display validation errors to your client. The user has wait for the page to reload every single time.
With MEAN and Mongoose:
Pain Point #1: We have only 1 if-statement on the backend and 2 on the frontend.
Pain Point #2: Adding or removing a data item consists solely of changing our view and our model, everything in between is completely content agnostic; we could easily add a date field to our schema with no changes to our AngularJS controller or our routes.
Pain Point #3: Our client has complete control over how we display server-side validation errors, and we never have to reload the page.
Comments
Post a Comment