Giter VIP home page Giter VIP logo

realm-cpp's Introduction

Realm

Realm is a mobile database that runs directly inside phones, tablets or wearables. This repository holds the source code for the C++ SDK Preview.

Why Use Realm

  • Intuitive to Developers: Realm’s object-oriented data model is simple to learn, doesn’t need an ORM, and lets you write less code.
  • Designed for Offline Use: Realm’s local database persists data on-disk, so apps work as well offline as they do online.
  • Built for Mobile: Realm is fully-featured, lightweight, and efficiently uses memory, disk space, and battery life.
  • Device Sync: Makes it simple to keep data in sync across users, devices, and your backend in real-time. Get started for free and create the cloud backend.

Object-Oriented: Streamline Your Code

Realm was built for mobile developers, with simplicity in mind. The idiomatic, object-oriented data model can save you thousands of lines of code.

#include <cpprealm/sdk.hpp>

using namespace realm::experimental;

// Define your models like regular structs.
struct Dog {
    std::string name;
    int64_t age;
};

REALM_SCHEMA(Dog, name, age)

struct Person {
    primary_key<std::string> _id;
    std::string name;
    int64_t age;
    // Create relationships by pointing an Object field to another Class
    Dog* dog;
};

REALM_SCHEMA(Person, _id, name, age, dog)

// Use them like regular objects.
auto dog = Dog { .name = "Rex", .age = 1 };
std::cout << "name of dog: " << dog.name << std::endl;
auto person = Person();
person._id = "something unique";
person.name = "John";
person.dog = &dog;

// Get the default Realm with automatic schema discovery.
realm::db_config config;
auto realm = db(std::move(config));
// Persist your data easily with a write transaction 
auto managed_person = realm.write([&realm, &person] {
    return realm.add(std::move(person));
});

// Query with type safety.
auto dogs = realm.objects<Dog>();
auto adult_dogs = dogs.where([](auto& d) {
    return d.age > 2;
});

Live Objects: Build Reactive Apps

Realm’s live objects mean data updated anywhere is automatically updated everywhere.

// Open the default realm.
realm::db_config config;
auto realm = db(std::move(config));

realm::notification_token token;

auto dog = Dog { .name = "Max" };

// Create a dog in the realm.
auto managed_dog = realm.write([&realm, &dog] {
    return realm.add(std::move(dog));
});

//  Set up the listener & observe object notifications.
token = managed_dog.observe([](auto&& change) {
    if (change.error) {
        std::cout<<"An error occurred: "<<error<<std::endl;
    } else if (change.is_deleted) {
        std::cout<<"The object was deleted."<<std::endl;
    } else {
        std::cout << "Property " << property.name << " changed to " << property.new_value << std::endl;
    }
}

// Update the dog's name to see the effect.
realm.write([&managed_dog] {
    managed_dog.name = "Wolfie"
});

Data Sync

The Atlas Device Sync makes it simple to keep data in sync across users, devices, and your backend in real-time.

auto app = realm::App("<app-id>");
auto user = app.login(realm::App::credentials::anonymous()).get();
auto realm = db(user.flexible_sync_configuration());

auto cars = realm.results<Car>();
realm.write([&cars](){
    for (auto& car : cars) {
        car.accelerate();
    }
});

Getting Started

See the detailed instructions in our docs.

The API reference is located here.

Installing Realm

MacOS / Linux / Windows

Prerequisites:

  • git, cmake, cxx17
git submodule update --init --recursive
mkdir build.debug
cd build.debug
cmake -D CMAKE_BUILD_TYPE=debug ..
sudo cmake --build . --target install  

You can then link to your library with -lcpprealm.

Note: If your target is Windows make sure to add the MSVC_RUNTIME_LIBRARY property to your target like so:

set_property(TARGET My_Target PROPERTY
      MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

realm-cpp's People

Contributors

cbush avatar dacharyc avatar fealebenpae avatar jsflax avatar leemaguire avatar otso avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.