Skip to content

Added modules for IndexedDb #4739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 60 commits into
base: main
Choose a base branch
from

Conversation

SandroMaglione
Copy link
Contributor

@SandroMaglione SandroMaglione commented Apr 15, 2025

Type

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

Add modules for IndexedDb. This PR includes the following new modules (in order of dependency between each other):

  • IndexedDbTable: define a table (with Schema)
  • IndexedDbVersion: collects tables in a database version
  • IndexedDbMigration: collects a sequence of versions and their schema + data migrations
  • IndexedDbDatabase: service that initialises the database and executes the migration
  • IndexedDbQuery: api to interact with the database

This also adds BrowserIndexedDb to @effect/platform-browser with a layer to create an instance of IndexedDb from the browser window.

Initial full API example included in the PR

Notes

  • I added the modules inside @effect/platform since they are technically "generic" (not dependent on the browser window)
  • Currently the migration doesn’t automatically create object stores and indexes (even if it would be possible). That's because the user may want to read the previous data for data migrations. Note that IndexedDb requires to delete the previous object store and create a new one whenever an index changes
  • Migrations and transactions cannot have async effects inside them. That's a "limitation" of IndexedDb. From the docs:

Transactions are tied very closely to the event loop. If you make a transaction and return to the event loop without using it then the transaction will become inactive.

  • There are more features that can be added (e.g. reactivity), but I would leave them as additions for later

Note

I plan to add more examples and README after the PR review, to avoid having to rewrite it all because of API changes


Notes for review

The modules can be reviewed in the following order (each directly depends/references the previous module):

  1. IndexedDbTable
  2. IndexedDbVersion
  3. IndexedDbMigration
  4. IndexedDbDatabase
  5. IndexedDbQuery

IndexedDbTable and IndexedDbVersion don't contain any logic, they are just data containers (schemas).

IndexedDbMigration contains code to execute migrations (e.g. createObjectStore/deleteObjectStore).

IndexedDbDatabase is mainly used to initialise the database inside its layer.

IndexedDbQuery is the main service used to extract a type safe API and execute queries. The code that interacts with IndexedDb is mainly inside internal/indexedDbQuery.ts.

IndexedDb is a service to provide a valid instance of IndexedDb (with BrowserIndexedDb exporting a window layer).

@github-project-automation github-project-automation bot moved this to Discussion Ongoing in PR Backlog Apr 15, 2025
Copy link

changeset-bot bot commented Apr 15, 2025

🦋 Changeset detected

Latest commit: 057dbf5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@effect/platform-browser Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@tim-smart
Copy link
Contributor

I think this should just live in platform-browser, I can't see us adding implementations for any other platform.

@SandroMaglione SandroMaglione requested a review from tim-smart May 5, 2025 04:09
@tim-smart tim-smart force-pushed the sandro/indexed-db branch from d78a6b8 to 26d36e7 Compare May 8, 2025 22:57
@SandroMaglione SandroMaglione requested a review from tim-smart May 9, 2025 10:42
* @since 1.0.0
* @category errors
*/
export const IndexedDbDatabaseError = internal.IndexedDbDatabaseError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs an explicit type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the error definition for internal/exported value and type

* @since 1.0.0
* @category errors
*/
export type IndexedDbQueryError = typeof internal.IndexedDbQueryError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't use internal types here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated error definition

export interface ClearAll<
Source extends IndexedDbVersion.IndexedDbVersion.AnyWithProps = never
> {
[Symbol.iterator](): Effect.EffectGenerator<Effect.Effect<void>>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the Symbol.iterator for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simplified clearAll and clear to just return an Effect instead of an intermediate interface

@SandroMaglione SandroMaglione requested a review from tim-smart May 13, 2025 04:02
@tim-smart tim-smart force-pushed the sandro/indexed-db branch 2 times, most recently from c05cc93 to 04d5141 Compare May 16, 2025 06:09
@tim-smart tim-smart force-pushed the sandro/indexed-db branch from 04d5141 to fb87fe1 Compare May 16, 2025 06:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Discussion Ongoing
Development

Successfully merging this pull request may close these issues.

2 participants