@onurege3467/pg-adapter
v1.2.0
Published
PostgreSQL database adapter with caching, hooks, and transactions
Maintainers
Readme
@onurege3467/pg-adapter
PostgreSQL database adapter with caching, lifecycle hooks, and transactions.
Installation
npm install @onurege3467/pg-adapter
npm install pgOptional (for Redis caching):
npm install redisUsage
import { createDatabase } from '@onurege3467/pg-adapter';
// Using connection string (e.g., for Neon or Heroku)
const db = createDatabase({
config: {
connectionString: 'postgres://user:pass@localhost:5432/mydb',
// SSL configuration when needed
ssl: { rejectUnauthorized: false }
}
});
// CRUD
await db.insert('users', { name: 'John', email: '[email protected]' });
await db.select('users', { where: { name: 'John' } });
await db.update('users', { name: 'Jane' }, { id: 1 });
await db.delete('users', { id: 1 });
// Upsert
await db.set('settings', { theme: 'dark' }, { key: 'theme' });
// Bulk operations
await db.bulkInsert('users', [{ name: 'A' }, { name: 'B' }]);
// Atomic counters
await db.increment('wallets', { balance: 100 }, { user_id: 1 });
await db.decrement('inventory', { stock: 1 }, { sku: 'PRO-123' });
// Lifecycle hooks
db.on('beforeInsert', (table, data) => {
console.log('Inserting into', table, data);
});
// Transactions
await db.transaction(async () => {
await db.insert('orders', { total: 100 });
await db.update('inventory', { stock: 10 }, { product_id: 1 });
});
// Close connection
await db.close();With Caching
const db = createDatabase({
config: {
host: 'localhost',
database: 'mydb',
username: 'user',
password: 'pass',
cache: {
type: 'memory', // or 'redis'
ttl: 60000 // 1 minute
}
}
});API
insert(table, data)- Insert a recordselect(table, where)- Select multiple recordsselectOne(table, where)- Select single recordupdate(table, data, where)- Update recordsdelete(table, where)- Delete recordsset(table, data, where)- Upsert (update or insert)bulkInsert(table, dataArray)- Bulk insertincrement(table, incs, where)- Atomic incrementdecrement(table, decs, where)- Atomic decrementtransaction(fn)- Run function in transactionon(hook, fn)- Register lifecycle hookgetMetrics()- Get performance metricsclose()- Close database connection
Hooks
beforeInsertafterInsertbeforeUpdateafterUpdatebeforeDeleteafterDelete
