📌

ArangoJSでCRUDおよび集計

2021/09/24に公開約9,500字

https://zenn.dev/rururu3/articles/1b7f1fa56756b8
をArangoJSでやってみました。
※無駄にトランザクション処理もしてる

ArangoJS

https://github.com/arangodb/arangojs

ソース

// TS: import { Database, aql } from "arangojs";
const { Database, aql } = require("arangojs");

// ArangoDBに接続
const db = new Database({
  url: "http://localhost:8529",
  databaseName: "_system",
  auth: { username: "ログインID", password: "ログインパスワード" },
});

// メイン関数
async function main() {
  try {
    // testコレクション取得
    const collection = db.collection("test");
    await collection.truncate();

    // create
    console.log(`
============================================================
create
============================================================
`);
    {
      // トランザクション処理
      const trx = await db.beginTransaction(collection);
      // データ追加
      for(let i = 1; i <= 100; i++) {
        await trx.step(async () => {
          collection.save({id: i, value: i * 10});
        });
      }
      const result = await trx.commit();
      console.log(result);
    }

    // read
    console.log(`
============================================================
read
============================================================
`);
    {
      const cursor = await db.query(aql`
        // testコレクションのデータをすべて回す
        FOR t IN test
          // 10で割り切れるデータだけにする
          FILTER t.id % 10 == 0
          // t.idを降順でソート
          SORT t.id DESC
          // tを返す
          RETURN t
      `);
      for await (const value of cursor) {
        console.log(JSON.stringify(value));
      }
    }

    // update
    console.log(`
============================================================
update
============================================================
`);
    {
      let cursor = await db.query(aql`
        // testコレクションのデータをすべて回す
        FOR t IN test
          FILTER
            t.id == 100
          // UPDATE文
          UPDATE t WITH { value: t.value + 1 } IN test
      `);
      for await (const value of cursor) {
        console.log(JSON.stringify(value));
      }

       cursor = await db.query(aql`
        // testコレクションのデータをすべて回す
        FOR t IN test
          FILTER
            t.id == 100
          RETURN t
      `);
      for await (const value of cursor) {
        console.log(JSON.stringify(value));
      }
    }

    // delete
    console.log(`
============================================================
delete
============================================================
`);
    {
      let cursor = await db.query(aql`
        // testコレクションのデータをすべて回す
        FOR t IN test
          FILTER
            t.id == 100
          // REMOVE文
          REMOVE t IN test
      `);
      for await (const value of cursor) {
        console.log(JSON.stringify(value));
      }

       cursor = await db.query(aql`
        // testコレクションのデータをすべて回す
        FOR t IN test
          FILTER
            t.id == 100
          RETURN t
      `);
      for await (const value of cursor) {
        console.log(JSON.stringify(value));
      }
    }

    // group
    console.log(`
============================================================
group
============================================================
`);
    {
      const cursor = await db.query(aql`
        // testコレクションのデータをすべて回す
        FOR t IN test
          FILTER
            // 50未満のデータを対象にする
            t.id < 50
          // 集計(FLOOR(t.id / 10)の結果が同じものでグループ化&結果はfに代入、sにグループ化されたデータの合計を代入、gに対象となったデータを入れる
          COLLECT f = FLOOR(t.id / 10) AGGREGATE s = SUM(t.value) INTO g
          // データを返す
          RETURN {
            f: f,
            sum: s,
            g: g
          }
      `);
      for await (const value of cursor) {
        console.log(JSON.stringify(value));
      }
    }
  } catch (err) {
    console.error(err.message);
  }
}

main();

結果

> node index.js


============================================================
create
============================================================

{ id: '76983', status: 'committed' }

============================================================
read
============================================================

{"_key":"77083","_id":"test/77083","_rev":"_d_Wr162--A","id":100,"value":1000}
{"_key":"77073","_id":"test/77073","_rev":"_d_Wr16q--A","id":90,"value":900}
{"_key":"77063","_id":"test/77063","_rev":"_d_Wr16e---","id":80,"value":800}
{"_key":"77053","_id":"test/77053","_rev":"_d_Wr16O--A","id":70,"value":700}
{"_key":"77043","_id":"test/77043","_rev":"_d_Wr16C---","id":60,"value":600}
{"_key":"77033","_id":"test/77033","_rev":"_d_Wr15y--_","id":50,"value":500}
{"_key":"77023","_id":"test/77023","_rev":"_d_Wr15m--_","id":40,"value":400}
{"_key":"77013","_id":"test/77013","_rev":"_d_Wr15W--A","id":30,"value":300}
{"_key":"77002","_id":"test/77002","_rev":"_d_Wr15G--_","id":20,"value":200}
{"_key":"76992","_id":"test/76992","_rev":"_d_Wr142--_","id":10,"value":100}

============================================================
update
============================================================

{"_key":"77083","_id":"test/77083","_rev":"_d_Wr17e---","id":100,"value":1001}

============================================================
delete
============================================================


============================================================
group
============================================================

{"f":0,"sum":450,"g":[{"t":{"_key":"76984","_id":"test/76984","_rev":"_d_Wr14i---","id":1,"value":10}},{"t":{"_key":"76985","_id":"test/76985","_rev":"_d_Wr14q---","id":4,"value":40}},{"t":{"_key":"76986","_id":"test/76986","_rev":"_d_Wr14q--_","id":3,"value":30}},{"t":{"_key":"76987","_id":"test/76987","_rev":"_d_Wr14u---","id":5,"value":50}},{"t":{"_key":"76988","_id":"test/76988","_rev":"_d_Wr14u--_","id":6,"value":60}},{"t":{"_key":"76989","_id":"test/76989","_rev":"_d_Wr14y---","id":7,"value":70}},{"t":{"_key":"76990","_id":"test/76990","_rev":"_d_Wr14y--_","id":8,"value":80}},{"t":{"_key":"76991","_id":"test/76991","_rev":"_d_Wr142---","id":9,"value":90}},{"t":{"_key":"77009","_id":"test/77009","_rev":"_d_Wr15S--_","id":2,"value":20}}]}
{"f":1,"sum":1450,"g":[{"t":{"_key":"76992","_id":"test/76992","_rev":"_d_Wr142--_","id":10,"value":100}},{"t":{"_key":"76993","_id":"test/76993","_rev":"_d_Wr142--A","id":11,"value":110}},{"t":{"_key":"76994","_id":"test/76994","_rev":"_d_Wr146---","id":12,"value":120}},{"t":{"_key":"76995","_id":"test/76995","_rev":"_d_Wr146--_","id":13,"value":130}},{"t":{"_key":"76996","_id":"test/76996","_rev":"_d_Wr15----","id":14,"value":140}},{"t":{"_key":"76997","_id":"test/76997","_rev":"_d_Wr15---_","id":15,"value":150}},{"t":{"_key":"76998","_id":"test/76998","_rev":"_d_Wr15---A","id":16,"value":160}},{"t":{"_key":"76999","_id":"test/76999","_rev":"_d_Wr15C---","id":17,"value":170}},{"t":{"_key":"77000","_id":"test/77000","_rev":"_d_Wr15C--_","id":18,"value":180}},{"t":{"_key":"77001","_id":"test/77001","_rev":"_d_Wr15G---","id":19,"value":190}}]}
{"f":2,"sum":2450,"g":[{"t":{"_key":"77002","_id":"test/77002","_rev":"_d_Wr15G--_","id":20,"value":200}},{"t":{"_key":"77003","_id":"test/77003","_rev":"_d_Wr15K---","id":21,"value":210}},{"t":{"_key":"77004","_id":"test/77004","_rev":"_d_Wr15K--_","id":22,"value":220}},{"t":{"_key":"77005","_id":"test/77005","_rev":"_d_Wr15O---","id":23,"value":230}},{"t":{"_key":"77006","_id":"test/77006","_rev":"_d_Wr15O--_","id":24,"value":240}},{"t":{"_key":"77007","_id":"test/77007","_rev":"_d_Wr15O--A","id":25,"value":250}},{"t":{"_key":"77008","_id":"test/77008","_rev":"_d_Wr15S---","id":26,"value":260}},{"t":{"_key":"77010","_id":"test/77010","_rev":"_d_Wr15S--A","id":27,"value":270}},{"t":{"_key":"77011","_id":"test/77011","_rev":"_d_Wr15W---","id":28,"value":280}},{"t":{"_key":"77012","_id":"test/77012","_rev":"_d_Wr15W--_","id":29,"value":290}}]}
{"f":3,"sum":3450,"g":[{"t":{"_key":"77013","_id":"test/77013","_rev":"_d_Wr15W--A","id":30,"value":300}},{"t":{"_key":"77014","_id":"test/77014","_rev":"_d_Wr15W--B","id":31,"value":310}},{"t":{"_key":"77015","_id":"test/77015","_rev":"_d_Wr15a---","id":32,"value":320}},{"t":{"_key":"77016","_id":"test/77016","_rev":"_d_Wr15a--_","id":33,"value":330}},{"t":{"_key":"77017","_id":"test/77017","_rev":"_d_Wr15e---","id":34,"value":340}},{"t":{"_key":"77018","_id":"test/77018","_rev":"_d_Wr15e--_","id":35,"value":350}},{"t":{"_key":"77019","_id":"test/77019","_rev":"_d_Wr15i---","id":36,"value":360}},{"t":{"_key":"77020","_id":"test/77020","_rev":"_d_Wr15i--_","id":37,"value":370}},{"t":{"_key":"77021","_id":"test/77021","_rev":"_d_Wr15i--A","id":38,"value":380}},{"t":{"_key":"77022","_id":"test/77022","_rev":"_d_Wr15m---","id":39,"value":390}}]}
{"f":4,"sum":4450,"g":[{"t":{"_key":"77023","_id":"test/77023","_rev":"_d_Wr15m--_","id":40,"value":400}},{"t":{"_key":"77024","_id":"test/77024","_rev":"_d_Wr15m--A","id":41,"value":410}},{"t":{"_key":"77025","_id":"test/77025","_rev":"_d_Wr15q---","id":42,"value":420}},{"t":{"_key":"77026","_id":"test/77026","_rev":"_d_Wr15q--_","id":43,"value":430}},{"t":{"_key":"77027","_id":"test/77027","_rev":"_d_Wr15q--A","id":44,"value":440}},{"t":{"_key":"77028","_id":"test/77028","_rev":"_d_Wr15u---","id":45,"value":450}},{"t":{"_key":"77029","_id":"test/77029","_rev":"_d_Wr15u--_","id":46,"value":460}},{"t":{"_key":"77030","_id":"test/77030","_rev":"_d_Wr15u--A","id":47,"value":470}},{"t":{"_key":"77031","_id":"test/77031","_rev":"_d_Wr15u--B","id":48,"value":480}},{"t":{"_key":"77032","_id":"test/77032","_rev":"_d_Wr15y---","id":49,"value":490}}]}

Discussion

ログインするとコメントできます