Learn more about our native MCP
Directus Logo
  • Use Cases and Features
    • Headless CMS
      Manage and deliver content with ease
    • Backend-as-a-Service
      Build and ship applications faster
    • Headless Commerce
      A single source of truth for products
    • 100+ More Use Cases
      Build anything (or everything)
    • Instant APIs
      Connect a database, get REST + GraphQL APIs
    • Granular Policy-Based Auth
      Provide secure, autonomous data access
    • Visual Automation Builder
      Automate content and data workflows with ease
    • 50+ More Features
      Get everything you need out-of-the-box
    Project Showcase
    Built With Directus

    Built With Directus

    See what everyone's been building with Directus

  • Learn More
    • Blog
      Read our latest articles and guides
    • Case Studies
      Case studies and success stories
    • Community Forum
      Questions and conversations
    • Agency Directory
      Browse our list of agency partners
    • About Us
      Learn more about Directus and the team
    • Wall of Love
      See what others are saying about us
    • Contact
      Have a general inquiry or question for us?
    • Support
      Reach out to Directus support
    Watch Directus TV
    Directus TV
    Video

    Directus TV

    Go down the rabbit hole with hours of original video content from our team.

  • Developers
  • Enterprise
  • Pricing
Book a DemoGet StartedLog In
GitHub logo36,016
Directus Marketplace
Marketplace
  1. Extensions
  2. Integrations
  3. Templates
Back to Extensions
bundle

Raw Query

Execute raw SQL queries in Directus with Monaco Editor

Directus Raw Query Extension Logo

Directus Extension: Raw Query

GitHub package.json version GitHub Release

NPM Version NPM Downloads Maintenance

GitHub Stars GitHub Forks Support me on Ko-fi


โœจ Features

Editor

  • ๐ŸŽจ Monaco Editor Integration: Professional SQL editor with syntax highlighting and error detection
  • ๐Ÿ”ฎ Smart Autocomplete: Intelligent suggestions for tables, columns, and SQL keywords
  • ๐Ÿ“Š Database Schema Awareness: Fetches your database structure on load for accurate completions
  • โšก Keyboard Shortcuts: Execute queries with Ctrl/Cmd + Enter
  • ๐Ÿ’พ Auto-save Draft: Automatically saves your work as you type - resume where you left off

Query Execution

  • ๐Ÿ“Š Multiple Queries: Execute multiple SQL queries at once (separated by semicolons)
  • ๐Ÿ“ˆ Results Display: View query results in a clean, tabular format with row counts
  • ๐ŸŽฏ Error Handling: Clear error messages for failed queries
  • ๐Ÿ’พ Database Agnostic: Works with PostgreSQL, MySQL, and other databases supported by Directus

Query History

  • ๐Ÿ“œ Query History: Last 50 executed queries stored in sidebar
  • ๐Ÿ”„ One-Click Reload: Click any history item to load it back into the editor
  • ๐Ÿงน Smart Deduplication: Same query only appears once (most recent execution)
  • โฑ๏ธ Timestamps: See when each query was executed ("Just now", "5m ago", etc.)
  • ๐Ÿ—‘๏ธ Clear History: Remove all history with one click

Security & Access

  • ๐Ÿ” Admin-Only Access: Only administrators can access and execute queries

โณ Installation

Install the extension via npm:

npm install directus-extension-raw-query@latest

# or

yarn add directus-extension-raw-query@latest

Or using Directus CLI:

npx directus-extension install directus-extension-raw-query

After installation, restart your Directus instance. The extension will be automatically loaded and available in the admin panel.

๐Ÿ– Requirements

Supported Directus versions:

  • Directus: >= 10.10.0

Supported Node versions:

  • Node: >= 18.x.x
  • npm: >= 8.0.0

We recommend always using the latest version of Directus to start your new projects.

๐Ÿ“– Usage

Getting Started

  1. Access the Module:
    • Log in to Directus as an administrator
    • Navigate to the "Raw Query" module in the sidebar (look for the code icon)

Writing Queries

  1. Use the Monaco Editor:
    • Write SQL queries with full syntax highlighting
    • Get intelligent autocomplete suggestions as you type:
      • Table names from your database
      • Column names with their data types
      • SQL keywords and functions
    • Your work is automatically saved as you type
    • Navigate away and return - your query will be restored
    • Write single or multiple queries (separate with semicolons)

Executing Queries

  1. Run Your Queries:

    • Click the play button (โ–ถ) in the top-right corner
    • Or press Ctrl/Cmd + Enter to execute
    • Results appear below the editor in real-time
  2. View Results:

    • Successful queries show a table with the results
    • Each query displays its row count
    • Failed queries show clear error messages
    • Results preserve newlines for readability

