Skip to content

Latest commit

 

History

History
135 lines (96 loc) · 4.91 KB

README.md

File metadata and controls

135 lines (96 loc) · 4.91 KB

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.