Skip to main content

Database Schema

🗄️ Database Schema: SurfAI Last Updated: September 13, 2025

This document details the PostgreSQL database schema, key tables, and relationships between tables in the SurfAI project. All tables and columns are defined based on TypeORM entities.

1. Overview

  • Database System: PostgreSQL
  • ORM: TypeORM
  • Key Entities: User, Workflow, GeneratedOutput

2. Detailed Table Specifications

A. users Table

This is the core table that stores user account information. It has a hybrid structure that supports both Google login and general email/password login.

Column NameTypeConstraintsDescription
idintegerPK, Auto-incrementUnique identifier for the user
emailvarcharUniqueUser's email address. Used as login ID.
displayNamevarcharNot NullName to be displayed to the user (nickname).
passwordvarcharNullable, Select=falsebcrypt hash of the password used for general sign-up.
googleIdvarcharUnique, NullableUnique Google ID for Google login.
imageUrlvarchar(2048)NullableURL of the user's profile picture. (Google or default image)
roleenumNot Null, Default='user'User's role. (admin or user)
currentHashedRefreshTokenvarcharNullable, Select=falsebcrypt hash of the JWT Refresh Token. Set to NULL on logout.
coinBalanceintegerNot Null, Default=0User's current coin balance.
createdAttimestamptzNot NullRecord creation timestamp (auto-generated)
updatedAttimestamptzNot NullRecord last modification timestamp (auto-updated)

B. coin_transactions Table

This table records the user's coin transaction history. All records of coin acquisition and consumption are stored here.

Column NameTypeConstraintsDescription
idintegerPK, Auto-incrementUnique identifier for the coin transaction
userIdintegerNot Null, FK (users.id)ID of the user who performed the transaction.
typeenumNot NullTransaction type (gain or deduct).
amountintegerNot NullAmount of coins changed (always positive).
reasonenumNot NullReason for the transaction (purchase, promotion, admin_adjustment, image_generation, video_generation, etc.).
relatedEntityIdvarcharNullableID of the related entity (e.g., generated_output.id for image generation).
currentBalanceintegerNot NullUser's final coin balance after this transaction.
createdAttimestamptzNot NullRecord creation timestamp.

C. workflows Table

This table manages both workflow "templates" and "user-specific workflow" instances where users have saved parameters.

Column NameTypeConstraintsDescription
idintegerPK, Auto-incrementUnique identifier for the workflow
namevarcharNot NullName of the workflow template or instance.
descriptiontextNullableDetailed description of the workflow.
categoryvarcharNullableTemplate category (e.g., image, video).
definitionjsonbNullableComfyUI's original workflow API format JSON.
parameter_mapjsonbNullableInformation mapping dynamic parameters to actual nodes.
previewImageUrltextNullableURL of the template's preview image to show in lists.
tagstext[]NullableArray of tags for template classification.
costintegerNot Null, Default=1Coin cost required to use this workflow template.
isPublicTemplatebooleanNot Null, Default=falsetrue if this is a public template visible to all users.
user_parameter_valuesjsonbNullableUser-defined parameter values.
isTemplatebooleanNot Null, Default=truetrue if it's an admin-created template, false if it's a user instance.
ownerUserIdintegerNullable, FK (users.id)Owner ID of this workflow.
sourceTemplateIdintegerNullable, FK (workflows.id)ID of the original template from which this workflow was derived.
createdAttimestamptzNot NullRecord creation timestamp
updatedAttimestamptzNot NullRecord last modification timestamp

D. generated_outputs Table

This table stores metadata for all generated outputs (images/videos) by users.

Column NameTypeConstraintsDescription
idintegerPK, Auto-incrementUnique identifier for the generated output
ownerUserIdintegerNot Null, FK (users.id)ID of the user who generated this output.
sourceWorkflowIdintegerNot Null, FK (workflows.id)ID of the workflow used for generation.
r2Urlvarchar(2048)Not NullUnique path URL of the actual file stored in Cloudflare R2.
originalFilenamevarcharNot NullOriginal filename generated by ComfyUI.
mimeTypevarcharNot NullMIME type of the file. (e.g., image/png, video/mp4)
promptIdvarcharNot Null, IndexComfyUI's prompt ID identifying the generation task.
usedParametersjsonbNullableRecord of dynamic parameter values entered by the user during generation.
durationfloatNullablePlayback time in seconds if the output is a video.
createdAttimestamptzNot NullRecord creation timestamp

E. social_connections Table

This table stores information about the various social media accounts connected by the user.

Column NameTypeConstraintsDescription
idintegerPK, Auto-incrementUnique identifier
userIdintegerNot Null, FK (users.id)SurfAI user ID
platformenumNot NullSNS platform (YOUTUBE, INSTAGRAM, X, etc.)
platformUsernamevarcharNot NullUser's name on that SNS
accessTokenvarcharNot Null, EncryptedAccess Token to be used for API requests
refreshTokenvarcharNullable, EncryptedRefresh Token to be used for reissuing Access Token
connectedAttimestamptzNot NullTimestamp of connection

3. Table Relationships (ERD Summary)

  • User (1) : (N) SocialConnection: One user can connect multiple social media accounts.
  • User (1) : (N) Workflow: One user can own multiple workflow instances.
  • User (1) : (N) GeneratedOutput: One user can generate multiple outputs.
  • User (1) : (N) CoinTransaction: One user can have multiple coin transaction records.
  • Workflow (1) : (N) Workflow: One workflow template can have multiple user instances. (Self-referencing relationship)
  • Workflow (1) : (N) GeneratedOutput: One workflow can be used to generate multiple outputs.

4. TypeORM Migrations

The SurfAI backend uses TypeORM migrations for database schema management. Instead of using synchronize: true in development, the migration approach is adopted for production environment stability and data integrity.

4.1. Migration Configuration

In the comfy-surfai-backend/src/app.module.ts file, the TypeORM configuration is as follows:

TypeOrmModule.forRoot({
// ... existing configuration ...
synchronize: false, // Must be set to false in production environments
migrations: [__dirname + '/migrations/**/*.js'], // Path to migration files
cli: {
migrationsDir: 'src/migrations', // Path where TypeORM CLI will generate migration files
},
}),

4.2. Migration Commands

You can manage migrations using the following commands in the comfy-surfai-backend directory.

A. Generate Migration File

Generates a new migration file based on entity changes.

npm run typeorm migration:generate -- -n <MigrationName>
# Example: npm run typeorm migration:generate -- -n UserTableUpdate

B. Run Migrations

Applies generated migration files to the database.

npm run typeorm migration:run

C. Rollback Migrations

Rolls back the most recently applied migration.

npm run typeorm migration:revert