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 🙏

© 2024 – Pkg Stats / Ryan Hefner

pgstrap

v1.0.5

Published

Bootstrap patterns for Typescript/Postgres projects

Downloads

235

Readme

pgstrap

npm version License: MIT

pgstrap is a powerful bootstrapping utility for Postgres/TypeScript projects, designed to streamline your database management workflow and enhance type safety.

Features

  • 🚀 Quick setup with TypeScript support
  • 📁 Organized migration structure in [src]/db/migrations
  • 🔧 "Standard Shorthands" for UUIDs and created_at timestamps
  • 🧪 Automated migration runs in tests
  • 🔄 Automatic type generation for Zapatos and Kysely
  • 🔁 Database reset and migration scripts
  • 📊 Automatic schema dumps with [src]/db/zapatos
  • 🏗️ Database structure dumps in [src]/db/structure

Installation

npm install pgstrap --save-dev

Quick Start

  1. Initialize pgstrap in your project:

    npx pgstrap init
  2. This will set up the necessary configuration and scripts in your package.json.

  3. Create your first migration:

    npm run db:create-migration my-first-migration
  4. Edit the migration file in src/db/migrations/.

  5. Run the migration:

    npm run db:migrate
  6. Generate types and structure:

    npm run db:generate

Usage

Available Scripts

  • npm run db:migrate - Run pending migrations
  • npm run db:reset - Drop and recreate the database, then run all migrations
  • npm run db:generate - Generate types and structure dumps
  • npm run db:create-migration - Create a new migration file

Configuration

After running pgstrap init, you'll find a pgstrap.config.js file in your project root. Customize it as needed:

module.exports = {
  defaultDatabase: "mydb",
  schemas: ["public"],
  dbDir: "./src/db", // optional, defaults to "./src/db"
}

Writing Migrations

Migrations in pgstrap are written using node-pg-migrate, which provides a powerful and flexible way to define database changes.

To create a new migration:

npm run db:create-migration my-migration-name

This will create a new file in src/db/migrations/ with a timestamp prefix.

Migration Structure

A typical migration file looks like this:

import { MigrationBuilder, ColumnDefinitions } from "node-pg-migrate"

export const shorthands: ColumnDefinitions | undefined = undefined

export async function up(pgm: MigrationBuilder): Promise<void> {
  // Your migration code here
}

export async function down(pgm: MigrationBuilder): Promise<void> {
  // Code to revert your migration (optional)
}

Example Migration

Here's an example of a migration that creates a new table:

export async function up(pgm: MigrationBuilder): Promise<void> {
  pgm.createTable("users", {
    id: "id",
    username: { type: "text", notNull: true },
    email: { type: "text", notNull: true, unique: true },
    created_at: {
      type: "timestamptz",
      notNull: true,
      default: pgm.func("current_timestamp"),
    },
  })

  pgm.createIndex("users", "username")
}

export async function down(pgm: MigrationBuilder): Promise<void> {
  pgm.dropTable("users")
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • This project was originally developed as seam-pgm by Seam Labs Inc. in 2024.
  • Special thanks to the Zapatos and Kysely projects for their excellent TypeScript database tools.

Support

If you encounter any issues or have questions, please file an issue on the GitHub repository.

Happy coding with pgstrap! 🚀