Modules@robinpath/postgres
postgres

@robinpath/postgres

0.1.5Node.jsPublic

PostgreSQL client with connection pooling, parameterized queries, transactions, RETURNING, and LISTEN/NOTIFY

PostgreSQL

PostgreSQL client with connection pooling, parameterized queries, transactions, RETURNING, and LISTEN/NOTIFY

Package: @robinpath/postgres | Category: Database | Type: Utility

Authentication

postgres.connect {"host": "localhost", "user": "postgres", "database": "mydb"}

Call this once at the start of your script before using any other function. Credentials persist for the duration of the script execution.

Use Cases

Use the postgres module when you need to:

  • Execute SQL query -- Use postgres.query to perform this operation
  • Execute query returning single row -- Use postgres.queryOne to perform this operation
  • Insert row with RETURNING -- Use postgres.insert to perform this operation
  • Insert multiple rows -- Use postgres.insertMany to perform this operation
  • Update rows with RETURNING -- Use postgres.update to perform this operation

Quick Reference

FunctionDescriptionReturns
connectConnect to PostgreSQL{name, connected}
queryExecute SQL queryResult rows
queryOneExecute query returning single rowSingle row or null
insertInsert row with RETURNINGInserted row
insertManyInsert multiple rowsInserted rows
updateUpdate rows with RETURNING{affectedRows, rows}
removeDelete rows with RETURNING{affectedRows, rows}
transactionExecute in transaction{success, results}
tablesList tables in schemaTable names
describeDescribe table columnsColumn definitions
countCount rowsCount
listenListen for NOTIFY events{channel, listening}
closeClose connection pooltrue
closeAllClose all poolstrue

Functions

connect

Connect to PostgreSQL

Module: postgres | Returns: object -- {name, connected}

postgres.connect {"host": "localhost", "user": "postgres", "database": "mydb"}
ParameterTypeRequiredDescription
optionsobjectYes{host, port, user, password, database, name, max, ssl}

query

Execute SQL query

Module: postgres | Returns: array -- Result rows

postgres.query "SELECT * FROM users WHERE id = $1" [1]
ParameterTypeRequiredDescription
sqlstringYesSQL with $1 params
paramsarrayNoParameters
connectionstringNoConnection name

queryOne

Execute query returning single row

Module: postgres | Returns: object -- Single row or null

postgres.queryOne "SELECT * FROM users WHERE id = $1" [1]
ParameterTypeRequiredDescription
sqlstringYesSQL
paramsarrayNoParameters
connectionstringNoConnection name

insert

Insert row with RETURNING

Module: postgres | Returns: object -- Inserted row

postgres.insert "users" {"name": "Alice"}
ParameterTypeRequiredDescription
tablestringYesTable
dataobjectYesColumn values
connectionstringNoConnection name

insertMany

Insert multiple rows

Module: postgres | Returns: array -- Inserted rows

postgres.insertMany "users" [{"name": "Alice"}, {"name": "Bob"}]
ParameterTypeRequiredDescription
tablestringYesTable
rowsarrayYesRow objects
connectionstringNoConnection name

update

Update rows with RETURNING

Module: postgres | Returns: object -- {affectedRows, rows}

postgres.update "users" {"name": "Bob"} "id = $1" [1]
ParameterTypeRequiredDescription
tablestringYesTable
dataobjectYesColumns to update
wherestringYesWHERE clause
paramsarrayNoWHERE params
connectionstringNoConnection name

remove

Delete rows with RETURNING

Module: postgres | Returns: object -- {affectedRows, rows}

postgres.remove "users" "id = $1" [1]
ParameterTypeRequiredDescription
tablestringYesTable
wherestringYesWHERE clause
paramsarrayNoWHERE params
connectionstringNoConnection name

transaction

Execute in transaction

Module: postgres | Returns: object -- {success, results}

postgres.transaction [{"sql": "INSERT INTO users (name) VALUES ($1)", "params": ["Alice"]}]
ParameterTypeRequiredDescription
queriesarrayYesArray of {sql, params}
connectionstringNoConnection name

tables

List tables in schema

Module: postgres | Returns: array -- Table names

postgres.tables "public"
ParameterTypeRequiredDescription
schemastringNoSchema (default public)
connectionstringNoConnection name

describe

Describe table columns

Module: postgres | Returns: array -- Column definitions

postgres.describe "users"
ParameterTypeRequiredDescription
tablestringYesTable
connectionstringNoConnection name

count

Count rows

Module: postgres | Returns: number -- Count

postgres.count "users"
ParameterTypeRequiredDescription
tablestringYesTable
wherestringNoWHERE clause
paramsarrayNoWHERE params
connectionstringNoConnection name

listen

Listen for NOTIFY events

Module: postgres | Returns: object -- {channel, listening}

postgres.listen "events"
ParameterTypeRequiredDescription
channelstringYesChannel name
connectionstringNoConnection name

close

Close connection pool

Module: postgres | Returns: boolean -- true

postgres.close
ParameterTypeRequiredDescription
connectionstringNoConnection name

closeAll

Close all pools

Module: postgres | Returns: boolean -- true

postgres.closeAll
ParameterTypeRequiredDescription
(none)NoCall with no arguments

Error Handling

All functions throw on failure. Common errors:

ErrorCause
PostgreSQL connection "..." not found. Call postgres.connect first.Check the error message for details
@desc "Query and validate result"
do
  set $result as postgres.query "SELECT * FROM users WHERE id = $1" [1]
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate en

Retrieve all items and loop through them.

@desc "Listen and iterate results"
do
  set $result as postgres.listen "events"
  each $item in $result
    print $item
  end
enddo

2. Multi-step PostgreSQL workflow

Chain multiple postgres operations together.

@desc "Connect, query, and more"
do
  set $r_connect as postgres.connect {"host": "localhost", "user": "postgres", "database": "mydb"}
  set $r_query as postgres.query "SELECT * FROM users WHERE id = $1" [1]
  set $r_queryOne as postgres.queryOne "SELECT * FROM users WHERE id = $1" [1]
  print "All operations complete"
enddo

3. Safe connect with validation

Check results before proceeding.

@desc "Connect and validate result"
do
  set $result as postgres.connect {"host": "localhost", "user": "postgres", "database": "mydb"}
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • mysql -- MySQL module for complementary functionality
  • mongo -- Mongo module for complementary functionality
  • redis -- Redis module for complementary functionality
  • supabase -- Supabase module for complementary functionality
  • firebase -- Firebase module for complementary functionality

Versions (1)

VersionTagPublished
0.1.5latest1 months ago
Install
$ robinpath add @robinpath/postgres

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.5
LicenseMIT
Unpacked Size11.7 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Category

data