Skip to main content

db.go

db.go - Overview

  1. Overview The db.go file defines the Repo struct and its associated methods for managing license keys and feature flags in a database. It handles operations such as retrieving, inserting, and updating license information, as well as managing feature status (active/inactive) and usage.

  2. Detailed Documentation

Repo

  • Purpose: Represents the license repository, providing methods to interact with the database for license and feature management.
  • Fields:
    • db: *sqlx.DB - Database connection for license data.
    • store: sqlstore.SQLStore - SQL store instance.

NewLicenseRepo

  • Purpose: Creates a new instance of the Repo struct.
  • Parameters:
    • db: *sqlx.DB - Database connection.
    • store: sqlstore.SQLStore - The sql store.
  • Returns: Repo - A new Repo instance.

Repo.GetLicensesV3

  • Purpose: Retrieves all license v3 data from the licenses_v3 table.
  • Parameters:
    • ctx: context.Context - Context for the database operation.
  • Returns: ([]*model.LicenseV3, error) - A slice of LicenseV3 pointers or an error if the operation fails.

Repo.GetActiveLicense

  • Purpose: Fetches the latest active license from the database. Converts the v3 license into v2.
  • Parameters:
    • ctx: context.Context - Context for the database operation.
  • Returns: (*model.License, *basemodel.ApiError) - A pointer to the active License struct and a possible ApiError. Returns nil license and nil error if no active license is found.

Repo.GetActiveLicenseV3

  • Purpose: Retrieves the latest active license v3 from the database.
  • Parameters:
    • ctx: context.Context - Context for the database operation.
  • Returns: (*model.LicenseV3, error) - A pointer to the active LicenseV3 struct or an error if the operation fails. Returns nil if no active license is found.

Repo.InsertLicenseV3

  • Purpose: Inserts a new license v3 into the licenses_v3 table.
  • Parameters:
    • ctx: context.Context - Context for the database operation.
    • l: *model.LicenseV3 - Pointer to the LicenseV3 struct containing license data.
  • Returns: *model.ApiError - An ApiError if the operation fails. Returns nil on success.

Repo.UpdateLicenseV3

  • Purpose: Updates an existing license v3 in the licenses_v3 table.
  • Parameters:
    • ctx: context.Context - Context for the database operation.
    • l: *model.LicenseV3 - Pointer to the LicenseV3 struct containing updated license data.
  • Returns: error - An error if the operation fails, nil otherwise.

Repo.CreateFeature

  • Purpose: Creates a new feature in the database.
  • Parameters:
    • req: *types.FeatureStatus - Pointer to the FeatureStatus struct containing feature data.
  • Returns: *basemodel.ApiError - An ApiError if the operation fails, nil otherwise.

Repo.GetFeature

  • Purpose: Retrieves a feature from the database by its name.
  • Parameters:
    • featureName: string - Name of the feature to retrieve.
  • Returns: (types.FeatureStatus, error) - The FeatureStatus struct and an error if the operation fails. Returns basemodel.ErrFeatureUnavailable if the feature is not found.

Repo.GetAllFeatures

  • Purpose: Retrieves all features from the database.
  • Parameters: None
  • Returns: ([]basemodel.Feature, error) - A slice of Feature structs and an error if the operation fails.

Repo.UpdateFeature

  • Purpose: Updates an existing feature in the database.
  • Parameters:
    • req: types.FeatureStatus - The FeatureStatus struct containing updated feature data.
  • Returns: error - An error if the operation fails, nil otherwise.

Repo.InitFeatures

  • Purpose: Initializes features in the database. Creates a feature if it doesn't exist, otherwise updates it.
  • Parameters:
    • req: []types.FeatureStatus - A slice of FeatureStatus structs containing feature data.
  • Returns: error - An error if the operation fails, nil otherwise.
  1. Code Examples N/A

  2. Clarity and Accuracy The documentation is based on the code provided.

  3. Markdown & MDX Perfection Verified.

  4. Edge Cases To Avoid Breaking MDX Verified.

Include in Getting Started: NO