1
0

More than 3 years have passed since last update.

node.jsでneo4jのOGMを使う①とりあえず動かしてみる

Posted at

node.jsでOGM(オブジェクトグラフマッピング)を使うと簡単にデータにアクセスできます。

環境変数の設定

①dotenvをインストールします。

npm i --save dotenv

②.envファイルをプロジェクトのディレクトリに作成

/.env
// .env
NEO4J_PROTOCOL=neo4j
NEO4J_HOST=localhost
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=neo4j
NEO4J_PORT=7687
NEO4J_DATABASE=neo4j
NEO4J_ENCRYPTION=ENCRYPTION_OFF

neodeのインストール

npm install --save neode

modesディレクトリを作成してモデルを定義

/model/user.js
module.exports = {
  id: {
    type: 'uuid',
    primary: true
  },
  name: 'string'
}
index.js
var neode = require('neode');
const instance = new Neode.fromEnv();//①.evnの設定でnoe4jデータベースに接続
instance.withDirectory(__dirname+'/../model');//②modelディレクトリ内のモデル定義を読み込み
instance.model("user").all() //③userをすべて読み込み
.then(collection => {//④userの読み込みに成功した場合の処理
            console.log(collection.length); // 1
            console.log(collection.get(0).get('name')); // 'Adam'
})

neo4jデータベースにテストデータ挿入
create (:user{uuid:"100",name:"test太郎"})

1
0
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
1
0