• Stars
    star
    702
  • Rank 62,155 (Top 2 %)
  • Language
    Dart
  • License
    Apache License 2.0
  • Created almost 4 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Realm is a mobile database: a replacement for SQLite & ORMs.
realm by MongoDB

License Realm Dart CI Coverage Status

Realm is a mobile database that runs directly inside phones, tablets or wearables. This repository holds the source code for the Realm SDK for Flutter™ and Dart™.

Features

  • Mobile-first: Realm is the first database built from the ground up to run directly inside phones, tablets, and wearables.
  • Simple: Realm’s object-oriented data model is simple to learn, doesn’t need an ORM, and the API lets you write less code to get apps up & running in minutes.
  • Modern: Realm supports latest Dart and Flutter versions and is build with sound null-safety.
  • Fast: Realm 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

  • Import Realm in a dart file app.dart

    import 'package:realm/realm.dart';  // import realm package
    
    part 'app.g.dart'; // declare a part file.
    
    @RealmModel() // define a data model class named `_Car`.
    class _Car {
      late String make;
    
      late String model;
    
      int? kilometers = 500;
    }
  • Generate RealmObject class Car from data model class _Car.

    dart run realm generate
    
  • Open a Realm and add some objects.

    var config = Configuration.local([Car.schema]);
    var realm = Realm(config);
    
    var car = Car("Tesla", "Model Y", kilometers: 5);
    realm.write(() {
      realm.add(car);
    });
  • Query objects in Realm.

    var cars = realm.all<Car>();
    Car myCar = cars[0];
    print("My car is ${myCar.make} model ${myCar.model}");
    
    cars = realm.all<Car>().query("make == 'Tesla'");
  • Get stream of result changes for a query.

    final cars = realm.all<Car>().query(r'make == $0', ['Tesla']);
    cars.changes.listen((changes) {
      print('Inserted indexes: ${changes.inserted}');
      print('Deleted indexes: ${changes.deleted}');
      print('Modified indexes: ${changes.modified}');
    });
    realm.write(() => realm.add(Car('VW', 'Polo', kilometers: 22000)));

Samples

For complete samples check the Realm Flutter and Dart Samples.

Documentation

For API documentation go to

Use realm package for Flutter and realm_dart package for Dart applications.

For complete documentation of the SDKs, go to the Realm SDK documentation.

If you are using the Realm SDK for the first time, refer to the Quick Start documentation.

To learn more about using Realm with Atlas App Services and Device Sync, refer to the following Realm SDK documentation:

Realm Flutter SDK

Realm Flutter package is published to realm.

Environment setup for Realm Flutter

  • Realm Flutter supports the platforms iOS, Android, Windows, MacOS and Linux.

  • Flutter 3.0.3 or newer.

  • For Flutter Desktop environment setup, see Desktop support for Flutter.

  • Cocoapods v1.11 or newer.

  • CMake 3.21 or newer.

Usage

The full contents of catalog.dart is listed after the usage

  • Add realm package to a Flutter application.

    flutter pub add realm
    
  • For running Flutter widget and unit tests run the following command to install the required native binaries.

    dart run realm install
    
  • Import Realm in a dart file (ex. catalog.dart).

    import 'package:realm/realm.dart';
  • Declare a part file catalog.g.dart in the begining of the catalog.dart dart file after all imports.

    import 'dart:io';
    
    part 'catalog.g.dart';
  • Create a data model class.

    It should start with an underscore _Item and be annotated with @RealmModel()

    @RealmModel()
    class _Item {
        @PrimaryKey()
        late int id;
    
        late String name;
    
        int price = 42;
    }
  • Generate RealmObject class Item from data model class _Item.

    On Flutter use dart run realm to run realm package commands

    dart run realm generate
    

    A new file catalog.g.dart will be created next to the catalog.dart.

    *The generated file should be committed to source control

  • Use the RealmObject class Item with Realm.

    // Create a Configuration object
    var config = Configuration.local([Item.schema]);
    
    // Opean a Realm
    var realm = Realm(config);
    
    var myItem = Item(0, 'Pen', price: 4);
    
    // Open a write transaction
    realm.write(() {
        realm.add(myItem);
        var item = realm.add(Item(1, 'Pencil')..price = 20);
    });
    
    // Objects `myItem` and `item` are now managed and persisted in the realm
    
    // Read object properties from realm
    print(myItem.name);
    print(myItem.price);
    
    // Update object properties
    realm.write(() {
        myItem.price = 20;
        myItem.name = "Special Pencil";
    });
    
    // Get objects from the realm
    
    // Get all objects of type
    var items = realm.all<Item>();
    
    // Get object by index
    var item = items[1];
    
    // Get object by primary key
    var itemByKey = realm.find<Item>(0);
    
    // Filter and sort object
    var objects = realm.query<Item>("name == 'Special Pencil'");
    var name = 'Pen';
    objects = realm.query<Item>(r'name == $0', [name]);
    
    // Close the realm
    realm.close();

