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

@plyo/pgcodebase

v1.6.0

Published

A tool for easy management of postgresql functions, views and triggers

Downloads

770

Readme

Installation

npm install pgcodebase

Usage

Pgcodebase is a tool for easy management of postgresql functions, triggers and views inspired by PgRebase.

const pgcodebase = require('pgcodebase')

pgcodebase({
  user: process.env.PGUSER,
  host: process.env.PGHOST,
  password: process.env.PGPASSWORD,
  database: process.env.PGDATABASE,
  port: process.env.PGPORT,
  dir: path.join(__dirname, 'pgcodebase'),
  schema: 'public',
  createOnly: false,
  dropOnly: false
}).catch(console.error)

pgcodebase call returns Promise.

Console

Usage: npx pgcodebase [options]

Options:

-u, --user <user>                           Postgresql user
-H, --host <host>                           Postgresql host
-p, --password <password>                   Postgresql password
-d, --database <database>                   Postgresql database name
-P, --port <port>                           Postgresql port
-C, --connection-string <connectionString>  Postgresql connection string
-s, --schema <schema>                       Postgresql schema to use
-D, --dir <dir>                             Sql files directory
-c, --create-only                           Only create entities in database, drop nothing
-r, --drop-only                             Only drop entities in database, create nothing
-h, --help                                  output usage information

If createOnly or dropOnly is true, then the script will only create or only drop entities respectively. It is useful to specify dropOnly before table migrations and createOnly after.

You can also configure pgcodebase using .env file. You will have to create .env file in the directory where you execute the command. In .env there may be variables PGUSER, PGHOST, PGPASSWORD, PGDATABASE, PGPORT, PGCODEBASE_CONNECTION_STRING and PGCODEBASE_DIR. Last one should contain an absolute or relative path to the directory with functions, triggers, grants and views. Other variables (except for PGCODEBASE_CONNECTION_STRING) are standard for postgresql.

Problem

Have you ever worked with postgresql functions, triggers, grants and views using migrations? It becomes increasingly complex when your codebase grows. If you want to change the signature of some function, postgresql requires you to drop and recreate all the functions that depend on your function.

Let me demonstrate you on an example. Usually you would create migrations to create functions. Migration 1 and Migration 2. Notice that function foo calls function bar therefore a dependency exists. Then if you need to modify body of the bar you will need to drop and recreate both functions: Migration 3. Imagine now you have multiple functions that depend on bar(). You will have to drop and recreate them all! This process becomes really painful as your codebase grows.

A solution to the problem is a tool that drops and recreates all the functions, triggers and views in the order that respects dependencies. In our example you just have to create a folder and 2 files in it: function_bar and function_foo. Then you can run pgcodebase whenever any change is made in those files.

Internally pgcodebase after resolving dependencies drops all entities and starts filling a table named pgcodebase_dropcodes with sql specified using drop-code comment. Simultaneously it is creating entities in database. To drop entities it executes sql code from pgcodebase_dropcodes table in reverse order.

Code base

All the sql that creates functions, triggers and views has to be in one directory at any depth. dir in config should be the absolute path to this folder. At the start of every sql file there must be a line like

-- drop-code drop function foo(bar integer)

After drop-code there should be a valid sql query that drops the entity created by this sql file.

If, for example, function baz() depends on function foo(bar integer) at the start of the file functions/baz.sql there must be a line

-- require functions/foo.sql

Note that the path is relative to your dir and also drop-code and requires (you can specify multiple) can be in any order at the start of a file.

Also folder require is supported

--- required functions

In this case all folder files would be required as dependencies.

Inspiration

This tool was inspired by PgRebase. PgRebase, however, cannot process dependencies between different types of entities. If you have a view that depends on some function, you are out of luck. Pgcodebase doesn't care about the types of your entities. It can create and drop them in any order specified with requires.