BT

Your opinion matters! Please fill in the InfoQ Readers’ Survey!

Realm Releases Object Database for Node.js

| by James Chesters Follow 2 Followers on Nov 22, 2016. Estimated reading time: 3 minutes |

Realm has launched an object database for Node.js, allowing mobile developers to create and send pre-populated Realms to clients.

Announcing the release of the open source database, the Realm technical team said that with Realm Node.js, developers "simply work with objects, as you’re used to, except that these objects persist to disk easily and performantly in a Realm. You don’t have to serialise them into JSON, and you don’t have to send them through an ORM to store them in tables."

Realm, which was launched two years ago, intends to provide an alternative to on-device technologies such as SQLite or Core Data, and followed its initial iOS database with an equivalent for Android, along with a database for React Native. Realm Node.js is the first object database available for Node.js.

The motivation for a version of Realm that worked on the server followed the release of the Realm Mobile Platform, when the team say they started receiving requests for a Node interface for the platform.

Realm Node.js uses Multiversion Concurrency Control to "provide concurrent access to the database across threads and processes," meaning that both readers and writers have a consistent view of the database. To coordinate this, Realm uses a notification system to update accessors when a write transaction completes. Developers can use Realm’s notification APIs with its notification system to update accessors when write transactions are complete.

The following example is provided, using the Express web framework to create HTTP endpoints, and Winston to log information about the requests:

var express = require('express'),
    util = require('util'),
    winston = require('winston');
    RealmWinston = require('./winston-realm').Realm;

var app = express();

// Use custom Winston transport: RealmWinston
// Writes log data to winston.realm
winston.add(RealmWinston, {});

app.get('/', function (req, res) {
  res.send('Hello World!');
  winston.info('Handled Hello World');
});

app.use(function (req, res, next) {
  res.status(404).send('Sorry can not find that!');
  winston.error('404 Error at: ' + req.url);
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

The example continues with saving log data in Realm, and starting another Node process, registering a Realm listener to react to changes:

'use strict';

var Realm = require('realm');

let winstonRealm = new Realm({
  path: 'winston.realm'
});

// Register listener to print out log messages at error level
winstonRealm.objects('Log').filtered('level = "error"').addListener((logs, changes) => {
  changes.insertions.forEach((index) => {
    let log = logs[index];
    console.log(log.message);
  })
});

Realm's team explains "The listener uses Realm’s support for collection notifications, which passes the specific changes as indexes corresponding to the inserted, deleted, or modified objects. The example adds a listener to a query of all logs at the error level and then prints the log message to the console."

In the JavaScript community, some users had questions about the object database for Node.js. As part of the Hacker News discussion, one user asked "Does this work with the new syncing stuff?"

Adam Fish, Realm's director of product, replied "This version does not include sync functionality, it is just for use with Realm locally. We wanted to expose it to the Node.js community as we think it has value on its own, such as interprocess communication."

Fish added that more updates around sync would be coming "very soon."

Asked whether Realm Node.js was a pure JavaScript implementation of realm, or a JavaScript wrapper around a realm process, Fish explained it was still using Realm Core, but exposed via JavaScript API.

Finally, asked "What's the performance like? Hard to believe this could compete with SQL" Fish replied that no formal benchmarks had been done against other SQL databases used server-side, and that Realm was more often compared against SQLite. Benchmarks for the Android database are available here.

Realm Node.js is available on NPM as a free and fully open source repo.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss
BT