Company
Date Published
Author
Oliver Rice
Word count
1080
Language
English
Hacker News points
134

Summary

PostgreSQL provides a rich and extensible type system that allows developers to create custom data types with validation rules, casting rules, and other features. In this article, we explore how to create a custom `semver` data type to represent Semantic Versioning values, with validation rules to make invalid states unrepresentable. We demonstrate how to store each component of the version as a separate field on a composite type, add validation rules using domains, and use a generated column to persist the text representation of the semver type. The article highlights the importance of aligning business logic with the database to improve performance, throughput, and simplify application code.