coresmart / persistencejs

persistence.js is an asynchronous Javascript database mapper library. You can use it in the browser, as well on the server (and you can share data models between them).
http://persistencejs.org
1.73k stars 240 forks source link

persistence.flush persists records even when .add never called iff hasOne is set #126

Open eagsalazar opened 10 years ago

eagsalazar commented 10 years ago

I can consistently reproduce this error in chrome.

Category = persistence.define('Category', {name: 'TEXT'})
Note = persistence.define('Note', {text: 'TEXT'})
Note.hasOne('category', Category)
persistence.schemaSync()

n = new Note({text: 'foobar'});
c = new Category({name: 'stuff'})
n.category = c

persistence.flush()

Both n and c are written to db. No persistence.add is ever called on either one.

Just another question, why not just allow for entities to be individually persisted by calling n.persist() for example? Having a global tracking and global flush is surprising and unusual for an orm at least in my experience.

eagsalazar commented 10 years ago

Also with global track/flush it isn't clear how to reliably implement validations in a wrapper class since the underlying model can be saved at any time without the opportunity to check for valid values.

zefhemel commented 10 years ago

Whenever entity objects are set to point to each other they are both persistnce.add'ed. The reason for this is to ensure that there are no broken links in the database, i.e. that a property of entity X points Y, and Y was never saved to the database.

tiagoengel commented 9 years ago

+1. Is there a workaround to avoid this? In my case this behavior is really a problem, a lot of objects are saved automatically without validation.