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

@juit/pgproxy-utils

v1.1.2

Published

This package provides a number of different utilities for developing and testing with `PGProxy`.

Downloads

593

Readme

PostgreSQL Proxy Utilities

This package provides a number of different utilities for developing and testing with PGProxy.

Test Databases

Few helpers are available to create and drop test databases while developing:

  • testdb(...): return a test database name. An optional parameter can be used to specify the database name prefix (defaults to test).
  • createdb(name, url): actually create a new database, and return its name.
    • name: the name of the database to create, defaults to the value returned by calling testdb().
    • url: the URL to connect to for creating the database, defaults to psql:///postgres (the local PostgreSQL instance via libpq)
  • dropdb(name, url): drop the specified database.
    • name: the name of the database to drop, required.
    • url: the URL to connect to for dropping the database, defaults to psql:///postgres (the local PostgreSQL instance via libpq)

Normally, those methods are used when running tests, in a pattern similar to the following:

let databaseName: string

beforeAll(async () => {
  databaseName = await createdb()
})

afterAll(async () => {
  await dropdb(databasename)
})

it('should run a test', async () => {
  const client = new PGClient(databaseName)
  /// ... use the client to test
})

Database Migrations

The migrate(...) function provides an extremely simplistic way to migrate databases.

Migration files should have names like 001-initial.sql, 002-second.sql, basically stating the migration order followed by a simple name describing it.

All migrations will be recorded in the database using the $migrations table.

The migrate(...) function requires two arguments:

  • url: the URL of the database to migrate, required.
  • options: an optional set of options including:
    • migrations: the directory where migration files reside, relative to the current working directory, defaults to ./sql.
    • additional: an additional set of migrations to be run (for example) migrations required to run unit tests, defaults to undefined.
    • group: a logical name grouping migrations together, when multiple sources of database migrations exist in the same database, defaults to default.

In unit tests, for example, migrations can be applied in the following way:

let databaseName: string

beforeAll(async () => {
  databaseName = await createdb()
  await migrate(databaseName, {
    migrations: './migrations',
    additional: './test/migrations',
  })
})

// run your tests, all migrations will be applied beforehand

Persister Schema Genration

Schema definitions for our Persister models (see @juit/pgproxy-persister) can be generated using a couple of functions:

  • extractSchema(...): extract the Schema definition from a database.
  • serializeSchema(...): serialize the extracted Schema as a Typescript DTS.

The extractSchema(...) function takes a couple of arguments:

  • url: the URL of the database whose schemas are to be extracted, required.
  • schemas: an array of database schema names to extract, defaulting to the single ['public'] schema.

The serializeSchema(...) takes the following arguments:

  • schema: the Schema for which the DTS should be generated, required.
  • id: the exported identifier of the schema, optional, defaults to Schema.
  • overrides: A Record mapping OID numbers to TypeScript types, in case the registry used by the client is capable of handling them. All known OIDs from the @juit/pgproxy-types library are already covered.

An extra couple of utilities are available for the schema extractor:

  • types: a collection of TypeScript types representing the common, well known types converted by PGProxy (e.g. strings, numbers, arrays, ...).
  • helpers: helper functions to generate extra types for serializeSchema:
    • makePostgresArrayType(...): given a type T, it'll return a type representing a postgres array, that is (T | null)[].
    • makeImportType(module, name, args): generate a type imported from the specified module, using the specified type arguments, for example: import('myModule').MyType<MyArg1, MyArg2>