dexie / Dexie.js

A Minimalistic Wrapper for IndexedDB
https://dexie.org
Apache License 2.0
11.68k stars 642 forks source link

how filter data in objec record #1250

Open behnam-maqsudi opened 3 years ago

behnam-maqsudi commented 3 years ago

Screen Shot 1399-12-26 at 15 35 45 can i use where or other filters in one object record

dfahlander commented 3 years ago

Please explain

behnam-maqsudi commented 3 years ago

@dfahlander I want to search the table. And has only one record and is stored as an object. I do not want to use JavaScript and I want to use Dexie methods! How I do that? I'm sorry if I can't write English properly

not my filter records like that

   return prerequisites['words']
   .orderBy(':id')
    .first()
    .then(function (result) {
      if (result) {
        return result.filter((item) => item.type != 2);
      }
    });

but i wanna use some Dexie methods like where({type:!2})

dfahlander commented 3 years ago

If you have a single record you don't need indexes to find it. If you'd have several records you can index their properties and use the index to find certain records. If you have array properties on your records, you can index them using MultiEntry index and search for records that have a certain value in its array property. Not sure if I still got your question but this is what I can say based on the info you've given me.

jandranrebbalangue commented 2 years ago

@dfahlander hi i would like to ask if how can i search the table in array of objects? in dexie method i can't index the object in multi entry index and in dexie how can i achieve the search inside the object of array?

dfahlander commented 2 years ago

Please examplify what you want. How does every stored record look like? An object with an array property? Show some JSON. I will try to explain how to index it.

jandranrebbalangue commented 2 years ago

image this is the items

jandranrebbalangue commented 2 years ago

i want to search it by supplier how can i display the product_name with the same supplier ?

dfahlander commented 2 years ago

And what is items? Is it a property of a single stored record or are you planning to insert each item as its own record in a table?

jandranrebbalangue commented 2 years ago

it is a property of a single stored record and i'm planning to make items an index so i can filter and search it by supplier so i can get all product_name in one supplier

dfahlander commented 2 years ago

IndexedDB is not optimized for storing large entries with arrays of items. Arrays can be indexed but only arrays of indexable types which does not include objects.

Could you change your model to instead store each item as its own record?

Think of IndexedDB more as a relational database than document database because it is backed and optimized as such. It is slow on handling deep structures and has poor indexing for those.

I can give an example on how you could change your model from using array prop into storing each item into its own record:

Let's say your request is to store several instances of your data containing the 'items' property - for example one per customer and each customer, so that would be the reason for not storing each item as its own record. Then, instead of storing the items on an array prop, store each item as its own record but add an indexed customer_id property. You can the have a customers table where you store the common data instead.

Customer_id is just an example, it might be something else that makes you need to separate the items once they are flattened into own records in a table. Or maybe you did only plan to store a single item and if so, just ignore my talk about 'customer_id' and proceed by storing each entry in its own record instead of storing a single large record.

const db = new Dexie('mydb');
db.version(1).stores({
  items: 'item_id, brand_id, product_id, [customer_id+product_id]', // Just some examples
  customer: 'customer_id'
});

Query all items for a certain customer

await db.items.where({customer_id: X}).toArray();

Query all items with a certain product_id

await db.items.where({product_id: Y}).toArray();

Query all items with a certain customer_id and product_id

await db.items.where({product_id: Y, customer_id: X}).toArray();
jandranrebbalangue commented 2 years ago

that make sense Thank you so much i will try it tomorrow

jandranrebbalangue commented 2 years ago

Hi @dfahlander i would like to ask again about items how can i filter [sold_date+product_id+supplier_id] or [sold_date+supplier_id+product_id] with between ??