localbase icon indicating copy to clipboard operation
localbase copied to clipboard

Adds support for collection filtering

Open charlieknoll opened this issue 4 years ago • 7 comments

charlieknoll avatar Dec 11 '20 22:12 charlieknoll

Thanks @charlieknoll. Can you please explain what is going on with this? What does the functionality do? How would it be used by the end user?

dannyconnell avatar Dec 30 '20 10:12 dannyconnell

Hi @dannyconnell, I was helping a friend out who was "querying" a collection of users for a user and then "querying" another collection of orders for that user. Currently localbase would support this:

  .doc({id: 1}).get().then(user => {
    const u = user
    db.collection('orders')
      .get().then(orders => {
         const userOrders = orders.filter(o=>o.userId == u.id)
         u.orders = userOrders
         console.log(u)
      })
  })

But with the pull request implemented he could do this:

  db.collection('users')
  .doc({id: 1}).get().then(user => {
    const u = user
   db.collection('orders')
      .get({keys: false, filter: {userId: u.id}}).then(orders => { 
       u.orders = orders
       console.log(u)
      })
  })

Looking at the code, I believe it would be more performant for large collections.

BTW, congratulations on your success with fudget!

I really enjoyed your PWA course. I felt your pain in the dev experience with clearing cache, refresh, refresh, etc. It inspired me to create a new dev tool called AppYoke. It's early going but you can check it out on my github.

charlieknoll avatar Jan 01 '21 00:01 charlieknoll

Tested working to filter the collection by other fields.

ckng avatar Jan 12 '21 14:01 ckng

Another alternative is to change get() to work similarly and consistently with the rest of the APIs. So get() should return all matched documents instead of only one result. But this will likely break backward compatibility. Or maintain get() for returning the first matched result and introduce a getAll() for all matched results.

db.collection('users').doc({ gender: 'male' }).getAll() // [ // { id: 1, name: 'Bill', age: 47 } // { id: 2, name: 'Paul', age: 34 } // ]

e.g. set(), update(), delete() matched all:

db.collection('users').doc({ gender: 'male' }).delete()

if more than one document is matched by your criteria e.g .doc({ gender: 'male' }) then all matched documents will be deleted.

ckng avatar Jan 13 '21 17:01 ckng

Please @dannyconnell, would be great to have this functionality.

jiuck avatar Jul 22 '21 09:07 jiuck

@dannyconnell come on man!

zakaria-chahboun avatar Oct 04 '21 16:10 zakaria-chahboun

Hi guys, I did a fork, everyone is welcome to bump the fixes on this repo, I'll upload to npm soon https://github.com/samuk190/localbase

samuk190 avatar Oct 03 '22 13:10 samuk190