Full contents of catalog.dart

import 'package:realm/realm.dart';

part 'catalog.g.dart';

@RealmModel()
class _Item {
    @PrimaryKey()
    late int id;

    late String name;

    int price = 42;
}

// Create a Configuration object
var config = Configuration.local([Item.schema]);

// Opean a Realm
var realm = Realm(config);

var myItem = Item(0, 'Pen', price: 4);

// Open a write transaction
realm.write(() {
    realm.add(myItem);
    var item = realm.add(Item(1, 'Pencil')..price = 20);
});

// Objects `myItem` and `item` are now managed and persisted in the realm

// Read object properties from realm
print(myItem.name);
print(myItem.price);

// Update object properties
realm.write(() {
    myItem.price = 20;
    myItem.name = "Special Pencil";
});

// Get objects from the realm

// Get all objects of type
var items = realm.all<Item>();

// Get object by index
var item = items[1];

// Get object by primary key
var itemByKey = realm.find<Item>(0);

// Filter and sort object
var objects = realm.query<Item>("name == 'Special Pencil'");
var name = 'Pen';
objects = realm.query<Item>(r'name == $0', [name]);

// Close the realm
realm.close();

Realm Dart Standalone SDK

Realm Dart package is published to realm_dart.

Environment setup for Realm Dart

  • Realm Dart supports the platforms Windows, Mac and Linux.

  • Dart SDK 2.17.5 or newer.

Usage

  • Add realm_dart package to a Dart application.

    dart pub add realm_dart
    
  • Install the realm_dart package into the application. This downloads and copies the required native binaries to the app directory.

    dart run realm_dart install
    
  • Import realm_dart in a dart file (ex. catalog.dart).

    import 'package:realm_dart/realm.dart';
  • To generate RealmObject classes with realm_dart use this command.

    On Dart use dart run realm_dart to run realm_dart package commands

    dart run realm_dart generate
    

    A new file catalog.g.dart will be created next to the catalog.dart.

    *The generated file should be committed to source control

  • The usage of the Realm Dart SDK is the same like the Realm Flutter above.

Sync data with Realm Flutter and Dart using Device Sync

This section is about how to use the Realm with Device Sync and how to connect to Atlas App Services.

I. Set up Atlas App Services

  1. Create an account on cloud.mongodb.com. Follow the instructions: Register a new Atlas Account.
  2. Create a new App following the instructions here: Create an App with Atlas App Services UI.
  3. Read Authentication Providers to see how to configure the appropriate authentication provider type.
  4. Go to the Device Sync menu and Enable Flexible Sync.
  5. Find and Copy the App ID of your new application.

II. Use Device Sync with the Realm

  1. Initialize the App Services App client and authenticate a user.

    String appId = "<Atlas App ID>";
    final appConfig = AppConfiguration(appId);
    final app = App(appConfig);
    final user = await app.logIn(Credentials.anonymous());
  2. Open a synced realm.

    final config = Configuration.flexibleSync(user, [Task.schema]);
    final realm = Realm(config);
  3. Add a sync subscription and write data.

    Only data matching the query in the subscription will be synced to the server and only data matching the subscription will be downloaded to the local device realm file.

    realm.subscriptions.update((mutableSubscriptions) {
    mutableSubscriptions.add(realm.query<Task>(r'status == $0 AND progressMinutes == $1', ["completed", 100]));
    });
    await realm.subscriptions.waitForSynchronization();
    realm.write(() {
      realm.add(Task(ObjectId(), "Send an email", "completed", 4));
      realm.add(Task(ObjectId(), "Create a meeting", "completed", 100));
      realm.add(Task(ObjectId(), "Call the manager", "init", 2));
    });
    realm.close();

