You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to create separate databases for each userId. The idea is to segregate the data for each user. In the code below you can see that if the currentUser has changed, we return pertaining db instance.
import{createRxDatabase,addRxPlugin}from'rxdb';import{getRxStorageDexie}from'rxdb/plugins/storage-dexie';import{heroSchema}from'./Schema';import{replicateCouchDB}from'rxdb/plugins/replication-couchdb';import{RxDBLeaderElectionPlugin}from'rxdb/plugins/leader-election';addRxPlugin(RxDBLeaderElectionPlugin);constsyncURL='http://'+window.location.hostname+':10102/';console.log('host: '+syncURL);letdbPromise=null;const_create=async(userId)=>{if(process.env.NODE_ENV==="development"){awaitimport('rxdb/plugins/dev-mode').then(module=>addRxPlugin(module.RxDBDevModePlugin));}console.log('DatabaseService: creating database..');constdb=awaitcreateRxDatabase({name: `heroesreactdb_${userId}`,storage: getRxStorageDexie()});console.log('DatabaseService: created database');window['db']=db;// show leadership in titledb.waitForLeadership().then(()=>{console.log('isLeader now');document.title='♛ '+document.title;});// create collectionsconsole.log('DatabaseService: create collections');awaitdb.addCollections({heroes: {schema: heroSchema,methods: {hpPercent(){returnthis.hp/this.maxHP*100;}}}});// hooksconsole.log('DatabaseService: add hooks');db.collections.heroes.preInsert(docObj=>{const{ color }=docObj;returndb.collections.heroes.findOne({selector: { color }}).exec().then(has=>{if(has!==null){console.error('another hero already has the color '+color);thrownewError('color already there');}returndb;});});// syncconsole.log('DatabaseService: sync');awaitPromise.all(Object.values(db.collections).map(async(col)=>{try{// create the CouchDB databaseawaitfetch(syncURL+col.name+`_${userId}`+'/',{method: 'PUT'});}catch(err){}}));console.log('DatabaseService: sync - start live');Object.values(db.collections).map(col=>col.name).map(colName=>{consturl=`${syncURL}${colName}_${userId}/`;console.log('url: '+url);constreplicationState=replicateCouchDB({collection: db[colName],
url,live: true,pull: {},push: {},autoStart: true});replicationState.error$.subscribe(err=>{console.log('Got replication error:');console.dir(err);});});returndb;};conststoredUserId=localStorage.getItem('userId');if(storedUserId===null){localStorage.setItem('userId','1');}letcurrentUser=null;exportconstrecreateDBPromise=()=>{constnewUser=localStorage.getItem('userId');if(newUser!==currentUser){dbPromise=_create(newUser);currentUser=newUser;}};exportconstget=()=>{recreateDBPromise();console.log('Current user:',currentUser);returndbPromise||_create(localStorage.getItem('userId'));};
Here you can see we have a button that toggles between user ids. These user ids are stored in the localStorage and referred when creating db instances as shown above. Also, when userId is changed I also intend to navigate to the home screen which I have managed to mimic using react-router-domuseNavigate function.
Note: These approaches here are all contrived. I have tried simplify this example as much as possible to make a point. However, the issue does exists in the full blown app where I am trying to switch between google oauth users and navigate from the signin page to the home page.
Screen.Recording.2024-04-20.at.11.31.42.PM.mov
The problem that I face here is that despite application storage having separate dbs for both the user ids. Switching between user ids fails to fetch data.
Not sure if this is bug or it is something that I am doing wrong?
Screen.Recording.2024-04-20.at.11.32.49.PM.mov
Although I would like to point out that reloading the whole page using window.location.reload(); while navigating avoids this issue!
You have to either cache the RxDatabase instance or call .destroy() on it before recreating it. It seems you are creating it twice for the same userId 1. This check is there to exactly prevent creating it twice.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed soon. If you still have a problem, make a PR with a test case or to prove that you have tried to fix the problem. Notice that only bugs in the rxdb premium plugins are ensured to be fixed by the maintainer. Everything else is expected to be fixed by the community, likely you must fix it by yourself.
I am trying to create separate databases for each
userId
. The idea is to segregate the data for each user. In the code below you can see that if thecurrentUser
has changed, we return pertaining db instance.Here you can see we have a button that toggles between user ids. These user ids are stored in the
localStorage
and referred when creating db instances as shown above. Also, whenuserId
is changed I also intend to navigate to the home screen which I have managed to mimic usingreact-router-dom
useNavigate
function.Note: These approaches here are all contrived. I have tried simplify this example as much as possible to make a point. However, the issue does exists in the full blown app where I am trying to switch between google oauth users and navigate from the signin page to the home page.
Screen.Recording.2024-04-20.at.11.31.42.PM.mov
The problem that I face here is that despite application storage having separate dbs for both the user ids. Switching between user ids fails to fetch data.
Not sure if this is bug or it is something that I am doing wrong?
Screen.Recording.2024-04-20.at.11.32.49.PM.mov
Although I would like to point out that reloading the whole page using
window.location.reload();
while navigating avoids this issue!I have created a Pr to my fork so that it is easier to see the changes that were made to reproduce this issue: https://github.com/iamsmkr/rxdb/pull/1/files
The text was updated successfully, but these errors were encountered: