
Company News
/Security News
Socket Selected for OpenAI's Cybersecurity Grant Program
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.
postgres-helper
Advanced tools
The all-in-one postgres module for SQL enjoyers.
After setting up the database connection, simply import sql to use it everywhere:
import sql from "postgres-helper";
async function getPeopleOver(age) {
return await sql`SELECT * FROM person WHERE age > ${age}`;
}
If you use TypeScript, you can utilize the automatically generated types from the migration system to specify the return type of your queries:
import Person from "postgres-helper/types/public/Person";
async function insertPerson(name: string, age: number): Promise<Person> {
const newPerson = await sql<
Person[]
>`INSERT INTO person (name, age) VALUES (${name}, ${age}) RETURNING *`;
return newPerson[0];
}
The simple dynamic query builder conditionally appends/omits query fragments. It works by nesting sql`` fragments within other sql`` calls or fragments. This allows you to build dynamic queries safely without risking sql injections through usual string concatenation.
const olderThan = x => sql`and age > ${ x }`
const filterAge = true
sql`
select
*
from users
where name is not null ${
filterAge
? olderThan(50)
: sql``
}
`
// Which results in:
select * from users where name is not null
// Or
select * from users where name is not null and age > 50
sql`
select
*
from users ${
id
? sql`where user_id = ${ id }`
: sql``
}
`
// Which results in:
select * from users
// Or
select * from users where user_id = $1
Using keywords or calling functions dynamically is also possible by using sql`` fragments.
const date = null
sql`
update users set updated_at = ${ date || sql`now()` }
`
// Which results in:
update users set updated_at = now()
Dynamic identifiers like table names and column names is also supported like so:
const table = 'users'
, column = 'id'
sql`
select ${ sql(column) } from ${ sql(table) }
`
// Which results in:
select "id" from "users"
Here's a quick oversight over all the ways to do interpolation in a query template string:
| Interpolation syntax | Usage | Example |
|---|---|---|
${ sql`` } | for keywords or sql fragments | sql`SELECT * FROM users ${sql`order by age desc` }` |
${ sql(string) } | for identifiers | sql`SELECT * FROM ${sql('table_name')` |
${ sql([] or {}, ...) } | for helpers | sql`INSERT INTO users ${sql({ name: 'Peter'})}` |
${ 'somevalue' } | for values | sql`SELECT * FROM users WHERE age = ${42}` |
For the full documentation on how to use the sql function, check out the postgres docs.
Before you can start querying your database, you need some tables. You can create them using the inbuilt migration system.
Creating a migration:
npx postgres-helper new <migration-name>
The generated migration file will look like this, allowing you to migrate with the syntax you already know from your queries (sql is available out of the box in migration files, no need to import it):
exports.up = async (sql) => {
await sql`
CREATE TABLE person (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
age INT NOT NULL
);
`;
};
exports.down = async (sql) => {
await sql`
DROP TABLE person;
`;
};
Running migrations:
npx postgres-helper up
Rolling back a migration:
npx postgres-helper down
For an overview over all available migration commands and arguments, run:
npx postgres-helper --help
If the outputPath is set correctly in your configuration, postgres-helper will automatically introspect the database and generate types for you after each migration. The generated types will reflect the current state of your database (not only the content of your migrations).
As the generated types are overwritten after each migration, you should not edit them in the outputPath directory.
To generate types independently of migrations, you can run:
npx postgres-helper typegen
If you need to go in-depth with the generated types, you can refer to the kanel documentation.
Install the package:
npm install postgres-helper
Run the initialization command (if you want automatically generated types, add the --typescript flag):
npx postgres-helper init --typescript
Edit the generated phconfig.js file in the postgres-helper directory to match your database configuration
The phconfig.js file looks like this:
module.exports = {
connection: {
host: "localhost",
port: 5432,
database: "database",
username: "username",
password: "password",
},
migrationPath: "./postgres-helper/migrations",
outputPath: "./postgres-helper/types",
};
The connection object needs to match your database configuration. The default options are generally adequate, but if you require additional customization, you can refer to the advanced configuration options. Simply add the desired options to the connection object.
The migrationPath is the path to the directory where your migration files are stored. Just keep the default. Please.
The outputPath is the path to the directory where the generated types are stored. If you didn't set the --typescript flag when running the initialization command, this line won't exist in your config and postgres-helper will not generate types for you.
To the authors of the postgres, kanel and ley packages on which this module is built.
FAQs
All-in-one Node.js PostgreSQL module
The npm package postgres-helper receives a total of 10 weekly downloads. As such, postgres-helper popularity was classified as not popular.
We found that postgres-helper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.

Security News
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.