Coder Social home page Coder Social logo

oatpp / oatpp-mongo Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 3.0 132 KB

Oat++ native BSON + MongoDB driver implementation based on Oat++ object-mapping sub-framework.

Home Page: https://oatpp.io/

License: Apache License 2.0

CMake 5.87% C++ 93.56% Shell 0.58%
oatpp mongodb bson object-mapping cpp

oatpp-mongo's Introduction

oatpp-mongo Build Status


NOTE:

  • BSON ObjectMapper - ready-to-use.
  • Database driver - in development. While you can do basic CRUD operations, it's still on POC stage. API is not ready and it's not recommended to use. To work with MongoDB - use BSON ObjectMapper + mongocxx driver.

oatpp-mongo is the oatpp native client for MongoDB. It contains DTO to BSON mapper plus database driver.

Find the complete example project using oatpp-mongo here

More about Oat++:

How To Build

oatpp-mongo has no extrernal dependencies (The main oatpp module is still required).
libmongoxcc is used (and linked) in module tests only. Use -DOATPP_BUILD_TESTS=OFF option to build without tests and without dependency on libmongoxcc.

Install oatpp-mongo

  • Clone this repository.

  • In the root of the repository run:

    mkdir build && cd build
    cmake -DOATPP_BUILD_TESTS=OFF ..
    make install

API

Temporary API (using libmongoxcc)

Since oatpp driver is not ready yet, you can use libmongoxcc together with oatpp BSON.

Why using oatpp BSON? - because it's based on oatpp object-mapping framework and it's extremely easy to use.

Create bsonxx::document From Any oatpp Object

/**
 * This is the utility function that you'll need while working libmongoxcc
 */
bsoncxx::document::value Database::createMongoDocument(const oatpp::Void &polymorph) {
  // if you have huge docs, you may want to increase starting BufferOutputStream size.
  // Or you may want to use oatpp::data::stream::ChunkedBuffer instead - for no-copy growth.
  oatpp::data::stream::BufferOutputStream stream;
  
  m_objectMapper.write(&stream, polymorph); //< Serialize oatpp object to BSON.
  
  bsoncxx::document::view view(stream.getData(), stream.getCurrentPosition());
  return bsoncxx::document::value(view);
}

Where m_objectMapper - is oatpp::mongo::bson::mapping::ObjectMapper.

Insert Document

Let's say you have such DTO defined:

class User : public oatpp::DTO {

  DTO_INIT(User, DTO)

  DTO_FIELD(String, _id);
  DTO_FIELD(String, username);
  DTO_FIELD(Boolean, active);
  DTO_FIELD(String, role);

};

Then you can insert your DTO in the database like this:

collection.insert_one(createMongoDocument(myDto));

You can also insert an arbitrary document using oatpp::Any

collection.insert_one(createMongoDocument(
  oatpp::Fields<oatpp::Any>({

    {"username", oatpp::String("Mr. Porridge")},
    {"role", oatpp::String("Admin")},
    {"jacket-color", oatpp::List<oatpp::String>({"red", "green", "blue"})}

  })
));

Read Document

Let's say we have the same DTO - User:

  auto result =
    collection.find_one(createMongoDocument( // <-- Filter
      oatpp::Fields<oatpp::String>({
        {"_id", oatpp::String("<id-to-find>")}
      })
    ));

  if(result) {
    auto view = result->view();
    auto bson = oatpp::String((const char*)view.data(), view.length(), false /* to not copy view data */);
    auto user = m_objectMapper.readFromString<oatpp::Object<User>>(bson);
    // TODO - do somthing with user:)
    // You can then serialize it to JSON using oatpp::parser::json::mapping::ObjectMapper
  }

Examples

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.