localbase
localbase copied to clipboard
Adds support for collection filtering
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?
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.
Tested working to filter the collection by other fields.
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.
Please @dannyconnell, would be great to have this functionality.
@dannyconnell come on man!
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