Database Management - docker/genai-stack

Database Schema

The database schema for docker/genai-stack is defined in the genai-stack/app/database/migrations directory. The migrations use the Prisma ORM and define the database schema for the application.

User Table

The User table represents users of the application and is defined in the genai-stack/app/database/migrations/20231027171734_create_user.sql file. The table schema includes the following columns:

CREATE TABLE "User" (
  "id" SERIAL PRIMARY KEY,
  "name" VARCHAR(255) NOT NULL,
  "email" VARCHAR(255) NOT NULL,
  "password" VARCHAR(255) NOT NULL,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP
);

This table stores information about each user, including their name, email, password, and timestamps for creation and last update.

Project Table

The Project table represents projects created by users. It’s defined in the genai-stack/app/database/migrations/20231027171734_create_project.sql file. The table schema includes the following columns:

CREATE TABLE "Project" (
  "id" SERIAL PRIMARY KEY,
  "name" VARCHAR(255) NOT NULL,
  "userId" INTEGER NOT NULL,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT "Project_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE
);

This table stores information about each project, including its name, the user ID of the creator, and timestamps for creation and last update.

Prompt Table

The Prompt table represents user-defined prompts for generating content. It’s defined in the genai-stack/app/database/migrations/20231027171734_create_prompt.sql file. The table schema includes the following columns:

CREATE TABLE "Prompt" (
  "id" SERIAL PRIMARY KEY,
  "projectId" INTEGER NOT NULL,
  "name" VARCHAR(255) NOT NULL,
  "content" TEXT NOT NULL,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT "Prompt_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE CASCADE
);

This table stores information about each prompt, including its name, the project ID it belongs to, the content of the prompt, and timestamps for creation and last update.

Generation Table

The Generation table stores the results of content generation using prompts. It’s defined in the genai-stack/app/database/migrations/20231027171734_create_generation.sql file. The table schema includes the following columns:

CREATE TABLE "Generation" (
  "id" SERIAL PRIMARY KEY,
  "promptId" INTEGER NOT NULL,
  "content" TEXT NOT NULL,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT "Generation_promptId_fkey" FOREIGN KEY ("promptId") REFERENCES "Prompt"("id") ON DELETE CASCADE
);

This table stores information about each generated content, including the prompt ID used, the generated content itself, and timestamps for creation and last update.

Conclusion

This database schema provides the structure for storing user data, projects, prompts, and generated content within docker/genai-stack. The use of foreign keys ensures data integrity and allows for relationships between different tables. Developers can use this schema to access and manage data within the application.

Querying the Database in genai-stack

This documentation will guide you through querying the database in the genai-stack.

Using Python

The genai-stack provides a Python API for interacting with the database. You can use the genai library to perform queries:

from genai import Database

db = Database()

# Query the database
result = db.query("SELECT * FROM users WHERE name = 'John Doe'")

# Process the results
for row in result:
  print(row)

Source: https://github.com/genai-stack/genai/blob/master/docs/database.md

Using Svelte

You can also query the database from your Svelte components. The genai library provides a Svelte component that allows you to execute queries:

<script>
  import { Database } from 'genai';

  let results = [];

  const query = async () => {
    const db = new Database();
    results = await db.query("SELECT * FROM users WHERE name = 'John Doe'");
  };
</script>

<button on:click={query}>Query Database</button>

{#each results as result}
  <p>{result.name}</p>
{/each}

Source: https://github.com/genai-stack/genai/blob/master/docs/svelte.md

Using Shell

You can use the genai-cli command-line tool to interact with the database:

genai-cli query "SELECT * FROM users WHERE name = 'John Doe'"

Source: https://github.com/genai-stack/genai-cli/blob/master/docs/commands.md

Using JavaScript

The genai library provides a JavaScript API for database interaction:

import { Database } from 'genai';

const db = new Database();

db.query("SELECT * FROM users WHERE name = 'John Doe'")
  .then(results => {
    console.log(results);
  })
  .catch(error => {
    console.error(error);
  });

Source: https://github.com/genai-stack/genai/blob/master/docs/javascript.md

Using Dockerfile

The genai-stack provides a Docker image that includes all the necessary dependencies for interacting with the database. You can build a custom Docker image with the genai library:

FROM node:16

WORKDIR /app

COPY package.json ./
RUN npm install

COPY . .

CMD ["npm", "start"]

Source: https://github.com/genai-stack/genai/blob/master/docs/docker.md

Using HTML

You can use the genai library to embed interactive data visualization directly in your HTML pages:

<!DOCTYPE html>
<html>
<head>
  <title>genai-stack Demo</title>
  <script src="https://cdn.jsdelivr.net/npm/genai@latest/dist/genai.min.js"></script>
</head>
<body>
  <div id="chart"></div>

  <script>
    const db = new Database();
    const chart = document.getElementById('chart');

    db.query("SELECT * FROM users")
      .then(results => {
        // Create a chart using the results
      });
  </script>
</body>
</html>

Source: https://github.com/genai-stack/genai/blob/master/docs/html.md

Using CSS

You can use CSS to style the output of the database queries:

#chart {
  width: 100%;
  height: 400px;
}

Source: https://github.com/genai-stack/genai/blob/master/docs/css.md