/orm-cxx

Modern C++ ORM Library

Primary LanguageC++MIT LicenseMIT

ORM C++

ORM Library for Modern C++(C++20) using native reflection.

C++ License: MIT Maintenance Generic badge Generic badge Generic badge PRs Welcome

clang++ g++ msvc codecov

🎯 Goal

The goal of the ORM C++ is to provide a decent Object-Relational Mapping library for C++ community.

πŸ†• Base on native reflection from modern C++(C++20)
βœ… 100% test coverage with unit tests and integration tests
πŸ—‚οΈ Support for multiple databases
βš™οΈ Support for multiple compilers
☠️ No macros (currently few πŸ˜•)
πŸš€ As low as possible runtime overhead
πŸ‘Ά Easy to use
πŸ“‰ As few dependencies as possible

βš™οΈ Usage

#include <optional>

#include "orm-cxx/orm.hpp"

struct ObjectModel
{
    // INTEGER - if field is optional it will not be marked as NOT NULL
    std::optional<int> field1;

    // TEXT NOT NULL
    std::string field2;

    // defining id_columns is optional
    inline static const std::vector<std::string> id_columns = {"field1", "field2"};

    // other way to define id column - will be over writen by using id_columns
    // int id;

    // defining table_name is optional, adding it will overwrite default table name
    inline static const std::string table_name = "object_model";

    // defining columns_names is optional, adding it will overwrite default columns names
    // not all columns have to be defined, others will get default names
    inline static const std::map<std::string, std::string> columns_names = {{"field1", "some_field1_name"},
                                                                            {"field2", "some_field2_name"}};
};

int main()
{
    // connect with standard connection string
    orm::Database database;
    database.connect("sqlite3://test.db");

    // drop table if exists
    database.deleteTable<ObjectModel>();

    // create table in database
    database.createTable<ObjectModel>();

    // create objects and insert them into table
    std::vector<ObjectModel> objects{{1, "test"}, {std::nullopt, "text"}};
    database.insert(objects);

    // define select query with builder pattern
    orm::Query<ObjectModel> query;
    query.limit(10).offset(5);

    // execute query
    auto queriedObjects = database.select(query);

    return 0;
}

πŸ“– Documentation

πŸ“ Consuming library with CMake (CMake 3.22 or newer)

  1. Add config to git submodules (execute in project root):
mkdir externals
cd externals
git submodule add https://github.com/wsekta/orm-cxx.git
  1. Link with library:
set(BUILD_CONFIG_CXX_TESTS OFF)
set(BUILD_ORM_CXX_EXAMPLE OFF)

add_subdirectory(externals/orm-cxx)

add_executable(main Main.cpp)

target_link_libraries(main orm-cxx)

βš’οΈ Compiler support

πŸ“¦ Dependencies

✨ Contributing

Feel free to join ORM C++ development! πŸš€

Please check CONTRIBUTING guide.