brikteknologier/seraph-model — pull request #53 by rorymadden (2013-09-26 14:52:11+00:00)
Title: Schema support to reduce validation/preparation effort on each model

I really like the functionality offered by seraph-model. One thing that I have found useful coming from Mongoose is the ability to set a schema. I know Neo4j is schemaless but I believe that this could be a timesaving over having to define quite common validations/preparations. The current approach is a bit of a hack but if this is something you are interested in we could make it more seamless in the model definition (e.g. var Beer = model(db, 'Beer', beerSchema) ). Apologies for the many updates in model.js but my setup automatically removes trailing spaces on save.

The attached pull request lets you set up a model like:
```js
var db = require('seraph')('http://locahost:7474')
var model = require('seraph-model').model
var parseSchema = require('seraph-model').parseSchema

var emailRegEx = /^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/;
var GENDER = ['male', 'female'];

var userSchema = {
first_name: {type: String, required: true, trim: true },
last_name: {type: String, required: true, trim: true },
email: {type: String, required: true, lowercase: true, match: emailRegEx},
gender: {type: String, enum:GENDER },
birthday: {type: Date },
age: {type: Number, default: 99 },
hobbies: {type: Array}
}
var User = model(db, 'User');
parseSchema(userSchema, User);

User.save({first_name: 'sample'}, cb) // will fail on missing required fields
```

Additionally, in order to support index definitions in the schema we would need to change the index support to use the new Neo4j 2.0 label indexes. This way you can set an index on a fieldName without having to set the value.

Look forward to hearing from you