aspen-cloud / triplit

A full-stack, syncing database that runs on both server and client. Pluggable storage (indexeddb, sqlite, durable objects), syncs over websockets, and works with your favorite framework (React, Solid, Vue, Svelte).
https://triplit.dev
GNU Affero General Public License v3.0
2.3k stars 69 forks source link

Case-insensitive sort #139

Open thanhnguyen2187 opened 4 months ago

thanhnguyen2187 commented 4 months ago

Hi. I'm trying to use query.order, but it seems like it is case-sensitive (A... comes before a...). Is there anyway to make the query case-insensitive (a... and A... are treated the same, but they come before b...)? Thanks!

wernst commented 4 months ago

There isn't an officially supported way to do this right now. But we have ideas for how to support these sorts of transformations in the query api in the future.

For now you have a few options:

  1. Sort on your results in javascript - this is simple but you won't be able to leverage additional apis in the query engine like limit

  2. Manually maintain a computed attribute Support for computed attributes is on our short term roadmap, but for now you need to manage them manually. You'll create a new attribute attr_lower and you'll have to manually maintain the invariant that attr_lower === lowercase(attr). Specifically when you mutateattr with insert and update you'll need to manually mutate the attr_lower attribute.

const text = 'AbC';
await client.insert('collection', { attr: text, attr_lower: lowercase(text) });

await client.update('collection', 'id', (entity) => {
  const updatedText = 'XyZ';
  entity.attr = updatedText;
  entity.attr_lower = lowercase(updatedText);
});

That should allow you to fully use the query engine client.query('collection').order('attr_lower', 'ASC').limit(10).

We discussed this approach in our Discord recently as well: https://discord.com/channels/1138467878623006720/1138467879113728033/1256648644736712715

Other features on our short term roadmap like triggers could also be helpful here eventually.

I'll leave this open for others to share any ideas.

sgup commented 3 months ago

related to this: localeCompare for numerical string sorting

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare