🛠️ VitNode is still in development! You can try it out, but it is not recommended to use it now in production.
Backend
Database
Overview

Database

Our plugin system allows you to create own tables in the database. VitNode using Drizzle ORM (opens in a new tab) for database management with PostgreSQL.

Initial setup

After created plugin using our plugin system you can find database folder in your plugin in admin folder.

        • functions.ts
        • index.ts
  • Schema

    Create File

    Inside database folder go to schema folder (If is your first schema, create folder). Create new TypeScript file with any name. Here is example file tree after created posts file:

          • posts.ts
        • functions.ts
        • index.ts
  • Create Schema

    Inside posts.ts file create schema for your tables. Follow Drizzle ORM (opens in a new tab) documentation for more information how create schema.

    Here is example schema for posts table:

    import { integer, pgTable, serial, text, varchar } from "drizzle-orm/pg-core";
    import { relations } from "drizzle-orm";
     
    import { forum_topics } from "./topics";
     
    import { core_users } from "@/src/apps/admin/core/database/schema/users";
     
    export const forum_posts = pgTable("forum_posts", {
      id: serial("id").primaryKey(),
      topic_id: integer("topic_id").references(() => forum_topics.id, {
        onDelete: "cascade"
      }),
      user_id: integer("user_id").references(() => core_users.id, {
        onDelete: "cascade"
      }),
      ip_address: varchar("ip_address", { length: 45 }),
      created: integer("created").notNull(),
      updated: integer("updated").notNull()
    });
     
    export const forum_posts_relations = relations(
      forum_posts,
      ({ many, one }) => ({
        topic: one(forum_topics, {
          fields: [forum_posts.topic_id],
          references: [forum_topics.id]
        }),
        user: one(core_users, {
          fields: [forum_posts.user_id],
          references: [core_users.id]
        }),
        content: many(forum_posts_content),
        timeline: many(forum_posts_timeline)
      })
    );

    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 (including relations),
    • VitNode using PostgreSQL,
    • The best naming convention for tables is snake_case with {your_plugin}_ prefix (example: forum_posts),

    Relations

    Relations is importatnt part of schema. Thanks to relations your database will be more readable and easier to use by you.

    For example, if you have topics table and posts table, you can create relation between them. If some topic will be removed, all posts related to this topic will be removed too.

    Your relations should be with _relations surfix.

    Import schema to root file.

    After created schema, you need to import it to index.ts file. Inside index.ts file you can import as many schemas as you want.

    Here is example index.ts file:

    import * as forums from "./schema/forums";
    import * as topics from "./schema/topics";
    import * as posts from "./schema/posts";
     
    export default {
      ...forums,
      ...topics,
      ...posts
    };

    Please remember to export all schemas from index.ts file. If you forget to export some schema, it will not be removed from database when you remove plugin.

    Generate tables

    Inside backend folder run:

    pnpm db

    Helpers

    VitNode provide a few helpers functions to help you build your query. We recommend using this helpers functions to build your query.