powersync-js icon indicating copy to clipboard operation
powersync-js copied to clipboard

Support dynamically adding tables in schema at runtime

Open nthtrung09it opened this issue 1 year ago • 9 comments

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.

nthtrung09it avatar Oct 07 '24 15:10 nthtrung09it

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.

rkistner avatar Oct 07 '24 15:10 rkistner

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

nthtrung09it avatar Oct 07 '24 16:10 nthtrung09it

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.

rkistner avatar Oct 08 '24 11:10 rkistner

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

nthtrung09it avatar Oct 10 '24 06:10 nthtrung09it

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

kobiebotha avatar Oct 10 '24 15:10 kobiebotha

hi @rkistner @kobiebotha how is it going?

nthtrung09it avatar Oct 22 '24 07:10 nthtrung09it

+1

amjadbouhouch avatar Nov 06 '24 22:11 amjadbouhouch

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

rkistner avatar Nov 12 '24 07:11 rkistner

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

nthtrung09it avatar Nov 29 '24 12:11 nthtrung09it