TinyORM

repository·main·Indexed 43 days ago

https://github.com/silverqx/tinyorm

A modern C++20 ORM library featuring a fluent query builder, relationship management, and a compiled migrations system. It supports MySQL, MariaDB, SQLite, and PostgreSQL. The package includes the 'tom' CLI tool for database lifecycle management and TinyDrivers, a low-level driver abstraction layer that provides a 1:1 API compatible replacement for the QtSql module.

Tokens
70.9K
Snippets
231
Records
295
Agent score
54%

What's inside TinyORM

  1. Introduction to TinyORM

    main

    TinyORM is an object-relational mapper (ORM) for C++. It maps database tables to "Model" classes. These models provide an interface to interact with database records, including retrieving, inserting, updating, and deleting data.

    Note: Before using TinyORM models, you must configure a database connection in your application.

  2. Overview of TinyORM

    main

    TinyORM is a modern C++20 ORM library designed to simplify database interactions. It is built with a focus on reliability, featuring extensive unit and functional testing (over 3300 tests) to ensure API and behavior compatibility across supported databases.

    Key characteristics:

    • Modern C++: Written using C++20 standards.
    • Dependencies: Requires QtCore and QtSql libraries.
    • Flexible Usage: You can use the full ORM features or use only the Query Builder to avoid writing raw SQL while maintaining database portability.
  3. Overview of TinyORM

    main
    TinyORM is an object-relational mapper (ORM) designed to simplify database interactions. It uses a 'Model' abstraction where each database table corresponds to a specific Model class. These models provide a high-level interface to perform CRUD (Create, Read, Update, Delete) operations on table records.
  4. Introduction to TinyDrivers

    main

    TinyDrivers is an underlying SQL database layer for TinyORM designed to replace the QtSql module. It provides a 1:1 API compatibility with QtSql, allowing you to swap the dependency at compile time without changing your application code.

    Key Features:

    • Supports both normal and prepared statements.
    • Supports TLS/SSL connections using MYSQL_OPT_SSL_MODE (e.g., verify_ca, verify_identity).
    • Supports transactions and re-using SqlQuery instances.
    • Uses smart pointers exclusively (no new keyword in the codebase).
    • Currently, only the MySQL driver is fully supported and finished.

    Note: All other database drivers described in the TinyORM documentation are only supported when linking against the original QtSql module.

  5. Introduction to TinyORM

    main

    TinyORM is a C++ library designed to simplify database interactions. It offers three primary ways to interact with data:

    1. Raw SQL: Execute direct SQL statements.
    2. Fluent Query Builder: Use a programmatic, chainable API to construct queries.
    3. TinyORM (Object-Relational Mapping): Map database tables to C++ objects.

    TinyORM provides first-party support for the following databases:

    • MySQL
    • MariaDB
    • PostgreSQL
    • SQLite
  6. Introduction to the TinyORM Query Builder

    main

    TinyORM's database query builder provides a fluent interface for creating and running database queries. It uses QSqlQuery parameter binding to protect against SQL injection, meaning you do not need to manually sanitize strings passed as query bindings.

    Security and Driver Warnings

    • Column Names: QSqlQuery does not support binding column names. Never allow user input to dictate column names (e.g., in order by clauses).
    • MySQL JSON Bug: If using the QMYSQL driver and your table contains json columns, you must explicitly name all columns instead of using the * shorthand. Using * with json columns in QMYSQL can result in empty results or invalid QVariant values. The QPSQL driver is not affected by this.
  7. Overview of TinyORM support tools

    main

    TinyORM provides a collection of support tools, primarily implemented as PowerShell (pwsh) scripts. These tools are designed to assist with development tasks such as:

    • Linting: Checking code quality.
    • Environment Management: Preparing build and runtime environments.
    • Driver Building: Specifically for building the QtSql MySQL driver.
    • Code Maintenance: Searching for TODO items within the codebase.
    • Deployment: Automating deployment processes.
  8. Overview of TinyORM features

    main

    TinyORM provides a comprehensive suite of database management tools:

    • Connection Management: Uses a database manager and the Orm::DB facade class for simplified syntax. Supports multi-threading and SSL.
    • Query Builder: Supports sub-queries, raw expressions, logical grouping, and all join types (left, right, cross, inner). Includes aggregate methods (min, max, sum, etc.) and existence queries (whereExists, exists).
    • ORM Capabilities: Supports one-to-one, one-to-many, and many-to-many relationships with eager/lazy loading. Implements the Active Record pattern with features like soft deleting, timestamps, and attribute casting.
    • Migrations & Seeders: A compiled system to manage schema changes (tables, columns, foreign keys, and indexes) and seed data.
    • CLI Tool (tom): A console application for managing migrations, seeding, wiping databases, and scaffolding models.
  9. Interacting with databases in TinyORM

    main

    TinyORM provides three primary ways to interact with a database, depending on your needs for control and abstraction:

    1. Raw SQL: Execute direct SQL statements for maximum control.
    2. Fluent Query Builder: Use a programmatic, chainable API to construct queries without writing manual SQL strings.
    3. TinyORM (ORM Layer): Use the high-level Object-Relational Mapping features to map database rows to application objects/entities.

    For a complete workflow, you can also utilize the built-in Migrations system for schema evolution and Seeding for populating initial data.

  10. What is the `tom` migrations application?

    main

    The tom application is a console-based migrations tool for TinyORM. It is used to manage database schema changes and can generate Data Definition Language (DDL) queries for all supported databases.

    Key characteristics:

    • It is a standalone C++ console application.
    • It can be built using CMake or qmake (via QtCreator).
    • It uses the tabulate library specifically for the migrate:status command to display information.
    • The source code is similar to the tom example application and the internal migrations used by TinyORM unit tests.
  11. Introduction to Database Migrations

    main

    Migrations act as version control for your database schema, allowing teams to define and share schema definitions. TinyORM uses the Schema facade to provide database-agnostic support for creating and manipulating tables across all supported database systems.

    Key components:

    • Schema facade: Used within migrations to expressively define tables, columns, and indexes.
    • tom console application: A small CLI tool used to generate and manage migrations and seeders.

    Important Constraints:

    • Recompilation: All migration logic is compiled. You must recompile your application after adding a new migration class.
    • Multi-threading: The Schema builder and migrations do not support multi-threading.