Skip to content

SurrealQL Conventions

jamster edited this page Nov 22, 2026 · 1 revision

SurrealQL Conventions

These conventions apply to all .surql files. Those files are used when there are a large number of queries to run, such as initial.surql.

Naming

  • Params: SCREAMING_SNAKE_CASE, for example $MAX_USERNAME_BYTES
  • Tables and fields: snake_case, for example user_settings

File Structure

  • Open with a reference block of -- comments if the file defines params or constants that need explaining, such as bytes size tables.
  • Group related definitions under a section banner:
-- =========================================================
-- SECTION NAME
-- =========================================================
  • Within a section, order each table block as: DEFINE TABLE, then fields, then indexes.
  • Leave a blank line between each DEFINE TABLE and DEFINTE INDEX. Leave two black lines between each table block.
  • Use IF NOT EXISTS on all definitions unless OVERWRITE is explicitly needed.

Example

-- Users
DEFINE TABLE IF NOT EXISTS users SCHEMAFULL;

DEFINE FIELD IF NOT EXISTS username ON TABLE users TYPE string
    ASSERT fn::bytes_within($value, $MIN_DEFAULT_BYTES, $MAX_USERNAME_BYTES);
DEFINE FIELD IF NOT EXISTS email ON TABLE users TYPE string
    ASSERT fn::bytes_within($value, $MIN_DEFAULT_BYTES, $MAX_EMAIL_BYTES);

DEFINE INDEX IF NOT EXISTS idx_username ON TABLE users COLUMNS username UNIQUE;
DEFINE INDEX IF NOT EXISTS idx_email ON TABLE users COLUMNS email UNIQUE;

Clone this wiki locally