LoginSignup
15
18

More than 5 years have passed since last update.

NodeでRealmに触れてみる

Last updated at Posted at 2016-11-24

Nodeにおける初のオブジェクトデータベースRealm Node.jsがリリースされました。

Realm自体は以前からありましたが、Swift、Objective-C、Java、Xamarin,React Nativeとモバイルアプリ向けだけでした。

NodeとPHPしかやってこなかった僕はこれまで触れたことはなかったですが、せっかくなので触れてみようかと思います。

ドキュメントも英語版ですがあります。(バージョンが少しふるいですが、日本語版ドキュメントもありました。)

今回はデータの登録と取得といった、動作確認程度の物を書きました。

今回のサンプルはこちらにあります。

環境

今回、ターミナルからデータの登録と取得をするためにcommanderを入れてます。

module version
node 6.1.0
commander 2.9.0
realm 0.15.0

realmのインストール

npm
npm install realm --save

yarnでインストールする場合は下記になります。

yarn
yarn add realm

ソース

user.js
'use strict';                                                                   

const Realm = require('realm');

function User() {
  if (!(this instanceof User)) return new User();
  this.schema = {
    name: 'Users',
    primaryKey: 'uuid',
    properties: {
      uuid: 'string',
      username: 'string',
      age: 'int',
      created_at: 'date'
    }
  };

  this.realm = new Realm({
    path: 'realm.db',
    schema: [this.schema]
  });
}

// データ登録
User.prototype.add = function(input) {
  this.realm.write(() => {
    this.realm.create(this.schema.name, input);
  });
}

// データ取得
User.prototype.get = function() {
  return this.realm.objects(this.schema.name);
}

module.exports = User; 

解説

スキーマの宣言

  this.schema = {
    name: 'Users',
    primaryKey: 'uuid',
    properties: {
      uuid: 'string',
      username: 'string',
      age: 'int',
      created_at: 'date'
    }
  };
  • name: テーブル名?にあたるものを入れます。
  • primaryKey: 主キーです。ただ、realmにはオートインクリメントの機能はないようですので、オートインクリメントなIDにしたい場合は自分で実装する必要があります。
  • properties: カラムの定義です。キーにカラム名、バリューに型を定義します。今回使用している型は、string, int, dateだけですが、他にも、listやdata,objectなどもあるようです。

DB生成

this.realm = new Realm({
  path: 'db/realm',
  schema: [this.schema]
});
  • pathにはデータの実体の保存先を指定するようです。指定しない場合はデフォルトで同じ階層につくられるようです。 今回は「db/realm」と指定したので、dbディレクトリ配下に以下のファイルが生成されてました。
-rw-r--r-- 1 user user 4096 11月 24 18:27 realm
-rw-r--r-- 1 user user 1664 11月 24 18:27 realm.lock
drwxr-xr-x 2 user user 4096 11月 24 18:27 realm.management //ディレクトリ
prw------- 1 user user    0 11月 24 18:43 realm.note // 名前付きパイプ

データの登録

  • 登録・変更・削除はトランザクション内(write)で行います。
  • createでオブジェクトを生成します。
  this.realm.write(() => {
    this.realm.create(this.schema.name, input);
  });

データの取得

  this.realm.objects(this.schema.name);
  • 単純に取得するだけならobjects関数で出来るようです。

動作確認

上記に載せたサンプルを落としてきます。

セットアップ

npm install

データの登録

node main.js -a "username: root, age: 21"

データの取得

node main.js -g
// => 
datas: Results {
  '0': 
   RealmObject {
     uuid: 'uuid_1479981347167',
     username: 'root',
     age: 21,
     created_at: 2016-11-24T09:55:47.167Z } }

ちゃんと登録されていました!

ちなみに、

console.log(datas[0].uuid);

これで、1件目のuuidにアクセスできました。

まとめ

急いで、書いたので雑ですいません。
でも、Realmは以前から興味あったのでこれからもっと触れていきたいと思います!

15
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
18