Using Query History

  1. Query History Sidebar:
    • Last 50 executed queries are saved automatically
    • Click any query to load it back into the editor
    • Queries show relative timestamps ("5m ago", "2h ago")
    • Duplicate queries are automatically merged (keeps most recent)
    • Clear all history with the "Clear All" button

๐Ÿ”’ Security

  • Admin-Only: The extension performs multiple security checks:
    1. User must be authenticated
    2. User must have admin privileges (req.accountability.admin)
    3. Module pre-registration check prevents non-admins from seeing it
  • Data Storage: Query history and drafts are stored in browser localStorage
  • No Password Exposure: Query results don't expose sensitive fields

๐Ÿ”Œ API Endpoints

Execute Query

The extension exposes a POST endpoint at /raw-query/execute:

POST /raw-query/execute
Content-Type: application/json

{
  "query": "SELECT * FROM directus_users LIMIT 10;"
}

Response:

{
  "success": true,
  "results": [
    {
      "query": "SELECT * FROM directus_users LIMIT 10;",
      "success": true,
      "data": [...],
      "rowCount": 10
    }
  ]
}

Fetch Database Schema

GET endpoint at /raw-query/schema to retrieve database structure for autocomplete:

GET /raw-query/schema

Response:

{
  "success": true,
  "tables": [
    {
      "name": "directus_users",
      "columns": [
        {
          "name": "id",
          "type": "uuid",
          "nullable": false
        },
        {
          "name": "email",
          "type": "varchar",
          "nullable": true
        }
      ]
    }
  ]
}

๐Ÿ’ป Development

Build the extension:

bun run build

Watch mode for development:

bun run dev

๐Ÿ—๏ธ Architecture

  • Type: Bundle Extension (contains both endpoint and module)
  • Endpoints:
    • /raw-query/execute (POST) - Handles query execution
    • /raw-query/schema (GET) - Fetches database schema for autocomplete
  • Module: Admin UI with Monaco Editor
  • Technologies: TypeScript, Vue 3, Monaco Editor
  • Storage: LocalStorage for query history (last 50) and draft auto-save
  • UI Components: Uses Directus's built-in UI library for consistent design
  • Autocomplete: Dynamic SQL completions based on actual database structure

๐Ÿ“ Example Queries

-- View all collections
SELECT * FROM information_schema.tables 
WHERE table_schema = 'public';

-- Count items in a collection
SELECT COUNT(*) FROM your_collection_name;

-- Update multiple records
UPDATE directus_users 
SET status = 'active' 
WHERE last_login > NOW() - INTERVAL '30 days';

๐Ÿ”ง Troubleshooting

  • Module not visible: Ensure you're logged in as an administrator
  • Query fails: Check the error message - it may be a SQL syntax error or permission issue
  • Results not showing: Ensure your query returns data (SELECT statements)
  • Editor not loading: Try refreshing the page - Monaco Editor loads dynamically
  • Autocomplete not working:
    • Check browser console for schema fetch errors
    • Ensure database has proper permissions for information_schema queries
    • Try refreshing the page to reload schema
  • Draft not restoring: Check browser localStorage isn't disabled
  • History not saving: Ensure localStorage has space (stores up to 50 queries)

๐Ÿ’พ LocalStorage Keys

The extension uses the following localStorage keys:

  • directus_raw_query_history: Stores the last 50 executed queries
  • directus_raw_query_draft: Stores the current editor content for auto-restore

To clear all stored data, use your browser's developer tools or click "Clear All" in the history sidebar.

๐Ÿค Contributing

Feel free to fork and make a Pull Request to this extension project. All the input is warmly welcome!

โญ๏ธ Show your support

Give a star if this project helped you.

๐Ÿ”— Links

  • NPM package
  • GitHub repository

๐Ÿ“„ License

MIT License Copyright 2025 creazy231

๐Ÿ™ Inspired By

This extension is inspired by the strapi-plugin-raw-query for Strapi CMS.

ForTheBadge built-with-love

directus-extension-raw-query

npm i directus-extension-raw-query

This extension is not sandboxed and can only be installed in self-hosted instances using npm. Learn more .

creazy231
1.0.2
Works with v10.10.0+
Updated 6 months ago
MIT License
9.1 MiB (4 files)
npm
Repository
Report Issue
  • Directus LogoDirectus Logo

    A composable backend to build your Headless CMS, BaaS, and more. 

  • Solutions
    • Headless CMS
    • Backend-as-a-Service
    • Product Information
    • 100+ Things to Build
  • Resources
    • Documentation
    • Guides
    • Community
    • Release Notes
  • Support
    • Issue Tracker
    • Feature Requests
    • Community Chat
    • Cloud Dashboard
  • Organization
    • About
    • Careers
    • Brand Assets
    • Contact
ยฉ2026 Monospace Inc
  • Cloud Policies
  • License
  • Terms
  • Privacy