npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

pgterra

v0.2.19

Published

Declarative schema management for Postgres

Downloads

242

Readme

Terra

Declarative PostgreSQL schema management.

Install

npm install -g pgterra

Usage

1. Create schema.sql:

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  email VARCHAR(255) NOT NULL UNIQUE
);

2. Preview changes:

pgterra plan

3. Apply:

pgterra apply

4. Update schema.sql:

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  email VARCHAR(255) NOT NULL UNIQUE,
  name VARCHAR(100) NOT NULL,        -- added
  active BOOLEAN DEFAULT true        -- added
);

CREATE TABLE posts (                 -- added
  id SERIAL PRIMARY KEY,
  title VARCHAR(255) NOT NULL,
  user_id INTEGER NOT NULL,
  CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id)
);

CREATE INDEX idx_user_email ON users (LOWER(email));  -- added

5. Terra generates the ALTER statements:

$ pgterra plan
ALTER TABLE users ADD COLUMN name VARCHAR(100) NOT NULL
ALTER TABLE users ADD COLUMN active BOOLEAN DEFAULT true
CREATE TABLE posts (...)
CREATE INDEX idx_user_email ON users (LOWER(email))

$ pgterra apply

Configuration

Database connection via DATABASE_URL or individual variables:

export DATABASE_URL="postgres://user:password@localhost:5432/mydb"

Or:

export DB_HOST=localhost
export DB_PORT=5432
export DB_NAME=mydb
export DB_USER=postgres
export DB_PASSWORD=password

What's supported

Tables & Columns: All PostgreSQL column types, default values, NOT NULL constraints

Functions & Procedures: User-defined functions and procedures with full PostgreSQL feature support

Triggers: Table triggers with BEFORE/AFTER/INSTEAD OF timing

Sequences: Custom sequences with configurable properties

Constraints:

-- Primary keys
id SERIAL PRIMARY KEY

-- Foreign keys with actions
CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE

-- Check constraints
CONSTRAINT check_positive CHECK (quantity > 0)

-- Unique constraints
email VARCHAR(255) UNIQUE
CONSTRAINT unique_email UNIQUE (email, domain)

Indexes:

-- Basic
CREATE INDEX idx_email ON users (email);

-- Partial
CREATE INDEX idx_active_users ON users (email) WHERE active = true;

-- Expression
CREATE INDEX idx_lower_email ON users (LOWER(email));

-- Concurrent (built automatically when safe)

ENUM types:

CREATE TYPE status AS ENUM ('pending', 'active', 'inactive');

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  status status NOT NULL
);

Views:

CREATE VIEW active_users AS
SELECT id, email FROM users WHERE active = true;

CREATE MATERIALIZED VIEW user_stats AS
SELECT COUNT(*) as total FROM users;

Functions:

CREATE FUNCTION calculate_total(quantity INT, price DECIMAL)
RETURNS DECIMAL
AS $$
  SELECT quantity * price
$$
LANGUAGE SQL IMMUTABLE;

Procedures:

CREATE PROCEDURE archive_old_posts(days_old INT)
LANGUAGE SQL
AS $$
  DELETE FROM posts WHERE created_at < NOW() - INTERVAL '1 day' * days_old;
$$;

Triggers:

-- First create a trigger function
CREATE FUNCTION update_modified_timestamp()
RETURNS TRIGGER
AS $$
  BEGIN
    NEW.modified_at = NOW();
    RETURN NEW;
  END;
$$
LANGUAGE plpgsql;

-- Then create the trigger
CREATE TRIGGER set_modified_timestamp
BEFORE UPDATE ON users
FOR EACH ROW
EXECUTE FUNCTION update_modified_timestamp();

Sequences:

CREATE SEQUENCE custom_id_seq
START 1000
INCREMENT 1
CACHE 20;

Commands

pgterra plan                    # Preview changes
pgterra plan -f custom.sql      # Use custom schema file
pgterra apply                   # Apply changes
pgterra apply -f custom.sql     # Apply from custom file

Development

Requires Bun:

git clone https://github.com/elitan/terra.git
cd terra
bun install

# Start test database
docker compose up -d

# Run tests
export DATABASE_URL="postgres://test_user:test_password@localhost:5487/sql_terraform_test"
bun test

License

MIT