1

We have a use case where logged in user will has list of products which can be shown even if not internet connection and i tried to achieve this using realm device sync (Flexible) using mongodb query (Aggregation) but it able to fetch products only when internet is available so my question is that is it possible with realm device sync that we sync products from mongodb using mongodb query and load even if there is no internet connection?

Below is sample code that i tried to integrate in our app but it

const config = {
    id: this.id
};
this.app = new Realm.App(config);

const credentials = Realm.Credentials.anonymous();
await this.app.logIn(credentials);


this.connection =  await Realm.open({
    schema: [Customer],
    sync: {
        user: this.app.currentUser,
        flexible: true,
        onError: (_session, error: any) => {
            console.log('sync error');
            console.log(error.name, error.message);
        }
    }
});

const mongodb = this.app.currentUser.mongoClient('mongodb-atlas');
const collectionUsers = mongodb.db('dev_database').collection('user_roles');

const pipeline = [{
    ...
}];

const result = await collectionUsers.aggregate(pipeline);
// Get result when user is online but do not loads after i switch to offline

So problem is above aggregate query doesn't work if user is offline

I tried realm mongodb query and expecting it should work when user is offline as well

  • 1
    I think you have two different connections going on here. Realm is a local first database and then later syncs to the server. Additionally, you can interact with the MongoDB server directly as well. The code kinda has both; generally speaking if you connect to a Sync'd Realm it would be like this `const realm = await Realm.open({` and then you'd perform operations with the retrieved Realm. When you're using collections - that's talking directly to the back end server, bypassing sync. – Jay Dec 20 '22 at 20:26
  • @Jay Ok so if i want to directly query the backend database which is in atlas mongodb then it wouldn't be synced offline? – Hardik Chavda Dec 21 '22 at 04:06
  • The backend can be directly accessed; create, read, update, delete and query with no syncing at all - that ability bypasses all of the features of Realm as you're interacting directly with the server. However, part of the power and flexibility of Realm is provide that capability through the SDK which allows you to take advantage of live objects and using observers to keep your UI and data fresh. Probably a good idea to review [Realm](https://www.mongodb.com/docs/realm/introduction/) and [Quick Start](https://www.mongodb.com/docs/realm/sdk/react-native/quick-start/) – Jay Dec 21 '22 at 20:04
  • Okay understood but as i posted our use case is to interacting directly with server database and keep that data offline when user is offline so can we really solve that problem using realm? There will be thousands of products will be added from server that we need to keep up to date in mobile for offline purpose by user wise. – Hardik Chavda Dec 22 '22 at 04:24
  • 1
    What you described is what Realm does; Realm is offline first so data manipulated through the Realm SDK's is stored locally, always available to the user and then syncs later. The process that you're using, writing directing to the server, bypasses that feature; when you interact directly with the server, data is not persisted locally or synced. Take a look at the links I included above as it contains a lot more data and examples of how to persist the data locally. For example, see [Create Realm Objects](https://www.mongodb.com/docs/realm/sdk/react-native/quick-start/#create-realm-objects) – Jay Dec 22 '22 at 18:47

0 Answers0