Skip to content

Commit

Permalink
Replace Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
biggora committed May 8, 2013
1 parent aa2257e commit 6f657e6
Showing 1 changed file with 48 additions and 43 deletions.
91 changes: 48 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,17 @@ var User = schema.define('User', {
});
```




## Usage
### Accessing a Model

```javascript
var Schema = require('caminte').Schema;
var db: {
driver : "mysql",
host : "localhost",
port : "3306",
username : "test",
password : "test",
database : "test"
};
var schema = new Schema(db.driver, db); // port number depends on your configuration

// define any custom method
User.prototype.getNameAndAge = function () {
return this.name + ', ' + this.age;
};

// models also accessible in schema:
schema.models.User;
schema.models.Post;
```

// setup relationships
### Setup Relationships

```javascript
User.hasMany(Post, {as: 'posts', foreignKey: 'userId'});
// creates instance methods:
// user.posts(conds)
Expand All @@ -97,21 +81,34 @@ Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});
// post.author() -- sync getter when called without params
// post.author(user) -- setter when called with object

schema.automigrate(); // required only for mysql NOTE: it will drop User and Post tables

// work with models:
var user = new User;
user.save(function (err) {
var post = user.posts.build({title: 'Hello world'});
post.save(console.log);
});
```

### Setup Validations

// or just call it as function (with the same result):
var user = User();
user.save(...);
```javascript
User.validatesPresenceOf('name', 'email')
User.validatesLengthOf('password', {min: 5, message: {min: 'Password is too short'}});
User.validatesInclusionOf('gender', {in: ['male', 'female']});
User.validatesExclusionOf('domain', {in: ['www', 'billing', 'admin']});
User.validatesNumericalityOf('age', {int: true});
User.validatesUniquenessOf('email', {message: 'email is not unique'});

// Common API methods
user.isValid(function (valid) {
if (!valid) {
user.errors // hash of errors {attr: [errmessage, errmessage, ...], attr: ...}
}
})
```

### Common API methods

```javascript
// just instantiate model
new Post
// save model (of course async)
Expand All @@ -136,25 +133,17 @@ User.count({where: {userId: user.id}}, cb)
user.destroy(cb);
// destroy all instances
User.destroyAll(cb);
```

### Define any Custom Method

// Setup validations
User.validatesPresenceOf('name', 'email')
User.validatesLengthOf('password', {min: 5, message: {min: 'Password is too short'}});
User.validatesInclusionOf('gender', {in: ['male', 'female']});
User.validatesExclusionOf('domain', {in: ['www', 'billing', 'admin']});
User.validatesNumericalityOf('age', {int: true});
User.validatesUniquenessOf('email', {message: 'email is not unique'});

user.isValid(function (valid) {
if (!valid) {
user.errors // hash of errors {attr: [errmessage, errmessage, ...], attr: ...}
}
})

```javascript
User.prototype.getNameAndAge = function () {
return this.name + ', ' + this.age;
};
```

## Callbacks
### Middleware (callbacks)

The following callbacks supported:

Expand All @@ -170,9 +159,26 @@ The following callbacks supported:
- beforeValidation
- afterValidation


```javascript
User.afterUpdate = function (next) {
this.updated_ts = new Date();
this.save();
next();
};
```

Each callback is class method of the model, it should accept single argument: `next`, this is callback which
should be called after end of the hook. Except `afterInitialize` because this method is syncronous (called after `new Model`).


### Automigrate
required only for mysql NOTE: it will drop User and Post tables

```javascript
schema.automigrate();
```

## Object lifecycle:

```javascript
Expand Down Expand Up @@ -208,7 +214,6 @@ Read the tests for usage examples: ./test/common_test.js
Validations: ./test/validations_test.js



## Your own database adapter

To use custom adapter, pass it's package name as first argument to `Schema` constructor:
Expand Down

0 comments on commit 6f657e6

Please sign in to comment.