Load Sample Data

Learn how to pre-populate data with our server SDKs

Many of our users have existing data sets that they'd like to import into the Realm Platform. One of the most common ways to do this is via our server-side SDKs. We'll show you how to build a basic import script using our Node.js SDK.

Looking for the quickest way to get started? Check out the code for the walkthrough here and follow the README

Prerequisites

To run through our loader example, you'll need to have a few things installed

  • Node v7.6.x or higher (we typically recommended the latest LTS)

  • A package manager like NPM

  • An IDE to work in (we typically recommend Visual Studio Code)

  • A cloud or self-hosted instance of the Realm Object Server

Install the required packages

To start, you'll want to download or copy our package.jsonwhich lists all of the required dependencies for this entire walkthrough.

After copying this to your working directory, simply perform an npm installfrom the command line of your working directory. You'll find that a number of these dependencies are not required, but we use them to generate some mock data. In practice, you should just need Realm.

Create a constants file with your server information

Since we'll be using our server information a few times during this walkthrough, we can store it in a constants file to save some time. Simply create a constants.js Then make sure to fill in your instance and user info. If you aren't sure of your URL, refer to the Realm Studio section:

constants.js
module.exports = {
username: "ADMIN_USERNAME",
password: "PASSWORD",
serverUrl: "INSTANCE_URL"
};

Declare your required libraries and server information

Create a new javascript file within your working directory. Within this file, we'll start by declaring a number of variables and libraries that we'll use in our script.

loader.js
const faker = require('faker')
const Realm = require('realm')
const fs = require('fs')
const uuidv1 = require('uuid/v1');
var randomWords = require('random-words');
const constants = require('./constants')
//change this to generate more or less items
var totalitems = 10
//provide a name to your specific Task List if desired
const myProjectName = 'Task List'

Define your Schemas

When opening a Realm, you'll need to pass in the schema configuration which you'll be working with. The application data that we're working with today is for a simple to-do app. In our app, we'll have projects that contain a list of individual items. You can find the syntax for model declarations within the database docs. Here are the details for javascript.

const Item = {
name: 'Item',
primaryKey: 'itemId',
properties: {
itemId: 'string',
body: 'string',
isDone: 'bool',
timestamp: 'date'
}
}
const Project = {
name: 'Project',
primaryKey: 'projectId',
properties: {
projectId: 'string',
owner: 'string',
name: 'string',
timestamp: 'date',
items: 'Item[]'
}
}

Login to Realm Platform and Open a Realm

Next we'll authenticate against our instance of the Realm Object Server and open up the default Realm. We'll also add a basic error handler to catch errors in case of a failure. You can find more details on authentication here (in case you'd like to use an alternate provider). You can find some of the specifics of opening a realm here.

const errorCallback = function errorCallback(message, isFatal, category, code) {
console.log(`Message: ${message} - isFatal: ${isFatal} - category: ${category} - code: ${code}`)
}
Realm.Sync.User.login(`${URL}`, username, password)
.then((user) => {
let config = user.createConfiguration();
config.schema = [Item, Project];
Realm.open(config)
.then((realm) => {
let itemResults = realm.objects('Item');
}
}

Write Sample Data to the Realm

Next we'll populate the Realm with some sample data. For the sake of brevity, we're just seeing random string data, but our goal is to show you the process. It is up to you to adapt this to your own meaningful dataset.

Realm.Sync.User.login(`${URL}`, username, password)
.then((user) => {
let config = user.createConfiguration();
config.schema = [Item, Project];
Realm.open(config)
.then((realm) => {
let itemResults = realm.objects('Item');
if (itemResults.length < totalitems) {
//write to the realm
realm.write(() => {
//create a project
var project = realm.create('Project', {
projectId: uuidv1(),
owner: user.identity,
name: myProjectName,
timestamp: new Date(),
items: []
}, true)
//create the tasks
for (let index = 0; index < totalitems; index++) {
var newItem = realm.create('Item', {
itemId: uuidv1(),
body: randomWords(),
isDone: false,
timestamp: new Date()
}, true)
project.items.push(newItem)
}
})
}
//good practice when writing data from a server script
realm.close()
})
});

Run your loader and view the results via Realm Studio

Now that we've completed our basic script, let's run it via the terminal:

node src/loader

Once it has completed, we can view the resulting data from Realm Studio. You'll find it within the /default Realm.

Learn how to sync this data with your Mobile App

To demonstrate Realm Platform's synchronization capabilities, we will display the data we just loaded into our instance via a mobile app. If you decided to skip the previous section, it is not a problem as you can simply create new data from the mobile side.

You're welcome to follow all the steps of these tutorials. However, feel free to skip the app building tutorials if you want to start viewing the data which we just pre-populated. You should see the data we just loaded as soon as you authenticate.

Getting Help

Having an issue with Realm Platform that you don't feel you can solve from the documentation? There are a number of avenues for you: