npg
PostgreSQL Integration
PostgreSQL driver with connection pooling, migrations, prepared statements, LISTEN/NOTIFY, and COPY bulk load.
- r2d2 pool
- Schema migrations
- Prepared statements
- LISTEN/NOTIFY
MongoDB Integration
Databases
nmongo talks to MongoDB from Niao. Connect with a URI, get a database handle, and work with collections as BSON documents, same mental model as the official drivers, just Niao-facing APIs.
It covers everyday CRUD, aggregation pipelines, index management, and multi-document transactions. GridFS is there for large blobs; change streams let you react to inserts and updates in real time instead of polling.
Import
import "nmongo" |
Quick start
import "nmongo" |
|
let db = nmongo.connect("mongodb://localhost:27017") |
let users = db.collection("users") |
users.insert_one({ name: "Niao", role: "admin" }) |
High-level overview of what nmongo is for.
Parse a connection string into pool size, timeouts, TLS, and auth settings instead of passing opaque flags around.
insert_one/many, find, find_one, update, delete, all take filter documents in the usual Mongo style.
Chain $match, $group, $lookup, $project, and the rest for reports and transforms server-side.
Create and drop indexes. Run ACID transactions across multiple documents in a session when consistency matters.
Watch a collection or database and get events as documents change, good for caches and sync jobs.
Store files larger than the 16 MB doc limit, chunked with metadata, for uploads and media.
42 functions and methods in nmongo. Grouped by category from the standard library docs.
| Signature | Description |
|---|---|
| nmongo.connect(opts) | Structured connect (preferred); credentials never logged |
| nmongo.connect_uri(uri) | Connect via MongoDB URI |
| nmongo.close(client) | Close client and invalidate handle |
| nmongo.ping(client) | Server health check |
| nmongo.list_databases(client) | Database names |
| Signature | Description |
|---|---|
| find(client, db, coll, filter?, opts?) | All matching documents |
| find_one(...) | First match or `nil` |
| insert_one(client, db, coll, doc) | `{inserted_id}` |
| insert_many(client, db, coll, docs, opts?) | `{inserted_ids}` |
| update_one/many(...) | `{matched, modified, upserted_id?}` |
| replace_one(...) | Same shape as update |
| delete_one/many(...) | `{deleted_count}` |
| count_documents(...) | Document count |
| distinct(client, db, coll, field, filter?) | Distinct values array |
| Signature | Description |
|---|---|
| aggregate(client, db, coll, pipeline, opts?) | Aggregation pipeline |
| create_index(client, db, coll, keys, opts?) | Returns index name |
| list_indexes(client, db, coll) | Index metadata |
| drop_index(client, db, coll, name) | Drop index |
| list_collections(client, db) | Collection names |
| drop_collection(client, db, coll) | Drop collection |
| Signature | Description |
|---|---|
| start_session(client) | Session handle |
| start_transaction(session, opts?) | Begin transaction |
| commit_transaction(session) | Commit |
| abort_transaction(session) | Abort |
| end_session(session) | End session |
| Signature | Description |
|---|---|
| gridfs_upload(client, db, filename, data, opts?) | Upload string or `byte_array` |
| gridfs_download(client, db, filename, opts?) | Returns `byte_array` |
| gridfs_delete(client, db, filename, opts?) | Delete by filename |
| gridfs_list(client, db, opts?) | List files + metadata |
| Signature | Description |
|---|---|
| watch(client, db, coll, pipeline?, opts?) | Returns watch handle (background thread) |
| watch_next(watch_id) | Next event or `nil` |
| watch_close(watch_id) | Stop stream |
| Signature | Description |
|---|---|
| object_id(hex) | ObjectId for filters/inserts (`{$oid: "..."}`) |
| is_object_id(s) | Validate hex string |
| to_extended_json(value) | Niao value → extended JSON string |
| from_extended_json(s) | Parse extended JSON → Niao value |
| Signature | Description |
|---|---|
| async_find(...) | Background find |
| async_bulk_write(...) | Background bulk insert loop |
| task_done(task) | Poll completion |
| task_wait(task) | Block until done |
| task_result(task) | Result value or error |
| task_cancel(task) | Cancel task |
More from Databases.
PostgreSQL Integration
PostgreSQL driver with connection pooling, migrations, prepared statements, LISTEN/NOTIFY, and COPY bulk load.
SQLite Integration
Embedded SQLite: file-based databases, migrations, prepared statements, transactions, and batch inserts. WAL on by default.