Realm为Node.js发布对象数据库

来源:互联网 发布:qq分组软件下载 编辑:程序博客网 时间:2024/05/29 04:47

Realm为Node.js启动了一个对象数据库项目,使用这款数据库,移动开发者可以向客户端发送预处理的Realm对象。

在宣布开源这款数据库时,Realm的技术团队说,使用Realm Node.js“可以像以前那样跟对象打交道,不同之处在于,在Realm里这些对象可以很容易地被持久化到磁盘上。你不需要把它们序列化成JSON,也不需要通过ORM框架来把它们保存到表里面”。

Realm项目在两年前启动,目标是作为SQLite或Core Data的替代方案,可以在iOS和Android平台上使用,同时支持React Native。Realm Node.js是第一个Node.js对象数据库。

之所以要为Realm开发一个服务器版本,是因为在Realm移动平台发布后,Realm技术团队收到了很多关于为该平台提供Node接口的呼声。

Realm Node.js使用多版本并发控制“为多线程和多进程环境提供并发的数据库访问”,也就是说,读进程和写进程看到的数据库视图是一致的。为了做到这点,在数据写入方完成事务时,Realm使用通知系统来通知数据读取方。开发者可以使用Realm的通知API和它的通知系统在写事务完成时通知读线程。

以下代码使用Express框架创建HTTP端点,并用Winston记录请求日志:

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.realmwinston.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!');});

接下来,日志会被保存到Realm,然后启动另一个Node进程,注册一个Realm监听器:

'use strict';var Realm = require('realm');let winstonRealm = new Realm({  path: 'winston.realm'});// Register listener to print out log messages at error levelwinstonRealm.objects('Log').filtered('level = "error"').addListener((logs, changes) => {  changes.insertions.forEach((index) => {    let log = logs[index];    console.log(log.message);  })});

Realm的技术团队解释说,“监听器依赖Realm的支持来收集通知,这些通知包含了插入、删除或修改对象的相关信息。例子里的监听器监听一个针对所有错误级别日志的查询,然后把这些日志消息在控制台上打印出来”。

在JavaScript社区,有些人对这款数据库提出质疑。Hacker News上有个用户问了一个问题:”这款数据库是否具有最新的同步功能?”Realm的产品总监Adam Fish回答说:“这个版本并不包含同步功能,不过它可以跟本地的Realm结合起来使用。我们之所以把它贡献给Node.js社区,是因为我们认为它能够提供特殊的价值,比如进程间通信”。

Fish补充说,“很快”会有跟同步功能相关的更新放出。

当被问及Realm Node.js是Realm的纯JavaScript实现还是对Realm进程的JavaScript包装时,Fish解释说,它仍然使用Realm Core,并通过JavaScript API暴露接口。

最后,有人问:“它的性能怎么样?很难相信它可以跟SQL媲美。”Fish回答说,目前还没有正式地拿它跟其它运行在服务器端的SQL数据库进行过对比,不过Realm跟SQLite可以一比高下。针对Android的数据库测试基准可以在这里看到。

Realm Node.js可以从NPM获取,它完全开源,可以自由使用。

英文原文:

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.realmwinston.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 levelwinstonRealm.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 Realms 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.
0 0
原创粉丝点击