powersync-ja / powersync-js

SDK that enables local-first and real-time reactive apps with embedded SQLite for JavaScript clients, including React Native and Web
https://www.powersync.com
Apache License 2.0
295 stars 21 forks source link

Support dynamically adding tables in schema at runtime #332

Open nthtrung09it opened 2 weeks ago

nthtrung09it commented 2 weeks ago

Imagine that I have two tables: pack_a and pack_b. In my application, I only want to fetch the items of pack_a when user enters the feature A, and only fetch the items of pack_b when user enters the feature B. For now, when my app launched and logged in, all items of pack_a and pack_b have been downloaded into my sqlite. How can I do it? Do you have a mechanism that dynamically adds tables into schema.

rkistner commented 2 weeks ago

The schema is not related to what data is being downloaded. I'd recommend to always have both tables in the schema, and use client parameters to control what is being synced.

nthtrung09it commented 2 weeks ago

@rkistner thanks for the quick reply.

1) I checked out the docs (https://docs.powersync.com/usage/sync-rules/advanced-topics/client-parameters-beta):

function connectPowerSync() {
  powerSync.connect(connector, {
    params: { "load_pack_a": true/false }
  });
}

When I want to change the param load_pack_a, I must disconnect and then connect again? Btw, do you have any examples of using the client parameters in react/react-native?

2) How about If I use webpack module federation for my mobile app, the tables may not been known. They only have been defined when the features have been loaded. when the feature A has been loaded, should i create a PowerSyncDatabase with schema only contains the pack_a table? and when the feature B has been loaded, should i create a PowerSyncDatabase with schema only contains the pack_b table? If I do this, and my app has (n) features, maybe my app will have (n) websockets. Do you have any advices?

rkistner commented 2 weeks ago

If the parameters change, you can just call connect again (it automatically disconnects and reconnects internally).

The current architecture is not compatible with lazy-loading parts of your schema in the same database - I'd recommend defining your schema in a single module instead, containing all the features.

If you do really want independent features, one approach is to use a completely separate PowerSyncDatabase for each. In that case, you would have a separate database and a separate connection per feature, and each of these connections will count towards your "peak concurrent connections" limit.

nthtrung09it commented 2 weeks ago

@rkistner do you have any example code about lazy loading with client parameters?

kobiebotha commented 2 weeks ago

@rkistner do you have any example code about lazy loading with client parameters?

We're working on some example code, should be available next week.

nthtrung09it commented 5 days ago

hi @rkistner @kobiebotha how is it going?