Skip to content

Schema & Version

victor edited this page Aug 16, 2025 · 2 revisions

πŸ›οΈ Smart_Store schema guide

Schema and Versioning in Smart_Store

Smart_Store uses a robust schema and versioning system to manage the structure and evolution of stored data objects. Each item type (e.g., User, Product, Transaction) has a defined schema that outlines its fields and data types. As the application evolves, schema changes are tracked using version numbers. To maintain compatibility with older data formats, Smart_Store supports versioned migrations. These are upgrade functions registered in the migrationRegistry that transform legacy data into the latest schema version during import.

Benefits of Versioning:

  • Ensures backward compatibility with older data
  • Enables safe schema evolution without breaking existing functionality
  • Supports automatic upgrades during import/export operations

This ensures that any User object from version 1 or 2 is automatically upgraded to version 3, preserving data integrity and consistency. Smart_Store’s schema and versioning system make it easy to adapt to changing business requirements while keeping your data clean, structured, and future-proof.

This is a practical example on how to use schema:

// Smart_Store Example: Schema-Based Item Management
// Author & Architect: Ndiukwu

// Include Smart_Store's item management system
#include "t_manager/ItemManager.h"

// Include Smart_Store's logging system
#include "err_log/Logger.hpp"

// Standard library headers
#include <iostream>
#include <string>

// Optional: JSON library used for schema definition and serialization
//#include <nlohmann/json.hpp>

// Define a structured item with schema support
struct WithSchema {
    std::string name;
    int age;
    std::string email;

    // Define the JSON schema for validation and interoperability
    static nlohmann::json schema() {
        return {
            {"type", "object"},
            {"properties", {
                {"name", {{"type", "string"}}},
                {"age", {{"type", "integer"}}},
                {"email", {{"type", "string"}, {"format", "email"}}}
            }},
            {"required", {"name", "age", "email"}}
        };
    }

    // Enable automatic serialization/deserialization using nlohmann::json
    NLOHMANN_DEFINE_TYPE_INTRUSIVE(WithSchema, name, age, email)
};

int main() {
    // Create an instance of Smart_Store's item manager
    ItemManager manager;

    // Add a new item with schema and assign a unique tag ("schema1")
    manager.addItem(std::make_shared<WithSchema>(WithSchema{"Ada", 30, "ada@example.com"}), "schema1");

    // Export all items managed by Smart_Store to a JSON file
    manager.exportToFile_Json("test_schemas.json");

    // Display all items currently stored in the manager
    manager.displayAll();

    // Retrieve the item tagged "schema1" and access its email field
    auto value = manager.getItem<WithSchema>("schema1")->email;

    // Output the retrieved email to the console
    std::cout << "Email from imported item: " << value << std::endl;

    return 0;
}

Expected output:

schema use

Another practical example on how to use schema:

#include "t_manager/ItemManager.h"
#include "err_log/Logger.hpp"

#include <iostream>
#include <string>

using json = nlohmann::json;

// Define a schema-based item structure
struct SchemaItem {
    std::string id;
    std::string tag;
    std::string type;
    json data;

    // Convert to JSON object
    json toJson() const {
        return {
            {"id", id},
            {"tag", tag},
            {"type", type},
            {"data", data}
        };
    }
};

int main() {
    ItemManager manager;

    // Define schema items manually
    SchemaItem item1 {
        "obj_101",
        "item1",
        "int",
        42
    };

    SchemaItem item2 {
        "obj_102",
        "item2",
        "string",
        "hello"
    };

    // Add items to Smart_Store
    manager.addItem(std::make_shared<json>(item1.toJson()), item1.tag);
    manager.addItem(std::make_shared<json>(item2.toJson()), item2.tag);

    // Export to JSON file
    manager.exportToFile_Json("test_schemas.json");

    // Display items in readable format
    std::cout << "\n:::: Stored Items ::::\n";
    LOG_CONTEXT(LogLevel::INFO, "Outputing: " + item1.tag + " - \n" + item1.toJson().dump(4), {});
    LOG_CONTEXT(LogLevel::INFO, "Outputing: " + item2.tag + " - \n" + item2.toJson().dump(4), {});

    return 0;
}

