Skip to content

Add parser and API

Nikola Milojevic requested to merge proposal-1-library into main

Usage

DataModels

DataModels are the core data structures in the Cloud Connector. Each model inherits from DataModel::Base and can define associations with other models. The data is loaded from the file structure into memory at runtime, making it efficient for read operations and relationship traversal.

module Gitlab
  module CloudConnector
    module DataModels
      class BackendService < DataModel::Base
        has_and_belongs_to_many :unit_primitives
      end

      class UnitPrimitive < DataModel::Base
        has_and_belongs_to_many :backend_services
        has_and_belongs_to_many :add_ons
        has_and_belongs_to_many :license_types
      end

      class AddOn < DataModel::Base
        has_and_belongs_to_many :unit_primitives
      end

      class LicenseType < DataModel::Base
        has_and_belongs_to_many :unit_primitives
      end
    end
  end
end

Associations

The library supports many-to-many relationships through the has_and_belongs_to_many association type. This sets up a bidirectional relationship between models where each instance can be associated with multiple instances of the other model.

class UnitPrimitive < DataModel::Base
  has_and_belongs_to_many :backend_services
end

unit_primitive = UnitPrimitive.find_by_name(:duo_chat)
unit_primitive.backend_services  # Returns a Relation containing associated bakcned services

Working with Relations

Relations are collections of associated records that support searching:

# Get all unit primitives for a backend service
service = Gitlab::CloudConnector::DataModels::BackendService.find_by_name(:ai_gateway)
service.unit_primitives  # Returns a Relation containing all associated unit primitives

# Search within unit primitives
service.unit_primitives.where(name: :duo_chat)
service.unit_primitives.find_by(name: :duo_chat)
service.unit_primitives.find_by_name(:duo_chat)

# Chain searches on many-to-many relationships
Gitlab::CloudConnector::DataModels::UnitPrimitive
  .where(add_ons: :duo_pro)
  .where(backend_services: :ai_gateway)
# or
Gitlab::CloudConnector::DataModels::UnitPrimitive
  .where(add_ons: :duo_pro, backend_services: :ai_gateway)

# Find unit primitives using specific add_ons
add_on = Gitlab::CloudConnector::DataModels::AddOn.find_by_name(:duo_pro)
add_on.unit_primitives.where(name: :duo_chat)

Note: All search values should be symbols (e.g., :duo_chat instead of 'duo_chat').

Edited by Nikola Milojevic

Merge request reports

Loading