Lucid ORM
PetraDBは@petradb/lucidパッケージを通じてAdonisJS Lucidドライバーを提供します。これによりLucidのORM、マイグレーション、シーダー、クエリビルダーをPetraDBの組み込みSQLエンジンで使用できます。
インストール
Section titled “インストール”npm install @petradb/lucid @petradb/knex knexセットアップ
Section titled “セットアップ”Lucid接続を作成する前に@petradb/lucidをインポートしてください。このインポートによりLucidの内部を修正し、petradbを有効なデータベースクライアントとして受け入れるようにします。
AdonisJSアプリ
Section titled “AdonisJSアプリ”AdonisJSプロジェクトでは、データベース設定の先頭にサイドエフェクトインポートを追加します。
import '@petradb/lucid'import { defineConfig } from '@adonisjs/lucid'
export default defineConfig({ connection: 'petradb', connections: { petradb: { client: 'petradb', connection: { storage: 'persistent', path: './data/app.petra', }, useNullAsDefault: true, }, },})スタンドアロン使用
Section titled “スタンドアロン使用”AdonisJSなしでLucidのDatabaseクラスを直接使用できます。
import '@petradb/lucid'import { Database } from '@adonisjs/lucid/database'
const db = new Database({ connection: 'petradb', connections: { petradb: { client: 'petradb' as any, connection: { storage: 'memory', }, useNullAsDefault: true, }, },}, logger, emitter)ストレージモード
Section titled “ストレージモード”connectionで設定します。
// インメモリ(デフォルト){ storage: "memory" }
// ファイルベースの永続ストレージ{ storage: "persistent", path: "./mydb.petra" }スキーマビルダー
Section titled “スキーマビルダー”// テーブル作成await db.schema.createTable("users", (t) => { t.increments("id") t.string("name").notNullable() t.string("email").unique() t.integer("age") t.boolean("active").defaultTo(true) t.timestamps(true, true)})
// テーブル/カラムの存在確認await db.schema.hasTable("users")await db.schema.hasColumn("users", "email")
// カラム追加await db.schema.alterTable("users", (t) => { t.string("bio")})
// テーブル削除await db.schema.dropTableIfExists("users")クエリビルダー
Section titled “クエリビルダー”// Insertawait db.table("users").insert({ name: "Alice", age: 30 })
// returning付きInsertconst [user] = await db.table("users") .insert({ name: "Bob", age: 25 }) .returning("*")
// Selectconst users = await db.from("users").where("age", ">", 25)const first = await db.from("users").where("name", "Alice").first()
// Updateawait db.from("users").where("name", "Alice").update({ age: 31 })
// Deleteawait db.from("users").where("active", false).delete()
// 集計const [{ count }] = await db.from("users").count("* as count")const result = await db.rawQuery("SELECT * FROM users WHERE age > ?", [25])トランザクション
Section titled “トランザクション”await db.transaction(async (trx) => { await trx.table("users").insert({ name: "Eve", age: 22 }) await trx.from("users").where("name", "Bob").update({ age: 26 })})ORMモデル
Section titled “ORMモデル”LucidのBaseModelを使用してモデルを定義します。
import { BaseModel, column } from '@adonisjs/lucid/orm'
class User extends BaseModel { @column({ isPrimary: true }) declare id: number
@column() declare name: string
@column() declare email: string}
// 作成const user = await User.create({ name: "Alice", email: "alice@example.com" })
// 検索const found = await User.find(user.id)const all = await User.all()
// 更新found.name = "Alicia"await found.save()
// 削除await found.delete()
// クエリスコープconst active = await User.query().where("active", true)マイグレーション
Section titled “マイグレーション”マイグレーションファイルを作成し、Lucidのマイグレーターで実行します。
import { BaseSchema } from '@adonisjs/lucid/schema'
export default class CreateUsersTable extends BaseSchema { async up() { this.schema.createTable("users", (t) => { t.increments("id") t.string("name").notNullable() t.string("email").unique() t.timestamps(true, true) }) }
async down() { this.schema.dropTable("users") }}ダイアレクト機能
Section titled “ダイアレクト機能”PetraDBダイアレクトがサポートする機能:
- スキーマイントロスペクション(
getAllTables、getAllViews、getAllTypes) RETURNING文- テーブルのトランケート
- すべてのテーブル/ビュー/型の削除
- トランザクション内のDDL(DMLロールバックと完全にアトミック)
サポートされていない機能:
- アドバイザリーロック(組み込みエンジンでは不要)
- ドメイン
クリーンアップ
Section titled “クリーンアップ”await db.manager.closeAll()