Convert Figma logo to code with AI

realm logorealm-js

Realm is a mobile database: an alternative to SQLite & key-value stores

5,729
565
5,729
612

Top Related Projects

A framework for building native applications using React

33,519

An open-source framework for making universal native apps with React. Expo runs on Android, iOS, and the web.

A powerful cross-platform UI toolkit for building native-quality iOS, Android, and Progressive Web Apps with HTML, CSS, and JavaScript.

164,677

Flutter makes it easy and fast to build beautiful apps for mobile and beyond

⚡ Empowering JavaScript with native platform APIs. ✨ Best of all worlds (TypeScript, Swift, Objective C, Kotlin, Java, Dart). Use what you love ❤️ Angular, Capacitor, Ionic, React, Solid, Svelte, Vue with: iOS (UIKit, SwiftUI), Android (View, Jetpack Compose), Dart (Flutter) and you name it compatible.

Quick Overview

Realm JS is a mobile database that runs directly inside phones, tablets, or wearables. It's a fast, easy-to-use alternative to SQLite and Core Data, allowing developers to build reactive mobile apps quickly. Realm JS supports React Native, Node.js, and Electron platforms.

Pros

  • Fast performance with a small footprint
  • Easy to use with a simple, object-oriented API
  • Cross-platform support (iOS, Android, and web)
  • Real-time synchronization capabilities

Cons

  • Limited query capabilities compared to SQL databases
  • Steeper learning curve for developers used to traditional relational databases
  • Potential migration challenges when updating schema
  • Less mature ecosystem compared to established databases

Code Examples

  1. Defining a Realm schema:
const PersonSchema = {
  name: 'Person',
  properties: {
    name: 'string',
    age: 'int',
    cars: 'Car[]'
  }
};

const CarSchema = {
  name: 'Car',
  properties: {
    make: 'string',
    model: 'string',
    year: 'int'
  }
};
  1. Opening a Realm and writing data:
const realm = await Realm.open({schema: [PersonSchema, CarSchema]});

realm.write(() => {
  const person = realm.create('Person', {
    name: 'John',
    age: 30,
    cars: [{make: 'Toyota', model: 'Corolla', year: 2020}]
  });
});
  1. Querying data from Realm:
const youngPeople = realm.objects('Person').filtered('age < 35');
const toyotaOwners = realm.objects('Person').filtered('cars.make == "Toyota"');

youngPeople.addListener((collection, changes) => {
  // React to changes in the collection
});

Getting Started

  1. Install Realm JS:
npm install realm
  1. Import and use Realm in your project:
import Realm from 'realm';

// Define your schema
const PersonSchema = {
  name: 'Person',
  properties: {
    name: 'string',
    age: 'int'
  }
};

// Open a Realm
const realm = await Realm.open({schema: [PersonSchema]});

// Use Realm in your app
realm.write(() => {
  realm.create('Person', {name: 'Alice', age: 25});
});

const people = realm.objects('Person');
console.log(`There are ${people.length} people in the database.`);

Competitor Comparisons

A framework for building native applications using React

Pros of React Native

  • Larger community and ecosystem, with more third-party libraries and resources
  • Cross-platform development for iOS and Android with a single codebase
  • Faster development and hot reloading for quicker iteration

Cons of React Native

  • Steeper learning curve, especially for developers new to React
  • Performance can be slower than native development for complex applications
  • Requires frequent updates to keep up with iOS and Android changes

Code Comparison

React Native:

import React from 'react';
import { View, Text } from 'react-native';

const App = () => (
  <View>
    <Text>Hello, React Native!</Text>
  </View>
);

Realm JS:

import Realm from 'realm';

const TaskSchema = {
  name: 'Task',
  properties: {
    _id: 'int',
    name: 'string',
    status: 'string?'
  }
};

const realm = await Realm.open({schema: [TaskSchema]});

React Native focuses on UI components and cross-platform development, while Realm JS is primarily used for local data storage and synchronization. React Native is more suitable for building entire mobile applications, whereas Realm JS is typically used as a database solution within an app. The choice between the two depends on the specific needs of your project, with React Native being a more comprehensive framework for app development and Realm JS serving as a specialized database solution.

33,519

An open-source framework for making universal native apps with React. Expo runs on Android, iOS, and the web.

Pros of Expo

  • Comprehensive development environment with built-in tools and services
  • Simplified app deployment and over-the-air updates
  • Large ecosystem of pre-built components and libraries

Cons of Expo

  • Limited access to native modules without ejecting
  • Larger app size due to included libraries
  • Potential performance overhead for complex applications

Code Comparison

Expo (initializing a new project):

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';

export default function App() {
  return (
    <View style={styles.container}>
      <Text>Hello, Expo!</Text>
    </View>
  );
}

Realm JS (setting up a database):

import Realm from 'realm';

const TaskSchema = {
  name: 'Task',
  properties: {
    _id: 'int',
    name: 'string',
    status: 'string?'
  }
};

const realm = await Realm.open({schema: [TaskSchema]});

While Expo provides a more comprehensive development environment for React Native apps, Realm JS focuses on efficient local data storage and synchronization. Expo simplifies the development process but may limit native module access, whereas Realm JS offers powerful database capabilities but requires more setup for a complete app development environment.

A powerful cross-platform UI toolkit for building native-quality iOS, Android, and Progressive Web Apps with HTML, CSS, and JavaScript.

Pros of Ionic Framework

  • Comprehensive UI component library for building cross-platform mobile and web apps
  • Strong community support and extensive documentation
  • Integrates well with popular web frameworks like Angular, React, and Vue

Cons of Ionic Framework

  • Steeper learning curve for developers new to web technologies
  • Performance may not match native apps for complex, graphics-intensive applications
  • Larger app size compared to native solutions

