Database Schema
Relevant source files
The following files were used as context for generating this wiki page:
The Traduora database schema is designed to support a multi-tenant translation management system. It uses a relational model to store projects, users, translation terms, and the translations themselves across various languages. The schema is built using TypeORM, which allows it to be compatible with multiple database systems, including PostgreSQL, MySQL, and SQLite.
The core of the schema revolves around the Project entity, which acts as a container for all related data, such as translation Terms, Labels for organization, and Translations for different locales. Users are associated with projects through a ProjectUser join entity that defines their role and permissions within that specific project.
Entity Relationship Diagram
The following diagram illustrates the main entities and their relationships within the Traduora database.
Database Configuration & Abstraction
Traduora supports multiple database backends. The configuration is managed in api/ormconfig.yaml, and the application uses a helper utility to abstract database-specific column types.
Configuration (ormconfig.yaml)
The database connection and TypeORM settings are defined in this YAML file. The type property specifies the database driver to use.
# api/ormconfig.yaml
type: postgres
host: localhost
port: 5432
username:
password:
database: traduora
synchronize: false
logging: false
entities:
- src/entity/**/*.ts
migrations:
- src/migrations/**/*.ts
subscribers:
- src/subscriber/**/*.ts
cli:
entitiesDir: src/entity
migrationsDir: src/migrations
subscribersDir: src/subscriber
Sources: api/ormconfig.yaml
Database Type Abstraction
The system is designed to be database-agnostic for PostgreSQL, MySQL, and SQLite. This is achieved through a helper that provides the correct column types based on the configured database.
The supported database types are defined in an enum:
// api/src/utils/database-type-helper.ts:5-9
export enum DbType {
POSTGRES = 'postgres',
MYSQL = 'mysql',
BETTER_SQLITE3 = 'better-sqlite3',
}
Sources: api/src/utils/database-type-helper.ts:5-9
Helper functions like getDbColumnType and getLexicalOrderClause are used throughout the entity definitions to ensure compatibility. For example, the BinaryColumnType.encryptedPassword helper returns different types and options depending on the active database.
Sources: api/src/utils/database-type-helper.ts:31-52
Database Migrations
Database schema changes are managed through TypeORM migrations. The initial schema is defined in the init migration file, which contains raw SQL queries to create tables and constraints for each supported database type.
For example, here is the PostgreSQL CREATE TABLE statement for the project entity:
-- From api/src/migrations/1537535282567-init.ts
CREATE TABLE IF NOT EXISTS "project" (
"id" uuid DEFAULT uuid_generate_v4 (),
"name" varchar(255) NOT NULL,
"date_created" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"date_modified" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ("id")
);
Sources: api/src/migrations/1537535282567-init.ts:11-16
Core Entities
Project
The Project entity is the central organizing unit in Traduora. It holds all terms, translations, labels, and user associations.
| Field | Type | Constraints | Description |
|---|---|---|---|
id | uuid | Primary Key, Generated | Unique identifier for the project. |
name | varchar(255) | Not Null | The name of the project. |
dateCreated | timestamp | Not Null, Default CURRENT_TIMESTAMP | Timestamp of when the project was created. |
dateModified | timestamp | Not Null, Default CURRENT_TIMESTAMP | Timestamp of the last modification. |
Relationships:
projectUsers: One-to-Many withProjectUser. A project can have many users with different roles.terms: One-to-Many withTerm. A project contains many translation terms.projectLocales: One-to-Many withProjectLocale. A project can have multiple languages/locales.labels: One-to-Many withLabel. A project can have many labels for organizing terms.
Sources: api/src/entity/project.entity.ts:14-48
User
The User entity represents a registered user of the application.
| Field | Type | Constraints | Description |
|---|---|---|---|
id | uuid | Primary Key, Generated | Unique identifier for the user. |
name | varchar(255) | Not Null | The user's full name. |
email | varchar(255) | Not Null, Unique | The user's email address, used for login. |
encryptedPassword | bytea / binary / blob | Not Null | The user's hashed password. |
lastLogin | timestamp | Nullable | Timestamp of the user's last login. |
Relationships:
projectUsers: One-to-Many withProjectUser. A user can be a member of multiple projects.
Sources: api/src/entity/user.entity.ts:16-52
Term
A Term is a key or string that needs to be translated. It is always associated with a single project.
| Field | Type | Constraints | Description |
|---|---|---|---|
id | uuid | Primary Key, Generated | Unique identifier for the term. |
value | varchar(255) | Not Null | The key or source string (e.g., app.title). |
context | varchar(1000) | Nullable | Optional context or description for translators. |
projectId | uuid | Not Null, Foreign Key | The ID of the project this term belongs to. |
Relationships:
project: Many-to-One withProject.translations: One-to-Many withTranslation. A term has many translations, one for each locale.labels: Many-to-Many withLabel, using theterm_labels_labeljoin table.
Sources: api/src/entity/term.entity.ts:14-46
Translation
A Translation represents the translated value of a Term for a specific ProjectLocale.
| Field | Type | Constraints | Description |
|---|---|---|---|
id | uuid | Primary Key, Generated | Unique identifier for the translation. |
value | varchar(8192) | Not Null | The translated string. |
termId | uuid | Not Null, Foreign Key | The ID of the term being translated. |
projectLocaleId | uuid | Not Null, Foreign Key | The ID of the project locale for this translation. |
Relationships:
term: Many-to-One withTerm.projectLocale: Many-to-One withProjectLocale.
Sources: api/src/entity/translation.entity.ts:13-35
Label
Labels are used to categorize and filter terms within a project.
| Field | Type | Constraints | Description |
|---|---|---|---|
id | uuid | Primary Key, Generated | Unique identifier for the label. |
value | varchar(255) | Not Null | The label's name (e.g., new, needs-review). |
color | varchar(7) | Not Null | The hex color code for the label (e.g., #FFFFFF). |
projectId | uuid | Not Null, Foreign Key | The ID of the project this label belongs to. |
Relationships:
project: Many-to-One withProject.terms: Many-to-Many withTerm. A label can be applied to many terms, and a term can have many labels.
Sources: api/src/entity/label.entity.ts:13-36
Summary
The Traduora database schema is a well-structured relational model centered around the Project entity. It effectively separates concerns between projects, users, terms, and their translations. The use of TypeORM and a database abstraction layer provides flexibility to deploy the application with different database backends, while migrations ensure a consistent and versioned schema. The relationships between entities are clearly defined, enabling the complex queries required for a translation management workflow.