Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Converting ULID Binary Data to String in Rails #39

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ukmshi
Copy link

@ukmshi ukmshi commented Jan 24, 2024

Description

This PR introduces new functionality to the ULID module, aimed at simplifying the interaction with ULID values stored as binary in MySQL databases when using Rails. Working directly with binary formats in Rails can be cumbersome, especially when dealing with model attributes. To address this, we've added methods to convert ULID values between binary and string representations seamlessly.

Background

Our application utilizes ULIDs as a more sortable and time-precise alternative to UUIDs. These ULIDs are generated and stored in a MySQL database in their binary form to save space and ensure efficient querying. However, Rails' native handling of binary data requires verbose and repetitive conversion code, hindering developer productivity and increasing the risk of errors.

Implementation

To address this issue, we've implemented a set of utility functions within the ULID module, specifically within the Generator submodule. These functions are designed to seamlessly integrate with Rails' ActiveRecord, allowing for the automatic conversion of binary ULID data to strings when records are loaded from the database, and vice versa when data is saved.

Key Additions:
binary_to_ulid_string: Converts binary ULID data to a string.
ulid_string_to_binary: Converts a ULID string back to binary format.

These methods extend the existing ULID module's functionality, making it more compatible with Rails applications and simplifying the handling of ULID identifiers.

Usage Example

To leverage the new functionality, we recommend using a custom ActiveRecord type. This approach encapsulates the conversion logic, ensuring that ULID fields are automatically handled throughout the application.

Here's how to define a custom type for ULID:

# app/types/ulid_type.rb
class ULIDType < ActiveRecord::Type::Binary
  def cast(value)
    value
  end

  def deserialize(value)
    raw_value = super(value)
    return if raw_value.nil?

    ULID.binary_to_ulid_string(raw_value.to_s)
  end

  def serialize(value)
    return if value.nil?

    raw_value = ULID.string_to_ulid_byte(value)
    super(raw_value)
  end
end

# Register the new type for use with ActiveRecord
ActiveRecord::Type.register(:ulid, ULIDType)

With this custom type in place, you can now declare ULID attributes in your models like so:

class MyModel < ApplicationRecord
  attribute :ulid, :ulid
end

This setup ensures that ULID attributes are automatically converted to strings when accessed and converted back to binary when saved to the database, simplifying the use of ULID in a Rails context.

Conclusion

By integrating these utility functions into the ULID module and leveraging ActiveRecord's custom types, we can significantly streamline the handling of ULID identifiers in Rails applications. This enhancement not only improves developer efficiency but also maintains the integrity and performance benefits of storing ULIDs in their binary form in the database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant