NodeJS를 사용하여 ArangoDB의 Collection에 액세스

50604 단어 arangodbtech

NodeJS로 ArangoDB의 Collection에 접근하기 전에


Collection 데이터 내용


https://www.arangodb.com/docs/stable/aql/examples.html
여기에 샘플 데이터가 있으니 내가 가서 가져올게

데이터 추가 방법


COLLECIONS에서 Add Collection 클릭
COLLECTIONSからAdd Collectionをクリック
샘플 데이터를 기준으로 Name을 users로 Save 클릭
サンプルデータに従ってNameをusersにしてSaveをクリック
클릭하여 전체 텍스트 보기
usersができてるのでクリック
오른쪽 위의 + 클릭
右上の方にある+をクリック
샘플 데이터
복제
[ 
  { "id": 100, "name": "John", "age": 37, "active": true, "gender": "m" },
  { "id": 101, "name": "Fred", "age": 36, "active": true, "gender": "m" },
  { "id": 102, "name": "Jacob", "age": 35, "active": false, "gender": "m" },
  { "id": 103, "name": "Ethan", "age": 34, "active": false, "gender": "m" },
  { "id": 104, "name": "Michael", "age": 33, "active": true, "gender": "m" },
  { "id": 105, "name": "Alexander", "age": 32, "active": true, "gender": "m" },
  { "id": 106, "name": "Daniel", "age": 31, "active": true, "gender": "m" },
  { "id": 107, "name": "Anthony", "age": 30, "active": true, "gender": "m" },
  { "id": 108, "name": "Jim", "age": 29, "active": true, "gender": "m" },
  { "id": 109, "name": "Diego", "age": 28, "active": true, "gender": "m" },
  { "id": 200, "name": "Sophia", "age": 37, "active": true, "gender": "f" },
  { "id": 201, "name": "Emma", "age": 36,  "active": true, "gender": "f" },
  { "id": 202, "name": "Olivia", "age": 35, "active": false, "gender": "f" },
  { "id": 203, "name": "Madison", "age": 34, "active": true, "gender": "f" },
  { "id": 204, "name": "Chloe", "age": 33, "active": true, "gender": "f" },
  { "id": 205, "name": "Eva", "age": 32, "active": false, "gender": "f" },
  { "id": 206, "name": "Abigail", "age": 31, "active": true, "gender": "f" },
  { "id": 207, "name": "Isabella", "age": 30, "active": true, "gender": "f" },
  { "id": 208, "name": "Mary", "age": 29, "active": true, "gender": "f" },
  { "id": 209, "name": "Mariah", "age": 28, "active": true, "gender": "f" }
]
Doocument bodyCreate 클릭
Document bodyコピペしてCreateをクリック
화면이 새로 고쳐지지 않았기 때문에 데이터를 적당히 다시 불러와서 추가했는지 확인해야 한다
画面がリフレッシュされないので適当にリロードなりしてデータ追加されてるのを確認する
똑같이
[
  { "from": 209, "to": 205, "type": "friend" },
  { "from": 206, "to": 108, "type": "friend" },
  { "from": 202, "to": 204, "type": "friend" },
  { "from": 200, "to": 100, "type": "friend" },
  { "from": 205, "to": 101, "type": "friend" },
  { "from": 209, "to": 203, "type": "friend" },
  { "from": 200, "to": 203, "type": "friend" },
  { "from": 100, "to": 208, "type": "friend" },
  { "from": 101, "to": 209, "type": "friend" },
  { "from": 206, "to": 102, "type": "friend" },
  { "from": 104, "to": 100, "type": "friend" },
  { "from": 104, "to": 108, "type": "friend" },
  { "from": 108, "to": 209, "type": "friend" },
  { "from": 206, "to": 106, "type": "friend" },
  { "from": 204, "to": 105, "type": "friend" },
  { "from": 208, "to": 207, "type": "friend" },
  { "from": 102, "to": 108, "type": "friend" },
  { "from": 207, "to": 203, "type": "friend" },
  { "from": 203, "to": 106, "type": "friend" },
  { "from": 202, "to": 108, "type": "friend" },
  { "from": 201, "to": 203, "type": "friend" },
  { "from": 105, "to": 100, "type": "friend" },
  { "from": 100, "to": 109, "type": "friend" },
  { "from": 207, "to": 109, "type": "friend" },
  { "from": 103, "to": 203, "type": "friend" },
  { "from": 208, "to": 104, "type": "friend" },
  { "from": 105, "to": 104, "type": "friend" },
  { "from": 103, "to": 208, "type": "friend" },
  { "from": 203, "to": 107, "type": "boyfriend" },
  { "from": 107, "to": 203, "type": "girlfriend" },
  { "from": 208, "to": 109, "type": "boyfriend" },
  { "from": 109, "to": 208, "type": "girlfriend" },
  { "from": 106, "to": 205, "type": "girlfriend" },
  { "from": 205, "to": 106, "type": "boyfriend" },
  { "from": 103, "to": 209, "type": "girlfriend" },
  { "from": 209, "to": 103, "type": "boyfriend" },
  { "from": 201, "to": 102, "type": "boyfriend" },
  { "from": 102, "to": 201, "type": "girlfriend" },
  { "from": 206, "to": 100, "type": "boyfriend" },
  { "from": 100, "to": 206, "type": "girlfriend" }
]
relations로 소장 등록 & 데이터 추가
결국 users와 Relations 같은 컬렉션 이름이 있으면 OK.
最終的にusersとrelationsというコレクション名があればOK

노드 프로그램


사용할 라이브러리


https://github.com/arangodb/arangojs

Arangojos 라이브러리 설치


npm install --save arangojs
## - or -
yarn add arangojs
// TS: import { Database, aql } from "arangojs";
const { Database, aql } = require("arangojs");

const db = new Database({
  url: "ArangoDBのURL",
  databaseName: "_system",
  auth: { username: "ユーザーID", password: "パスワード" },
});
const Users = db.collection("users");

async function main() {
  try {
    const users = await db.query(aql`
      FOR user IN ${Users}
        FILTER user.name == "Alexander"
        RETURN user
    `);
    for await (const user of users) {
      console.log(user);
    }
  } catch (err) {
    console.error(err.message);
  }
}

main();
ArangoDBのURLhttp://ArangoDBが動作してるIPアドレス:8529databaseName_system이고 이미지 오른쪽 상단은 DB:SYSTEM이라고 적힌 곳에서 판단할 수 있다(URL에도 기재되어 있기 때문에_db/_system판단할 수 있다)
_db/_system

실행 결과


{
  _key: '42214',
  _id: 'users/42214',
  _rev: '_c9FPyEa--D',
  id: 105,
  name: 'Alexander',
  age: 32,
  active: true,
  gender: 'm'
}
_key, _id 또는 _rev는 ArrangoDB에서 사용되며 환경에 따라 다름

좋은 웹페이지 즐겨찾기