Open purpleCapibara opened 3 days ago
➤ PM Bot commented:
Jira ticket: RJS-2914
I managed to seed database by using realmRef
, checking whether db is empty and running seeding script, but not sure whether there are any edge cases to be aware in this case.
function App(): React.JSX.Element {
const realmRef = useRef<Realm | null>(null);
useEffect(() => {
if (realmRef.current?.isEmpty) {
realm.write(() => {
Object.entries(RealmFeatureFlag).forEach(([, value]) => {
realm.create(REALM_TYPE_FEATURE_FLAGS, {
name: value,
value: false,
});
});
});
}
}, []);
return (
<RealmProvider
realmRef={realmRef}
schema={[FeatureFlagsSchema]}
>
<SafeAreaView>
<FeatureFlagView />
</SafeAreaView>
</RealmProvider>
);
}
Edit: found even better solution
const realm = new Realm(realmConfig);
// From the docs:
// **Note:** the hooks returned from `createRealmContext` using an existing Realm can be used outside of the scope of the provider.
const { RealmProvider, useRealm, useQuery } = createRealmContext(realm);
const useSeedRealmOnFirstOpen = () => {
const realm = useRealm();
useEffect(() => {
if (realm.isEmpty) {
// initialise feature flags
realm.write(() => {
Object.entries(RealmFeatureFlag).forEach(([, value]) => {
realm.create<RealmFeatureFlags>(
REALM_TYPE_FEATURE_FLAGS,
{
name: value,
value: false,
},
Realm.UpdateMode.Never,
);
});
});
}
}, [realm]);
};
export const RealmProvider = ({ children }: Props) => {
useSeedRealmOnFirstOpen();
return <ReactRealmProvider>{children}</ReactRealmProvider>;
};
How frequently does the bug occur?
Always
Description
Realm documentation mentions optional
onFirstOpen
callback that can be used to seed database with data if necessary. https://www.mongodb.com/docs/realm-sdks/js/latest/types/BaseConfiguration.htmlI'm currently trying to implement Realm DB in a React Native app that needs to seed data on the first load. This callback seems like the best way to do it, but I was not able to make it work in my production app and in a simple repro.
This issue seems to be present in the latest available version of
realm-js
(12.3.1),@realm/react
(0.11), andreact-native
(0.76) with New Architecture enabled. My production app is still onreact-native
0.74 using old architecture,realm-js
12.11.1, and@realm/react
(0.8.0) and issue seems to be present there too.Callback is not triggered for encrypted databases too.
Last year there was a similar issue opened, but no repro was provided: https://github.com/realm/realm-js/issues/6143
Stacktrace & log output
No response
Can you reproduce the bug?
Always
Reproduction Steps
onFirstOpen
callback and create data that is needed for created schemaRepository with reproduction: https://github.com/purpleCapibara/realm-onFirstLoad-repro/blob/main/App.tsx
Version
"@realm/react": "^0.11.0", "react": "18.3.1", "react-native": "0.76.0", "realm": "^12.13.1"
What services are you using?
Local Database only
Are you using encryption?
No
Platform OS and version(s)
Android 15 & iOS 18
Build environment
Output from
npx react-native info
:Logs
Cocoapods version
1.15.2