ichiriac / sofa-odm

NodeJS Object Data Model for Couchbase
http://ichiriac.github.io/sofa-odm
MIT License
7 stars 0 forks source link

Ignore undefined fields of schema onCreate #1

Open ArkeologeN opened 9 years ago

ArkeologeN commented 9 years ago

Currently, if you pass any field that is not part of your declare or model, it would still allow you to set one. e.g:

var TUser = db.declare('TUser', {
    autoincrement: false,
    properties: {
      fUserFirstName: 'string',
      fUserLastName: 'string',
      fUserFullName: 'string',
      fUserPassword: 'string',
....

When you directly refer any object (in my case req.body), it will set those fields over object and it looks like this:

{ _id: false,
  fUserFirstName: 'hamza',
  fUserLastName: 'waqas',
  fUserEmail: 'hamza@hamza.com',
  username: 'admin',
  fUserPassword: 'admin' }
// `username` must have not been there in this object.

It was reproduced using this:

var user = TUser.create(req.body);
  console.log(user);

Thanks!

ichiriac commented 9 years ago

I'm agree with you, objects should serialize only their own parameters for storage.2 ways to do that :

1 - as Javascript is permissive you could construct or assign to an object a property that should not be stored in couchbase.

2 - from construct or when an undefinded property is set it should trigger an error

You see other ways to do this ?

ArkeologeN commented 9 years ago

I think going with 1st solution is fine. However, we could interpolate properties from schema when firing a save event or action. Initially:

What do you think?

ichiriac commented 9 years ago

If we go with the first solution, I could be permissive on construct & setters, and at onSave pass to couchbase an new intermediate POJO mapped with only defined properties.