To learn more about how to sync data with Realm using Device Sync, refer to the Quick Start with Sync documentation.

Building the source

See CONTRIBUTING.md for instructions about building the source.

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 [email protected].

License

Realm Flutter and Dart SDKs and Realm Core are published under the Apache License 2.0.

The "Dart" name and logo and the "Flutter" name and logo are trademarks owned by Google.

More Repositories

1

SwiftLint

A tool to enforce Swift style and conventions.
Swift
18,359
star
2

realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
Objective-C
16,122
star
3

realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
Java
11,441
star
4

jazzy

Soulful docs for Swift & Objective-C
Ruby
7,317
star
5

realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
TypeScript
5,588
star
6

realm-dotnet

Realm is a mobile database: a replacement for SQLite & ORMs
C#
1,209
star
7

realm-core

Core database component for the Realm Mobile Database SDKs
C++
993
star
8

realm-kotlin

Kotlin Multiplatform and Android SDK for the Realm Mobile Database: Build Better Apps Faster.
Kotlin
819
star
9

SwiftCov

A tool to generate test code coverage information for Swift.
Swift
563
star
10

realm-browser-osx

DEPRECATED - Realm Browser for Mac OS X has been replaced by realm-studio which is cross platform.
Objective-C
502
star
11

realm-android-adapters

Adapters for combining Realm Java with Android UI components and framework classes
Java
414
star
12

realm-tasks

To Do app built with Realm, inspired by Clear for iOS
Swift
368
star
13

summer-of-swift

An ephemeral contest to learn Swift by doing
308
star
14

realm-object-server

Tracking of issues related to the Realm Object Server and other general issues not related to the specific SDK's
Shell
293
star
15

realm-studio

Realm Studio
TypeScript
277
star
16

RealmContent

Light Realm-powered content management system
Swift
238
star
17

realm-cocoa-converter

A library that provides the ability to import/export Realm files from a variety of data container formats.
Swift
219
star
18

realm-draw

The official Realm Draw app used in promotional videos
C#
163
star
19

github-gantt

Generate Gantt Charts From Github Issues!
JavaScript
154
star
20

realm-object-store

Cross-platform abstractions used within Realm products
C++
118
star
21

realm-kotlin-samples

Samples demonstrating the usage of Realm-Kotlin SDK
Kotlin
85
star
22

RChat

Swift
81
star
23

realm-graphql

GraphQL client for Realm Object Server
TypeScript
80
star
24

realm-loginkit

A generic interface for logging in to Realm Mobile Platform apps
Swift
74
star
25

realm-dart-samples

Samples for Realm Flutter and Realm Dart SDKs
C++
69
star
26

EventKit

A template conference app, featuring real-time schedule and data changes & running on Realm 🚀
Swift
63
star
27

realm-cpp

Realm C++
C++
59
star
28

realm-scanner

A scanning app that can analyze and report on any photos it is given
Java
54
star
29

react-realm-context

Components that simplifies using Realm with React
TypeScript
51
star
30

realm-graphql-service

GraphQL service for Realm Object Server
TypeScript
43
star
31

RealmPop

Java
39
star
32

my-first-realm-app

ToDo demo app using Realm and Realm Object Server to synchronize tasks.
Java
38
star
33

realm-android-user-store

Java
36
star
34

realm-dvdrental

Demo inventory application which synchronizes data originating in Postgres via the Realm Postgres data connector.
Swift
36
star
35

task-tracker-swiftui

Simple task manager using Realm and SwiftUI
Swift
34
star
36

realm-inventory

An sample inventory app demonstrating safe counters via Lists and Realm Counters
Swift
33
star
37

