The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
silence prettier
Oliver Ricebad8331e522e
diff --git a/apps/www/_blog/2023-01-25-type-constraints-in-65-lines-of-sql.mdx b/apps/www/_blog/2023-01-25-type-constraints-in-65-lines-of-sql.mdxindex 64122a85df3..32c8d93ef53 100644--- a/apps/www/_blog/2023-01-25-type-constraints-in-65-lines-of-sql.mdx+++ b/apps/www/_blog/2023-01-25-type-constraints-in-65-lines-of-sql.mdx@@ -44,6 +44,7 @@ For our purposes, we’ll assume that the SemVer type is a critical component ofTo that end, we’ll store each component of the version as a separate field on a [composite type](https://www.postgresql.org/docs/current/rowtypes.html).+{/* prettier-ignore */}```sqlcreate type semver_components as (major int,@@ -56,6 +57,7 @@ create type semver_components as (We can create an instance of this type in SQL by casting a tuple as the `semver_components` type.+{/* prettier-ignore */}```sqlselect(1, 2, 3, array['beta', '1'], array['meta'])::semver_components@@ -64,6 +66,7 @@ selectUnfortunately, our definition is far too permissive.+{/* prettier-ignore */}```sqlselect(null, -500, null, array['?'], array[''])::semver_components@@ -86,6 +89,7 @@ We need to add some validation rules to meet our “make invalid states unrepresLet's codify some SemVer rules, layer them on the `semver_components` type, and give the new domain a friendly name.+{/* prettier-ignore */}```sqlcreate domain semveras semver_components@@ -109,6 +113,7 @@ create domain semverwhich references a helper function:+{/* prettier-ignore */}```sqlcreate or replace function semver_elements_match_regex(parts text[],@@ -137,6 +142,7 @@ $$;Now, if we repeat our positive and negative test cases using the `semver` type (vs `semver_components`) we still accept valid states:+{/* prettier-ignore */}```sql-- Success Caseselect@@ -146,6 +152,7 @@ selectwhile invalid states are rejected with an error:+{/* prettier-ignore */}```sql-- Failure Caseselect@@ -158,6 +165,7 @@ selectOur validation doesn’t have to be called manually. The `semver` domain can be used anywhere you’d use the `semver_components` type and the validations are automatically applied.+{/* prettier-ignore */}```sql-- A table with a semver columncreate table package_version(@@ -188,6 +196,7 @@ We’re 48 lines of SQL in and have solved for making invalid states unrepresentNow that our data type is well constrained, you might notice that selecting values from a `semver` typed column returns a tuple, rather than the SemVer string we’re used to seeing.+{/* prettier-ignore */}```sqlselect*@@ -205,6 +214,7 @@ For example: `(2,0,0,"{rc,1}",)` vs `2.0.0-rc.1`We could work around that problem with some [custom casts](https://www.postgresql.org/docs/current/sql-createcast.html), but I’d recommend keeping everything explicit with a function call.+{/* prettier-ignore */}```sqlcreate or replace function semver_to_text(semver)returns text@@ -226,6 +236,7 @@ $$;Which allows us to query the `package_version` table and retrieve a string representation of the data.+{/* prettier-ignore */}```sqlselectid,@@ -243,6 +254,7 @@ id | package_name | verOr, better yet, use a [generated column](https://www.postgresql.org/docs/current/ddl-generated-columns.html)+{/* prettier-ignore */}```sqlcreate table package_version(id bigserial primary key,