I have a well built application in react native, it works fine both at web and mobile platforms. Except one thing
I always have to comment one of the imports
import LokiJSAdapter from '@nozbe/watermelondb/adapters/lokijs';
or
import SQLiteAdapter from '@nozbe/watermelondb/adapters/sqlite';
depending upon what platform I have to run it.
I can import even conditionally but problem is that I can not afford to change the result of export statement i.e. export { database }
See the full code of file
import { Database } from '@nozbe/watermelondb';
import { DbModels, DbSchema } from './schema';
let mowebDb = 'moweb';
// import LokiJSAdapter from '@nozbe/watermelondb/adapters/lokijs';
// let adapter = new LokiJSAdapter({
// schema: DbSchema,
// dbName: mowebDb,
// useWebWorker: false,
// useIncrementalIndexedDB: true,
// })
import SQLiteAdapter from '@nozbe/watermelondb/adapters/sqlite';
let adapter = new SQLiteAdapter({
dbName: mowebDb,
schema: DbSchema,
});
const database = new Database({
adapter,
actionsEnabled: true,
modelClasses: DbModels,
});
export { database }
Can I add a condition rather than commenting lines, to produce same result as below?
To keep your export consistent while conditionally importing, try dynamic imports inside an async function, then set your adapter based on the platform. Unfortunately, this approach will return a Promise, not a simple object. To work around this, you might initialize the database inside an async function and ensure all database operations wait until this initialization is complete. It's not as clean as a direct export, but it allows for conditional logic while maintaining consistency in what your modules export. Keep in mind that React Native might require specific setup for dynamic imports to work smoothly.