mcpField
D
drupal-queries

drupal-queries

github.com/edutrulデータ・データベース7352 回インストール21 週間前に更新
リポジトリ

drupal-queries について

drupal-queriesは、Drupal開発者向けのデータベースクエリ参照スキルです。クエリ構築やデータ変更、トランザクションを扱い、エンティティにはEntityQuery、SQLにはパラメーター指定を推奨します。

主な機能

  1. 01条件、並べ替え、結合クエリの構築
  2. 02挿入、更新、削除、upsert
  3. 03エンティティにはEntityQueryを優先
  4. 04データベースサービスの注入とトランザクション
  5. 05パラメーター指定でSQL連結を回避

活用シーン

  1. 01Drupalコンテンツとフィールドデータの検索
  2. 02カスタムテーブルの安全な読み書き
  3. 03複数のデータベース操作をトランザクション化

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/edutrul/drupal-ai --skill drupal-queries

インストール後、Claude Code で/drupal-queriesを入力して読み込まれたか確認します。

同梱ファイルREADME.md

詳細

Drupal データベースクエリ

重要:SQLを連結しないでください

// WRONG — SQL injection risk
$result = $this->database->query("SELECT * FROM {node} WHERE type = '$type'");

// CORRECT — parameterized query
$result = $this->database->query(
  "SELECT nid, title FROM {node} WHERE type = :type",
  [':type' => $type]
);

SELECTクエリ

$query = $this->database->select('node_field_data', 'n');
$query->fields('n', ['nid', 'title', 'status']);
$query->condition('n.type', 'article');
$query->condition('n.status', 1);
$query->orderBy('n.created', 'DESC');
$query->range(0, 10);

$results = $query->execute()->fetchAll();

// Fetch as associative array
$results = $query->execute()->fetchAllAssoc('nid');

// Fetch single value
$count = $query->countQuery()->execute()->fetchField();

JOINを使ったSELECT

$query = $this->database->select('node_field_data', 'n');
$query->join('node__field_tags', 'tags', 'n.nid = tags.entity_id');
$query->fields('n', ['nid', 'title']);
$query->condition('tags.field_tags_target_id', $tid);
$query->condition('n.status', 1);

INSERT

$this->database->insert('my_table')
  ->fields([
    'uid' => $uid,
    'data' => serialize($data),
    'created' => \Drupal::time()->getRequestTime(),
  ])
  ->execute();

アップサート(INSERTまたはUPDATE)

$this->database->upsert('my_table')
  ->key('uid')
  ->fields(['uid', 'data', 'updated'])
  ->values([
    'uid' => $uid,
    'data' => serialize($data),
    'updated' => \Drupal::time()->getRequestTime(),
  ])
  ->execute();

UPDATE

$this->database->update('my_table')
  ->fields(['data' => serialize($data)])
  ->condition('uid', $uid)
  ->execute();

DELETE

$this->database->delete('my_table')
  ->condition('uid', $uid)
  ->execute();

エンティティクエリ(エンティティにはこちらを推奨)

// Always prefer EntityQuery over raw SQL for entities
$query = $this->entityTypeManager->getStorage('node')->getQuery()
  ->accessCheck(TRUE)
  ->condition('type', 'article')
  ->condition('status', 1)
  ->sort('created', 'DESC')
  ->range(0, 10);

$nids = $query->execute();

データベースサービスの注入

use Drupal\Core\Database\Connection;

public function __construct(
  private readonly Connection $database,
) {}

サービスID:database

トランザクション

$transaction = $this->database->startTransaction();
try {
  $this->database->insert('my_table')->fields([...])->execute();
  $this->database->update('other_table')->fields([...])->execute();
}
catch (\Exception $e) {
  $transaction->rollBack();
  throw $e;
}
// Transaction commits when $transaction goes out of scope

コメント