unity-examples

C#
28
star
38

realm-teamwork-MR

A Realm demo app showing an idealized version of a field-service type application using multiple Realms, permissions, etc
Swift
26
star
39

FindOurDevices

A React Native + MongoDB Realm application for allowing users to see location and movement of their own devices or those of people in the same private group.
JavaScript
26
star
40

node-template-project

A template for your Node and TypeScript Project with Visual Studio Code Debugging!
TypeScript
22
star
41

realm-flipper-plugin

A Flipper plugin to debug React Native applications using a Realm database.
TypeScript
19
star
42

realm-java-benchmarks

Kotlin
18
star
43

realm-drawkit

A modular drawing library that uses RMP for collaboration
Swift
17
star
44

aws-devicefarm

Github action for triggering runs on AWS devicefarm
JavaScript
17
star
45

Scrumdinger

Showing how the app from Apple's SwiftUI tutorial can be enhanced by adding Realm
Swift
15
star
46

awesome-realm

A curated list of awesome Realm resources, libraries, tools and applications
14
star
47

roc-ios

Swift
14
star
48

realm-search

An example implementation of synchronizing specific objects from a massive global Realm.
Swift
13
star
49

roc-ios-controller

A Chat Controller powered by Realm and Chatto
Swift
13
star
50

realm-puzzle

A small collaborative game where players work to complete a jigsaw puzzle.
Objective-C
11
star
51

jazzy-integration-specs

Integration specs for https://github.com/realm/jazzy
HTML
10
star
52

realm-MultiUserTasksTutorial

Walk-though of constructing a multi-user example using Realm Tasks
Swift
9
star
53

charts

A Collection of Helm Charts
Smarty
9
star
54

realm-sync-demos

Demo apps for MongoDB Realm Sync
Kotlin
9
star
55

realm-surveys

A reactive survey application powered by Realm
Swift
9
star
56

realm-connectors

Realm Object Server Data Connectors
8
star
57

RCurrency

Swift
8
star
58

realm-cloud-functions-demo

Realm & IBM Cloud Functions demo
JavaScript
8
star
59

realm-dotnet-groupedcollection

C#
8
star
60

electron-react-samples

TypeScript
7
star
61

realm-dotnet-lfs

C#
7
star
62

gitbook

📝 Modern documentation format and toolchain using Git and Markdown
JavaScript
7
star
63

FindOurDevices-backend

A backend MongoDB Realm application for allowing users to see location and movement of their own devices or those of people in the same private group.
JavaScript
7
star
64

Realm-Drawing

Swift
6
star
65

Realm-Sweeper

Swift
6
star
66

feedback-manager

A simple Realm-powered feedback app that employs the Azure Text Analytics API to extract sentiment and key phrases from tickets
C#
5
star
67

realm-swift-samples

Sample applications for realm-swift database
Swift
5
star
68

BarCodes-Demo

A small demo app to show how to scan barcode and place into into a Realm
Swift
5
star
69

realm-qna

question and answer app
Swift
4
star
70

realm-SharedTasks

Swift
3
star
71

realm-dotnet-samples

C#
3
star
72

unity-examples-3d-chess

Examples and tutorials for the Realm Unity SDK.
3
star
73

xamarin-examples-architecture

Test
C#
3
star
74

realm-flexible-sync-test-api

Sample App for A/B testing two version of the Flexible Sync API
Swift
3
star
75

realm-tools

Various tools for testing Realm
TypeScript
2
star
76

ci-actions

A repository for commonly used GitHub Actions inside the various realm repositories
TypeScript
2
star
77

global-notifier-design-patterns

C++
2
star
78

realm.github.io

HTML
2
star
79

realm-object-server-cognito-auth

Cognito authentication provider for Realm Object Server
TypeScript
2
star
80

aws-devicefarm-sample-data

Sample data to use with the AWS devicefarm action
Java
1
star
81

realm-lua-bootcamp

Teaching the basics of building a Realm SDK
C++
1
star
82

realm-crowdcircle

A collaborative experimental game, played between 6 teams
Swift
1
star
83

realm-js-playground

JavaScript
1
star