Code Comparison

Ionic Framework (TypeScript):

import { Component } from '@angular/core';
import { IonicModule } from '@ionic/angular';

@Component({
  selector: 'app-home',
  template: '<ion-button>Click me</ion-button>',
  standalone: true,
  imports: [IonicModule],
})
export class HomePage {}

Realm JS (JavaScript):

const Realm = require('realm');

const TaskSchema = {
  name: 'Task',
  properties: {
    _id: 'int',
    name: 'string',
    status: 'string?'
  }
};

const realm = await Realm.open({schema: [TaskSchema]});

While Ionic Framework focuses on UI components and cross-platform development, Realm JS is primarily a mobile database solution. Ionic is better suited for building entire applications with a rich user interface, whereas Realm JS excels in efficient local data storage and synchronization for mobile apps.

164,677

Flutter makes it easy and fast to build beautiful apps for mobile and beyond

Pros of Flutter

  • Cross-platform development for mobile, web, and desktop from a single codebase
  • Rich set of customizable widgets for building native interfaces
  • Hot reload feature for faster development and iteration

Cons of Flutter

  • Larger app size compared to native applications
  • Steeper learning curve for developers new to Dart programming language
  • Limited access to some platform-specific features

Code Comparison

Flutter (Dart):

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

Realm.js (JavaScript):

import Realm from 'realm';

const realm = await Realm.open({
  schema: [MySchema]
});

Key Differences

  • Flutter is a UI toolkit for building cross-platform applications, while Realm.js is a mobile database solution
  • Flutter uses Dart programming language, whereas Realm.js is used with JavaScript/TypeScript
  • Flutter focuses on UI development, while Realm.js specializes in data persistence and synchronization

Use Cases

Flutter:

  • Developing visually appealing, cross-platform mobile applications
  • Creating responsive web applications with a native feel
  • Building desktop applications with a unified codebase

Realm.js:

  • Implementing offline-first mobile applications
  • Managing complex data models in JavaScript/TypeScript projects
  • Synchronizing data between mobile devices and servers

⚡ Empowering JavaScript with native platform APIs. ✨ Best of all worlds (TypeScript, Swift, Objective C, Kotlin, Java, Dart). Use what you love ❤️ Angular, Capacitor, Ionic, React, Solid, Svelte, Vue with: iOS (UIKit, SwiftUI), Android (View, Jetpack Compose), Dart (Flutter) and you name it compatible.

Pros of NativeScript

  • Allows development of truly native mobile apps using JavaScript, TypeScript, or Angular
  • Provides access to native platform APIs and UI components
  • Supports a wide range of plugins for extending functionality

Cons of NativeScript

  • Steeper learning curve compared to Realm JS
  • Larger app size due to bundling of the entire framework
  • Performance can be slower than native development in some cases

Code Comparison

NativeScript (UI component):

<StackLayout>
  <Label text="Hello, NativeScript!" />
  <Button text="Click me" tap="onButtonTap" />
</StackLayout>

Realm JS (data model):

const Realm = require('realm');

const PersonSchema = {
  name: 'Person',
  properties: {
    name: 'string',
    age: 'int'
  }
};

const realm = await Realm.open({schema: [PersonSchema]});

NativeScript focuses on creating native UI components and handling user interactions, while Realm JS is primarily used for data persistence and management. NativeScript provides a more comprehensive solution for building entire mobile applications, whereas Realm JS specializes in efficient local database operations.

NativeScript offers greater flexibility in UI design and native API access, but may require more setup and configuration. Realm JS, on the other hand, provides a simpler and more straightforward approach to data storage and synchronization, but lacks the full-fledged app development capabilities of NativeScript.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

[!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

  • Mobile-first: Realm is the first database built from the ground up to run directly inside phones, tablets and wearables.
  • Simple: Data is directly exposed as objects and queryable by code, removing the need for ORM's riddled with performance & maintenance issues.
  • Modern: The database supports relationships, generics, and vectorization.
  • Fast: It is faster than even raw SQLite on common operations, while maintaining an extremely rich feature set.
  • MongoDB Atlas Device Sync: Makes it simple to keep data in sync across users, devices, and your backend in real time. Get started for free with a template application and create the cloud backend.

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

  • Accurate TypeScript definitions @realm/babel-plugin to transform TypeScript classes to Realm schemas. An example of a model class is:
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.

Getting Help

  • Need help with your code?: Look for previous questions on the #realm tag — or ask a new question. You can also check out our Community Forum where general questions about how to do something can be discussed.
  • Have a bug to report? Open an issue. If possible, include the version of Realm, a full log, the Realm file, and a project that shows the issue.
  • Have a feature request? Open an issue. Tell us what the feature should do, and why you want the feature.

Contributing

See CONTRIBUTING.md for more details!

Known issues

  • Realm is not compatible with the legacy Chrome Debugger. The following debugging methods are supported:
    • Hermes Debugger is the recommended way for debugging modern React Native apps.
    • Safari also has a similar feature set, but requires some setup and only supports debugging in iOS.
    • NOTE: For the above methods, it is not necessary to enable Debug with Chrome in the Debug Menu.

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.

  • install the expo-dev-client:
    npx expo install expo-dev-client
    
  • build the dev client for iOS
    npx expo run:ios
    
  • build the dev client for Android
    npx expo run:android
    
  • start the bundler without building
    npx expo start --dev-client
    

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:

  • What version of Realm is being installed.
  • The OS platform and version which is being used.
  • If a JavaScript framework (currently React Native and Electron) is used and its version.
  • Which JavaScript engine is being used.
  • Node.js version number.
  • TypeScript version if used.
  • An anonymous machine identifier and hashed application name to aggregate the other information on.

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.

NPM DownloadsLast 30 Days