Kouch is an ODM for couchase.
Couchbase ODM heavily inspired by
WARNING: Still heavily in development, incomplete, and only lightly tested.
Here is an example of a
User model that is stored in the
default bucket and shows off many of the features of kouch:
// create the base schemaUserSchema =name:first: Stringlast: Stringemail: type: String key: truepassword: Stringage: type: Number min: 18bio: type: String match: /[\w\d]+/ trim: truedate: type: Date default: Datenow;// set the key prefix for when a comment is storedUserSchemaset'key.prefix' 'user:account:';// a setter for the first name that capitalizes itUserSchemapath'name.first'setreturn valsubstr0 1toUpperCase + valsubstr1;;// middlewareUserSchemapre'save'var user = this;// only hash the password if the password has changedif !thisisModified'password'return next;bcrypthashuserpassword 8if err return nexterr;userpassword = hash;next;;;// instance methods on an instance of the model created from this schema// will be accessible at: (new UserModel()).x// need a good example here...;// static method on the model created from this schema// will be accessible at: UserModel.authenticatethisloademailif err return cberr;bcryptcomparepassword userpasswordif err return cberr;if !isMatch return cb'Your email and password combination is invalid.';cbnull user;;;;// virtual property, not persisted to the DBUserSchemavirtual'name.full'getreturn thisnamefirst + ' ' + thisnamelast;setvar split = namesplit' ';thisnamefirst = split0;thisnamelast = split1;;// create the model from the schemavar UserModel = kouchmodel'User' 'default' UserSchema;// create an instance of the model with datavar user =name:first: 'Chad'last: 'Engler'email: 'firstname.lastname@example.org'password: 'secret'age: 21bio: 'I am a person!';// save the userusersave;
The MIT License (MIT)
Copyright (c) 2014 Chad Engler
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.