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

Crashing for parent to child prefetch #110

Open davidleureka opened 11 years ago

davidleureka commented 11 years ago

I'm working with the sample code, specifically prefetching relationships.

This works:

var allTasks = Task.all(session).prefetch("category");

But this does not work:

var allTasks = Task.all(session).prefetch("tags");

node_modules/persistencejs/lib/persistence.store.sql.js:653 var thisMeta = meta.hasOne[prefetchField].type.meta; ^ TypeError: Cannot read property 'type' of undefined

I see the documentation says

"prefetch(rel) Returns a new QueryCollection that prefetches entities linked through relationship rel, note that this only works for one-to-one and many-to-one relationships."

Does that mean that given a child you can get the parent (e.g. given a task you can get the category), but given a parent you cannot get the children (given a category you cannot get the tasks)?

zefhemel commented 11 years ago

Indeed, prefetching only works to references to a single entity, not to a collection (such as tags, it's plural).

davidleureka commented 11 years ago

Ok, thanks for the quick feedback.