realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.62k stars 558 forks source link

Your app is relying on a Realm global, which will be removed in realm-js v13, please update your code to ensure you import Realm #6209

Closed hpelitebook745G2 closed 2 months ago

hpelitebook745G2 commented 7 months ago

How frequently does the bug occur?

Always

Description

I wanted to implement a Singleton class for Realm to ensure reusability throughout the application. Creating a separate instance for each component doesn't seem practical.

Stacktrace & log output

WARN  Your app is relying on a Realm global, which will be removed in realm-js v13, please update your code to ensure you import Realm:

 import Realm from "realm"; // For ES Modules
 const Realm = require("realm"); // For CommonJS

 To determine where, put this in the top of your index file:
 import Realm from "realm";
 Realm.flags.THROW_ON_GLOBAL_REALM = true

Can you reproduce the bug?

Always

Reproduction Steps

  1. Create a file that holds Realm instance
const realm = new Realm({ schema: [TestSchema] })

export default realm
  1. Import this from your component
  2. Get the warning

Versions:

"@realm/react": "^0.6.1",
"realm": "^12.2.1",
"realm-flipper-plugin-device": "^1.1.0",

Version

12.2.1

What services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

iOS 17 and Android 11.0

Build environment

Which debugger for React Native: Flipper Version 0.228.0 (50.0.0)

Cocoapods version

1.13.0

kraenhansen commented 7 months ago

Please try setting the THROW_ON_GLOBAL_REALM as outlined here, to figure out where you're using Realm without an import:

import { flags } from "realm";
flags.THROW_ON_GLOBAL_REALM = true;
buster95 commented 6 months ago

this is because you are using Realm without import check your Schemas and every part you use Realm.

for example you can define a schema in this way

export class PersonSchema extends Realm.Object<Person> {
  name!: string
  age?: number

  static schema: Realm.ObjectSchema = {
    name: 'Person',
    properties: {
      name: 'string',
      age: 'int?',
    },
  }
}

but the right way is this

import Realm from 'realm' // <-- check yours imports

export class PersonSchema extends Realm.Object<Person> {
  name!: string
  age?: number

  static schema: Realm.ObjectSchema = {
    name: 'Person',
    properties: {
      name: 'string',
      age: 'int?',
    },
  }
}
congduong97 commented 5 months ago
Screenshot 2023-12-01 at 12 10 08

Thanks @buster95 , it worked. my problem when create new item use Realm.BSON for id without import Realm