Adds type definitions, validations, and default values to ActiveRecord::Store for structured JSON/HStore columns in Rails.
ActiveRecord::TypedStore is a Ruby gem that extends ActiveRecord::Store with type definitions, validations, and default values for JSON or HStore database columns in Rails applications. It provides type safety and ActiveModel attribute behavior while maintaining the flexibility of schema-less storage, eliminating the need for migrations when adding or removing attributes.
Rails developers using PostgreSQL JSON/HStore columns or similar schema-less storage who need type safety, validations, and default values without sacrificing flexibility.
Developers choose ActiveRecord::TypedStore because it brings ActiveRecord's familiar migration syntax and type casting to schema-less storage, reducing bugs with type safety while keeping the agility of not requiring database migrations for attribute changes.
ActiveRecord::Store but with type definition
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Mimics ActiveRecord migration syntax, making it intuitive for Rails developers to define typed attributes without a new DSL, as shown in the usage example with familiar methods like s.string and s.boolean.
Supports boolean, string, integer, datetime, array, and custom types with automatic type casting, ensuring data consistency as demonstrated in the example where 'age' is cast from string to integer.
Integrates with ActiveModel for validations, change tracking, and attribute methods like *_changed?, allowing developers to use Rails patterns, such as validating age with presence true.
Works with any custom coder, JSON columns, or Postgres HStore, providing adaptability to different database setups as detailed in the serialization methods section.
As admitted in the README, attributes cannot be queried in SQL unless using JSON or Postgres HStore types, which restricts database-level data manipulation and indexing.
When using Postgres HStore, array attributes don't work and any types are converted to strings, limiting functionality as explicitly stated in the HStore limitations section.
Type casting and validations are handled at the application level, which could introduce performance overhead compared to database-native type constraints, especially for large-scale operations.