Expected output:

array use

Versioning & custom IDs:

In this section, you will learn how to work with your custom ID and version control in Smart_Store.

#include "t_manager/ItemManager.h"
#include "err_log/Logger.hpp"

#include <iostream>
#include <string>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

// Helper to create a schema-patterned item
json createSchemaItem(const std::string& id, const std::string& tag, const std::string& type, const json& value) {
    json innerData = {
        {"id", id},
        {"tag", tag},
        {"type", type},
        {"data", value}
    };

    json item = {
        {"id", id},
        {"tag", tag},
        {"type", type},
        {"data", innerData}
    };

    return item;
}

int main() {
    ItemManager manager;

    // Register types explicitly (optional but useful for concept-based systems)
    manager.addItem(std::make_shared<int>(0), "dummy_int");
    manager.addItem(std::make_shared<std::string>(""), "dummy_str");

    // Create schema-patterned items
    json item1 = createSchemaItem("obj_101", "item1", "int", 42);
    json item2 = createSchemaItem("obj_102", "item2", "string", "hello");

    // Add items to Smart_Store
    manager.addItem(std::make_shared<json>(item1), "item1");
    manager.addItem(std::make_shared<json>(item2), "item2");

    // Export to file
    manager.exportToFile_Json("test_schemas.json");

    // Display stored items
    LOG_CONTEXT(LogLevel::INFO, ":::: Items stored in ItemManager ::::", {});
    LOG_CONTEXT(LogLevel::INFO, "Output1: " + item1.dump(4), {});
    LOG_CONTEXT(LogLevel::INFO, "Output2: " + item2.dump(4), {});

    return 0;
}

Expected output:

schema ID

Application of version:

// Smart_Store Example: Schema-Based Item Management with Versioning
// Author & Architect: Ndiukwu

#include "t_manager/ItemManager.h"
#include "err_log/Logger.hpp"

#include <iostream>
#include <string>
#include <nlohmann/json.hpp>

// Define a structured item with schema and version support
struct WithSchemaV2 {
    std::string name;
    int age;
    std::string email;
    int version = 2; // Add version tracking

    // Define the JSON schema for validation and interoperability
    static nlohmann::json schema() {
        return {
            {"type", "object"},
            {"properties", {
                {"name", {{"type", "string"}}},
                {"age", {{"type", "integer"}}},
                {"email", {{"type", "string"}, {"format", "email"}}},
                {"version", {{"type", "integer"}}}
            }},
            {"required", {"name", "age", "email", "version"}}
        };
    }

    // Enable automatic serialization/deserialization
    NLOHMANN_DEFINE_TYPE_INTRUSIVE(WithSchemaV2, name, age, email, version)
};

// Optional: Migration logic from v1 to v2
WithSchemaV2 migrateFromV1(const WithSchemaV2& oldItem) {
    WithSchemaV2 migrated = oldItem;
    migrated.version = 2;
    migrated.name += " (migrated)";
    return migrated;
}

int main() {
    // Create an instance of Smart_Store's item manager
    ItemManager manager;

    // Add a new item with schema and assign a unique tag ("schema1")
    auto item = std::make_shared<WithSchemaV2>(WithSchemaV2{"Ada", 30, "ada@example.com", 2});
    manager.addItem(item, "schema1");

    // Export all items managed by Smart_Store to a JSON file
    manager.exportToFile_Json("test_schemas.json");

    // Display all items currently stored in the manager
    manager.displayAll();

    // Retrieve the item tagged "schema1"
    auto retrieved = manager.getItem<WithSchemaV2>("schema1");

    // Output the retrieved email and version to the console
    std::cout << "Email from imported item: " << retrieved->email << std::endl;
    std::cout << "Schema version: " << retrieved->version << std::endl;

    return 0;
}

Expected output:

version shot

πŸ›οΈ Smart_Store

::| Development Guide |::

If you encounter any issues and bugs, please reach out via email or GitHub issues.

Clone this wiki locally