Knex.js
PetraDBは@petradb/knexパッケージを通じてKnex.jsダイアレクトを提供します。これによりKnexのクエリビルダー、スキーマビルダー、マイグレーションをPetraDBの組み込みSQLエンジンで使用できます。
インストール
Section titled “インストール”npm install @petradb/knex knexセットアップ
Section titled “セットアップ”import Knex from "knex";import PetraDBClient from "@petradb/knex";
const knex = Knex({ client: PetraDBClient, connection: { storage: "memory" }, useNullAsDefault: true,});ストレージモード
Section titled “ストレージモード”// インメモリ(デフォルト){ storage: "memory" }
// ファイルベースの永続ストレージ{ storage: "persistent", path: "./mydb.petra" }スキーマビルダー
Section titled “スキーマビルダー”// テーブル作成await knex.schema.createTable("users", (t) => { t.increments("id"); t.string("name").notNullable(); t.integer("age"); t.boolean("active").defaultTo(true); t.timestamps(true, true);});
// テーブルの存在確認const exists = await knex.schema.hasTable("users");
// カラムの存在確認const hasAge = await knex.schema.hasColumn("users", "age");
// カラム追加await knex.schema.alterTable("users", (t) => { t.string("email");});
// インデックス作成await knex.schema.alterTable("users", (t) => { t.index(["name"]);});
// テーブル削除await knex.schema.dropTableIfExists("users");// Insertawait knex("users").insert({ name: "Alice", age: 30 });await knex("users").insert([ { name: "Bob", age: 25 }, { name: "Carol", age: 35 },]);
// returning付きInsertconst [inserted] = await knex("users") .insert({ name: "Dave", age: 28 }) .returning("*");
// Selectconst users = await knex("users").where("age", ">", 25);const first = await knex("users").where("name", "Alice").first();
// Updateconst updated = await knex("users") .where("name", "Alice") .update({ age: 31 });
// returning付きUpdateconst [changed] = await knex("users") .where("name", "Alice") .update({ age: 32 }) .returning("*");
// Deleteconst deleted = await knex("users").where("active", false).del();
// 集計const [{ count }] = await knex("users").count("* as count");const [{ max }] = await knex("users").max("age as max");await knex.schema.createTable("orders", (t) => { t.increments("id"); t.integer("user_id").references("id").inTable("users"); t.string("product"); t.decimal("amount", 10, 2);});
const results = await knex("orders") .join("users", "orders.user_id", "users.id") .select("users.name", "orders.product", "orders.amount");トランザクション
Section titled “トランザクション”await knex.transaction(async (trx) => { await trx("users").insert({ name: "Eve", age: 22 }); await trx("users").where("name", "Bob").update({ age: 26 });});サポートされるカラム型
Section titled “サポートされるカラム型”| Knexメソッド | PetraDB型 |
|---|---|
increments() | SERIAL PRIMARY KEY |
bigIncrements() | BIGSERIAL PRIMARY KEY |
integer() | INTEGER |
bigint() | BIGINT |
smallint() | SMALLINT |
tinyint() | SMALLINT |
float() | DOUBLE |
double() | DOUBLE |
decimal(p, s) | NUMERIC(p, s) |
string(n) / varchar(n) | VARCHAR(n) |
text() | TEXT |
boolean() | BOOLEAN |
date() | DATE |
timestamp() | TIMESTAMP |
uuid() | UUID |
json() | JSON |
jsonb() | JSONB |
binary() | BYTEA |
enum() | TEXT CHECK (...)またはネイティブENUM |
クリーンアップ
Section titled “クリーンアップ”await knex.destroy();