dexie / Dexie.js

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

Make Dexie more treeshakable #1584

Open minht11 opened 2 years ago

minht11 commented 2 years ago

Right now if you want to use one method of the library you have to pay the bundle size cost of them all. If you want to use sort you must also include filter. If you want to just insert item into DB you must also include delete method and the problem is that Dexie has a lot of of methods, in a future it's likely that there will be even more. That's not ideal, so making things more treeshable would be big win. I am not quite sure on the exact API and it's very likely that this would be a breaking change unless treeshable API would be provided together with the old one.

Maybe each table creation could take composable with methods it want to use, so typescript types and chaining API still works like this

export class FriendsDB extends Dexie {
  static methods = [delete, put, someOtherMethod]

  friends!: Table<Friend, 'id'>;

  constructor() {
    super("FriendsDB");
    this.version(1).stores({
      friends: '++id, name, age'
    });
    this.friends.mapToClass(Friend);
  }
}

or/and each method can be a function instead which takes table or DB as an argument like put(db, ...restOfTheArgs) instead. Similar how it's done here https://github.com/jakearchibald/idb-keyval/blob/main/custom-stores.md

dfahlander commented 2 years ago

Thanks for the input. New built-in features, like liveQuery was added in respect of tree-shakability. Old methods have been kept so far for backward compatibility. In my view, all the static Dexie methods should be moved out and become tree-shakable in dexie@4.0. Keeping this issue open.

luckylooke commented 1 year ago

I would prefer way how rxjs solve this using operators instead of methods. Each operator is imported separately when needed, lovely architecture ❤️.