realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.8k stars 576 forks source link
mobile-database mobile-first nosql-database reactive reactnative realm realm-mobile-database realtime-database

[!WARNING] We announced the deprecation of Atlas Device Sync + Realm SDKs in September 2024. For more information please see:

For a version of RealmJS without sync features, install from community on npm or see the community git branch.

realm by MongoDB

The Realm Database

Realm is a mobile database that runs directly inside phones, tablets or wearables. This project hosts the JavaScript & TypeScript implementation of Realm. Currently, we support React Native (JSC & Hermes on iOS & Android), Node.js and Electron (on Windows, MacOS and Linux).

What are the Atlas Device SDKs?

Atlas Device Sync

The Atlas Device SDKs are a collection of language and platform specific SDKs, each with a suite of app development tools optimized for data access and persistence on mobile and edge devices. Use the SDKs to build data-driven mobile, edge, web, desktop, and IoT apps.

It might help to think of the Realm database as the persistance layer of the Atlas Device SDKs.

Features

Getting Started

Please see the detailed instructions in our docs to use Atlas Device SDK for Node.js and Atlas Device SDK for React Native. Please notice that currently only Node.js version 18 or later is supported. For React Native users, we have a compatibility matrix showing which versions are supported.

Documentation

Atlas Device SDKs for React Native and Node.js

The documentation for the Atlas Device SDK for React Native can be found at mongodb.com/docs/atlas/device-sdks/sdk/react-native/. The documentation for the Atlas Device SDK for Node.js can be found at mongodb.com/docs/atlas/device-sdks/sdk/node/.

The API reference is located at docs.mongodb.com/realm-sdks/js/latest/.

If you are using React Native, please also take a look the README for @realm/react, which provides React hooks to make working with Realm easier.

TypeScript models

TypeScript is a popular alternative to pure JavaScript as it provide static typing. Our TypeScript support consists of two parts

class Task extends Realm.Object<Task, "description"> {
  _id = new Realm.BSON.ObjectId();
  description!: string;
  @index
  isComplete = false;

  static primaryKey = "_id";

  constructor(realm, description: string) {
    super(realm, { description });
  }
}

Integration with React Native

The Atlas Device SDK for React Native provides persistence of objects and advanced queries for persisted objects. You can have easier integration with React Native by using @realm/react.

Template apps

We have TypeScript templates to help you get started using Realm. Follow the links to your desired template and follow the instructions there to get up and running fast.

Contributing

See CONTRIBUTING.md for more details!

Known issues

Building the SDK

For instructions on building the SDK from the source, see the building.md file.

Troubleshooting missing binary

It's possible after installing and running Realm that one encounters the error Could not find the Realm binary. Here are are some tips to help with this.

Compatibility

Consult our COMPATIBILITY.md to ensure you are running compatible version of realm with the supported versions of node, react-native or expo.

React Native

iOS

Typically this error occurs when the pod dependencies haven't been updating. Try running the following command:

npx pod-install

If that still doesn't help it's possible there are some caching errors with your build or your pod dependencies. The following commands can be used to safely clear these caches:

rm -rf ios/Pods
rm ios/Podfile.lock
rm -rf ~/Library/Developer/Xcode/DerivedData

Afterwards, reinstall pods and try again. If this still doesn't work, ensure that prebuilds/apple/realm-core.xcframework directory exists and contains a binary for your platform and architecture. If this is missing, try reinstalling the realm npm package and as well as CocoaPods.

Android

This can occur when installing realm and not performing a clean build. The following commands can be used to clear your cache:

cd android
./gradlew clean

Afterwards, try and rebuild for Android. If you are still encountering problems, ensure that node_moduels/realm/react-native/android/src/main/jniLibs contains a realm binary for your architecture. If this is missing, try reinstalling the realm npm package.

Expo

If you are using Expo, a common pitfall is not installing the expo-dev-client and using the Development Client specific scripts to build and run your React Native project in Expo. The Development Client allows you to create a local version of Expo Go which includes 3rd party libraries such as Realm. If you would like to use realm in an Expo project, the following steps can help.

Node/Electron

When running npm install realm the realm binaries for the detected architecture are downloaded into node_modules/realm/prebuilds. If this directory is missing or empty, ensure that there weren't any network issues reported on installation.

Analytics

Asynchronously submits install information to Realm.

Why are we doing this? In short, because it helps us build a better product for you. None of the data personally identifies you, your employer or your app, but it will help us understand what language you use, what Node.js versions you target, etc. Having this info will help prioritizing our time, adding new features and deprecating old features. Collecting an anonymized application path & anonymized machine identifier is the only way for us to count actual usage of the other metrics accurately. If we don’t have a way to deduplicate the info reported, it will be useless, as a single developer npm install-ing the same app 10 times would report 10 times more than another developer that only installs once, making the data all but useless. No one likes sharing data unless it’s necessary, we get it, and we’ve debated adding this for a long long time. If you truly, absolutely feel compelled to not send this data back to Realm, then you can set an env variable named REALM_DISABLE_ANALYTICS.

Currently the following information is reported:

Moreover, we unconditionally write various constants to a file which we might use at runtime.

Code of Conduct

This project adheres to the MongoDB Code of Conduct. By participating, you are expected to uphold this code. Please report unacceptable behavior to community-conduct@mongodb.com.

License

Realm JS and Realm Core are published under the Apache License 2.0.

Feedback

If you use Realm and are happy with it, all we ask is that you please consider sending out a tweet mentioning @realm to share your thoughts

And if you don't like it, please let us know what you would like improved, so we can fix it!

Contributors

Made with contrib.rocks.