sylvainpolletvillard / ObjectModel

Strong Dynamically Typed Object Modeling for JavaScript
http://objectmodel.js.org
MIT License
467 stars 30 forks source link

database _pk is handled as private #48

Closed beebase closed 7 years ago

beebase commented 7 years ago

I have a naming collision. My database (ArangoDb) only accepts _key as primary key. When I create a record offline I have to add {_key : uuid} in order to sync with the db when coming online again. if _key is missing in the record, the db will create it's own _key.

The problem is that ObjectModel sees _key as private and strips it when the record is sent to the db server. How can I solve this?

sylvainpolletvillard commented 7 years ago

Hi,

You can customize the convention for private keys. For example:

Model.conventionForPrivate = function(key){ return key.startsWith("private_") }

In the next version of ObjectModel, you will be able to set this convention specifically for one model. Currently it applies on all models.

beebase commented 7 years ago

[edit: ah thanks, I just read your comment above.]

I couldn't find an example in the docs but I guess this is the solution

Model.conventionForPrivate = (key) => { 
    return key[0] === '#'  // only props prefixed with '#' are now handled as private
}
sylvainpolletvillard commented 7 years ago

If you don't want private at all, you can also return false

beebase commented 7 years ago

I just upgraded from 2.6.2 to 3.1 and I'm getting private prop errors again 22:67 Uncaught TypeError: cannot modify private _from

In my code i am doing this:

import { Model } from 'objectmodel'
Model.conventionForPrivate = (key) => { return key[0] === '#' }

Are there any breaking changes perhaps?

beebase commented 7 years ago

ah ok.

Model.conventionForPrivate and Model.conventionForConstant have been moved to Model.prototype so that you can change these conventions specifically by model

sylvainpolletvillard commented 7 years ago

yes, just change Model.conventionForPrivate to Model.prototype.conventionForPrivate

gotjoshua commented 6 years ago

I think a small example in the docs to make this explicit could be helpful. I spent a bit more time that I would have liked to find this hint...