• Start

Schema design

Schema design

What DEFINE does in SurrealDB and how to inspect what you have defined.

Almost everything structural in SurrealDB starts with DEFINE: namespaces, databases, tables, fields, indexes, functions, access methods, parameters, and more. The DEFINE overview in the API docs shows the syntax for each statment. Each such resource also has an ALTER and REMOVE statement if you need to change or remove a definition.

A schema will quickly grow to the point that it is no longer possible to keep in your head. The INFO statement can be used to show what definitions exist. INFO statements can be used on a variety of resources, such as INFO FOR DATABASE, INFO FOR TABLE table_name, INFO FOR INDEX index_name, and so on.

DEFINE FIELD name ON TABLE person TYPE string COMMENT "Todo: add assertion for maximum length";
INFO FOR TABLE person;
{
events: {},
fields: {
name: "DEFINE FIELD name ON person TYPE string COMMENT 'Todo: add assertion for maximum length' PERMISSIONS FULL"
},
indexes: {},
lives: {},
tables: {}
}

Users and other global objects show up when you ask at database level:

DEFINE USER db_user ON DATABASE PASSWORD "strongpassword" ROLES OWNER;
DEFINE TABLE person SCHEMAFULL;
INFO FOR DB;
{
accesses: {},
analyzers: {},
functions: {},
models: {},
params: {},
tables: {
person: 'DEFINE TABLE person TYPE ANY SCHEMAFULL PERMISSIONS NONE'
},
users: {
db_user: "DEFINE USER db_user ON DATABASE PASSHASH '$argon2id$v=19$m=19456,t=2,p=1$P5nVYXOMvk6rEz67kjL5Dg$0T/XNmgIaB+lK0IPspg1l8LruzNK96jd/PvktRCB/ww' ROLES OWNER"
}
}

Was this page helpful?