Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/richytong/postgres-extended
https://github.com/richytong/postgres-extended
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/richytong/postgres-extended
- Owner: richytong
- Created: 2018-07-29T08:17:14.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-07-30T22:09:09.000Z (over 6 years ago)
- Last Synced: 2024-11-06T14:16:59.141Z (about 2 months ago)
- Language: JavaScript
- Size: 86.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Postgres Extended
Sweet API wrapper for Postgres on top of `pg-pool`.# Usage
```Javascript
import Postgres from 'postgres-extended';const { database, close } = Postgres({
host: YOUR_HOST,
port: YOUR_PORT,
user: YOUR_USER,
password: YOUR_PASSWORD,
database: YOUR_DB_NAME,
});// fetch users with array variables and index interpolation
const users = await database.query(`
SELECT * FROM users WHERE userId = $1
`,['1']);// fetch users with object variables and name interpolation
const users = await database.query(`
SELECT * FROM users WHERE userId = @userId
`, { userId: '1' });// insert
const userRowToInsert = {
userId: '2',
name: 'manting',
email: '[email protected]',
created: Date.now(),
updated: Date.now(),
};
const rowsYouJustInserted = await database.table('users').insert(userRowToInsert);// upsert
const userRowToUpsert = {
userId: '3',
name: 'tommy',
email: '[email protected]',
created: Date.now(),
updated: Date.now(),
};
const rowsYouJustUpserted = await database.table('users').upsert(userRowToUpsert, {
conflictTarget: 'userId', // (required) look for conflicts on userId, use array for multiple conflict lookup columns
excludeOnConflict: 'created', // don't overwrite created timestamp when you update, use array for multiple rows excluded
})// update
// why update when you can upsert?// when you're done
await close();
```