Skip to content
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

Support dynamically adding tables in schema at runtime #332

Open
nthtrung09it opened this issue Oct 7, 2024 · 9 comments
Open

Support dynamically adding tables in schema at runtime #332

nthtrung09it opened this issue Oct 7, 2024 · 9 comments

Comments

@nthtrung09it
Copy link

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
Copy link
Contributor

rkistner commented Oct 7, 2024

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
Copy link
Author

@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?

  1. 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
Copy link
Contributor

rkistner commented Oct 8, 2024

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
Copy link
Author

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

@kobiebotha
Copy link
Contributor

@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
Copy link
Author

hi @rkistner @kobiebotha how is it going?

@amjadbouhouch
Copy link

+1

@rkistner
Copy link
Contributor

@nthtrung09it @amjadbouhouch Apologies for the delay. We now have an example with sync rules here:
https://docs.powersync.com/usage/use-case-examples/prioritized-sync

The specific example is for having different "priorities" for synced data, but the same could apply for lazy-loading different sets of data.

@nthtrung09it
Copy link
Author

Hi @rkistner, do you have example code for the prioritized-sync and lazy load?
And it seems that the Client Parameters still in Beta.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants