📚

AloeDBを触ってみました

2022/01/21に公開

AloeDBとは

AloeDB
ざっくり言えばNoSQL

触る経緯

toranoana.deno #4において出てきたので触ってみた
toranoana.deno #4で出てきます

動かすまで

file.json

※空ファイル

index.ts

import { Database } from 'https://deno.land/x/aloedb/mod.ts';

// Structure of stored documents
interface Film {
    title: string;
    year: number;
    film: boolean;
    genres: string[];
    authors: { director: string };
}

// Initialization
const db = new Database<Film>('./file.json');

// Insert operations
await db.insertOne({
    title: 'Drive',
    year: 2012,
    film: true,
    genres: ['crime', 'drama', 'noir'],
    authors: { director: 'Nicolas Winding Refn' }
});

// Search operations
const found: Film = await db.findOne({ title: 'Drive', film: true });

// Update operations
await db.updateOne({ title: 'Drive' }, { year: 2011 });

// Delete operations
await db.deleteOne({ title: 'Drive' });

実行

PS E:\DenoProject\AloedbSample> deno run index.ts
Check file:///E:/DenoProject/AloedbSample/index.ts
error: TS2322 [ERROR]: Type 'Film | null' is not assignable to type 'Film'.
  Type 'null' is not assignable to type 'Film'.
const found: Film = await db.findOne({ title: 'Drive', film: true });
      ~~~~~
    at file:///E:/DenoProject/AloedbSample/index.ts:25:7

エラー出た…
ぐぐったらnull割当もしろっていう感じだったので

const found: Film | null = await db.findOne({ title: 'Drive', film: true });

で再度実行

PS E:\DenoProject\AloedbSample> deno run index.ts
Check file:///E:/DenoProject/AloedbSample/index.ts
error: Uncaught PermissionDenied: Requires read access to "./file.json", run again with the --allow-read flag
                Deno.lstatSync(path);
                     ^
    at Object.opSync (deno:core/01_core.js:142:12)
    at Object.lstatSync (deno:runtime/js/30_fs.js:220:22)
    at existsSync (https://deno.land/x/aloedb@0.9.0/lib/reader.ts:67:8)
    at Function.readSync (https://deno.land/x/aloedb@0.9.0/lib/reader.ts:35:7)
    at Database.loadSync (https://deno.land/x/aloedb@0.9.0/lib/database.ts:222:32)
    at new Database (https://deno.land/x/aloedb@0.9.0/lib/database.ts:30:22)
    at file:///E:/DenoProject/AloedbSample/index.ts:13:12

これは読み取り権限がないためなので&下記処理で書き込み権限も必要なので
deno run --allow-read --allow-write index.ts
で実行で

PS E:\DenoProject\AloedbSample> deno run --allow-read --allow-write index.ts
PS E:\DenoProject\AloedbSample> 

問題なく終了
file.json

[]

となる。

※サンプルプログラム結局データ作って消してるので空配列になるので、await db.deleteOne({ title: 'Drive' });をコメントアウトして実行すると

file.json

[
	{
		"title": "Drive",
		"year": 2011,
		"film": true,
		"genres": [
			"crime",
			"drama",
			"noir"
		],
		"authors": {
			"director": "Nicolas Winding Refn"
		}
	}
]

となる。

メモリ上だけで動作させる

aloedbのページを見ていくと

// In-memory database
const db = new Database();

って書いてる通り、この指定をすればメモリ上だけの動作になる。

import { Database } from 'https://deno.land/x/aloedb/mod.ts';

// Structure of stored documents
interface Film {
    title: string;
    year: number;
    film: boolean;
    genres: string[];
    authors: { director: string };
}

// Initialization
// const db = new Database<Film>('./file.json');
const db = new Database<Film>();

// Insert operations
await db.insertOne({
    title: 'Drive',
    year: 2012,
    film: true,
    genres: ['crime', 'drama', 'noir'],
    authors: { director: 'Nicolas Winding Refn' }
});

// Search operations
const found: Film | null = await db.findOne({ title: 'Drive', film: true });
console.log(found);

// Update operations
await db.updateOne({ title: 'Drive' }, { year: 2011 });

const found2: Film | null = await db.findOne({ title: 'Drive', film: true });
console.log(found2);

// Delete operations
await db.deleteOne({ title: 'Drive' });

結果

PS E:\DenoProject\AloedbSample> deno run index.ts
Check file:///E:/DenoProject/AloedbSample/index.ts
{
  title: "Drive",
  year: 2012,
  film: true,
  genres: [ "crime", "drama", "noir" ],
  authors: { director: "Nicolas Winding Refn" }
}
{
  title: "Drive",
  year: 2011,
  film: true,
  genres: [ "crime", "drama", "noir" ],
  authors: { director: "Nicolas Winding Refn" }
}

※当然ながらファイル読み込みやら書き込みはないので--allow-read --allow-writeは不要

Discussion