nmongo
MongoDB Integration
MongoDB driver with pooling, CRUD, aggregations, indexes, transactions, GridFS, and change streams.
- Connection pooling
- BSON CRUD
- Aggregation pipelines
- Change streams
PostgreSQL Integration
Databases
npg is the Postgres driver. Connections come from an r2d2 pool so you're not opening a new TCP socket per query. Parameterized queries use $1-style placeholders; prepared statements are there when you're running the same SQL in a tight loop.
Migrations follow the same up/down file pattern as the other SQL drivers. LISTEN/NOTIFY gives you pub/sub without Redis; COPY handles bulk loads when INSERT row-by-row would be too slow.
Import
import "npg" |
Quick start
import "npg" |
|
let pool = npg.connect("postgres://user:pass@localhost/mydb") |
let rows = pool.query("SELECT id, name FROM users WHERE active = $1", [true]) |
High-level overview of what npg is for.
r2d2 pool with min/max connections, idle timeout, and health checks before a connection goes back into rotation.
Numbered SQL files applied in order, recorded in a migrations table so prod and staging stay aligned.
Parse once, execute many times with different bind values, saves planning overhead on hot paths.
Subscribe to a channel and wake up when another session NOTIFYs, handy for cache invalidation or job triggers.
Stream rows into a table through Postgres's COPY protocol instead of thousands of single INSERTs.
Kick off a slow query on a background thread and poll for the result without blocking the main VM loop.
52 functions and methods in npg. Grouped by category from the standard library docs.
| Signature | Description |
|---|---|
| npg.connect(url) | Connect via PostgreSQL URL |
| npg.connect_opts(opts) | Object: `host`, `port`, `user`, `password`, `database`, `sslmode`, `connect_timeout`, `application_name`, or `url` |
| npg.close(conn) | Close connection and invalidate handles |
| npg.ping(conn) | `SELECT 1` health check |
| npg.conninfo(conn) | Redacted connection info (password hidden) |
| npg.configure(conn, opts) | Session GUCs: `statement_timeout`, `lock_timeout`, `search_path`, `timezone` |
| npg.server_version(conn) | Server version string |
| npg.is_in_transaction(conn) | Bool (client-tracked transaction state) |
| Signature | Description |
|---|---|
| npg.pool(opts) | Create pool; opts include URL or discrete fields + `max_size`, `min_idle`, `max_lifetime_secs`, `connection_timeout_secs` |
| npg.pool_close(pool) | Drain and close pool |
| npg.pool_get(pool) | Checkout connection handle |
| npg.pool_status(pool) | `{size, idle, in_use}` |
| Signature | Description |
|---|---|
| npg.exec(conn, sql, params?) | DDL/DML without result set; returns affected row count |
| npg.exec_many(conn, sql_list) | Multiple statements in one transaction |
| npg.migrate(conn, migrations) | Apply `{version, sql}` objects in order; tracks `_npg_schema_version` |
| npg.table_exists(conn, schema?, name) | `information_schema` lookup (default schema `public`) |
| npg.list_tables(conn, schema?) | Table names |
| npg.table_info(conn, table, schema?) | Column metadata: `name`, `type`, `nullable`, `default` |
| npg.list_indexes(conn, schema?, table?) | Index metadata |
| Signature | Description |
|---|---|
| npg.query(conn, sql, params?, format?) | All rows; `format` is `"object"` (default) or `"array"` |
| npg.query_row(conn, sql, params?) | First row object or `nil` |
| npg.query_value(conn, sql, params?) | Scalar |
| npg.query_column(conn, sql, params?) | First column of all rows |
| Signature | Description |
|---|---|
| npg.prepare(conn, sql) | Statement handle |
| npg.bind(stmt, index, value) | Positional bind (1-based) |
| npg.stmt_exec(stmt) | Execute without rows |
| npg.stmt_query(stmt, format?) | Execute with rows |
| npg.stmt_reset(stmt) | Clear bindings |
| npg.finalize(stmt) | Free statement |
| Signature | Description |
|---|---|
| npg.begin(conn, opts?) | `isolation`, `read_only`, `deferrable` |
| npg.commit(conn) | Commit |
| npg.rollback(conn) | Rollback |
| npg.savepoint(conn, name) | `SAVEPOINT` |
| npg.rollback_to(conn, name) | `ROLLBACK TO SAVEPOINT` |
| Signature | Description |
|---|---|
| npg.batch(conn, sql, rows) | Repeated exec in one transaction |
| npg.insert(conn, table, data, schema?) | Insert from object; returns inserted row (`RETURNING *`) |
| npg.copy_from(conn, table, columns, rows) | Bulk load via `COPY … FROM STDIN` (CSV) |
| Signature | Description |
|---|---|
| npg.listen(conn, channel) | `LISTEN` |
| npg.unlisten(conn, channel?) | `UNLISTEN` / `UNLISTEN *` |
| npg.notify(conn, channel, payload?) | `pg_notify` |
| npg.poll_notify(conn, timeout_ms?) | Drain pending notifications |
| npg.advisory_lock(conn, key) | Session advisory lock |
| npg.advisory_unlock(conn, key) | Release lock |
| Signature | Description |
|---|---|
| npg_async_exec(conn, sql, params?) | Background write |
| npg_async_query(conn, sql, params?, format?) | Background read |
| npg_task_done(id) | Poll completion |
| npg_task_wait(id) | Block until done |
| npg_task_result(id) | Result or error value |
| npg_task_cancel(id) | Cancel pending task |
| Signature | Description |
|---|---|
| npg.version() | Runtime library version |
| npg.escape_literal(s) | Safe literal quoting |
| npg.quote_ident(name) | Identifier quoting |
More from Databases.
MongoDB Integration
MongoDB driver with pooling, CRUD, aggregations, indexes, transactions, GridFS, and change streams.
SQLite Integration
Embedded SQLite: file-based databases, migrations, prepared statements, transactions, and batch inserts. WAL on by default.