Skip to main content

Database Setup

Database Configuration for CodeChat API

Database configuration is a crucial aspect to ensure the proper functioning of the CodeChat API. This application supports both NoSQL databases (MongoDB) and relational databases (PostgreSQL, MySQL, MariaDB), using Prisma ORM (Object-Relational Mapping) to manage database operations. Prisma ORM makes database access, migration and management easy.

Integrating Prisma ORM

Prisma ORM is integrated to facilitate database operations, enhancing access, migration, and management processes. Depending on your database selection, use the appropriate migration commands:

Note: Ensure proper script file permissions with chmod +x ./scripts/*

NoSQL Database Setup

  • MongoDB:

    MongoDB, as a NoSQL database, provides high flexibility. It differs from traditional SQL databases by automatically generating databases and collections when data is inserted. This mechanism eliminates the need for conventional SQL migrations. MongoDB is particularly suited for projects that demand rapid scaling or frequent schema evolution.

    Initialize with:

    # Set DATABASE_PROVIDER to 'mongodb'
    export DATABASE_PROVIDER=mongodb
    source ./scripts/run_database_operation_generate.sh

SQL Database Setup

For new database instances, execute one of the following:

  • MySQL or MariaDB:

    # Set DATABASE_PROVIDER to 'mysql'
    export DATABASE_PROVIDER=mysql
    source ./scripts/run_database_operation_migrate.sh
  • PostgreSQL:

    # Set DATABASE_PROVIDER to 'postgresql'
    export DATABASE_PROVIDER=postgresql
    source ./scripts/run_database_operation_migrate.sh

Deployment Process

For pre-existing relational databases (PostgreSQL, MySQL, or MariaDB), adhere to these steps:

  1. Environment Variable Configuration: Define the DATABASE_PROVIDER environment variable to correspond with your chosen relational database (use postgresql for PostgreSQL and mysql for MySQL/MariaDB). This setting informs Prisma ORM of the specific database in use.

  2. Schema Deployment: Execute source ./scripts/run_database_operation_deploy.sh to implement schema changes. This command, effectively a shortcut for prisma deploy, inspects and modifies the database schema as needed, while preserving existing data.

Utilizing Prisma Studio

  • Access and Manage Data

    Utilize Prisma Studio to interact with your data models and synchronize them with your database:

    npx prisma studio --schema ./prisma/mysql-schema.prisma
    # or
    npx prisma studio --schema ./prisma/postgresql-schema.prisma
    # or
    npx prisma studio --schema ./prisma/mongodb-schema.prisma

This configuration ensures seamless integration and management of your database systems, leveraging Prisma ORM's capabilities.


This revised documentation aims to be more structured, clear, and instructive, ensuring a smoother setup process for different database types.