Packages/@pgpm/types

@pgpm/types

Core PostgreSQL data types with deploy/verify/revert SQL scripts

v0.10.0
data-types

Install Package

pgpm install @pgpm/types

Documentation

@pgpm/types

Core PostgreSQL data types with SQL scripts.

Overview

@pgpm/types provides a collection of validated PostgreSQL domain types for common data formats. These domains enforce data integrity at the database level through regex-based validation, ensuring that only properly formatted data is stored.

Features

  • email: Case-insensitive email address validation
  • url: HTTP/HTTPS URL validation
  • hostname: Domain name validation
  • image: JSON-based image metadata with URL and MIME type
  • attachment: JSON-based file attachment metadata with URL and MIME type
  • upload: File upload metadata
  • single_select: Single selection field
  • multiple_select: Multiple selection field

Installation

If you have pgpm installed:

pgpm install @pgpm/types
pgpm deploy

This is a quick way to get started. The sections below provide more detailed installation options.

Prerequisites

# Install pgpm CLI 
npm install -g pgpm

# Start local Postgres (via Docker) and export env vars
pgpm docker start
eval "$(pgpm env)"

Tip: Already running Postgres? Skip the Docker step and just export your PG* environment variables.

Add to an Existing Package

# 1. Install the package
pgpm install @pgpm/types

# 2. Deploy locally
pgpm deploy 

Add to a New Project

# 1. Create a workspace
pgpm init workspace

# 2. Create your first module
cd my-workspace
pgpm init

# 3. Install a package
cd packages/my-module
pgpm install @pgpm/types

# 4. Deploy everything
pgpm deploy --createdb --database mydb1

Usage

Creating Tables with Validated Types

CREATE TABLE customers (
  id serial PRIMARY KEY,
  email email,
  website url,
  domain hostname,
  profile_image image,
  document attachment
);

Email Domain

The email domain validates email addresses using a comprehensive regex pattern and stores them as case-insensitive text (citext).

-- Valid emails
INSERT INTO customers (email) VALUES
  ('user@example.com'),
  ('john.doe@company.co.uk'),
  ('support+tag@service.io');

-- Invalid email (will fail)
INSERT INTO customers (email) VALUES ('not-an-email');
-- ERROR: value for domain email violates check constraint

Validation Pattern: RFC-compliant email format with support for special characters and subdomains.

URL Domain

The url domain validates HTTP and HTTPS URLs.

-- Valid URLs
INSERT INTO customers (website) VALUES
  ('http://example.com'),
  ('https://www.example.com/path?query=value'),
  ('http://foo.bar/path_(with)_parens');

-- Invalid URLs (will fail)
INSERT INTO customers (website) VALUES
  ('ftp://example.com'),  -- Only http/https allowed
  ('example.com'),        -- Missing protocol
  ('http://');            -- Incomplete URL

Validation Pattern: Requires http:// or https:// protocol and valid URL structure.

Hostname Domain

The hostname domain validates domain names without protocol or path.

-- Valid hostnames
INSERT INTO customers (domain) VALUES
  ('example.com'),
  ('subdomain.example.com'),
  ('my-site.co.uk');

-- Invalid hostnames (will fail)
INSERT INTO customers (domain) VALUES
  ('http://example.com'),           -- No protocol allowed
  ('example.com/path'),              -- No path allowed
  ('invalid..domain.com');           -- Invalid format

Validation Pattern: Standard domain name format with support for subdomains and hyphens.

Image and Attachment Domains

The image and attachment domains store JSON objects with URL and MIME type information.

-- Valid image
INSERT INTO customers (profile_image) VALUES
  ('{"url": "https://cdn.example.com/photo.jpg", "mime": "image/jpeg"}'::json);

-- Valid attachment
INSERT INTO customers (document) VALUES
  ('{"url": "https://storage.example.com/file.pdf", "mime": "application/pdf"}'::json);

Structure: Both domains expect JSON objects with url and mime properties.

Domain Types Reference

DomainBase TypeDescriptionExample
emailcitextCase-insensitive email addressuser@example.com
urltextHTTP/HTTPS URLhttps://example.com/path
hostnametextDomain name without protocolexample.com
imagejsonImage metadata with URL and MIME{"url": "...", "mime": "image/jpeg"}
attachmentjsonFile attachment metadata{"url": "...", "mime": "application/pdf"}
uploadtextFile upload identifierVarious formats
single_selecttextSingle selection valueText value
multiple_selecttext[]Multiple selection valuesArray of text values

Validation Benefits

Using domain types provides several advantages over plain text columns:

  1. Data Integrity: Invalid data is rejected at insert/update time
  2. Self-Documenting: Column types clearly indicate expected format
  3. Consistent Validation: Same rules applied across all tables
  4. Database-Level Enforcement: No reliance on application-level validation alone

Dependencies

  • @pgpm/verify: Verification utilities for database objects
  • PostgreSQL citext extension (for email domain)

Testing

pnpm test

The test suite validates:

  • Email format validation (valid and invalid cases)
  • URL format validation with extensive test cases
  • Hostname format validation
  • Image and attachment JSON structure validation

Related Tooling

  • pgpm: 🖥️ PostgreSQL Package Manager for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
  • pgsql-test: 📊 Isolated testing environments with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
  • supabase-test: 🧪 Supabase-native test harness preconfigured for the local Supabase stack—per-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
  • graphile-test: 🔐 Authentication mocking for Graphile-focused test helpers and emulating row-level security contexts.
  • pgsql-parser: 🔄 SQL conversion engine that interprets and converts PostgreSQL syntax.
  • libpg-query-node: 🌉 Node.js bindings for libpg_query, converting SQL into parse trees.
  • pg-proto-parser: 📦 Protobuf parser for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.

Disclaimer

AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.

No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.

Install pgpm CLI

npm install -g pgpm
# Start local Postgres (via Docker)
pgpm docker start
eval "$(pgpm env)"

Workspace Setup

# 1. Create a workspace
pgpm init workspace
cd my-app

# 2. Create your first module
pgpm init
cd packages/your-module

# 3. Install a package
pgpm install @pgpm/types

# 4. Deploy everything
pgpm deploy --createdb --database mydb1