Registry / database / postgres-gen-dao

postgres-gen-dao

JSON →
library0.18.2jsnpmunverified

A simple DAO library for PostgreSQL built on postgres-gen and pg, providing basic CRUD operations with automatic marshalling between objects and tables. Current stable version is 0.18.2 (mature, infrequent releases). Key differentiators: uses promise-based query methods compatible with generators, supports object-relation mapping with automatic ID and timestamp handling, offers a ql query language for referencing multiple DAO tables in SQL, and includes upsert functionality that determines insert vs update based on record state. Minimal dependencies and simple API focused on reducing boilerplate for common patterns.

npm install postgres-gen-dao
INSTALL
IMPORT
SIG · POSTGRES-GEN-DAO
P
postgres-gen-dao
databasejavascriptv0.18.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
const dao = require('postgres-gen-dao');
import dao from 'postgres-gen-dao';
This package is CommonJS-only and does not provide ESM exports. Use require() in Node.js.
ql
const { ql } = require('postgres-gen-dao');
const ql = require('postgres-gen-dao/ql');
ql is a named export from the main package, not a subpath export.
dao.init
const dao = require('postgres-gen-dao'); const books = dao({ db: db, table: 'books' });
const books = require('postgres-gen-dao')({ db: db, table: 'books' });
Initialize by calling the default export with config object. The returned object is both the DAO instance and a Promise for initialization.

Initialize postgres-gen-dao with a postgres-gen database connection, perform upsert, find, findOne, and delete operations.

const pg = require('pg'); const postgresGen = require('postgres-gen'); const dao = require('postgres-gen-dao'); const db = postgresGen('postgres://user:password@localhost:5432/mydb', pg); const books = dao({ db: db, table: 'books' }); async function example() { await books.upsert({ author: 'John', title: 'Book1', published: 2020 }); const allBooks = await books.find(); console.log(allBooks); const book = await books.findOne('id = ?', 1); console.log(book); await dao.delete(book); } example().catch(console.error);
Debug
Known issues
gotchaDAO initialization is asynchronous; query methods may produce malformed SQL if used before init completes, especially with ql queries referencing multiple DAOs.
fix
Await the DAO promise before executing dependent queries: const books = await dao({ db, table: 'books' });
affects: all
breakingBreaking change: In version 0.18.0, the signature of dao() changed from (table, db) to ({ db, table }) object configuration.
fix
Use object config: dao({ db, table: 'books' }) instead of dao('books', db).
affects: >=0.18.0
deprecatedKeyless table support is deprecated and partially functional; updates and deletes rely on cloning values into a generated member, which may cause unexpected behavior.
fix
Avoid keyless tables or ensure records are unique and re-fetch before update/delete.
affects: all
gotchaThe upsert method determines insert or update based on whether the DAO loaded the record or required fields are present; this can lead to accidental inserts if object lacks expected keys.
fix
Explicitly use insert() or update() when the operation is known, or ensure objects have required fields (e.g., id).
affects: all
gotchaThe delete method when passed a query string does not perform a dry-run and returns the row count; it cannot be rolled back if used outside a transaction.
fix
Always wrap delete by query in a transaction db.transaction(...) if you need rollback capability.
affects: all
Errors
Common errors & fixes
TypeError: dao is not a function
Trying to import the default export as a named import or using ESM import syntax incorrectly.
fix
Use const dao = require('postgres-gen-dao');
Error: Cannot find module 'postgres-gen'
Postgres-gen peer dependency is not installed.
fix
Run npm install postgres-gen
Error: relation "books" does not exist
The table specified in DAO config does not exist in the database.
fix
Create the table or correct the table name in the DAO config.
Upgrade
Version history
0.18.2latest on npm
Audit
Dependencies
postgres-genrequiredPeer dependency required for database connection and query execution
pgrequiredUnderlying PostgreSQL driver used through postgres-gen
Agent activity
4 hits · last 30 days
node
4
Resources
postgres-gen-dao — npm install postgres-gen-dao · libregistry