Quick Start with Sync - Node.js SDK
On this page
Overview
This page contains information to quickly get Realm Database integrated into your app. Before you begin, ensure you have:
Import Realm
At the top of your source files where you want to use Realm, add the following line to import the SDK.
Initialize the App
To use Atlas App Services features such as authentication and sync, you must access your App using your App ID. You can find your App ID in the Atlas App Services UI.
Define Your Object Model
If you have not enabled Atlas Device Sync or you enabled Sync with Development Mode, you can define your object model directly in code.
If you have enabled Sync but turned off Development Mode, you can copy and paste the object model definitions that App Services generated for you from the SDKs tab in the Atlas App Services UI. You must re-enable Development Mode if you want to make changes to the object model definition from client side code.
Authenticate a User
To authenticate and log in a user, call App.logIn(). When anonymous authentication is enabled, users can immediately log into your app without providing any identifying information:
Open a Realm
Once you have enabled Atlas Device Sync and authenticated a user, you can open a synced realm by calling Realm.open():
Create, Read, Update, and Delete Objects
Once you have opened a realm, you can modify it and its objects in a write transaction block.
Create New Objects
To create a new object, start a new transaction by calling Realm.write() and then call Realm.create() in the transaction callback function:
Get a Collection of Objects
You can retrieve a live collection that contains all objects of a given type in the realm:
Filter & Query Collections
You can filter the objects in a collection by providing a query:
Modify an Object
To modify an object, update its properties in a Realm.write() transaction block:
Delete an Object
To delete an object, pass it to Realm.delete() in a Realm.write() transaction block:
Watch for Changes
You can watch a realm, collection, or object for changes by registering event handlers with the Realm.addListener() Object.addListener() Collection.addListener() methods.
To remove all registered realm, object, and collection listeners associated with a realm, call Realm.removeAllListeners().
To remove a specific listener, call Object.removeListener() on the object that is being watched with the listener function as the argument.
Log Out
You can log out an authenticated user by calling User.logOut():
Complete Example
Code
The following code is a fully runnable example that includes all of the concepts
on this page. Make sure to replace <Your App ID>
with your App ID.
Output
Running the above code should output something like the following:
Logged in anonymously with user id: 5f1f9fb75512f2cb670ad033 Connection[1]: Session[1]: client_reset_config = false, Realm exists = false, async open = false, client reset = false Connection[1]: Connected to endpoint '75.2.1.110:443' (from '192.168.1.154:60136') created two tasks: go grocery shopping & go exercise task { "_id": "5f1f9fb70a74d46af33c7a02", "_partition": null, "name": "go grocery shopping", "status": "Open" } inserted task: "go grocery shopping" inserted task: "go exercise" modified task: { "_id": "5f1f9fb70a74d46af33c7a02", "_partition": null, "name": "go grocery shopping", "status": "InProgress" } Connection[1]: Disconnected