Package Exports
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@pgpm/types) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@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 deployThis 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 mydb1Usage
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 constraintValidation 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 URLValidation 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 formatValidation 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
| Domain | Base Type | Description | Example |
|---|---|---|---|
email |
citext |
Case-insensitive email address | user@example.com |
url |
text |
HTTP/HTTPS URL | https://example.com/path |
hostname |
text |
Domain name without protocol | example.com |
image |
json |
Image metadata with URL and MIME | {"url": "...", "mime": "image/jpeg"} |
attachment |
json |
File attachment metadata | {"url": "...", "mime": "application/pdf"} |
upload |
text |
File upload identifier | Various formats |
single_select |
text |
Single selection value | Text value |
multiple_select |
text[] |
Multiple selection values | Array of text values |
Validation Benefits
Using domain types provides several advantages over plain text columns:
- Data Integrity: Invalid data is rejected at insert/update time
- Self-Documenting: Column types clearly indicate expected format
- Consistent Validation: Same rules applied across all tables
- Database-Level Enforcement: No reliance on application-level validation alone
Dependencies
@pgpm/verify: Verification utilities for database objects- PostgreSQL
citextextension (for email domain)
Testing
pnpm testThe 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.