Schema
How to create a database schema in VitNode.
VitNode use Drizzle ORM with PostgreSQL to manage database.
Create File
After created plugin create new file inside database/schema
folder (categories.ts
is an example here). If is your first schema, create folder schema
inside database
folder.
Create Schema
Inside your new file categories.ts
create schema for your tables. Follow Drizzle ORM documentation for more information how create schema.
Schema
Here is example schema for blog_categories
table:
You can read more about column types here.
Good to know
- Inside
schema
folder you can create as many files as you want (folders not supported), - You can create as many schemas as you want inside one file,
- The best naming convention for tables is
snake_case
with{your_plugin}_
prefix (example:blog_categories
), - You can import other schemas only from
core
plugin.
Indexes
Indexes are important part of schema. Thanks to indexes your database will be faster.
For example, if you have blog_articles
table and you want to search by author_id
, you can create index for this column.
You can read more about how to create indexes.
Relations
Relations is importatnt part of schema. Thanks to relations your database will be more readable and easier to use by you.
Your relations should be with _relations
surfix.
You can read more about how to create relations.
Import schema to root file
After created schema, you need to import it to database/index.ts
file to add these schemas into TypeScript type. Inside index.ts
file you can import as many schemas as you want.
Here is example index.ts
file: