@airbee-project/kasane-client で、TypeScript から Kasane を操作します。
インストール
Section titled “インストール”npm install https://github.com/AirBee-Project/spatial-id-ts/releases/download/v0.0.3/airbee-project-kasane-client-0.0.3.tgzyarn add https://github.com/AirBee-Project/spatial-id-ts/releases/download/v0.0.3/airbee-project-kasane-client-0.0.3.tgzpnpm add https://github.com/AirBee-Project/spatial-id-ts/releases/download/v0.0.3/airbee-project-kasane-client-0.0.3.tgzbun add https://github.com/AirBee-Project/spatial-id-ts/releases/download/v0.0.3/airbee-project-kasane-client-0.0.3.tgzNode.js 18 以上・Bun・ブラウザで動きます。TypeScript では、moduleResolution を "Bundler" にしてください。
{ "compilerOptions": { "module": "ESNext", "moduleResolution": "Bundler", "target": "ES2022", "strict": true }}クイックスタート
Section titled “クイックスタート”接続してデータベースを選びます。
import { KasaneClient } from "@airbee-project/kasane-client";
const client = await KasaneClient.connect("https://サーバーのURL", "ユーザー名", "パスワード");const db = client.database("データベース名");ログイン状態はクライアントが維持するので、再ログインの処理は要りません。URL を配列で渡すと、サーバーが [unavailable] を返したときに次のサーバーへ切り替えます。
const client = await KasaneClient.connect(["https://kasane-1.example.com", "https://kasane-2.example.com"], "ユーザー名", "パスワード");テーブル hello を作り、東京駅付近の空間IDに 42 を書き込んで検索します。
import { SingleId } from "@airbee-project/kasane-client";
export default async function ({ db }) { // テーブルを作る(値の型は整数、最大ズームレベル 25) await db.createTable("hello", "int", 25); const table = db.table("hello");
// 東京駅付近の空間IDに、値 42 を書き込む const tokyo = SingleId.parse("20/0/931389/412900"); await table.insert(42, tokyo);
// 同じ空間IDを検索する const results = await table.search(tokyo).toArray(); for (const r of results) { console.log(`${r.id} => ${r.value}`); }}