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

Filtering based on joined table. #111

Open franklloydteh opened 11 years ago

franklloydteh commented 11 years ago

I have the following defined.

Ingredient = persistence.define(TABLE_INGREDIENT_NAME, {
    ingredientName: "TEXT"
});
Inventory = persistence.define(TABLE_INVENTORY_NAME, {
    quantity:"TEXT",
    unit:"TEXT"
});
Inventory.hasOne('ingredient',Ingredient);
Ingredient.hasOne('inventory',Inventory);

I would like to fetch an inventory based on the ingredientName, is this not possible ?

Inventory.all().prefetch('ingredient').filter('ingredientName','=',ingredientName).list(null,callback);
kendalen commented 11 years ago

I know it's an issue 3 months old, but I stumbled upon it just a few days ago. I badly needed the same feature for a couple of PhoneGap projects, so I made it by myself, since it's not supported. I forked the project and commit the changes I made. Since I'm far from being a javascript guru, this code could have bugs and could break retrocompatibility (or maybe could be just bad written). Of course I tried not to, but I'm still testing it. It looks like ok, until now. I tried to implement support for filtering and ordering on joined table (both hasOne and hasMany), with a Hibernate-like dot notation (in the example you made, your filter call would have been:

Inventory.all().prefetch('ingredient')
    .filter('ingredient.ingredientName','=',ingredientName)
    .list(null,callback);

and the filter would have worked even without the prefetch) I also added a new function:

QueryCollection.prototype.queryOptions

Returns a new query collection with the provided options. Valid options are:

  1. 'prefetch' - string or array of strings
  2. 'filter' - object with properties 'property', 'operator' and 'value' or array of these objects
  3. 'order' - object with properties 'property', 'ascending' and 'caseSensitive' or array of these objects @param options hash with set of option keys - option values @return the query collection configured with the provided options This was made to avoid multiple call to QueryCollection.prototype.clone function.

As I said, I am not an expert. The fork ( https://github.com/kendalen/persistencejs ) is public, if it can be of any help and if, after some tests, it proves stable as it looks like now, I could make a pull request to Zef (by the way, Zef: great work!). I am new to GitHub, too. :(

dennisholmer commented 10 years ago

@kendalen Please, make a pull request on your fork, it looks awesome :)

bamirzada commented 10 years ago

Does not look like this patch was ever taken into the main project...can you confirm yes/no ?

kendalen commented 10 years ago

As far as I know, no, it wasn't. I still have to make a pull request: I hope to find some times in the next few days to review the code and read how to make the request (my first time :) ). Thank you both.