Showing preview only (707K chars total). Download the full file or copy to clipboard to get everything.
Repository: porsager/postgres
Branch: master
Commit: 5c8135f3df1b
Files: 81
Total size: 678.4 KB
Directory structure:
gitextract_gowjwbrv/
├── .eslintrc.json
├── .github/
│ └── workflows/
│ └── test.yml
├── CHANGELOG.md
├── README.md
├── UNLICENSE
├── cf/
│ ├── polyfills.js
│ ├── src/
│ │ ├── bytes.js
│ │ ├── connection.js
│ │ ├── errors.js
│ │ ├── index.js
│ │ ├── large.js
│ │ ├── query.js
│ │ ├── queue.js
│ │ ├── result.js
│ │ ├── subscribe.js
│ │ └── types.js
│ └── test.js
├── cjs/
│ ├── package.json
│ ├── src/
│ │ ├── bytes.js
│ │ ├── connection.js
│ │ ├── errors.js
│ │ ├── index.js
│ │ ├── large.js
│ │ ├── query.js
│ │ ├── queue.js
│ │ ├── result.js
│ │ ├── subscribe.js
│ │ └── types.js
│ └── tests/
│ ├── bootstrap.js
│ ├── copy.csv
│ ├── index.js
│ ├── pg_hba.conf
│ ├── select-param.sql
│ ├── select.sql
│ └── test.js
├── deno/
│ ├── README.md
│ ├── mod.js
│ ├── package.json
│ ├── polyfills.js
│ ├── src/
│ │ ├── bytes.js
│ │ ├── connection.js
│ │ ├── errors.js
│ │ ├── index.js
│ │ ├── large.js
│ │ ├── query.js
│ │ ├── queue.js
│ │ ├── result.js
│ │ ├── subscribe.js
│ │ └── types.js
│ ├── tests/
│ │ ├── bootstrap.js
│ │ ├── copy.csv
│ │ ├── index.js
│ │ ├── pg_hba.conf
│ │ ├── select-param.sql
│ │ ├── select.sql
│ │ └── test.js
│ └── types/
│ └── index.d.ts
├── package.json
├── src/
│ ├── bytes.js
│ ├── connection.js
│ ├── errors.js
│ ├── index.js
│ ├── large.js
│ ├── query.js
│ ├── queue.js
│ ├── result.js
│ ├── subscribe.js
│ └── types.js
├── tests/
│ ├── bootstrap.js
│ ├── copy.csv
│ ├── index.js
│ ├── pg_hba.conf
│ ├── select-param.sql
│ ├── select.sql
│ └── test.js
├── transpile.cf.js
├── transpile.cjs
├── transpile.deno.js
└── types/
├── index.d.ts
├── package.json
└── tsconfig.json
================================================
FILE CONTENTS
================================================
================================================
FILE: .eslintrc.json
================================================
{
"root": true,
"env": {
"es2020": true,
"node": true
},
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"rules": {
"comma-dangle": 2,
"no-cond-assign": 2,
"no-console": 1,
"no-constant-condition": 2,
"no-control-regex": 2,
"no-debugger": 2,
"no-dupe-args": 2,
"no-dupe-keys": 2,
"no-duplicate-case": 2,
"no-empty": 2,
"no-empty-character-class": 2,
"no-ex-assign": 2,
"no-extra-boolean-cast": 2,
"no-extra-semi": 2,
"no-func-assign": 2,
"no-inner-declarations": 2,
"no-invalid-regexp": 2,
"no-irregular-whitespace": 2,
"no-negated-in-lhs": 0,
"no-obj-calls": 2,
"no-regex-spaces": 2,
"no-sparse-arrays": 2,
"no-unexpected-multiline": 2,
"no-unreachable": 2,
"use-isnan": 2,
"valid-typeof": 2,
"accessor-pairs": 2,
"array-callback-return": 0,
"consistent-return": 0,
"curly": [
2,
"multi-or-nest",
"consistent"
],
"default-case": 2,
"dot-location": [
2,
"property"
],
"dot-notation": [
2,
{
"allowPattern": "^[a-z]+(_[a-z]+)+$"
}
],
"eqeqeq": [
"error",
"always",
{
"null": "ignore"
}
],
"no-alert": 2,
"no-caller": 2,
"no-case-declarations": 2,
"no-div-regex": 2,
"no-else-return": 2,
"no-empty-function": 2,
"no-empty-pattern": 2,
"no-eq-null": 0,
"no-eval": 2,
"no-extend-native": 2,
"no-extra-bind": 2,
"no-extra-label": 2,
"no-fallthrough": 2,
"no-floating-decimal": 2,
"no-implicit-coercion": 0,
"no-implicit-globals": 2,
"no-implied-eval": 2,
"no-invalid-this": 2,
"no-iterator": 2,
"no-labels": 2,
"no-lone-blocks": 2,
"no-loop-func": 2,
"no-magic-numbers": 0,
"no-multi-spaces": [
2,
{
"ignoreEOLComments": true,
"exceptions": {
"Array": true,
"Property": true,
"VariableDeclarator": true,
"ImportDeclaration": true,
"TernaryExpressions": true,
"Comments": true
}
}
],
"no-multi-str": 2,
"no-native-reassign": 2,
"no-new": 2,
"no-new-func": 2,
"no-new-wrappers": 2,
"no-octal": 2,
"no-octal-escape": 2,
"no-param-reassign": 0,
"no-proto": 2,
"no-redeclare": 2,
"no-return-assign": 0,
"no-script-url": 2,
"no-self-assign": 2,
"no-self-compare": 2,
"no-sequences": 0,
"no-throw-literal": 2,
"no-unmodified-loop-condition": 2,
"no-unused-expressions": 0,
"no-unused-labels": 2,
"no-useless-call": 2,
"no-useless-concat": 2,
"no-useless-escape": 2,
"no-void": 2,
"no-with": 2,
"wrap-iife": 2,
"no-delete-var": 2,
"no-label-var": 2,
"no-restricted-globals": 2,
"no-shadow": 0,
"no-shadow-restricted-names": 2,
"no-undef": 2,
"no-undef-init": 2,
"no-unused-vars": 2,
"no-use-before-define": [
2,
{
"functions": false,
"variables": false
}
],
"callback-return": 0,
"global-require": 2,
"handle-callback-err": 2,
"no-mixed-requires": 2,
"no-new-require": 2,
"no-path-concat": 2,
"no-process-env": 2,
"no-process-exit": 2,
"array-bracket-spacing": [
2,
"never"
],
"block-spacing": [
2,
"always"
],
"brace-style": [
2,
"1tbs",
{
"allowSingleLine": true
}
],
"camelcase": 0,
"comma-spacing": 2,
"comma-style": [
2,
"first",
{
"exceptions": {
"ArrayExpression": true,
"ObjectExpression": true
}
}
],
"consistent-this": [
2,
"self"
],
"eol-last": 2,
"indent": [
2,
2,
{
"MemberExpression": "off",
"flatTernaryExpressions": true,
"VariableDeclarator": {
"const": 2
},
"FunctionExpression": {
"parameters": "first"
},
"CallExpression": {
"arguments": "off"
},
"ArrayExpression": "first",
"ObjectExpression": "first"
}
],
"key-spacing": [
0,
{
"beforeColon": false,
"afterColon": true,
"mode": "minimum"
}
],
"keyword-spacing": 2,
"linebreak-style": 2,
"lines-around-comment": 0,
"max-depth": [
2,
5
],
"max-len": [
2,
150
],
"max-nested-callbacks": [
2,
5
],
"max-params": [
2,
5
],
"max-statements-per-line": 0,
"new-cap": [
2,
{
"capIsNew": false
}
],
"new-parens": 2,
"newline-after-var": 0,
"newline-before-return": 0,
"no-array-constructor": 2,
"no-bitwise": 0,
"no-continue": 2,
"no-lonely-if": 2,
"no-mixed-spaces-and-tabs": 2,
"no-negated-condition": 0,
"no-new-object": 2,
"no-spaced-func": 2,
"no-trailing-spaces": 1,
"no-unneeded-ternary": 2,
"no-whitespace-before-property": 2,
"object-curly-spacing": [
2,
"always"
],
"one-var-declaration-per-line": [
2,
"always"
],
"quote-props": [
2,
"as-needed"
],
"quotes": [
2,
"single"
],
"semi": [
2,
"never"
],
"space-before-blocks": 2,
"space-before-function-paren": [
2,
"never"
],
"space-infix-ops": 2,
"space-unary-ops": 2,
"spaced-comment": 2,
"arrow-spacing": 2,
"constructor-super": 2,
"no-class-assign": 2,
"no-confusing-arrow": 0,
"no-const-assign": 2,
"no-dupe-class-members": 2,
"no-duplicate-imports": 2,
"no-new-symbol": 2,
"no-this-before-super": 2,
"no-useless-constructor": 2,
"no-var": 2,
"object-shorthand": 0,
"prefer-arrow-callback": 0,
"prefer-const": 2,
"prefer-rest-params": 0,
"prefer-spread": 0
}
}
================================================
FILE: .github/workflows/test.yml
================================================
name: test
on: [push, pull_request]
jobs:
test:
name: Node v${{ matrix.node }} on PostgreSQL v${{ matrix.postgres }}
strategy:
fail-fast: false
matrix:
node: ['12', '14', '16', '18', '20', '21', '22', '23', '24']
postgres: ['12', '13', '14', '15', '16', '17']
runs-on: ubuntu-latest
services:
postgres:
image: postgres:${{ matrix.postgres }}
env:
POSTGRES_USER: postgres
POSTGRES_HOST_AUTH_METHOD: trust
ports:
- 5433:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- run: |
date
sudo apt purge postgresql-16
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install "postgresql-${{ matrix.postgres }}"
sudo cp ./tests/pg_hba.conf /etc/postgresql/${{ matrix.postgres }}/main/pg_hba.conf
sudo sed -i 's/.*wal_level.*/wal_level = logical/' /etc/postgresql/${{ matrix.postgres }}/main/postgresql.conf
sudo sed -i 's/.*max_prepared_transactions.*/max_prepared_transactions = 100/' /etc/postgresql/${{ matrix.postgres }}/main/postgresql.conf
sudo sed -i 's/.*ssl = .*/ssl = on/' /etc/postgresql/${{ matrix.postgres }}/main/postgresql.conf
openssl req -new -x509 -nodes -days 365 -text -subj "/CN=localhost" -extensions v3_req -config <(cat /etc/ssl/openssl.cnf <(printf "\n[v3_req]\nbasicConstraints=critical,CA:TRUE\nkeyUsage=nonRepudiation,digitalSignature,keyEncipherment\nsubjectAltName=DNS:localhost")) -keyout server.key -out server.crt
sudo cp server.key /etc/postgresql/${{ matrix.postgres }}/main/server.key
sudo cp server.crt /etc/postgresql/${{ matrix.postgres }}/main/server.crt
sudo chmod og-rwx /etc/postgresql/${{ matrix.postgres }}/main/server.key
sudo systemctl start postgresql.service
sudo systemctl status postgresql.service
pg_isready
sudo -u postgres psql -c "SHOW hba_file;"
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm test
env:
PGUSER: postgres
PGSOCKET: /var/run/postgresql
================================================
FILE: CHANGELOG.md
================================================
# Changelog
## v3.2.4 - 25 May 2022
- Allow setting keep_alive: false bee62f3
- Fix support for null in arrays - fixes #371 b04c853
## v3.2.3 - 23 May 2022
- Fix Only use setKeepAlive in Deno if available 28fbbaf
- Fix wrong helper match on multiple occurances 02f3854
#### Typescript related
- Fix Deno assertRejects compatibility (#365) 0f0af92
- Fix include missing boolean type in JSONValue union (#373) 1817387
## v3.2.2 - 15 May 2022
- Properly handle errors thrown on commit 99ddae4
## v3.2.1 - 15 May 2022
- Exclude target_session_attrs from connection obj 43f1442
## v3.2.0 - 15 May 2022
- Add `sslmode=verify-full` support e67da29
- Add support for array of fragments 342bf55
- Add uri decode of host in url - fixes #346 1adc113
- Add passing of rest url params to connection (ootb support cockroach urls) 41ed84f
- Fix Deno partial writes 452a30d
- Fix `as` dynamic helper 3300c40
- Fix some nested fragments usage 9bfa902
- Fix missing columns on `Result` when using simple protocol - fixes #350 1e2e298
- Fix fragments in transactions - fixes #333 75914c7
#### Typescript related
- Upgrade/fix types (#357) 1e6d312
- Add optional `onlisten` callback to `listen()` on TypeScript (#360) 6b749b2
- Add implicit custom type inference (#361) 28512bf
- Fix and improve sql() helper types (#338) c1de3d8
- Fix update query type def for `.writable()` and `.readable()` to return promises (#347) 51269ce
- Add bigint to typescript Serializable - fixes #330 f1e41c3
## v3.1.0 - 22 Apr 2022
- Add close method to close but not end connections forever 94fea8f
- Add .values() method to return rows as arrays of values 56873c2
- Support transform.undefined - fixes #314 eab71e5
- Support nested fragments values and dynamics - fixes #326 86445ca
- Fix deno close sequence f76af24
- Fix subscribe reconnect and add onsubscribe method - fixes #315 5097345
- Deno ts fix - fixes #327 50403a1
## v3.0.6 - 19 Apr 2022
- Properly close connections in Deno cbc6a75
- Only write end message if socket is open 13950af
- Improve query cancellation 01c2c68
- Use monotonically increasing time for timeout - fixes #316 9d7a21d
- Add support for dynamic columns with `returning` - fixes #317 04644c0
- Fix type errors in TypeScript deno projects (#313) 822fb21
- Execute forEach instantly 44e9fbe
## v3.0.5 - 6 Apr 2022
- Fix transaction execution timing 28bb0b3
- Add optional onlisten function to listen 1dc2fd2
- Fix dynamic in helper after insert #305 4d63a59
## v3.0.4 - 5 Apr 2022
- Ensure drain only dequeues if ready - fixes #303 2e5f017
## v3.0.3 - 4 Apr 2022
- Run tests with github actions b536d0d
- Add custom socket option - fixes #284 5413f0c
- Fix sql function overload type inference (#294) 3c4e90a
- Update deno std to 0.132 and enable last tests 50762d4
- Send proper client-encoding - Fixes #288 e5b8554
## v3.0.2 - 31 Mar 2022
- Fix BigInt handling 36a70df
- Fix unsubscribing (#300) b6c597f
- Parse update properly with identity full - Fixes #296 3ed11e7
## v3.0.1 - 30 Mar 2022
- Improve connection queue handling + fix leak cee1a57
- Use publications option - fixes #295 b5ceecc
- Throw proper query error if destroyed e148a0a
- Transaction rejects with rethrown error - fixes #289 f7c8ae6
- Only create origin stacktrace for tagged and debug - fixes #290 a782edf
- Include types and readme in deno release - fixes #287 9068820
- Disable fetch_types for Subscribe options 72e0cdb
- Update TypeScript types with v3 changes (#293) db05836
## v3.0.0 - 24 Mar 2022
This is a complete rewrite to better support all the features that I was trying to get into v2. There are a few breaking changes from v2 beta, which some (myself included) was using in production, so I'm skipping a stable v2 release and going straight to v3.
Here are some of the new things available, but check the updated docs.
- Dynamic query builder based on raw sql
- Realtime subscribe to db changes through logical replication
- Multi-host support for High Availability setups
- Postgres input parameter types from `ParameterDescription`
- Deno support
- Cursors as async iterators
- `.describe()` to only get query input types and column definitions
- Support for Large Objects
- `max_lifetime` for connections
- Cancellation of requests
- Converted to ESM (with CJS support)
- Typescript support (Credit @minigugus)
### Breaking changes from v2 -> v3
- Cursors are always called with `Result` arrays (previously cursor 1 would return a row object, where > 1 would return an array of rows)
- `.writable()` and `.readable()` is now async (returns a Promise that resolves to the stream)
- Queries now returns a lazy promise instead of being executed immediately. This means the query won't be sent until awaited (.then, .catch, .finally is called) or until `.execute()` is manually called.
- `.stream()` is renamed to `.forEach`
- Returned results are now it's own `Result` class extending `Array` instead of an Array with extra properties (actually shouldn't be breaking unless you're doing something funny)
- Parameters are now cast using the types returned from Postgres ParameterDescription with a fallback to the previously inferred types
- Only tested with node v12 and up
- Implicit array value to multiple parameter expansion removed (use sql([...]) instead)
### Breaking changes from v1 -> v2 (v2 never moved on from beta)
- All identifiers from `sql()` in queries are now always quoted
- Undefined parameters are no longer allowed
- Rename timeout option to `idle_timeout`
- Default to 10 connections instead of number of CPUs
- Numbers that cannot be safely cast to JS Number are returned as string. This happens for eg, `select count(*)` because `count()` returns a 64 bit integer (int8), so if you know your `count()` won't be too big for a js number just cast in your query to int4 like `select count(*)::int`
## v1.0.2 - 21 Jan 2020
- Fix standard postgres user env var (#20) cce5ad7
- Ensure url or options is not falsy bc549b0
- Add support for dynamic password b2ab9fb
- Fix hiding pass from options 3f76b98
## v1.0.1 - 3 Jan 2020
- Fix #3 url without db and trailing slash 45d4233
- Fix stream promise - resolve with correct result 730df2c
- Fix return value of unsafe query with multiple statements 748f198
- Fix destroy before connected f682ca1
- Fix params usage for file() call without options e4f12a4
- Various Performance improvements
## v1.0.0 - 22 Dec 2019
- Initial release
================================================
FILE: README.md
================================================
<img align="left" width="440" height="180" alt="Fastest full PostgreSQL nodejs client" src="https://raw.githubusercontent.com/porsager/postgres/master/postgresjs.svg?sanitize=true">
- [🚀 Fastest full-featured node & deno client](https://github.com/porsager/postgres-benchmarks#results)
- 🏷 ES6 Tagged Template Strings at the core
- 🏄♀️ Simple surface API
- 🖊️ Dynamic query support
- 💬 Chat and help on [Gitter](https://gitter.im/porsager/postgres)
- 🐦 Follow on [Twitter](https://twitter.com/rporsager)
<br>
## Getting started
<br>
<img height="220" width="458" alt="Good UX with Postgres.js" src="https://raw.githubusercontent.com/porsager/postgres/master/demo.gif">
<br>
### Installation
```bash
$ npm install postgres
```
### Usage
Create your `sql` database instance
```js
// db.js
import postgres from 'postgres'
const sql = postgres({ /* options */ }) // will use psql environment variables
export default sql
```
Simply import for use elsewhere
```js
// users.js
import sql from './db.js'
async function getUsersOver(age) {
const users = await sql`
select
name,
age
from users
where age > ${ age }
`
// users = Result [{ name: "Walter", age: 80 }, { name: 'Murray', age: 68 }, ...]
return users
}
async function insertUser({ name, age }) {
const users = await sql`
insert into users
(name, age)
values
(${ name }, ${ age })
returning name, age
`
// users = Result [{ name: "Murray", age: 68 }]
return users
}
```
#### ESM dynamic imports
The library can be used with ESM dynamic imports as well as shown here.
```js
const { default: postgres } = await import('postgres')
```
## Table of Contents
* [Connection](#connection)
* [Queries](#queries)
* [Building queries](#building-queries)
* [Advanced query methods](#advanced-query-methods)
* [Transactions](#transactions)
* [Data Transformation](#data-transformation)
* [Listen & notify](#listen--notify)
* [Realtime subscribe](#realtime-subscribe)
* [Numbers, bigint, numeric](#numbers-bigint-numeric)
* [Result Array](#result-array)
* [Connection details](#connection-details)
* [Custom Types](#custom-types)
* [Teardown / Cleanup](#teardown--cleanup)
* [Error handling](#error-handling)
* [TypeScript support](#typescript-support)
* [Reserving connections](#reserving-connections)
* [Changelog](./CHANGELOG.md)
## Connection
### `postgres([url], [options])`
You can use either a `postgres://` url connection string or the options to define your database connection properties. Options in the object will override any present in the url. Options will fall back to the same environment variables as psql.
```js
const sql = postgres('postgres://username:password@host:port/database', {
host : '', // Postgres ip address[s] or domain name[s]
port : 5432, // Postgres server port[s]
database : '', // Name of database to connect to
username : '', // Username of database user
password : '', // Password of database user
...and more
})
```
More options can be found in the [Connection details section](#connection-details).
## Queries
### ```await sql`...` -> Result[]```
Postgres.js utilizes [Tagged template functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates) to process query parameters **before** interpolation. Using tagged template literals benefits developers by:
1. **Enforcing** safe query generation
2. Giving the ` sql`` ` function powerful [utility](#dynamic-inserts) and [query building](#building-queries) features.
Any generic value will be serialized according to an inferred type, and replaced by a PostgreSQL protocol placeholder `$1, $2, ...`. The parameters are then sent separately to the database which handles escaping & casting.
All queries will return a `Result` array, with objects mapping column names to each row.
```js
const xs = await sql`
insert into users (
name, age
) values (
'Murray', 68
)
returning *
`
// xs = [{ user_id: 1, name: 'Murray', age: 68 }]
```
> Please note that queries are first executed when `awaited` – or instantly by using [`.execute()`](#execute).
### Query parameters
Parameters are automatically extracted and handled by the database so that SQL injection isn't possible. No special handling is necessary, simply use tagged template literals as usual.
```js
const name = 'Mur'
, age = 60
const users = await sql`
select
name,
age
from users
where
name like ${ name + '%' }
and age > ${ age }
`
// users = [{ name: 'Murray', age: 68 }]
```
> Be careful with quotation marks here. Because Postgres infers column types, you do not need to wrap your interpolated parameters in quotes like `'${name}'`. This will cause an error because the tagged template replaces `${name}` with `$1` in the query string, leaving Postgres to do the interpolation. If you wrap that in a string, Postgres will see `'$1'` and interpret it as a string as opposed to a parameter.
### Dynamic column selection
```js
const columns = ['name', 'age']
await sql`
select
${ sql(columns) }
from users
`
// Which results in:
select "name", "age" from users
```
### Dynamic inserts
```js
const user = {
name: 'Murray',
age: 68
}
await sql`
insert into users ${
sql(user, 'name', 'age')
}
`
// Which results in:
insert into users ("name", "age") values ($1, $2)
// The columns can also be given with an array
const columns = ['name', 'age']
await sql`
insert into users ${
sql(user, columns)
}
`
```
**You can omit column names and simply execute `sql(user)` to get all the fields from the object as columns**. Be careful not to allow users to supply columns that you do not want to be inserted.
#### Multiple inserts in one query
If you need to insert multiple rows at the same time it's also much faster to do it with a single `insert`. Simply pass an array of objects to `sql()`.
```js
const users = [{
name: 'Murray',
age: 68,
garbage: 'ignore'
},
{
name: 'Walter',
age: 80
}]
await sql`insert into users ${ sql(users, 'name', 'age') }`
// Is translated to:
insert into users ("name", "age") values ($1, $2), ($3, $4)
// Here you can also omit column names which will use object keys as columns
await sql`insert into users ${ sql(users) }`
// Which results in:
insert into users ("name", "age") values ($1, $2), ($3, $4)
```
### Dynamic columns in updates
This is also useful for update queries
```js
const user = {
id: 1,
name: 'Murray',
age: 68
}
await sql`
update users set ${
sql(user, 'name', 'age')
}
where user_id = ${ user.id }
`
// Which results in:
update users set "name" = $1, "age" = $2 where user_id = $3
// The columns can also be given with an array
const columns = ['name', 'age']
await sql`
update users set ${
sql(user, columns)
}
where user_id = ${ user.id }
`
```
### Multiple updates in one query
To create multiple updates in a single query, it is necessary to use arrays instead of objects to ensure that the order of the items correspond with the column names.
```js
const users = [
[1, 'John', 34],
[2, 'Jane', 27],
]
await sql`
update users set name = update_data.name, age = (update_data.age)::int
from (values ${sql(users)}) as update_data (id, name, age)
where users.id = (update_data.id)::int
returning users.id, users.name, users.age
`
```
### Dynamic values and `where in`
Value lists can also be created dynamically, making `where in` queries simple too.
```js
const users = await sql`
select
*
from users
where age in ${ sql([68, 75, 23]) }
`
```
or
```js
const [{ a, b, c }] = await sql`
select
*
from (values ${ sql(['a', 'b', 'c']) }) as x(a, b, c)
`
```
## Building queries
Postgres.js features a simple dynamic query builder by conditionally appending/omitting query fragments.
It works by nesting ` sql`` ` fragments within other ` sql`` ` calls or fragments. This allows you to build dynamic queries safely without risking sql injections through usual string concatenation.
### Partial queries
```js
const olderThan = x => sql`and age > ${ x }`
const filterAge = true
await sql`
select
*
from users
where name is not null ${
filterAge
? olderThan(50)
: sql``
}
`
// Which results in:
select * from users where name is not null
// Or
select * from users where name is not null and age > 50
```
### Dynamic filters
```js
await sql`
select
*
from users ${
id
? sql`where user_id = ${ id }`
: sql``
}
`
// Which results in:
select * from users
// Or
select * from users where user_id = $1
```
### Dynamic ordering
```js
const id = 1
const order = {
username: 'asc'
created_at: 'desc'
}
await sql`
select
*
from ticket
where account = ${ id }
order by ${
Object.entries(order).flatMap(([column, order], i) =>
[i ? sql`,` : sql``, sql`${ sql(column) } ${ order === 'desc' ? sql`desc` : sql`asc` }`]
)
}
`
```
### SQL functions
Using keywords or calling functions dynamically is also possible by using ``` sql`` ``` fragments.
```js
const date = null
await sql`
update users set updated_at = ${ date || sql`now()` }
`
// Which results in:
update users set updated_at = now()
```
### Table names
Dynamic identifiers like table names and column names is also supported like so:
```js
const table = 'users'
, column = 'id'
await sql`
select ${ sql(column) } from ${ sql(table) }
`
// Which results in:
select "id" from "users"
```
### Quick primer on interpolation
Here's a quick oversight over all the ways to do interpolation in a query template string:
| Interpolation syntax | Usage | Example |
| ------------- | ------------- | ------------- |
| `${ sql`` }` | for keywords or sql fragments | ``await sql`SELECT * FROM users ${sql`order by age desc` }` `` |
| `${ sql(string) }` | for identifiers | ``await sql`SELECT * FROM ${sql('table_name')` `` |
| `${ sql([] or {}, ...) }` | for helpers | ``await sql`INSERT INTO users ${sql({ name: 'Peter'})}` `` |
| `${ 'somevalue' }` | for values | ``await sql`SELECT * FROM users WHERE age = ${42}` `` |
## Advanced query methods
### Cursors
#### ```await sql``.cursor([rows = 1], [fn])```
Use cursors if you need to throttle the amount of rows being returned from a query. You can use a cursor either as an [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) or with a callback function. For a callback function new results won't be requested until the promise / async callback function has resolved.
##### callback function
```js
await sql`
select
*
from generate_series(1,4) as x
`.cursor(async([row]) => {
// row = { x: 1 }
await http.request('https://example.com/wat', { row })
})
```
##### for await...of
```js
// for await...of
const cursor = sql`select * from generate_series(1,4) as x`.cursor()
for await (const [row] of cursor) {
// row = { x: 1 }
await http.request('https://example.com/wat', { row })
}
```
A single row will be returned by default, but you can also request batches by setting the number of rows desired in each batch as the first argument to `.cursor`:
```js
await sql`
select
*
from generate_series(1,1000) as x
`.cursor(10, async rows => {
// rows = [{ x: 1 }, { x: 2 }, ... ]
await Promise.all(rows.map(row =>
http.request('https://example.com/wat', { row })
))
})
```
If an error is thrown inside the callback function no more rows will be requested and the outer promise will reject with the thrown error.
You can close the cursor early either by calling `break` in the `for await...of` loop, or by returning the token `sql.CLOSE` from the callback function.
```js
await sql`
select * from generate_series(1,1000) as x
`.cursor(row => {
return Math.random() > 0.9 && sql.CLOSE // or sql.END
})
```
### Instant iteration
#### ```await sql``.forEach(fn)```
If you want to handle rows returned by a query one by one, you can use `.forEach` which returns a promise that resolves once there are no more rows.
```js
await sql`
select created_at, name from events
`.forEach(row => {
// row = { created_at: '2019-11-22T14:22:00Z', name: 'connected' }
})
// No more rows
```
### Query Descriptions
#### ```await sql``.describe() -> Result[]```
Rather than executing a given query, `.describe` will return information utilized in the query process. This information can include the query identifier, column types, etc.
This is useful for debugging and analyzing your Postgres queries. Furthermore, **`.describe` will give you access to the final generated query string that would be executed.**
### Rows as Array of Values
#### ```sql``.values()```
Using `.values` will return rows as an array of values for each column, instead of objects.
This can be useful to receive identically named columns, or for specific performance/transformation reasons. The column definitions are still included on the result array, plus access to parsers for each column.
### Rows as Raw Array of Buffers
#### ```sql``.raw()```
Using `.raw` will return rows as an array with `Buffer` values for each column, instead of objects.
This can be useful for specific performance/transformation reasons. The column definitions are still included on the result array, plus access to parsers for each column.
### Queries in Files
#### `await sql.file(path, [args], [options]) -> Result[]`
Using a file for a query is also supported with optional parameters to use if the file includes `$1, $2, etc`
```js
const result = await sql.file('query.sql', ['Murray', 68])
```
### Multiple statements in one query
#### ```await sql``.simple()```
The postgres wire protocol supports ["simple"](https://www.postgresql.org/docs/current/protocol-flow.html#id-1.10.6.7.4) and ["extended"](https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY) queries. "simple" queries supports multiple statements, but does not support any dynamic parameters. "extended" queries support parameters but only one statement. To use "simple" queries you can use
```sql``.simple()```. That will create it as a simple query.
```js
await sql`select 1; select 2;`.simple()
```
### Copy to/from as Streams
Postgres.js supports [`COPY ...`](https://www.postgresql.org/docs/14/sql-copy.html) queries, which are exposed as [Node.js streams](https://nodejs.org/api/stream.html).
#### ```await sql`copy ... from stdin`.writable() -> Writable```
```js
import { pipeline } from 'node:stream/promises'
// Stream of users with the default tab delimitated cells and new-line delimitated rows
const userStream = Readable.from([
'Murray\t68\n',
'Walter\t80\n'
])
const query = await sql`copy users (name, age) from stdin`.writable()
await pipeline(userStream, query);
```
#### ```await sql`copy ... to stdout`.readable() -> Readable```
##### Using Stream Pipeline
```js
import { pipeline } from 'node:stream/promises'
import { createWriteStream } from 'node:fs'
const readableStream = await sql`copy users (name, age) to stdout`.readable()
await pipeline(readableStream, createWriteStream('output.tsv'))
// output.tsv content: `Murray\t68\nWalter\t80\n`
```
##### Using `for await...of`
```js
const readableStream = await sql`
copy (
select name, age
from users
where age = 68
) to stdout
`.readable()
for await (const chunk of readableStream) {
// chunk.toString() === `Murray\t68\n`
}
```
> **NOTE** This is a low-level API which does not provide any type safety. To make this work, you must match your [`copy query` parameters](https://www.postgresql.org/docs/14/sql-copy.html) correctly to your [Node.js stream read or write](https://nodejs.org/api/stream.html) code. Ensure [Node.js stream backpressure](https://nodejs.org/en/learn/modules/backpressuring-in-streams) is handled correctly to avoid memory exhaustion.
### Canceling Queries in Progress
Postgres.js supports, [canceling queries in progress](https://www.postgresql.org/docs/7.1/protocol-protocol.html#AEN39000). It works by opening a new connection with a protocol level startup message to cancel the current query running on a specific connection. That means there is no guarantee that the query will be canceled, and due to the possible race conditions it might even result in canceling another query. This is fine for long running queries, but in the case of high load and fast queries it might be better to simply ignore results instead of canceling.
```js
const query = sql`select pg_sleep 100`.execute()
setTimeout(() => query.cancel(), 100)
const result = await query
```
### Execute
#### ```await sql``.execute()```
The lazy Promise implementation in Postgres.js is what allows it to distinguish [Nested Fragments](#building-queries) from the main outer query. This also means that queries are always executed at the earliest in the following tick. If you have a specific need to execute the query in the same tick, you can call `.execute()`
### Unsafe raw string queries
<details>
<summary>Advanced unsafe use cases</summary>
### `await sql.unsafe(query, [args], [options]) -> Result[]`
If you know what you're doing, you can use `unsafe` to pass any string you'd like to postgres. Please note that this can lead to SQL injection if you're not careful.
```js
sql.unsafe('select ' + danger + ' from users where id = ' + dragons)
```
By default, `sql.unsafe` assumes the `query` string is sufficiently dynamic that prepared statements do not make sense, and so defaults them to off. If you'd like to re-enable prepared statements, you can pass `{ prepare: true }`.
You can also nest `sql.unsafe` within a safe `sql` expression. This is useful if only part of your fraction has unsafe elements.
```js
const triggerName = 'friend_created'
const triggerFnName = 'on_friend_created'
const eventType = 'insert'
const schema_name = 'app'
const table_name = 'friends'
await sql`
create or replace trigger ${sql(triggerName)}
after ${sql.unsafe(eventType)} on ${sql.unsafe(`${schema_name}.${table_name}`)}
for each row
execute function ${sql(triggerFnName)}()
`
await sql`
create role friend_service with login password ${sql.unsafe(`'${password}'`)}
`
```
</details>
## Transactions
#### BEGIN / COMMIT `await sql.begin([options = ''], fn) -> fn()`
Use `sql.begin` to start a new transaction. Postgres.js will reserve a connection for the transaction and supply a scoped `sql` instance for all transaction uses in the callback function. `sql.begin` will resolve with the returned value from the callback function.
`BEGIN` is automatically sent with the optional options, and if anything fails `ROLLBACK` will be called so the connection can be released and execution can continue.
```js
const [user, account] = await sql.begin(async sql => {
const [user] = await sql`
insert into users (
name
) values (
'Murray'
)
returning *
`
const [account] = await sql`
insert into accounts (
user_id
) values (
${ user.user_id }
)
returning *
`
return [user, account]
})
```
Do note that you can often achieve the same result using [`WITH` queries (Common Table Expressions)](https://www.postgresql.org/docs/current/queries-with.html) instead of using transactions.
It's also possible to pipeline the requests in a transaction if needed by returning an array with queries from the callback function like this:
```js
const result = await sql.begin(sql => [
sql`update ...`,
sql`update ...`,
sql`insert ...`
])
```
#### SAVEPOINT `await sql.savepoint([name], fn) -> fn()`
```js
sql.begin('read write', async sql => {
const [user] = await sql`
insert into users (
name
) values (
'Murray'
)
`
const [account] = (await sql.savepoint(sql =>
sql`
insert into accounts (
user_id
) values (
${ user.user_id }
)
`
).catch(err => {
// Account could not be created. ROLLBACK SAVEPOINT is called because we caught the rejection.
})) || []
return [user, account]
})
.then(([user, account]) => {
// great success - COMMIT succeeded
})
.catch(() => {
// not so good - ROLLBACK was called
})
```
#### PREPARE TRANSACTION `await sql.prepare([name]) -> fn()`
Indicates that the transactions should be prepared using the [`PREPARE TRANSACTION [NAME]`](https://www.postgresql.org/docs/current/sql-prepare-transaction.html) statement
instead of being committed.
```js
sql.begin('read write', async sql => {
const [user] = await sql`
insert into users (
name
) values (
'Murray'
)
`
await sql.prepare('tx1')
})
```
## Data Transformation
Postgres.js allows for transformation of the data passed to or returned from a query by using the `transform` option.
Built in transformation functions are:
* For camelCase - `postgres.camel`, `postgres.toCamel`, `postgres.fromCamel`
* For PascalCase - `postgres.pascal`, `postgres.toPascal`, `postgres.fromPascal`
* For Kebab-Case - `postgres.kebab`, `postgres.toKebab`, `postgres.fromKebab`
These built in transformations will only convert to/from snake_case. For example, using `{ transform: postgres.toCamel }` will convert the column names to camelCase only if the column names are in snake_case to begin with. `{ transform: postgres.fromCamel }` will convert camelCase only to snake_case.
By default, using `postgres.camel`, `postgres.pascal` and `postgres.kebab` will perform a two-way transformation - both the data passed to the query and the data returned by the query will be transformed:
```js
// Transform the column names to and from camel case
const sql = postgres({ transform: postgres.camel })
await sql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER, b_test TEXT)`
await sql`INSERT INTO camel_case ${ sql([{ aTest: 1, bTest: 1 }]) }`
const data = await sql`SELECT ${ sql('aTest', 'bTest') } FROM camel_case`
console.log(data) // [ { aTest: 1, bTest: '1' } ]
```
To only perform half of the transformation (eg. only the transformation **to** or **from** camel case), use the other transformation functions:
```js
// Transform the column names only to camel case
// (for the results that are returned from the query)
postgres({ transform: postgres.toCamel })
await sql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER)`
await sql`INSERT INTO camel_case ${ sql([{ a_test: 1 }]) }`
const data = await sql`SELECT a_test FROM camel_case`
console.log(data) // [ { aTest: 1 } ]
```
```js
// Transform the column names only from camel case
// (for interpolated inserts, updates, and selects)
const sql = postgres({ transform: postgres.fromCamel })
await sql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER)`
await sql`INSERT INTO camel_case ${ sql([{ aTest: 1 }]) }`
const data = await sql`SELECT ${ sql('aTest') } FROM camel_case`
console.log(data) // [ { a_test: 1 } ]
```
> Note that Postgres.js does not rewrite the static parts of the tagged template strings. So to transform column names in your queries, the `sql()` helper must be used - eg. `${ sql('columnName') }` as in the examples above.
### Transform `undefined` Values
By default, Postgres.js will throw the error `UNDEFINED_VALUE: Undefined values are not allowed` when undefined values are passed
```js
// Transform the column names to and from camel case
const sql = postgres({
transform: {
undefined: null
}
})
await sql`CREATE TABLE IF NOT EXISTS transform_undefined (a_test INTEGER)`
await sql`INSERT INTO transform_undefined ${ sql([{ a_test: undefined }]) }`
const data = await sql`SELECT a_test FROM transform_undefined`
console.log(data) // [ { a_test: null } ]
```
To combine with the built in transform functions, spread the transform in the `transform` object:
```js
// Transform the column names to and from camel case
const sql = postgres({
transform: {
...postgres.camel,
undefined: null
}
})
await sql`CREATE TABLE IF NOT EXISTS transform_undefined (a_test INTEGER)`
await sql`INSERT INTO transform_undefined ${ sql([{ aTest: undefined }]) }`
const data = await sql`SELECT ${ sql('aTest') } FROM transform_undefined`
console.log(data) // [ { aTest: null } ]
```
### Custom Transform Functions
To specify your own transformation functions, you can use the `column`, `value` and `row` options inside of `transform`, each an object possibly including `to` and `from` keys:
* `to`: The function to transform the outgoing query column name to, i.e `SELECT ${ sql('aName') }` to `SELECT a_name` when using `postgres.toCamel`.
* `from`: The function to transform the incoming query result column name to, see example below.
> Both parameters are optional, if not provided, the default transformation function will be used.
```js
// Implement your own functions, look at postgres.toCamel, etc
// as a reference:
// https://github.com/porsager/postgres/blob/4241824ffd7aa94ffb482e54ca9f585d9d0a4eea/src/types.js#L310-L328
function transformColumnToDatabase() { /* ... */ }
function transformColumnFromDatabase() { /* ... */ }
const sql = postgres({
transform: {
column: {
to: transformColumnToDatabase,
from: transformColumnFromDatabase,
},
value: { /* ... */ },
row: { /* ... */ }
}
})
```
## Listen & notify
When you call `.listen`, a dedicated connection will be created to ensure that you receive notifications instantly. This connection will be used for any further calls to `.listen`. The connection will automatically reconnect according to a backoff reconnection pattern to not overload the database server.
### Listen `await sql.listen(channel, onnotify, [onlisten]) -> { state }`
`.listen` takes the channel name, a function to handle each notify, and an optional function to run every time listen is registered and ready (happens on initial connect and reconnects). It returns a promise which resolves once the `LISTEN` query to Postgres completes, or if there is already a listener active.
```js
await sql.listen('news', payload => {
const json = JSON.parse(payload)
console.log(json.this) // logs 'is'
})
```
The optional `onlisten` method is great to use for a very simply queue mechanism:
```js
await sql.listen(
'jobs',
(x) => run(JSON.parse(x)),
( ) => sql`select unfinished_jobs()`.forEach(run)
)
function run(job) {
// And here you do the work you please
}
```
### Notify `await sql.notify(channel, payload) -> Result[]`
Notify can be done as usual in SQL, or by using the `sql.notify` method.
```js
sql.notify('news', JSON.stringify({ no: 'this', is: 'news' }))
```
## Realtime subscribe
Postgres.js implements the logical replication protocol of PostgreSQL to support subscription to real-time updates of `insert`, `update` and `delete` operations.
> **NOTE** To make this work you must [create the proper publications in your database](https://www.postgresql.org/docs/current/sql-createpublication.html), enable logical replication by setting `wal_level = logical` in `postgresql.conf` and connect using either a replication or superuser.
### Quick start
#### Create a publication (eg. in migration)
```sql
CREATE PUBLICATION alltables FOR ALL TABLES
```
#### Subscribe to updates
```js
const sql = postgres({ publications: 'alltables' })
const { unsubscribe } = await sql.subscribe(
'insert:events',
(row, { command, relation, key, old }) => {
// Callback function for each row change
// tell about new event row over eg. websockets or do something else
},
() => {
// Callback on initial connect and potential reconnects
}
)
```
### Subscribe pattern
You can subscribe to specific operations, tables, or even rows with primary keys.
#### `operation` `:` `schema` `.` `table` `=` `primary_key`
**`operation`** is one of ``` * | insert | update | delete ``` and defaults to `*`
**`schema`** defaults to `public`
**`table`** is a specific table name and defaults to `*`
**`primary_key`** can be used to only subscribe to specific rows
### Examples
```js
sql.subscribe('*', () => /* everything */ )
sql.subscribe('insert', () => /* all inserts */ )
sql.subscribe('*:users', () => /* all operations on the public.users table */ )
sql.subscribe('delete:users', () => /* all deletes on the public.users table */ )
sql.subscribe('update:users=1', () => /* all updates on the users row with a primary key = 1 */ )
```
## Numbers, bigint, numeric
`Number` in javascript is only able to represent 2<sup>53</sup>-1 safely which means that types in PostgreSQLs like `bigint` and `numeric` won't fit into `Number`.
Since Node.js v10.4 we can use [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) to match the PostgreSQL type `bigint` which is returned for eg. `count(*)`. Unfortunately, it doesn't work with `JSON.stringify` out of the box, so Postgres.js will return it as a string.
If you want to use `BigInt` you can add this custom type:
```js
const sql = postgres({
types: {
bigint: postgres.BigInt
}
})
```
There is currently no guaranteed way to handle `numeric` / `decimal` types in native Javascript. **These [and similar] types will be returned as a `string`**. The best way in this case is to use [custom types](#custom-types).
## Result Array
The `Result` Array returned from queries is a custom array allowing for easy destructuring or passing on directly to JSON.stringify or general Array usage. It includes the following properties.
### .count
The `count` property is the number of affected rows returned by the database. This is useful for insert, update and delete operations to know the number of rows since .length will be 0 in these cases if not using `RETURNING ...`.
### .command
The `command` run by the query - eg. one of `SELECT`, `UPDATE`, `INSERT`, `DELETE`
### .columns
The `columns` returned by the query useful to determine types, or map to the result values when using `.values()`
```js
{
name : String, // Column name,
type : oid, // PostgreSQL oid column type
parser: Function // The function used by Postgres.js for parsing
}
```
### .statement
The `statement` contains information about the statement implicitly created by Postgres.js.
```js
{
name : String, // The auto generated statement name
string : String, // The actual query string executed
types : [oid], // An array of oid expected as input parameters
columns : [Column] // Array of columns - same as Result.columns
}
```
### .state
This is the state `{ pid, secret }` of the connection that executed the query.
## Connection details
### All Postgres options
```js
const sql = postgres('postgres://username:password@host:port/database', {
host : '', // Postgres ip address[es] or domain name[s]
port : 5432, // Postgres server port[s]
path : '', // unix socket path (usually '/tmp')
database : '', // Name of database to connect to
username : '', // Username of database user
password : '', // Password of database user
ssl : false, // true, prefer, require, tls.connect options
sslnegotiation : null, // direct
max : 10, // Max number of connections
max_lifetime : null, // Max lifetime in seconds (more info below)
idle_timeout : 0, // Idle connection timeout in seconds
connect_timeout : 30, // Connect timeout in seconds
prepare : true, // Automatic creation of prepared statements
types : [], // Array of custom types, see more below
onnotice : fn, // Default console.log, set false to silence NOTICE
onparameter : fn, // (key, value) when server param change
debug : fn, // Is called with (connection, query, params, types)
socket : fn, // fn returning custom socket to use
transform : {
undefined : undefined, // Transforms undefined values (eg. to null)
column : fn, // Transforms incoming column names
value : fn, // Transforms incoming row values
row : fn // Transforms entire rows
},
connection : {
application_name : 'postgres.js', // Default application_name
... // Other connection parameters, see https://www.postgresql.org/docs/current/runtime-config-client.html
},
target_session_attrs : null, // Use 'read-write' with multiple hosts to
// ensure only connecting to primary
fetch_types : true, // Automatically fetches types on connect
// on initial connection.
})
```
Note that `max_lifetime = 60 * (30 + Math.random() * 30)` by default. This resolves to an interval between 30 and 60 minutes to optimize for the benefits of prepared statements **and** working nicely with Linux's OOM killer.
### Dynamic passwords
When clients need to use alternative authentication schemes such as access tokens or connections to databases with rotating passwords, provide either a synchronous or asynchronous function that will resolve the dynamic password value at connection time.
```js
const sql = postgres(url, {
// Other connection config
...
// Password function for the database user
password : async () => await signer.getAuthToken(),
})
```
### SSL
Although [vulnerable to MITM attacks](https://security.stackexchange.com/a/229297/174913), a common configuration for the `ssl` option for some cloud providers is to set `rejectUnauthorized` to `false` (if `NODE_ENV` is `production`):
```js
const sql =
process.env.NODE_ENV === 'production'
? // "Unless you're using a Private or Shield Heroku Postgres database, Heroku Postgres does not currently support verifiable certificates"
// https://help.heroku.com/3DELT3RK/why-can-t-my-third-party-utility-connect-to-heroku-postgres-with-ssl
postgres({ ssl: { rejectUnauthorized: false } })
: postgres()
```
For more information regarding `ssl` with `postgres`, check out the [Node.js documentation for tls](https://nodejs.org/dist/latest-v16.x/docs/api/tls.html#new-tlstlssocketsocket-options).
### Multi-host connections - High Availability (HA)
Multiple connection strings can be passed to `postgres()` in the form of `postgres('postgres://localhost:5432,localhost:5433', ...)`. This works the same as native the `psql` command. Read more at [multiple host URIs](https://www.postgresql.org/docs/13/libpq-connect.html#LIBPQ-MULTIPLE-HOSTS).
Connections will be attempted in order of the specified hosts/ports. On a successful connection, all retries will be reset. This ensures that hosts can come up and down seamlessly.
If you specify `target_session_attrs: 'primary'` or `PGTARGETSESSIONATTRS=primary` Postgres.js will only connect to the primary host, allowing for zero downtime failovers.
### The Connection Pool
Connections are created lazily once a query is created. This means that simply doing const `sql = postgres(...)` won't have any effect other than instantiating a new `sql` instance.
> No connection will be made until a query is made.
For example:
```js
const sql = postgres() // no connections are opened
await sql`...` // one connection is now opened
await sql`...` // previous opened connection is reused
// two connections are opened now
await Promise.all([
sql`...`,
sql`...`
])
```
> When there are high amount of concurrent queries, `postgres` will open as many connections as needed up until `max` number of connections is reached. By default `max` is 10. This can be changed by setting `max` in the `postgres()` call. Example - `postgres('connectionURL', { max: 20 })`.
This means that we get a much simpler story for error handling and reconnections. Queries will be sent over the wire immediately on the next available connection in the pool. Connections are automatically taken out of the pool if you start a transaction using `sql.begin()`, and automatically returned to the pool once your transaction is done.
Any query which was already sent over the wire will be rejected if the connection is lost. It'll automatically defer to the error handling you have for that query, and since connections are lazy it'll automatically try to reconnect the next time a query is made. The benefit of this is no weird generic "onerror" handler that tries to get things back to normal, and also simpler application code since you don't have to handle errors out of context.
There are no guarantees about queries executing in order unless using a transaction with `sql.begin()` or setting `max: 1`. Of course doing a series of queries, one awaiting the other will work as expected, but that's just due to the nature of js async/promise handling, so it's not necessary for this library to be concerned with ordering.
Since this library automatically creates prepared statements, it also has a default max lifetime for connections to prevent memory bloat on the database itself. This is a random interval for each connection between 45 and 90 minutes. This allows multiple connections to independently come up and down without affecting the service.
### Connection timeout
By default, connections will not close until `.end()` is called. However, it may be useful to have them close automatically when:
- re-instantiating multiple ` sql`` ` instances
- using Postgres.js in a Serverless environment (Lambda, etc.)
- using Postgres.js with a database service that automatically closes connections after some time (see [`ECONNRESET` issue](https://github.com/porsager/postgres/issues/179))
This can be done using the `idle_timeout` or `max_lifetime` options. These configuration options specify the number of seconds to wait before automatically closing an idle connection and the maximum time a connection can exist, respectively.
For example, to close a connection that has either been idle for 20 seconds or existed for more than 30 minutes:
```js
const sql = postgres({
idle_timeout: 20,
max_lifetime: 60 * 30
})
```
### Cloudflare Workers support
Postgres.js has built-in support for the [TCP socket API](https://developers.cloudflare.com/workers/runtime-apis/tcp-sockets/) in Cloudflare Workers, which is [on-track](https://github.com/wintercg/proposal-sockets-api) to be standardized and adopted in Node.js and other JavaScript runtimes, such as Deno.
You can use Postgres.js directly in a Worker, or to benefit from connection pooling and query caching, via the [Hyperdrive](https://developers.cloudflare.com/hyperdrive/learning/connect-to-postgres/#driver-examples) service available to Workers by passing the Hyperdrive `connectionString` when creating a new `postgres` client as follows:
```ts
// Requires Postgres.js 3.4.0 or later
import postgres from 'postgres'
interface Env {
HYPERDRIVE: Hyperdrive;
}
export default async fetch(req: Request, env: Env, ctx: ExecutionContext) {
// The Postgres.js library accepts a connection string directly
const sql = postgres(env.HYPERDRIVE.connectionString)
const results = await sql`SELECT * FROM users LIMIT 10`
return Response.json(results)
}
```
In `wrangler.toml` you will need to enable the `nodejs_compat` compatibility flag to allow Postgres.js to operate in the Workers environment:
```toml
compatibility_flags = ["nodejs_compat"]
```
### Auto fetching of array types
Postgres.js will automatically fetch table/array-type information when it first connects to a database.
If you have revoked access to `pg_catalog` this feature will no longer work and will need to be disabled.
You can disable this feature by setting `fetch_types` to `false`.
### Environmental variables
It is also possible to connect to the database without a connection string or any options. Postgres.js will fall back to the common environment variables used by `psql` as in the table below:
```js
const sql = postgres()
```
| Option | Environment Variables |
| ------------------ | ------------------------ |
| `host` | `PGHOST` |
| `port` | `PGPORT` |
| `database` | `PGDATABASE` |
| `username` | `PGUSERNAME` or `PGUSER` |
| `password` | `PGPASSWORD` |
| `application_name` | `PGAPPNAME` |
| `idle_timeout` | `PGIDLE_TIMEOUT` |
| `connect_timeout` | `PGCONNECT_TIMEOUT` |
### Prepared statements
Prepared statements will automatically be created for any queries where it can be inferred that the query is static. This can be disabled by using the `prepare: false` option. For instance — this is useful when [using PGBouncer in `transaction mode`](https://github.com/porsager/postgres/issues/93#issuecomment-656290493).
**update**: [since 1.21.0](https://www.pgbouncer.org/2023/10/pgbouncer-1-21-0)
PGBouncer supports protocol-level named prepared statements when [configured
properly](https://www.pgbouncer.org/config.html#max_prepared_statements)
## Custom Types
You can add ergonomic support for custom types, or simply use `sql.typed(value, type)` inline, where type is the PostgreSQL `oid` for the type and the correctly serialized string. _(`oid` values for types can be found in the `pg_catalog.pg_type` table.)_
Adding Query helpers is the cleanest approach which can be done like this:
```js
const sql = postgres({
types: {
rect: {
// The pg_types oid to pass to the db along with the serialized value.
to : 1337,
// An array of pg_types oids to handle when parsing values coming from the db.
from : [1337],
//Function that transform values before sending them to the db.
serialize : ({ x, y, width, height }) => [x, y, width, height],
// Function that transforms values coming from the db.
parse : ([x, y, width, height]) => { x, y, width, height }
}
}
})
// Now you can use sql.typed.rect() as specified above
const [custom] = await sql`
insert into rectangles (
name,
rect
) values (
'wat',
${ sql.typed.rect({ x: 13, y: 37, width: 42, height: 80 }) }
)
returning *
`
// custom = { name: 'wat', rect: { x: 13, y: 37, width: 42, height: 80 } }
```
### Custom socket
Easily do in-process ssh tunneling to your database by providing a custom socket for Postgres.js to use. The function (optionally async) must return a socket-like duplex stream.
Here's a sample using [ssh2](https://github.com/mscdex/ssh2)
```js
import ssh2 from 'ssh2'
const sql = postgres({
...options,
socket: ({ host: [host], port: [port] }) => new Promise((resolve, reject) => {
const ssh = new ssh2.Client()
ssh
.on('error', reject)
.on('ready', () =>
ssh.forwardOut('127.0.0.1', 12345, host, port,
(err, socket) => err ? reject(err) : resolve(socket)
)
)
.connect(sshOptions)
})
})
```
## Teardown / Cleanup
To ensure proper teardown and cleanup on server restarts use `await sql.end()` before `process.exit()`.
Calling `sql.end()` will reject new queries and return a Promise which resolves when all queries are finished and the underlying connections are closed. If a `{ timeout }` option is provided any pending queries will be rejected once the timeout (in seconds) is reached and the connections will be destroyed.
#### Sample shutdown using [Prexit](https://github.com/porsager/prexit)
```js
import prexit from 'prexit'
prexit(async () => {
await sql.end({ timeout: 5 })
await new Promise(r => server.close(r))
})
```
## Reserving connections
### `await sql.reserve()`
The `reserve` method pulls out a connection from the pool, and returns a client that wraps the single connection. This can be used for running queries on an isolated connection.
```ts
const reserved = await sql.reserve()
await reserved`select * from users`
await reserved.release()
```
### `reserved.release()`
Once you have finished with the reserved connection, call `release` to add it back to the pool.
## Error handling
Errors are all thrown to related queries and never globally. Errors coming from database itself are always in the [native Postgres format](https://www.postgresql.org/docs/current/errcodes-appendix.html), and the same goes for any [Node.js errors](https://nodejs.org/api/errors.html#errors_common_system_errors) eg. coming from the underlying connection.
Query errors will contain a stored error with the origin of the query to aid in tracing errors.
Query errors will also contain the `query` string and the `parameters`. These are not enumerable to avoid accidentally leaking confidential information in logs. To log these it is required to specifically access `error.query` and `error.parameters`, or set `debug: true` in options.
There are also the following errors specifically for this library.
##### UNSAFE_TRANSACTION
> Only use sql.begin or max: 1
To ensure statements in a transaction runs on the same connection (which is required for them to run inside the transaction), you must use [`sql.begin(...)`](#transactions) or only allow a single connection in options (`max: 1`).
##### UNDEFINED_VALUE
> Undefined values are not allowed
Postgres.js won't accept `undefined` as values in tagged template queries since it becomes ambiguous what to do with the value. If you want to set something to null, use `null` explicitly.
##### MESSAGE_NOT_SUPPORTED
> X (X) is not supported
Whenever a message is received from Postgres which is not supported by this library. Feel free to file an issue if you think something is missing.
##### MAX_PARAMETERS_EXCEEDED
> Max number of parameters (65534) exceeded
The postgres protocol doesn't allow more than 65534 (16bit) parameters. If you run into this issue there are various workarounds such as using `sql([...])` to escape values instead of passing them as parameters.
##### SASL_SIGNATURE_MISMATCH
> Message type X not supported
When using SASL authentication the server responds with a signature at the end of the authentication flow which needs to match the one on the client. This is to avoid [man-in-the-middle attacks](https://en.wikipedia.org/wiki/Man-in-the-middle_attack). If you receive this error the connection was canceled because the server did not reply with the expected signature.
##### NOT_TAGGED_CALL
> Query not called as a tagged template literal
Making queries has to be done using the sql function as a [tagged template](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates). This is to ensure parameters are serialized and passed to Postgres as query parameters with correct types and to avoid SQL injection.
##### AUTH_TYPE_NOT_IMPLEMENTED
> Auth type X not implemented
Postgres supports many different authentication types. This one is not supported.
##### CONNECTION_CLOSED
> write CONNECTION_CLOSED host:port
This error is thrown if the connection was closed without an error. This should not happen during normal operations, so please create an issue if this was unexpected.
##### CONNECTION_ENDED
> write CONNECTION_ENDED host:port
This error is thrown if the user has called [`sql.end()`](#teardown--cleanup) and performed a query afterward.
##### CONNECTION_DESTROYED
> write CONNECTION_DESTROYED host:port
This error is thrown for any queries that were pending when the timeout to [`sql.end({ timeout: X })`](#teardown--cleanup) was reached.
##### CONNECT_TIMEOUT
> write CONNECT_TIMEOUT host:port
This error is thrown if the startup phase of the connection (tcp, protocol negotiation, and auth) took more than the default 30 seconds or what was specified using `connect_timeout` or `PGCONNECT_TIMEOUT`.
##### COPY_IN_PROGRESS
> You cannot execute queries during copy
This error is thrown if trying to run a query during a copy operation (writable / readable).
## TypeScript support
`postgres` has TypeScript support. You can pass a row list type for your queries in this way:
```ts
interface User {
id: number
name: string
}
const users = await sql<User[]>`SELECT * FROM users`
users[0].id // ok => number
users[1].name // ok => string
users[0].invalid // fails: `invalid` does not exists on `User`
```
However, be sure to check the array length to avoid accessing properties of `undefined` rows:
```ts
const users = await sql<User[]>`SELECT * FROM users WHERE id = ${id}`
if (!users.length)
throw new Error('Not found')
return users[0]
```
You can also prefer destructuring when you only care about a fixed number of rows.
In this case, we recommend you to prefer using tuples to handle `undefined` properly:
```ts
const [user]: [User?] = await sql`SELECT * FROM users WHERE id = ${id}`
if (!user) // => User | undefined
throw new Error('Not found')
return user // => User
// NOTE:
const [first, second]: [User?] = await sql`SELECT * FROM users WHERE id = ${id}` // fails: `second` does not exist on `[User?]`
const [first, second] = await sql<[User?]>`SELECT * FROM users WHERE id = ${id}` // don't fail : `second: User | undefined`
```
We do our best to type all the public API, however types are not always updated when features are added or changed. Feel free to open an issue if you have trouble with types.
## Migration tools
Postgres.js doesn't come with any migration solution since it's way out of scope, but here are some modules that support Postgres.js for migrations:
- https://github.com/porsager/postgres-shift
- https://github.com/lukeed/ley
- https://github.com/JAForbes/pgmg
## Thank you
A really big thank you to [@JAForbes](https://twitter.com/jmsfbs) who introduced me to Postgres and still holds my hand navigating all the great opportunities we have.
Thanks to [@ACXgit](https://twitter.com/andreacoiutti) for initial tests and dogfooding.
Also thanks to [Ryan Dahl](https://github.com/ry) for letting me have the `postgres` npm package name.
================================================
FILE: UNLICENSE
================================================
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org/>
================================================
FILE: cf/polyfills.js
================================================
import { EventEmitter } from 'node:events'
import { Buffer } from 'node:buffer'
const Crypto = globalThis.crypto
let ids = 1
const tasks = new Set()
const v4Seg = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
const v4Str = `(${v4Seg}[.]){3}${v4Seg}`
const IPv4Reg = new RegExp(`^${v4Str}$`)
const v6Seg = '(?:[0-9a-fA-F]{1,4})'
const IPv6Reg = new RegExp(
'^(' +
`(?:${v6Seg}:){7}(?:${v6Seg}|:)|` +
`(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|` +
`(?:${v6Seg}:){5}(?::${v4Str}|(:${v6Seg}){1,2}|:)|` +
`(?:${v6Seg}:){4}(?:(:${v6Seg}){0,1}:${v4Str}|(:${v6Seg}){1,3}|:)|` +
`(?:${v6Seg}:){3}(?:(:${v6Seg}){0,2}:${v4Str}|(:${v6Seg}){1,4}|:)|` +
`(?:${v6Seg}:){2}(?:(:${v6Seg}){0,3}:${v4Str}|(:${v6Seg}){1,5}|:)|` +
`(?:${v6Seg}:){1}(?:(:${v6Seg}){0,4}:${v4Str}|(:${v6Seg}){1,6}|:)|` +
`(?::((?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` +
')(%[0-9a-zA-Z-.:]{1,})?$'
)
const textEncoder = new TextEncoder()
export const crypto = {
randomBytes: l => Crypto.getRandomValues(Buffer.alloc(l)),
pbkdf2Sync: async(password, salt, iterations, keylen) =>
Crypto.subtle.deriveBits(
{
name: 'PBKDF2',
hash: 'SHA-256',
salt,
iterations
},
await Crypto.subtle.importKey(
'raw',
textEncoder.encode(password),
'PBKDF2',
false,
['deriveBits']
),
keylen * 8,
['deriveBits']
),
createHash: type => ({
update: x => ({
digest: encoding => {
if (!(x instanceof Uint8Array)) {
x = textEncoder.encode(x)
}
let prom
if (type === 'sha256') {
prom = Crypto.subtle.digest('SHA-256', x)
} else if (type === 'md5') {
prom = Crypto.subtle.digest('md5', x)
} else {
throw Error('createHash only supports sha256 or md5 in this environment, not ${type}.')
}
if (encoding === 'hex') {
return prom.then((arrayBuf) => Buffer.from(arrayBuf).toString('hex'))
} else if (encoding) {
throw Error(`createHash only supports hex encoding or unencoded in this environment, not ${encoding}`)
} else {
return prom
}
}
})
}),
createHmac: (type, key) => ({
update: x => ({
digest: async() =>
Buffer.from(
await Crypto.subtle.sign(
'HMAC',
await Crypto.subtle.importKey('raw', key, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']),
textEncoder.encode(x)
)
)
})
})
}
export const performance = globalThis.performance
export const process = {
env: {}
}
export const os = {
userInfo() {
return { username: 'postgres' }
}
}
export const fs = {
readFile() {
throw new Error('Reading files not supported on CloudFlare')
}
}
export const net = {
isIP: (x) => IPv4Reg.test(x) ? 4 : IPv6Reg.test(x) ? 6 : 0,
Socket
}
export { setImmediate, clearImmediate }
export const tls = {
connect({ socket: tcp, servername }) {
tcp.writer.releaseLock()
tcp.reader.releaseLock()
tcp.readyState = 'upgrading'
tcp.raw = tcp.raw.startTls({ servername })
tcp.raw.closed.then(
() => tcp.emit('close'),
(e) => tcp.emit('error', e)
)
tcp.writer = tcp.raw.writable.getWriter()
tcp.reader = tcp.raw.readable.getReader()
tcp.writer.ready.then(() => {
tcp.read()
tcp.readyState = 'upgrade'
})
return tcp
}
}
function Socket() {
const tcp = Object.assign(new EventEmitter(), {
readyState: 'open',
raw: null,
writer: null,
reader: null,
connect,
write,
end,
destroy,
read
})
return tcp
async function connect(port, host) {
try {
tcp.readyState = 'opening'
const { connect } = await import('cloudflare:sockets')
tcp.raw = connect(host + ':' + port, tcp.ssl ? { secureTransport: 'starttls' } : {})
tcp.raw.closed.then(
() => {
tcp.readyState !== 'upgrade'
? close()
: ((tcp.readyState = 'open'), tcp.emit('secureConnect'))
},
(e) => tcp.emit('error', e)
)
tcp.writer = tcp.raw.writable.getWriter()
tcp.reader = tcp.raw.readable.getReader()
tcp.ssl ? readFirst() : read()
tcp.writer.ready.then(() => {
tcp.readyState = 'open'
tcp.emit('connect')
})
} catch (err) {
error(err)
}
}
function close() {
if (tcp.readyState === 'closed')
return
tcp.readyState = 'closed'
tcp.emit('close')
}
function write(data, cb) {
tcp.writer.write(data).then(cb, error)
return true
}
function end(data) {
return data
? tcp.write(data, () => tcp.raw.close())
: tcp.raw.close()
}
function destroy() {
tcp.destroyed = true
tcp.end()
}
async function read() {
try {
let done
, value
while (({ done, value } = await tcp.reader.read(), !done))
tcp.emit('data', Buffer.from(value))
} catch (err) {
error(err)
}
}
async function readFirst() {
const { value } = await tcp.reader.read()
tcp.emit('data', Buffer.from(value))
}
function error(err) {
tcp.emit('error', err)
tcp.emit('close')
}
}
function setImmediate(fn) {
const id = ids++
tasks.add(id)
queueMicrotask(() => {
if (tasks.has(id)) {
fn()
tasks.delete(id)
}
})
return id
}
function clearImmediate(id) {
tasks.delete(id)
}
================================================
FILE: cf/src/bytes.js
================================================
import { Buffer } from 'node:buffer'
const size = 256
let buffer = Buffer.allocUnsafe(size)
const messages = 'BCcDdEFfHPpQSX'.split('').reduce((acc, x) => {
const v = x.charCodeAt(0)
acc[x] = () => {
buffer[0] = v
b.i = 5
return b
}
return acc
}, {})
const b = Object.assign(reset, messages, {
N: String.fromCharCode(0),
i: 0,
inc(x) {
b.i += x
return b
},
str(x) {
const length = Buffer.byteLength(x)
fit(length)
b.i += buffer.write(x, b.i, length, 'utf8')
return b
},
i16(x) {
fit(2)
buffer.writeUInt16BE(x, b.i)
b.i += 2
return b
},
i32(x, i) {
if (i || i === 0) {
buffer.writeUInt32BE(x, i)
return b
}
fit(4)
buffer.writeUInt32BE(x, b.i)
b.i += 4
return b
},
z(x) {
fit(x)
buffer.fill(0, b.i, b.i + x)
b.i += x
return b
},
raw(x) {
buffer = Buffer.concat([buffer.subarray(0, b.i), x])
b.i = buffer.length
return b
},
end(at = 1) {
buffer.writeUInt32BE(b.i - at, at)
const out = buffer.subarray(0, b.i)
b.i = 0
buffer = Buffer.allocUnsafe(size)
return out
}
})
export default b
function fit(x) {
if (buffer.length - b.i < x) {
const prev = buffer
, length = prev.length
buffer = Buffer.allocUnsafe(length + (length >> 1) + x)
prev.copy(buffer)
}
}
function reset() {
b.i = 0
return b
}
================================================
FILE: cf/src/connection.js
================================================
import { Buffer } from 'node:buffer'
import { setImmediate, clearImmediate } from '../polyfills.js'
import { net } from '../polyfills.js'
import { tls } from '../polyfills.js'
import { crypto } from '../polyfills.js'
import Stream from 'node:stream'
import { performance } from '../polyfills.js'
import { stringify, handleValue, arrayParser, arraySerializer } from './types.js'
import { Errors } from './errors.js'
import Result from './result.js'
import Queue from './queue.js'
import { Query, CLOSE } from './query.js'
import b from './bytes.js'
export default Connection
let uid = 1
const Sync = b().S().end()
, Flush = b().H().end()
, SSLRequest = b().i32(8).i32(80877103).end(8)
, ExecuteUnnamed = Buffer.concat([b().E().str(b.N).i32(0).end(), Sync])
, DescribeUnnamed = b().D().str('S').str(b.N).end()
, noop = () => { /* noop */ }
const retryRoutines = new Set([
'FetchPreparedStatement',
'RevalidateCachedQuery',
'transformAssignedExpr'
])
const errorFields = {
83 : 'severity_local', // S
86 : 'severity', // V
67 : 'code', // C
77 : 'message', // M
68 : 'detail', // D
72 : 'hint', // H
80 : 'position', // P
112 : 'internal_position', // p
113 : 'internal_query', // q
87 : 'where', // W
115 : 'schema_name', // s
116 : 'table_name', // t
99 : 'column_name', // c
100 : 'data type_name', // d
110 : 'constraint_name', // n
70 : 'file', // F
76 : 'line', // L
82 : 'routine' // R
}
function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose = noop } = {}) {
const {
sslnegotiation,
ssl,
max,
user,
host,
port,
database,
parsers,
transform,
onnotice,
onnotify,
onparameter,
max_pipeline,
keep_alive,
backoff,
target_session_attrs
} = options
const sent = Queue()
, id = uid++
, backend = { pid: null, secret: null }
, idleTimer = timer(end, options.idle_timeout)
, lifeTimer = timer(end, options.max_lifetime)
, connectTimer = timer(connectTimedOut, options.connect_timeout)
let socket = null
, cancelMessage
, errorResponse = null
, result = new Result()
, incoming = Buffer.alloc(0)
, needsTypes = options.fetch_types
, backendParameters = {}
, statements = {}
, statementId = Math.random().toString(36).slice(2)
, statementCount = 1
, closedTime = 0
, remaining = 0
, hostIndex = 0
, retries = 0
, length = 0
, delay = 0
, rows = 0
, serverSignature = null
, nextWriteTimer = null
, terminated = false
, incomings = null
, results = null
, initial = null
, ending = null
, stream = null
, chunk = null
, ended = null
, nonce = null
, query = null
, final = null
const connection = {
queue: queues.closed,
idleTimer,
connect(query) {
initial = query
reconnect()
},
terminate,
execute,
cancel,
end,
count: 0,
id
}
queues.closed && queues.closed.push(connection)
return connection
async function createSocket() {
let x
try {
x = options.socket
? (await Promise.resolve(options.socket(options)))
: new net.Socket()
} catch (e) {
error(e)
return
}
x.on('error', error)
x.on('close', closed)
x.on('drain', drain)
return x
}
async function cancel({ pid, secret }, resolve, reject) {
try {
cancelMessage = b().i32(16).i32(80877102).i32(pid).i32(secret).end(16)
await connect()
socket.once('error', reject)
socket.once('close', resolve)
} catch (error) {
reject(error)
}
}
function execute(q) {
if (terminated)
return queryError(q, Errors.connection('CONNECTION_DESTROYED', options))
if (stream)
return queryError(q, Errors.generic('COPY_IN_PROGRESS', 'You cannot execute queries during copy'))
if (q.cancelled)
return
try {
q.state = backend
query
? sent.push(q)
: (query = q, query.active = true)
build(q)
return write(toBuffer(q))
&& !q.describeFirst
&& !q.cursorFn
&& sent.length < max_pipeline
&& (!q.options.onexecute || q.options.onexecute(connection))
} catch (error) {
sent.length === 0 && write(Sync)
errored(error)
return true
}
}
function toBuffer(q) {
if (q.parameters.length >= 65534)
throw Errors.generic('MAX_PARAMETERS_EXCEEDED', 'Max number of parameters (65534) exceeded')
return q.options.simple
? b().Q().str(q.statement.string + b.N).end()
: q.describeFirst
? Buffer.concat([describe(q), Flush])
: q.prepare
? q.prepared
? prepared(q)
: Buffer.concat([describe(q), prepared(q)])
: unnamed(q)
}
function describe(q) {
return Buffer.concat([
Parse(q.statement.string, q.parameters, q.statement.types, q.statement.name),
Describe('S', q.statement.name)
])
}
function prepared(q) {
return Buffer.concat([
Bind(q.parameters, q.statement.types, q.statement.name, q.cursorName),
q.cursorFn
? Execute('', q.cursorRows)
: ExecuteUnnamed
])
}
function unnamed(q) {
return Buffer.concat([
Parse(q.statement.string, q.parameters, q.statement.types),
DescribeUnnamed,
prepared(q)
])
}
function build(q) {
const parameters = []
, types = []
const string = stringify(q, q.strings[0], q.args[0], parameters, types, options)
!q.tagged && q.args.forEach(x => handleValue(x, parameters, types, options))
q.prepare = options.prepare && ('prepare' in q.options ? q.options.prepare : true)
q.string = string
q.signature = q.prepare && types + string
q.onlyDescribe && (delete statements[q.signature])
q.parameters = q.parameters || parameters
q.prepared = q.prepare && q.signature in statements
q.describeFirst = q.onlyDescribe || (parameters.length && !q.prepared)
q.statement = q.prepared
? statements[q.signature]
: { string, types, name: q.prepare ? statementId + statementCount++ : '' }
typeof options.debug === 'function' && options.debug(id, string, parameters, types)
}
function write(x, fn) {
chunk = chunk ? Buffer.concat([chunk, x]) : Buffer.from(x)
if (fn || chunk.length >= 1024)
return nextWrite(fn)
nextWriteTimer === null && (nextWriteTimer = setImmediate(nextWrite))
return true
}
function nextWrite(fn) {
const x = socket.write(chunk, fn)
nextWriteTimer !== null && clearImmediate(nextWriteTimer)
chunk = nextWriteTimer = null
return x
}
function connectTimedOut() {
errored(Errors.connection('CONNECT_TIMEOUT', options, socket))
socket.destroy()
}
async function secure() {
if (sslnegotiation !== 'direct') {
write(SSLRequest)
const canSSL = await new Promise(r => socket.once('data', x => r(x[0] === 83))) // S
if (!canSSL && ssl === 'prefer')
return connected()
}
const options = {
socket,
servername: net.isIP(socket.host) ? undefined : socket.host
}
if (sslnegotiation === 'direct')
options.ALPNProtocols = ['postgresql']
if (ssl === 'require' || ssl === 'allow' || ssl === 'prefer')
options.rejectUnauthorized = false
else if (typeof ssl === 'object')
Object.assign(options, ssl)
socket.removeAllListeners()
socket = tls.connect(options)
socket.on('secureConnect', connected)
socket.on('error', error)
socket.on('close', closed)
socket.on('drain', drain)
}
/* c8 ignore next 3 */
function drain() {
!query && onopen(connection)
}
function data(x) {
if (incomings) {
incomings.push(x)
remaining -= x.length
if (remaining > 0)
return
}
incoming = incomings
? Buffer.concat(incomings, length - remaining)
: incoming.length === 0
? x
: Buffer.concat([incoming, x], incoming.length + x.length)
while (incoming.length > 4) {
length = incoming.readUInt32BE(1)
if (length >= incoming.length) {
remaining = length - incoming.length
incomings = [incoming]
break
}
try {
handle(incoming.subarray(0, length + 1))
} catch (e) {
query && (query.cursorFn || query.describeFirst) && write(Sync)
errored(e)
}
incoming = incoming.subarray(length + 1)
remaining = 0
incomings = null
}
}
async function connect() {
terminated = false
backendParameters = {}
socket || (socket = await createSocket())
if (!socket)
return
connectTimer.start()
if (options.socket)
return ssl ? secure() : connected()
socket.on('connect', ssl ? secure : connected)
if (options.path)
return socket.connect(options.path)
socket.ssl = ssl
socket.connect(port[hostIndex], host[hostIndex])
socket.host = host[hostIndex]
socket.port = port[hostIndex]
hostIndex = (hostIndex + 1) % port.length
}
function reconnect() {
setTimeout(connect, closedTime ? Math.max(0, closedTime + delay - performance.now()) : 0)
}
function connected() {
try {
statements = {}
needsTypes = options.fetch_types
statementId = Math.random().toString(36).slice(2)
statementCount = 1
lifeTimer.start()
socket.on('data', data)
keep_alive && socket.setKeepAlive && socket.setKeepAlive(true, 1000 * keep_alive)
const s = StartupMessage()
write(s)
} catch (err) {
error(err)
}
}
function error(err) {
if (connection.queue === queues.connecting && options.host[retries + 1])
return
errored(err)
while (sent.length)
queryError(sent.shift(), err)
}
function errored(err) {
stream && (stream.destroy(err), stream = null)
query && queryError(query, err)
initial && (queryError(initial, err), initial = null)
}
function queryError(query, err) {
if (query.reserve)
return query.reject(err)
if (!err || typeof err !== 'object')
err = new Error(err)
'query' in err || 'parameters' in err || Object.defineProperties(err, {
stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug },
query: { value: query.string, enumerable: options.debug },
parameters: { value: query.parameters, enumerable: options.debug },
args: { value: query.args, enumerable: options.debug },
types: { value: query.statement && query.statement.types, enumerable: options.debug }
})
query.reject(err)
}
function end() {
return ending || (
!connection.reserved && onend(connection),
!connection.reserved && !initial && !query && sent.length === 0
? (terminate(), new Promise(r => socket && socket.readyState !== 'closed' ? socket.once('close', r) : r()))
: ending = new Promise(r => ended = r)
)
}
function terminate() {
terminated = true
if (stream || query || initial || sent.length)
error(Errors.connection('CONNECTION_DESTROYED', options))
clearImmediate(nextWriteTimer)
if (socket) {
socket.removeListener('data', data)
socket.removeListener('connect', connected)
socket.readyState === 'open' && socket.end(b().X().end())
}
ended && (ended(), ending = ended = null)
}
async function closed(hadError) {
incoming = Buffer.alloc(0)
remaining = 0
incomings = null
clearImmediate(nextWriteTimer)
socket.removeListener('data', data)
socket.removeListener('connect', connected)
idleTimer.cancel()
lifeTimer.cancel()
connectTimer.cancel()
socket.removeAllListeners()
socket = null
if (initial)
return reconnect()
!hadError && (query || sent.length) && error(Errors.connection('CONNECTION_CLOSED', options, socket))
closedTime = performance.now()
hadError && options.shared.retries++
delay = (typeof backoff === 'function' ? backoff(options.shared.retries) : backoff) * 1000
onclose(connection, Errors.connection('CONNECTION_CLOSED', options, socket))
}
/* Handlers */
function handle(xs, x = xs[0]) {
(
x === 68 ? DataRow : // D
x === 100 ? CopyData : // d
x === 65 ? NotificationResponse : // A
x === 83 ? ParameterStatus : // S
x === 90 ? ReadyForQuery : // Z
x === 67 ? CommandComplete : // C
x === 50 ? BindComplete : // 2
x === 49 ? ParseComplete : // 1
x === 116 ? ParameterDescription : // t
x === 84 ? RowDescription : // T
x === 82 ? Authentication : // R
x === 110 ? NoData : // n
x === 75 ? BackendKeyData : // K
x === 69 ? ErrorResponse : // E
x === 115 ? PortalSuspended : // s
x === 51 ? CloseComplete : // 3
x === 71 ? CopyInResponse : // G
x === 78 ? NoticeResponse : // N
x === 72 ? CopyOutResponse : // H
x === 99 ? CopyDone : // c
x === 73 ? EmptyQueryResponse : // I
x === 86 ? FunctionCallResponse : // V
x === 118 ? NegotiateProtocolVersion : // v
x === 87 ? CopyBothResponse : // W
/* c8 ignore next */
UnknownMessage
)(xs)
}
function DataRow(x) {
let index = 7
let length
let column
let value
const row = query.isRaw ? new Array(query.statement.columns.length) : {}
for (let i = 0; i < query.statement.columns.length; i++) {
column = query.statement.columns[i]
length = x.readInt32BE(index)
index += 4
value = length === -1
? null
: query.isRaw === true
? x.subarray(index, index += length)
: column.parser === undefined
? x.toString('utf8', index, index += length)
: column.parser.array === true
? column.parser(x.toString('utf8', index + 1, index += length))
: column.parser(x.toString('utf8', index, index += length))
query.isRaw
? (row[i] = query.isRaw === true
? value
: transform.value.from ? transform.value.from(value, column) : value)
: (row[column.name] = transform.value.from ? transform.value.from(value, column) : value)
}
query.forEachFn
? query.forEachFn(transform.row.from ? transform.row.from(row) : row, result)
: (result[rows++] = transform.row.from ? transform.row.from(row) : row)
}
function ParameterStatus(x) {
const [k, v] = x.toString('utf8', 5, x.length - 1).split(b.N)
backendParameters[k] = v
if (options.parameters[k] !== v) {
options.parameters[k] = v
onparameter && onparameter(k, v)
}
}
function ReadyForQuery(x) {
if (query) {
if (errorResponse) {
query.retried
? errored(query.retried)
: query.prepared && retryRoutines.has(errorResponse.routine)
? retry(query, errorResponse)
: errored(errorResponse)
} else {
query.resolve(results || result)
}
} else if (errorResponse) {
errored(errorResponse)
}
query = results = errorResponse = null
result = new Result()
connectTimer.cancel()
if (initial) {
if (target_session_attrs) {
if (!backendParameters.in_hot_standby || !backendParameters.default_transaction_read_only)
return fetchState()
else if (tryNext(target_session_attrs, backendParameters))
return terminate()
}
if (needsTypes) {
initial.reserve && (initial = null)
return fetchArrayTypes()
}
initial && !initial.reserve && execute(initial)
options.shared.retries = retries = 0
initial = null
return
}
while (sent.length && (query = sent.shift()) && (query.active = true, query.cancelled))
Connection(options).cancel(query.state, query.cancelled.resolve, query.cancelled.reject)
if (query)
return // Consider opening if able and sent.length < 50
connection.reserved
? !connection.reserved.release && x[5] === 73 // I
? ending
? terminate()
: (connection.reserved = null, onopen(connection))
: connection.reserved()
: ending
? terminate()
: onopen(connection)
}
function CommandComplete(x) {
rows = 0
for (let i = x.length - 1; i > 0; i--) {
if (x[i] === 32 && x[i + 1] < 58 && result.count === null)
result.count = +x.toString('utf8', i + 1, x.length - 1)
if (x[i - 1] >= 65) {
result.command = x.toString('utf8', 5, i)
result.state = backend
break
}
}
final && (final(), final = null)
if (result.command === 'BEGIN' && max !== 1 && !connection.reserved)
return errored(Errors.generic('UNSAFE_TRANSACTION', 'Only use sql.begin, sql.reserved or max: 1'))
if (query.options.simple)
return BindComplete()
if (query.cursorFn) {
result.count && query.cursorFn(result)
write(Sync)
}
}
function ParseComplete() {
query.parsing = false
}
function BindComplete() {
!result.statement && (result.statement = query.statement)
result.columns = query.statement.columns
}
function ParameterDescription(x) {
const length = x.readUInt16BE(5)
for (let i = 0; i < length; ++i)
!query.statement.types[i] && (query.statement.types[i] = x.readUInt32BE(7 + i * 4))
query.prepare && (statements[query.signature] = query.statement)
query.describeFirst && !query.onlyDescribe && (write(prepared(query)), query.describeFirst = false)
}
function RowDescription(x) {
if (result.command) {
results = results || [result]
results.push(result = new Result())
result.count = null
query.statement.columns = null
}
const length = x.readUInt16BE(5)
let index = 7
let start
query.statement.columns = Array(length)
for (let i = 0; i < length; ++i) {
start = index
while (x[index++] !== 0);
const table = x.readUInt32BE(index)
const number = x.readUInt16BE(index + 4)
const type = x.readUInt32BE(index + 6)
query.statement.columns[i] = {
name: transform.column.from
? transform.column.from(x.toString('utf8', start, index - 1))
: x.toString('utf8', start, index - 1),
parser: parsers[type],
table,
number,
type
}
index += 18
}
result.statement = query.statement
if (query.onlyDescribe)
return (query.resolve(query.statement), write(Sync))
}
async function Authentication(x, type = x.readUInt32BE(5)) {
(
type === 3 ? AuthenticationCleartextPassword :
type === 5 ? AuthenticationMD5Password :
type === 10 ? SASL :
type === 11 ? SASLContinue :
type === 12 ? SASLFinal :
type !== 0 ? UnknownAuth :
noop
)(x, type)
}
/* c8 ignore next 5 */
async function AuthenticationCleartextPassword() {
const payload = await Pass()
write(
b().p().str(payload).z(1).end()
)
}
async function AuthenticationMD5Password(x) {
const payload = 'md5' + (
await md5(
Buffer.concat([
Buffer.from(await md5((await Pass()) + user)),
x.subarray(9)
])
)
)
write(
b().p().str(payload).z(1).end()
)
}
async function SASL() {
nonce = (await crypto.randomBytes(18)).toString('base64')
b().p().str('SCRAM-SHA-256' + b.N)
const i = b.i
write(b.inc(4).str('n,,n=*,r=' + nonce).i32(b.i - i - 4, i).end())
}
async function SASLContinue(x) {
const res = x.toString('utf8', 9).split(',').reduce((acc, x) => (acc[x[0]] = x.slice(2), acc), {})
const saltedPassword = await crypto.pbkdf2Sync(
await Pass(),
Buffer.from(res.s, 'base64'),
parseInt(res.i), 32,
'sha256'
)
const clientKey = await hmac(saltedPassword, 'Client Key')
const auth = 'n=*,r=' + nonce + ','
+ 'r=' + res.r + ',s=' + res.s + ',i=' + res.i
+ ',c=biws,r=' + res.r
serverSignature = (await hmac(await hmac(saltedPassword, 'Server Key'), auth)).toString('base64')
const payload = 'c=biws,r=' + res.r + ',p=' + xor(
clientKey, Buffer.from(await hmac(await sha256(clientKey), auth))
).toString('base64')
write(
b().p().str(payload).end()
)
}
function SASLFinal(x) {
if (x.toString('utf8', 9).split(b.N, 1)[0].slice(2) === serverSignature)
return
/* c8 ignore next 5 */
errored(Errors.generic('SASL_SIGNATURE_MISMATCH', 'The server did not return the correct signature'))
socket.destroy()
}
function Pass() {
return Promise.resolve(typeof options.pass === 'function'
? options.pass()
: options.pass
)
}
function NoData() {
result.statement = query.statement
result.statement.columns = []
if (query.onlyDescribe)
return (query.resolve(query.statement), write(Sync))
}
function BackendKeyData(x) {
backend.pid = x.readUInt32BE(5)
backend.secret = x.readUInt32BE(9)
}
async function fetchArrayTypes() {
needsTypes = false
const types = await new Query([`
select b.oid, b.typarray
from pg_catalog.pg_type a
left join pg_catalog.pg_type b on b.oid = a.typelem
where a.typcategory = 'A'
group by b.oid, b.typarray
order by b.oid
`], [], execute)
types.forEach(({ oid, typarray }) => addArrayType(oid, typarray))
}
function addArrayType(oid, typarray) {
if (!!options.parsers[typarray] && !!options.serializers[typarray]) return
const parser = options.parsers[oid]
options.shared.typeArrayMap[oid] = typarray
options.parsers[typarray] = (xs) => arrayParser(xs, parser, typarray)
options.parsers[typarray].array = true
options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid], options, typarray)
}
function tryNext(x, xs) {
return (
(x === 'read-write' && xs.default_transaction_read_only === 'on') ||
(x === 'read-only' && xs.default_transaction_read_only === 'off') ||
(x === 'primary' && xs.in_hot_standby === 'on') ||
(x === 'standby' && xs.in_hot_standby === 'off') ||
(x === 'prefer-standby' && xs.in_hot_standby === 'off' && options.host[retries])
)
}
function fetchState() {
const query = new Query([`
show transaction_read_only;
select pg_catalog.pg_is_in_recovery()
`], [], execute, null, { simple: true })
query.resolve = ([[a], [b]]) => {
backendParameters.default_transaction_read_only = a.transaction_read_only
backendParameters.in_hot_standby = b.pg_is_in_recovery ? 'on' : 'off'
}
query.execute()
}
function ErrorResponse(x) {
if (query) {
(query.cursorFn || query.describeFirst) && write(Sync)
errorResponse = Errors.postgres(parseError(x))
} else {
errored(Errors.postgres(parseError(x)))
}
}
function retry(q, error) {
delete statements[q.signature]
q.retried = error
execute(q)
}
function NotificationResponse(x) {
if (!onnotify)
return
let index = 9
while (x[index++] !== 0);
onnotify(
x.toString('utf8', 9, index - 1),
x.toString('utf8', index, x.length - 1)
)
}
async function PortalSuspended() {
try {
const x = await Promise.resolve(query.cursorFn(result))
rows = 0
x === CLOSE
? write(Close(query.portal))
: (result = new Result(), write(Execute('', query.cursorRows)))
} catch (err) {
write(Sync)
query.reject(err)
}
}
function CloseComplete() {
result.count && query.cursorFn(result)
query.resolve(result)
}
function CopyInResponse() {
stream = new Stream.Writable({
autoDestroy: true,
write(chunk, encoding, callback) {
socket.write(b().d().raw(chunk).end(), callback)
},
destroy(error, callback) {
callback(error)
socket.write(b().f().str(error + b.N).end())
stream = null
},
final(callback) {
socket.write(b().c().end())
final = callback
stream = null
}
})
query.resolve(stream)
}
function CopyOutResponse() {
stream = new Stream.Readable({
read() { socket.resume() }
})
query.resolve(stream)
}
/* c8 ignore next 3 */
function CopyBothResponse() {
stream = new Stream.Duplex({
autoDestroy: true,
read() { socket.resume() },
/* c8 ignore next 11 */
write(chunk, encoding, callback) {
socket.write(b().d().raw(chunk).end(), callback)
},
destroy(error, callback) {
callback(error)
socket.write(b().f().str(error + b.N).end())
stream = null
},
final(callback) {
socket.write(b().c().end())
final = callback
}
})
query.resolve(stream)
}
function CopyData(x) {
stream && (stream.push(x.subarray(5)) || socket.pause())
}
function CopyDone() {
stream && stream.push(null)
stream = null
}
function NoticeResponse(x) {
onnotice
? onnotice(parseError(x))
: console.log(parseError(x)) // eslint-disable-line
}
/* c8 ignore next 3 */
function EmptyQueryResponse() {
/* noop */
}
/* c8 ignore next 3 */
function FunctionCallResponse() {
errored(Errors.notSupported('FunctionCallResponse'))
}
/* c8 ignore next 3 */
function NegotiateProtocolVersion() {
errored(Errors.notSupported('NegotiateProtocolVersion'))
}
/* c8 ignore next 3 */
function UnknownMessage(x) {
console.error('Postgres.js : Unknown Message:', x[0]) // eslint-disable-line
}
/* c8 ignore next 3 */
function UnknownAuth(x, type) {
console.error('Postgres.js : Unknown Auth:', type) // eslint-disable-line
}
/* Messages */
function Bind(parameters, types, statement = '', portal = '') {
let prev
, type
b().B().str(portal + b.N).str(statement + b.N).i16(0).i16(parameters.length)
parameters.forEach((x, i) => {
if (x === null)
return b.i32(0xFFFFFFFF)
type = types[i]
parameters[i] = x = type in options.serializers
? options.serializers[type](x)
: '' + x
prev = b.i
b.inc(4).str(x).i32(b.i - prev - 4, prev)
})
b.i16(0)
return b.end()
}
function Parse(str, parameters, types, name = '') {
b().P().str(name + b.N).str(str + b.N).i16(parameters.length)
parameters.forEach((x, i) => b.i32(types[i] || 0))
return b.end()
}
function Describe(x, name = '') {
return b().D().str(x).str(name + b.N).end()
}
function Execute(portal = '', rows = 0) {
return Buffer.concat([
b().E().str(portal + b.N).i32(rows).end(),
Flush
])
}
function Close(portal = '') {
return Buffer.concat([
b().C().str('P').str(portal + b.N).end(),
b().S().end()
])
}
function StartupMessage() {
return cancelMessage || b().inc(4).i16(3).z(2).str(
Object.entries(Object.assign({
user,
database,
client_encoding: 'UTF8'
},
options.connection
)).filter(([, v]) => v).map(([k, v]) => k + b.N + v).join(b.N)
).z(2).end(0)
}
}
function parseError(x) {
const error = {}
let start = 5
for (let i = 5; i < x.length - 1; i++) {
if (x[i] === 0) {
error[errorFields[x[start]]] = x.toString('utf8', start + 1, i)
start = i + 1
}
}
return error
}
function md5(x) {
return crypto.createHash('md5').update(x).digest('hex')
}
function hmac(key, x) {
return crypto.createHmac('sha256', key).update(x).digest()
}
function sha256(x) {
return crypto.createHash('sha256').update(x).digest()
}
function xor(a, b) {
const length = Math.max(a.length, b.length)
const buffer = Buffer.allocUnsafe(length)
for (let i = 0; i < length; i++)
buffer[i] = a[i] ^ b[i]
return buffer
}
function timer(fn, seconds) {
seconds = typeof seconds === 'function' ? seconds() : seconds
if (!seconds)
return { cancel: noop, start: noop }
let timer
return {
cancel() {
timer && (clearTimeout(timer), timer = null)
},
start() {
timer && clearTimeout(timer)
timer = setTimeout(done, seconds * 1000, arguments)
}
}
function done(args) {
fn.apply(null, args)
timer = null
}
}
================================================
FILE: cf/src/errors.js
================================================
export class PostgresError extends Error {
constructor(x) {
super(x.message)
this.name = this.constructor.name
Object.assign(this, x)
}
}
export const Errors = {
connection,
postgres,
generic,
notSupported
}
function connection(x, options, socket) {
const { host, port } = socket || options
const error = Object.assign(
new Error(('write ' + x + ' ' + (options.path || (host + ':' + port)))),
{
code: x,
errno: x,
address: options.path || host
}, options.path ? {} : { port: port }
)
Error.captureStackTrace(error, connection)
return error
}
function postgres(x) {
const error = new PostgresError(x)
Error.captureStackTrace(error, postgres)
return error
}
function generic(code, message) {
const error = Object.assign(new Error(code + ': ' + message), { code })
Error.captureStackTrace(error, generic)
return error
}
/* c8 ignore next 10 */
function notSupported(x) {
const error = Object.assign(
new Error(x + ' (B) is not supported'),
{
code: 'MESSAGE_NOT_SUPPORTED',
name: x
}
)
Error.captureStackTrace(error, notSupported)
return error
}
================================================
FILE: cf/src/index.js
================================================
import { process } from '../polyfills.js'
import { os } from '../polyfills.js'
import { fs } from '../polyfills.js'
import {
mergeUserTypes,
inferType,
Parameter,
Identifier,
Builder,
toPascal,
pascal,
toCamel,
camel,
toKebab,
kebab,
fromPascal,
fromCamel,
fromKebab
} from './types.js'
import Connection from './connection.js'
import { Query, CLOSE } from './query.js'
import Queue from './queue.js'
import { Errors, PostgresError } from './errors.js'
import Subscribe from './subscribe.js'
import largeObject from './large.js'
Object.assign(Postgres, {
PostgresError,
toPascal,
pascal,
toCamel,
camel,
toKebab,
kebab,
fromPascal,
fromCamel,
fromKebab,
BigInt: {
to: 20,
from: [20],
parse: x => BigInt(x), // eslint-disable-line
serialize: x => x.toString()
}
})
export default Postgres
function Postgres(a, b) {
const options = parseOptions(a, b)
, subscribe = options.no_subscribe || Subscribe(Postgres, { ...options })
let ending = false
const queries = Queue()
, connecting = Queue()
, reserved = Queue()
, closed = Queue()
, ended = Queue()
, open = Queue()
, busy = Queue()
, full = Queue()
, queues = { connecting, reserved, closed, ended, open, busy, full }
const connections = [...Array(options.max)].map(() => Connection(options, queues, { onopen, onend, onclose }))
const sql = Sql(handler)
Object.assign(sql, {
get parameters() { return options.parameters },
largeObject: largeObject.bind(null, sql),
subscribe,
CLOSE,
END: CLOSE,
PostgresError,
options,
reserve,
listen,
begin,
close,
end
})
return sql
function Sql(handler) {
handler.debug = options.debug
Object.entries(options.types).reduce((acc, [name, type]) => {
acc[name] = (x) => new Parameter(x, type.to)
return acc
}, typed)
Object.assign(sql, {
types: typed,
typed,
unsafe,
notify,
array,
json,
file
})
return sql
function typed(value, type) {
return new Parameter(value, type)
}
function sql(strings, ...args) {
const query = strings && Array.isArray(strings.raw)
? new Query(strings, args, handler, cancel)
: typeof strings === 'string' && !args.length
? new Identifier(options.transform.column.to ? options.transform.column.to(strings) : strings)
: new Builder(strings, args)
return query
}
function unsafe(string, args = [], options = {}) {
arguments.length === 2 && !Array.isArray(args) && (options = args, args = [])
const query = new Query([string], args, handler, cancel, {
prepare: false,
...options,
simple: 'simple' in options ? options.simple : args.length === 0
})
return query
}
function file(path, args = [], options = {}) {
arguments.length === 2 && !Array.isArray(args) && (options = args, args = [])
const query = new Query([], args, (query) => {
fs.readFile(path, 'utf8', (err, string) => {
if (err)
return query.reject(err)
query.strings = [string]
handler(query)
})
}, cancel, {
...options,
simple: 'simple' in options ? options.simple : args.length === 0
})
return query
}
}
async function listen(name, fn, onlisten) {
const listener = { fn, onlisten }
const sql = listen.sql || (listen.sql = Postgres({
...options,
max: 1,
idle_timeout: null,
max_lifetime: null,
fetch_types: false,
onclose() {
Object.entries(listen.channels).forEach(([name, { listeners }]) => {
delete listen.channels[name]
Promise.all(listeners.map(l => listen(name, l.fn, l.onlisten).catch(() => { /* noop */ })))
})
},
onnotify(c, x) {
c in listen.channels && listen.channels[c].listeners.forEach(l => l.fn(x))
}
}))
const channels = listen.channels || (listen.channels = {})
, exists = name in channels
if (exists) {
channels[name].listeners.push(listener)
const result = await channels[name].result
listener.onlisten && listener.onlisten()
return { state: result.state, unlisten }
}
channels[name] = { result: sql`listen ${
sql.unsafe('"' + name.replace(/"/g, '""') + '"')
}`, listeners: [listener] }
const result = await channels[name].result
listener.onlisten && listener.onlisten()
return { state: result.state, unlisten }
async function unlisten() {
if (name in channels === false)
return
channels[name].listeners = channels[name].listeners.filter(x => x !== listener)
if (channels[name].listeners.length)
return
delete channels[name]
return sql`unlisten ${
sql.unsafe('"' + name.replace(/"/g, '""') + '"')
}`
}
}
async function notify(channel, payload) {
return await sql`select pg_notify(${ channel }, ${ '' + payload })`
}
async function reserve() {
const queue = Queue()
const c = open.length
? open.shift()
: await new Promise((resolve, reject) => {
const query = { reserve: resolve, reject }
queries.push(query)
closed.length && connect(closed.shift(), query)
})
move(c, reserved)
c.reserved = () => queue.length
? c.execute(queue.shift())
: move(c, reserved)
c.reserved.release = true
const sql = Sql(handler)
sql.release = () => {
c.reserved = null
onopen(c)
}
return sql
function handler(q) {
c.queue === full
? queue.push(q)
: c.execute(q) || move(c, full)
}
}
async function begin(options, fn) {
!fn && (fn = options, options = '')
const queries = Queue()
let savepoints = 0
, connection
, prepare = null
try {
await sql.unsafe('begin ' + options.replace(/[^a-z ]/ig, ''), [], { onexecute }).execute()
return await Promise.race([
scope(connection, fn),
new Promise((_, reject) => connection.onclose = reject)
])
} catch (error) {
throw error
}
async function scope(c, fn, name) {
const sql = Sql(handler)
sql.savepoint = savepoint
sql.prepare = x => prepare = x.replace(/[^a-z0-9$-_. ]/gi)
let uncaughtError
, result
name && await sql`savepoint ${ sql(name) }`
try {
result = await new Promise((resolve, reject) => {
const x = fn(sql)
Promise.resolve(Array.isArray(x) ? Promise.all(x) : x).then(resolve, reject)
})
if (uncaughtError)
throw uncaughtError
} catch (e) {
await (name
? sql`rollback to ${ sql(name) }`
: sql`rollback`
)
throw e instanceof PostgresError && e.code === '25P02' && uncaughtError || e
}
if (!name) {
prepare
? await sql`prepare transaction '${ sql.unsafe(prepare) }'`
: await sql`commit`
}
return result
function savepoint(name, fn) {
if (name && Array.isArray(name.raw))
return savepoint(sql => sql.apply(sql, arguments))
arguments.length === 1 && (fn = name, name = null)
return scope(c, fn, 's' + savepoints++ + (name ? '_' + name : ''))
}
function handler(q) {
q.catch(e => uncaughtError || (uncaughtError = e))
c.queue === full
? queries.push(q)
: c.execute(q) || move(c, full)
}
}
function onexecute(c) {
connection = c
move(c, reserved)
c.reserved = () => queries.length
? c.execute(queries.shift())
: move(c, reserved)
}
}
function move(c, queue) {
c.queue.remove(c)
queue.push(c)
c.queue = queue
queue === open
? c.idleTimer.start()
: c.idleTimer.cancel()
return c
}
function json(x) {
return new Parameter(x, 3802)
}
function array(x, type) {
if (!Array.isArray(x))
return array(Array.from(arguments))
return new Parameter(x, type || (x.length ? inferType(x) || 25 : 0), options.shared.typeArrayMap)
}
function handler(query) {
if (ending)
return query.reject(Errors.connection('CONNECTION_ENDED', options, options))
if (open.length)
return go(open.shift(), query)
if (closed.length)
return connect(closed.shift(), query)
busy.length
? go(busy.shift(), query)
: queries.push(query)
}
function go(c, query) {
return c.execute(query)
? move(c, busy)
: move(c, full)
}
function cancel(query) {
return new Promise((resolve, reject) => {
query.state
? query.active
? Connection(options).cancel(query.state, resolve, reject)
: query.cancelled = { resolve, reject }
: (
queries.remove(query),
query.cancelled = true,
query.reject(Errors.generic('57014', 'canceling statement due to user request')),
resolve()
)
})
}
async function end({ timeout = null } = {}) {
if (ending)
return ending
await 1
let timer
return ending = Promise.race([
new Promise(r => timeout !== null && (timer = setTimeout(destroy, timeout * 1000, r))),
Promise.all(connections.map(c => c.end()).concat(
listen.sql ? listen.sql.end({ timeout: 0 }) : [],
subscribe.sql ? subscribe.sql.end({ timeout: 0 }) : []
))
]).then(() => clearTimeout(timer))
}
async function close() {
await Promise.all(connections.map(c => c.end()))
}
async function destroy(resolve) {
await Promise.all(connections.map(c => c.terminate()))
while (queries.length)
queries.shift().reject(Errors.connection('CONNECTION_DESTROYED', options))
resolve()
}
function connect(c, query) {
move(c, connecting)
c.connect(query)
return c
}
function onend(c) {
move(c, ended)
}
function onopen(c) {
if (queries.length === 0)
return move(c, open)
let max = Math.ceil(queries.length / (connecting.length + 1))
, ready = true
while (ready && queries.length && max-- > 0) {
const query = queries.shift()
if (query.reserve)
return query.reserve(c)
ready = c.execute(query)
}
ready
? move(c, busy)
: move(c, full)
}
function onclose(c, e) {
move(c, closed)
c.reserved = null
c.onclose && (c.onclose(e), c.onclose = null)
options.onclose && options.onclose(c.id)
queries.length && connect(c, queries.shift())
}
}
function parseOptions(a, b) {
if (a && a.shared)
return a
const env = process.env // eslint-disable-line
, o = (!a || typeof a === 'string' ? b : a) || {}
, { url, multihost } = parseUrl(a)
, query = [...url.searchParams].reduce((a, [b, c]) => (a[b] = c, a), {})
, host = o.hostname || o.host || multihost || url.hostname || env.PGHOST || 'localhost'
, port = o.port || url.port || env.PGPORT || 5432
, user = o.user || o.username || url.username || env.PGUSERNAME || env.PGUSER || osUsername()
o.no_prepare && (o.prepare = false)
query.sslmode && (query.ssl = query.sslmode, delete query.sslmode)
'timeout' in o && (console.log('The timeout option is deprecated, use idle_timeout instead'), o.idle_timeout = o.timeout) // eslint-disable-line
query.sslrootcert === 'system' && (query.ssl = 'verify-full')
const ints = ['idle_timeout', 'connect_timeout', 'max_lifetime', 'max_pipeline', 'backoff', 'keep_alive']
const defaults = {
max : globalThis.Cloudflare ? 3 : 10,
ssl : false,
sslnegotiation : null,
idle_timeout : null,
connect_timeout : 30,
max_lifetime : max_lifetime,
max_pipeline : 100,
backoff : backoff,
keep_alive : 60,
prepare : true,
debug : false,
fetch_types : true,
publications : 'alltables',
target_session_attrs: null
}
return {
host : Array.isArray(host) ? host : host.split(',').map(x => x.split(':')[0]),
port : Array.isArray(port) ? port : host.split(',').map(x => parseInt(x.split(':')[1] || port)),
path : o.path || host.indexOf('/') > -1 && host + '/.s.PGSQL.' + port,
database : o.database || o.db || (url.pathname || '').slice(1) || env.PGDATABASE || user,
user : user,
pass : o.pass || o.password || url.password || env.PGPASSWORD || '',
...Object.entries(defaults).reduce(
(acc, [k, d]) => {
const value = k in o ? o[k] : k in query
? (query[k] === 'disable' || query[k] === 'false' ? false : query[k])
: env['PG' + k.toUpperCase()] || d
acc[k] = typeof value === 'string' && ints.includes(k)
? +value
: value
return acc
},
{}
),
connection : {
application_name: env.PGAPPNAME || 'postgres.js',
...o.connection,
...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {})
},
types : o.types || {},
target_session_attrs: tsa(o, url, env),
onnotice : o.onnotice,
onnotify : o.onnotify,
onclose : o.onclose,
onparameter : o.onparameter,
socket : o.socket,
transform : parseTransform(o.transform || { undefined: undefined }),
parameters : {},
shared : { retries: 0, typeArrayMap: {} },
...mergeUserTypes(o.types)
}
}
function tsa(o, url, env) {
const x = o.target_session_attrs || url.searchParams.get('target_session_attrs') || env.PGTARGETSESSIONATTRS
if (!x || ['read-write', 'read-only', 'primary', 'standby', 'prefer-standby'].includes(x))
return x
throw new Error('target_session_attrs ' + x + ' is not supported')
}
function backoff(retries) {
return (0.5 + Math.random() / 2) * Math.min(3 ** retries / 100, 20)
}
function max_lifetime() {
return 60 * (30 + Math.random() * 30)
}
function parseTransform(x) {
return {
undefined: x.undefined,
column: {
from: typeof x.column === 'function' ? x.column : x.column && x.column.from,
to: x.column && x.column.to
},
value: {
from: typeof x.value === 'function' ? x.value : x.value && x.value.from,
to: x.value && x.value.to
},
row: {
from: typeof x.row === 'function' ? x.row : x.row && x.row.from,
to: x.row && x.row.to
}
}
}
function parseUrl(url) {
if (!url || typeof url !== 'string')
return { url: { searchParams: new Map() } }
let host = url
host = host.slice(host.indexOf('://') + 3).split(/[?/]/)[0]
host = decodeURIComponent(host.slice(host.indexOf('@') + 1))
const urlObj = new URL(url.replace(host, host.split(',')[0]))
return {
url: {
username: decodeURIComponent(urlObj.username),
password: decodeURIComponent(urlObj.password),
host: urlObj.host,
hostname: urlObj.hostname,
port: urlObj.port,
pathname: urlObj.pathname,
searchParams: urlObj.searchParams
},
multihost: host.indexOf(',') > -1 && host
}
}
function osUsername() {
try {
return os.userInfo().username // eslint-disable-line
} catch (_) {
return process.env.USERNAME || process.env.USER || process.env.LOGNAME // eslint-disable-line
}
}
================================================
FILE: cf/src/large.js
================================================
import Stream from 'node:stream'
export default function largeObject(sql, oid, mode = 0x00020000 | 0x00040000) {
return new Promise(async(resolve, reject) => {
await sql.begin(async sql => {
let finish
!oid && ([{ oid }] = await sql`select lo_creat(-1) as oid`)
const [{ fd }] = await sql`select lo_open(${ oid }, ${ mode }) as fd`
const lo = {
writable,
readable,
close : () => sql`select lo_close(${ fd })`.then(finish),
tell : () => sql`select lo_tell64(${ fd })`,
read : (x) => sql`select loread(${ fd }, ${ x }) as data`,
write : (x) => sql`select lowrite(${ fd }, ${ x })`,
truncate : (x) => sql`select lo_truncate64(${ fd }, ${ x })`,
seek : (x, whence = 0) => sql`select lo_lseek64(${ fd }, ${ x }, ${ whence })`,
size : () => sql`
select
lo_lseek64(${ fd }, location, 0) as position,
seek.size
from (
select
lo_lseek64($1, 0, 2) as size,
tell.location
from (select lo_tell64($1) as location) tell
) seek
`
}
resolve(lo)
return new Promise(async r => finish = r)
async function readable({
highWaterMark = 2048 * 8,
start = 0,
end = Infinity
} = {}) {
let max = end - start
start && await lo.seek(start)
return new Stream.Readable({
highWaterMark,
async read(size) {
const l = size > max ? size - max : size
max -= size
const [{ data }] = await lo.read(l)
this.push(data)
if (data.length < size)
this.push(null)
}
})
}
async function writable({
highWaterMark = 2048 * 8,
start = 0
} = {}) {
start && await lo.seek(start)
return new Stream.Writable({
highWaterMark,
write(chunk, encoding, callback) {
lo.write(chunk).then(() => callback(), callback)
}
})
}
}).catch(reject)
})
}
================================================
FILE: cf/src/query.js
================================================
const originCache = new Map()
, originStackCache = new Map()
, originError = Symbol('OriginError')
export const CLOSE = {}
export class Query extends Promise {
constructor(strings, args, handler, canceller, options = {}) {
let resolve
, reject
super((a, b) => {
resolve = a
reject = b
})
this.tagged = Array.isArray(strings.raw)
this.strings = strings
this.args = args
this.handler = handler
this.canceller = canceller
this.options = options
this.state = null
this.statement = null
this.resolve = x => (this.active = false, resolve(x))
this.reject = x => (this.active = false, reject(x))
this.active = false
this.cancelled = null
this.executed = false
this.signature = ''
this[originError] = this.handler.debug
? new Error()
: this.tagged && cachedError(this.strings)
}
get origin() {
return (this.handler.debug
? this[originError].stack
: this.tagged && originStackCache.has(this.strings)
? originStackCache.get(this.strings)
: originStackCache.set(this.strings, this[originError].stack).get(this.strings)
) || ''
}
static get [Symbol.species]() {
return Promise
}
cancel() {
return this.canceller && (this.canceller(this), this.canceller = null)
}
simple() {
this.options.simple = true
this.options.prepare = false
return this
}
async readable() {
this.simple()
this.streaming = true
return this
}
async writable() {
this.simple()
this.streaming = true
return this
}
cursor(rows = 1, fn) {
this.options.simple = false
if (typeof rows === 'function') {
fn = rows
rows = 1
}
this.cursorRows = rows
if (typeof fn === 'function')
return (this.cursorFn = fn, this)
let prev
return {
[Symbol.asyncIterator]: () => ({
next: () => {
if (this.executed && !this.active)
return { done: true }
prev && prev()
const promise = new Promise((resolve, reject) => {
this.cursorFn = value => {
resolve({ value, done: false })
return new Promise(r => prev = r)
}
this.resolve = () => (this.active = false, resolve({ done: true }))
this.reject = x => (this.active = false, reject(x))
})
this.execute()
return promise
},
return() {
prev && prev(CLOSE)
return { done: true }
}
})
}
}
describe() {
this.options.simple = false
this.onlyDescribe = this.options.prepare = true
return this
}
stream() {
throw new Error('.stream has been renamed to .forEach')
}
forEach(fn) {
this.forEachFn = fn
this.handle()
return this
}
raw() {
this.isRaw = true
return this
}
values() {
this.isRaw = 'values'
return this
}
async handle() {
!this.executed && (this.executed = true) && await 1 && this.handler(this)
}
execute() {
this.handle()
return this
}
then() {
this.handle()
return super.then.apply(this, arguments)
}
catch() {
this.handle()
return super.catch.apply(this, arguments)
}
finally() {
this.handle()
return super.finally.apply(this, arguments)
}
}
function cachedError(xs) {
if (originCache.has(xs))
return originCache.get(xs)
const x = Error.stackTraceLimit
Error.stackTraceLimit = 4
originCache.set(xs, new Error())
Error.stackTraceLimit = x
return originCache.get(xs)
}
================================================
FILE: cf/src/queue.js
================================================
export default Queue
function Queue(initial = []) {
let xs = initial.slice()
let index = 0
return {
get length() {
return xs.length - index
},
remove: (x) => {
const index = xs.indexOf(x)
return index === -1
? null
: (xs.splice(index, 1), x)
},
push: (x) => (xs.push(x), x),
shift: () => {
const out = xs[index++]
if (index === xs.length) {
index = 0
xs = []
} else {
xs[index - 1] = undefined
}
return out
}
}
}
================================================
FILE: cf/src/result.js
================================================
export default class Result extends Array {
constructor() {
super()
Object.defineProperties(this, {
count: { value: null, writable: true },
state: { value: null, writable: true },
command: { value: null, writable: true },
columns: { value: null, writable: true },
statement: { value: null, writable: true }
})
}
static get [Symbol.species]() {
return Array
}
}
================================================
FILE: cf/src/subscribe.js
================================================
import { Buffer } from 'node:buffer'
const noop = () => { /* noop */ }
export default function Subscribe(postgres, options) {
const subscribers = new Map()
, slot = 'postgresjs_' + Math.random().toString(36).slice(2)
, state = {}
let connection
, stream
, ended = false
const sql = subscribe.sql = postgres({
...options,
transform: { column: {}, value: {}, row: {} },
max: 1,
fetch_types: false,
idle_timeout: null,
max_lifetime: null,
connection: {
...options.connection,
replication: 'database'
},
onclose: async function() {
if (ended)
return
stream = null
state.pid = state.secret = undefined
connected(await init(sql, slot, options.publications))
subscribers.forEach(event => event.forEach(({ onsubscribe }) => onsubscribe()))
},
no_subscribe: true
})
const end = sql.end
, close = sql.close
sql.end = async() => {
ended = true
stream && (await new Promise(r => (stream.once('close', r), stream.end())))
return end()
}
sql.close = async() => {
stream && (await new Promise(r => (stream.once('close', r), stream.end())))
return close()
}
return subscribe
async function subscribe(event, fn, onsubscribe = noop, onerror = noop) {
event = parseEvent(event)
if (!connection)
connection = init(sql, slot, options.publications)
const subscriber = { fn, onsubscribe }
const fns = subscribers.has(event)
? subscribers.get(event).add(subscriber)
: subscribers.set(event, new Set([subscriber])).get(event)
const unsubscribe = () => {
fns.delete(subscriber)
fns.size === 0 && subscribers.delete(event)
}
return connection.then(x => {
connected(x)
onsubscribe()
stream && stream.on('error', onerror)
return { unsubscribe, state, sql }
})
}
function connected(x) {
stream = x.stream
state.pid = x.state.pid
state.secret = x.state.secret
}
async function init(sql, slot, publications) {
if (!publications)
throw new Error('Missing publication names')
const xs = await sql.unsafe(
`CREATE_REPLICATION_SLOT ${ slot } TEMPORARY LOGICAL pgoutput NOEXPORT_SNAPSHOT`
)
const [x] = xs
const stream = await sql.unsafe(
`START_REPLICATION SLOT ${ slot } LOGICAL ${
x.consistent_point
} (proto_version '1', publication_names '${ publications }')`
).writable()
const state = {
lsn: Buffer.concat(x.consistent_point.split('/').map(x => Buffer.from(('00000000' + x).slice(-8), 'hex')))
}
stream.on('data', data)
stream.on('error', error)
stream.on('close', sql.close)
return { stream, state: xs.state }
function error(e) {
console.error('Unexpected error during logical streaming - reconnecting', e) // eslint-disable-line
}
function data(x) {
if (x[0] === 0x77) {
parse(x.subarray(25), state, sql.options.parsers, handle, options.transform)
} else if (x[0] === 0x6b && x[17]) {
state.lsn = x.subarray(1, 9)
pong()
}
}
function handle(a, b) {
const path = b.relation.schema + '.' + b.relation.table
call('*', a, b)
call('*:' + path, a, b)
b.relation.keys.length && call('*:' + path + '=' + b.relation.keys.map(x => a[x.name]), a, b)
call(b.command, a, b)
call(b.command + ':' + path, a, b)
b.relation.keys.length && call(b.command + ':' + path + '=' + b.relation.keys.map(x => a[x.name]), a, b)
}
function pong() {
const x = Buffer.alloc(34)
x[0] = 'r'.charCodeAt(0)
x.fill(state.lsn, 1)
x.writeBigInt64BE(BigInt(Date.now() - Date.UTC(2000, 0, 1)) * BigInt(1000), 25)
stream.write(x)
}
}
function call(x, a, b) {
subscribers.has(x) && subscribers.get(x).forEach(({ fn }) => fn(a, b, x))
}
}
function Time(x) {
return new Date(Date.UTC(2000, 0, 1) + Number(x / BigInt(1000)))
}
function parse(x, state, parsers, handle, transform) {
const char = (acc, [k, v]) => (acc[k.charCodeAt(0)] = v, acc)
Object.entries({
R: x => { // Relation
let i = 1
const r = state[x.readUInt32BE(i)] = {
schema: x.toString('utf8', i += 4, i = x.indexOf(0, i)) || 'pg_catalog',
table: x.toString('utf8', i + 1, i = x.indexOf(0, i + 1)),
columns: Array(x.readUInt16BE(i += 2)),
keys: []
}
i += 2
let columnIndex = 0
, column
while (i < x.length) {
column = r.columns[columnIndex++] = {
key: x[i++],
name: transform.column.from
? transform.column.from(x.toString('utf8', i, i = x.indexOf(0, i)))
: x.toString('utf8', i, i = x.indexOf(0, i)),
type: x.readUInt32BE(i += 1),
parser: parsers[x.readUInt32BE(i)],
atttypmod: x.readUInt32BE(i += 4)
}
column.key && r.keys.push(column)
i += 4
}
},
Y: () => { /* noop */ }, // Type
O: () => { /* noop */ }, // Origin
B: x => { // Begin
state.date = Time(x.readBigInt64BE(9))
state.lsn = x.subarray(1, 9)
},
I: x => { // Insert
let i = 1
const relation = state[x.readUInt32BE(i)]
const { row } = tuples(x, relation.columns, i += 7, transform)
handle(row, {
command: 'insert',
relation
})
},
D: x => { // Delete
let i = 1
const relation = state[x.readUInt32BE(i)]
i += 4
const key = x[i] === 75
handle(key || x[i] === 79
? tuples(x, relation.columns, i += 3, transform).row
: null
, {
command: 'delete',
relation,
key
})
},
U: x => { // Update
let i = 1
const relation = state[x.readUInt32BE(i)]
i += 4
const key = x[i] === 75
const xs = key || x[i] === 79
? tuples(x, relation.columns, i += 3, transform)
: null
xs && (i = xs.i)
const { row } = tuples(x, relation.columns, i + 3, transform)
handle(row, {
command: 'update',
relation,
key,
old: xs && xs.row
})
},
T: () => { /* noop */ }, // Truncate,
C: () => { /* noop */ } // Commit
}).reduce(char, {})[x[0]](x)
}
function tuples(x, columns, xi, transform) {
let type
, column
, value
const row = transform.raw ? new Array(columns.length) : {}
for (let i = 0; i < columns.length; i++) {
type = x[xi++]
column = columns[i]
value = type === 110 // n
? null
: type === 117 // u
? undefined
: column.parser === undefined
? x.toString('utf8', xi + 4, xi += 4 + x.readUInt32BE(xi))
: column.parser.array === true
? column.parser(x.toString('utf8', xi + 5, xi += 4 + x.readUInt32BE(xi)))
: column.parser(x.toString('utf8', xi + 4, xi += 4 + x.readUInt32BE(xi)))
transform.raw
? (row[i] = transform.raw === true
? value
: transform.value.from ? transform.value.from(value, column) : value)
: (row[column.name] = transform.value.from
? transform.value.from(value, column)
: value
)
}
return { i: xi, row: transform.row.from ? transform.row.from(row) : row }
}
function parseEvent(x) {
const xs = x.match(/^(\*|insert|update|delete)?:?([^.]+?\.?[^=]+)?=?(.+)?/i) || []
if (!xs)
throw new Error('Malformed subscribe pattern: ' + x)
const [, command, path, key] = xs
return (command || '*')
+ (path ? ':' + (path.indexOf('.') === -1 ? 'public.' + path : path) : '')
+ (key ? '=' + key : '')
}
================================================
FILE: cf/src/types.js
================================================
import { Buffer } from 'node:buffer'
import { Query } from './query.js'
import { Errors } from './errors.js'
export const types = {
string: {
to: 25,
from: null, // defaults to string
serialize: x => '' + x
},
number: {
to: 0,
from: [21, 23, 26, 700, 701],
serialize: x => '' + x,
parse: x => +x
},
json: {
to: 114,
from: [114, 3802],
serialize: x => JSON.stringify(x),
parse: x => JSON.parse(x)
},
boolean: {
to: 16,
from: 16,
serialize: x => x === true ? 't' : 'f',
parse: x => x === 't'
},
date: {
to: 1184,
from: [1082, 1114, 1184],
serialize: x => (x instanceof Date ? x : new Date(x)).toISOString(),
parse: x => new Date(x)
},
bytea: {
to: 17,
from: 17,
serialize: x => '\\x' + Buffer.from(x).toString('hex'),
parse: x => Buffer.from(x.slice(2), 'hex')
}
}
class NotTagged { then() { notTagged() } catch() { notTagged() } finally() { notTagged() }}
export class Identifier extends NotTagged {
constructor(value) {
super()
this.value = escapeIdentifier(value)
}
}
export class Parameter extends NotTagged {
constructor(value, type, array) {
super()
this.value = value
this.type = type
this.array = array
}
}
export class Builder extends NotTagged {
constructor(first, rest) {
super()
this.first = first
this.rest = rest
}
build(before, parameters, types, options) {
const keyword = builders.map(([x, fn]) => ({ fn, i: before.search(x) })).sort((a, b) => a.i - b.i).pop()
return keyword.i === -1
? escapeIdentifiers(this.first, options)
: keyword.fn(this.first, this.rest, parameters, types, options)
}
}
export function handleValue(x, parameters, types, options) {
let value = x instanceof Parameter ? x.value : x
if (value === undefined) {
x instanceof Parameter
? x.value = options.transform.undefined
: value = x = options.transform.undefined
if (value === undefined)
throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')
}
return '$' + (types.push(
x instanceof Parameter
? (parameters.push(x.value), x.array
? x.array[x.type || inferType(x.value)] || x.type || firstIsString(x.value)
: x.type
)
: (parameters.push(x), inferType(x))
))
}
const defaultHandlers = typeHandlers(types)
export function stringify(q, string, value, parameters, types, options) { // eslint-disable-line
for (let i = 1; i < q.strings.length; i++) {
string += (stringifyValue(string, value, parameters, types, options)) + q.strings[i]
value = q.args[i]
}
return string
}
function stringifyValue(string, value, parameters, types, o) {
return (
value instanceof Builder ? value.build(string, parameters, types, o) :
value instanceof Query ? fragment(value, parameters, types, o) :
value instanceof Identifier ? value.value :
value && value[0] instanceof Query ? value.reduce((acc, x) => acc + ' ' + fragment(x, parameters, types, o), '') :
handleValue(value, parameters, types, o)
)
}
function fragment(q, parameters, types, options) {
q.fragment = true
return stringify(q, q.strings[0], q.args[0], parameters, types, options)
}
function valuesBuilder(first, parameters, types, columns, options) {
return first.map(row =>
'(' + columns.map(column =>
stringifyValue('values', row[column], parameters, types, options)
).join(',') + ')'
).join(',')
}
function values(first, rest, parameters, types, options) {
const multi = Array.isArray(first[0])
const columns = rest.length ? rest.flat() : Object.keys(multi ? first[0] : first)
return valuesBuilder(multi ? first : [first], parameters, types, columns, options)
}
function select(first, rest, parameters, types, options) {
typeof first === 'string' && (first = [first].concat(rest))
if (Array.isArray(first))
return escapeIdentifiers(first, options)
let value
const columns = rest.length ? rest.flat() : Object.keys(first)
return columns.map(x => {
value = first[x]
return (
value instanceof Query ? fragment(value, parameters, types, options) :
value instanceof Identifier ? value.value :
handleValue(value, parameters, types, options)
) + ' as ' + escapeIdentifier(options.transform.column.to ? options.transform.column.to(x) : x)
}).join(',')
}
const builders = Object.entries({
values,
in: (...xs) => {
const x = values(...xs)
return x === '()' ? '(null)' : x
},
select,
as: select,
returning: select,
'\\(': select,
update(first, rest, parameters, types, options) {
return (rest.length ? rest.flat() : Object.keys(first)).map(x =>
escapeIdentifier(options.transform.column.to ? options.transform.column.to(x) : x) +
'=' + stringifyValue('values', first[x], parameters, types, options)
)
},
insert(first, rest, parameters, types, options) {
const columns = rest.length ? rest.flat() : Object.keys(Array.isArray(first) ? first[0] : first)
return '(' + escapeIdentifiers(columns, options) + ')values' +
valuesBuilder(Array.isArray(first) ? first : [first], parameters, types, columns, options)
}
}).map(([x, fn]) => ([new RegExp('((?:^|[\\s(])' + x + '(?:$|[\\s(]))(?![\\s\\S]*\\1)', 'i'), fn]))
function notTagged() {
throw Errors.generic('NOT_TAGGED_CALL', 'Query not called as a tagged template literal')
}
export const serializers = defaultHandlers.serializers
export const parsers = defaultHandlers.parsers
export const END = {}
function firstIsString(x) {
if (Array.isArray(x))
return firstIsString(x[0])
return typeof x === 'string' ? 1009 : 0
}
export const mergeUserTypes = function(types) {
const user = typeHandlers(types || {})
return {
serializers: Object.assign({}, serializers, user.serializers),
parsers: Object.assign({}, parsers, user.parsers)
}
}
function typeHandlers(types) {
return Object.keys(types).reduce((acc, k) => {
types[k].from && [].concat(types[k].from).forEach(x => acc.parsers[x] = types[k].parse)
if (types[k].serialize) {
acc.serializers[types[k].to] = types[k].serialize
types[k].from && [].concat(types[k].from).forEach(x => acc.serializers[x] = types[k].serialize)
}
return acc
}, { parsers: {}, serializers: {} })
}
function escapeIdentifiers(xs, { transform: { column } }) {
return xs.map(x => escapeIdentifier(column.to ? column.to(x) : x)).join(',')
}
export const escapeIdentifier = function escape(str) {
return '"' + str.replace(/"/g, '""').replace(/\./g, '"."') + '"'
}
export const inferType = function inferType(x) {
return (
x instanceof Parameter ? x.type :
x instanceof Date ? 1184 :
x instanceof Uint8Array ? 17 :
(x === true || x === false) ? 16 :
typeof x === 'bigint' ? 20 :
Array.isArray(x) ? inferType(x[0]) :
0
)
}
const escapeBackslash = /\\/g
const escapeQuote = /"/g
function arrayEscape(x) {
return x
.replace(escapeBackslash, '\\\\')
.replace(escapeQuote, '\\"')
}
export const arraySerializer = function arraySerializer(xs, serializer, options, typarray) {
if (Array.isArray(xs) === false)
return xs
if (!xs.length)
return '{}'
const first = xs[0]
// Only _box (1020) has the ';' delimiter for arrays, all other types use the ',' delimiter
const delimiter = typarray === 1020 ? ';' : ','
if (Array.isArray(first) && !first.type)
return '{' + xs.map(x => arraySerializer(x, serializer, options, typarray)).join(delimiter) + '}'
return '{' + xs.map(x => {
if (x === undefined) {
x = options.transform.undefined
if (x === undefined)
throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')
}
return x === null
? 'null'
: '"' + arrayEscape(serializer ? serializer(x.type ? x.value : x) : '' + x) + '"'
}).join(delimiter) + '}'
}
const arrayParserState = {
i: 0,
char: null,
str: '',
quoted: false,
last: 0
}
export const arrayParser = function arrayParser(x, parser, typarray) {
arrayParserState.i = arrayParserState.last = 0
return arrayParserLoop(arrayParserState, x, parser, typarray)
}
function arrayParserLoop(s, x, parser, typarray) {
const xs = []
// Only _box (1020) has the ';' delimiter for arrays, all other types use the ',' delimiter
const delimiter = typarray === 1020 ? ';' : ','
for (; s.i < x.length; s.i++) {
s.char = x[s.i]
if (s.quoted) {
if (s.char === '\\') {
s.str += x[++s.i]
} else if (s.char === '"') {
xs.push(parser ? parser(s.str) : s.str)
s.str = ''
s.quoted = x[s.i + 1] === '"'
s.last = s.i + 2
} else {
s.str += s.char
}
} else if (s.char === '"') {
s.quoted = true
} else if (s.char === '{') {
s.last = ++s.i
xs.push(arrayParserLoop(s, x, parser, typarray))
} else if (s.char === '}') {
s.quoted = false
s.last < s.i && xs.push(parser ? parser(x.slice(s.last, s.i)) : x.slice(s.last, s.i))
s.last = s.i + 1
break
} else if (s.char === delimiter && s.p !== '}' && s.p !== '"') {
xs.push(parser ? parser(x.slice(s.last, s.i)) : x.slice(s.last, s.i))
s.last = s.i + 1
}
s.p = s.char
}
s.last < s.i && xs.push(parser ? parser(x.slice(s.last, s.i + 1)) : x.slice(s.last, s.i + 1))
return xs
}
export const toCamel = x => {
let str = x[0]
for (let i = 1; i < x.length; i++)
str += x[i] === '_' ? x[++i].toUpperCase() : x[i]
return str
}
export const toPascal = x => {
let str = x[0].toUpperCase()
for (let i = 1; i < x.length; i++)
str += x[i] === '_' ? x[++i].toUpperCase() : x[i]
return str
}
export const toKebab = x => x.replace(/_/g, '-')
export const fromCamel = x => x.replace(/([A-Z])/g, '_$1').toLowerCase()
export const fromPascal = x => (x.slice(0, 1) + x.slice(1).replace(/([A-Z])/g, '_$1')).toLowerCase()
export const fromKebab = x => x.replace(/-/g, '_')
function createJsonTransform(fn) {
return function jsonTransform(x, column) {
return typeof x === 'object' && x !== null && (column.type === 114 || column.type === 3802)
? Array.isArray(x)
? x.map(x => jsonTransform(x, column))
: Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: jsonTransform(v, column) }), {})
: x
}
}
toCamel.column = { from: toCamel }
toCamel.value = { from: createJsonTransform(toCamel) }
fromCamel.column = { to: fromCamel }
export const camel = { ...toCamel }
camel.column.to = fromCamel
toPascal.column = { from: toPascal }
toPascal.value = { from: createJsonTransform(toPascal) }
fromPascal.column = { to: fromPascal }
export const pascal = { ...toPascal }
pascal.column.to = fromPascal
toKebab.column = { from: toKebab }
toKebab.value = { from: createJsonTransform(toKebab) }
fromKebab.column = { to: fromKebab }
export const kebab = { ...toKebab }
kebab.column.to = fromKebab
================================================
FILE: cf/test.js
================================================
// Add your database url and run this file with the below two commands to test pages and workers
// npx wrangler@latest pages dev ./cf --script-path test.js --compatibility-date=2023-06-20 --log-level=debug --compatibility-flag=nodejs_compat
// npx wrangler@latest dev ./cf/test.js --compatibility-date=2023-06-20 --log-level=debug --compatibility-flag=nodejs_compat
import postgres from './src/index.js'
const DATABASE_URL = ''
export default {
async fetch() {
const sql = postgres(DATABASE_URL)
const rows = await sql`SELECT table_name FROM information_schema.columns`
return new Response(rows.map((e) => e.table_name).join('\n'))
}
}
================================================
FILE: cjs/package.json
================================================
{"type":"commonjs"}
================================================
FILE: cjs/src/bytes.js
================================================
const size = 256
let buffer = Buffer.allocUnsafe(size)
const messages = 'BCcDdEFfHPpQSX'.split('').reduce((acc, x) => {
const v = x.charCodeAt(0)
acc[x] = () => {
buffer[0] = v
b.i = 5
return b
}
return acc
}, {})
const b = Object.assign(reset, messages, {
N: String.fromCharCode(0),
i: 0,
inc(x) {
b.i += x
return b
},
str(x) {
const length = Buffer.byteLength(x)
fit(length)
b.i += buffer.write(x, b.i, length, 'utf8')
return b
},
i16(x) {
fit(2)
buffer.writeUInt16BE(x, b.i)
b.i += 2
return b
},
i32(x, i) {
if (i || i === 0) {
buffer.writeUInt32BE(x, i)
return b
}
fit(4)
buffer.writeUInt32BE(x, b.i)
b.i += 4
return b
},
z(x) {
fit(x)
buffer.fill(0, b.i, b.i + x)
b.i += x
return b
},
raw(x) {
buffer = Buffer.concat([buffer.subarray(0, b.i), x])
b.i = buffer.length
return b
},
end(at = 1) {
buffer.writeUInt32BE(b.i - at, at)
const out = buffer.subarray(0, b.i)
b.i = 0
buffer = Buffer.allocUnsafe(size)
return out
}
})
module.exports = b
function fit(x) {
if (buffer.length - b.i < x) {
const prev = buffer
, length = prev.length
buffer = Buffer.allocUnsafe(length + (length >> 1) + x)
prev.copy(buffer)
}
}
function reset() {
b.i = 0
return b
}
================================================
FILE: cjs/src/connection.js
================================================
const net = require('net')
const tls = require('tls')
const crypto = require('crypto')
const Stream = require('stream')
const { performance } = require('perf_hooks')
const { stringify, handleValue, arrayParser, arraySerializer } = require('./types.js')
const { Errors } = require('./errors.js')
const Result = require('./result.js')
const Queue = require('./queue.js')
const { Query, CLOSE } = require('./query.js')
const b = require('./bytes.js')
module.exports = Connection
let uid = 1
const Sync = b().S().end()
, Flush = b().H().end()
, SSLRequest = b().i32(8).i32(80877103).end(8)
, ExecuteUnnamed = Buffer.concat([b().E().str(b.N).i32(0).end(), Sync])
, DescribeUnnamed = b().D().str('S').str(b.N).end()
, noop = () => { /* noop */ }
const retryRoutines = new Set([
'FetchPreparedStatement',
'RevalidateCachedQuery',
'transformAssignedExpr'
])
const errorFields = {
83 : 'severity_local', // S
86 : 'severity', // V
67 : 'code', // C
77 : 'message', // M
68 : 'detail', // D
72 : 'hint', // H
80 : 'position', // P
112 : 'internal_position', // p
113 : 'internal_query', // q
87 : 'where', // W
115 : 'schema_name', // s
116 : 'table_name', // t
99 : 'column_name', // c
100 : 'data type_name', // d
110 : 'constraint_name', // n
70 : 'file', // F
76 : 'line', // L
82 : 'routine' // R
}
function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose = noop } = {}) {
const {
sslnegotiation,
ssl,
max,
user,
host,
port,
database,
parsers,
transform,
onnotice,
onnotify,
onparameter,
max_pipeline,
keep_alive,
backoff,
target_session_attrs
} = options
const sent = Queue()
, id = uid++
, backend = { pid: null, secret: null }
, idleTimer = timer(end, options.idle_timeout)
, lifeTimer = timer(end, options.max_lifetime)
, connectTimer = timer(connectTimedOut, options.connect_timeout)
let socket = null
, cancelMessage
, errorResponse = null
, result = new Result()
, incoming = Buffer.alloc(0)
, needsTypes = options.fetch_types
, backendParameters = {}
, statements = {}
, statementId = Math.random().toString(36).slice(2)
, statementCount = 1
, closedTime = 0
, remaining = 0
, hostIndex = 0
, retries = 0
, length = 0
, delay = 0
, rows = 0
, serverSignature = null
, nextWriteTimer = null
, terminated = false
, incomings = null
, results = null
, initial = null
, ending = null
, stream = null
, chunk = null
, ended = null
, nonce = null
, query = null
, final = null
const connection = {
queue: queues.closed,
idleTimer,
connect(query) {
initial = query
reconnect()
},
terminate,
execute,
cancel,
end,
count: 0,
id
}
queues.closed && queues.closed.push(connection)
return connection
async function createSocket() {
let x
try {
x = options.socket
? (await Promise.resolve(options.socket(options)))
: new net.Socket()
} catch (e) {
error(e)
return
}
x.on('error', error)
x.on('close', closed)
x.on('drain', drain)
return x
}
async function cancel({ pid, secret }, resolve, reject) {
try {
cancelMessage = b().i32(16).i32(80877102).i32(pid).i32(secret).end(16)
await connect()
socket.once('error', reject)
socket.once('close', resolve)
} catch (error) {
reject(error)
}
}
function execute(q) {
if (terminated)
return queryError(q, Errors.connection('CONNECTION_DESTROYED', options))
if (stream)
return queryError(q, Errors.generic('COPY_IN_PROGRESS', 'You cannot execute queries during copy'))
if (q.cancelled)
return
try {
q.state = backend
query
? sent.push(q)
: (query = q, query.active = true)
build(q)
return write(toBuffer(q))
&& !q.describeFirst
&& !q.cursorFn
&& sent.length < max_pipeline
&& (!q.options.onexecute || q.options.onexecute(connection))
} catch (error) {
sent.length === 0 && write(Sync)
errored(error)
return true
}
}
function toBuffer(q) {
if (q.parameters.length >= 65534)
throw Errors.generic('MAX_PARAMETERS_EXCEEDED', 'Max number of parameters (65534) exceeded')
return q.options.simple
? b().Q().str(q.statement.string + b.N).end()
: q.describeFirst
? Buffer.concat([describe(q), Flush])
: q.prepare
? q.prepared
? prepared(q)
: Buffer.concat([describe(q), prepared(q)])
: unnamed(q)
}
function describe(q) {
return Buffer.concat([
Parse(q.statement.string, q.parameters, q.statement.types, q.statement.name),
Describe('S', q.statement.name)
])
}
function prepared(q) {
return Buffer.concat([
Bind(q.parameters, q.statement.types, q.statement.name, q.cursorName),
q.cursorFn
? Execute('', q.cursorRows)
: ExecuteUnnamed
])
}
function unnamed(q) {
return Buffer.concat([
Parse(q.statement.string, q.parameters, q.statement.types),
DescribeUnnamed,
prepared(q)
])
}
function build(q) {
const parameters = []
, types = []
const string = stringify(q, q.strings[0], q.args[0], parameters, types, options)
!q.tagged && q.args.forEach(x => handleValue(x, parameters, types, options))
q.prepare = options.prepare && ('prepare' in q.options ? q.options.prepare : true)
q.string = string
q.signature = q.prepare && types + string
q.onlyDescribe && (delete statements[q.signature])
q.parameters = q.parameters || parameters
q.prepared = q.prepare && q.signature in statements
q.describeFirst = q.onlyDescribe || (parameters.length && !q.prepared)
q.statement = q.prepared
? statements[q.signature]
: { string, types, name: q.prepare ? statementId + statementCount++ : '' }
typeof options.debug === 'function' && options.debug(id, string, parameters, types)
}
function write(x, fn) {
chunk = chunk ? Buffer.concat([chunk, x]) : Buffer.from(x)
if (fn || chunk.length >= 1024)
return nextWrite(fn)
nextWriteTimer === null && (nextWriteTimer = setImmediate(nextWrite))
return true
}
function nextWrite(fn) {
const x = socket.write(chunk, fn)
nextWriteTimer !== null && clearImmediate(nextWriteTimer)
chunk = nextWriteTimer = null
return x
}
function connectTimedOut() {
errored(Errors.connection('CONNECT_TIMEOUT', options, socket))
socket.destroy()
}
async function secure() {
if (sslnegotiation !== 'direct') {
write(SSLRequest)
const canSSL = await new Promise(r => socket.once('data', x => r(x[0] === 83))) // S
if (!canSSL && ssl === 'prefer')
return connected()
}
const options = {
socket,
servername: net.isIP(socket.host) ? undefined : socket.host
}
if (sslnegotiation === 'direct')
options.ALPNProtocols = ['postgresql']
if (ssl === 'require' || ssl === 'allow' || ssl === 'prefer')
options.rejectUnauthorized = false
else if (typeof ssl === 'object')
Object.assign(options, ssl)
socket.removeAllListeners()
socket = tls.connect(options)
socket.on('secureConnect', connected)
socket.on('error', error)
socket.on('close', closed)
socket.on('drain', drain)
}
/* c8 ignore next 3 */
function drain() {
!query && onopen(connection)
}
function data(x) {
if (incomings) {
incomings.push(x)
remaining -= x.length
if (remaining > 0)
return
}
incoming = incomings
? Buffer.concat(incomings, length - remaining)
: incoming.length === 0
? x
: Buffer.concat([incoming, x], incoming.length + x.length)
while (incoming.length > 4) {
length = incoming.readUInt32BE(1)
if (length >= incoming.length) {
remaining = length - incoming.length
incomings = [incoming]
break
}
try {
handle(incoming.subarray(0, length + 1))
} catch (e) {
query && (query.cursorFn || query.describeFirst) && write(Sync)
errored(e)
}
incoming = incoming.subarray(length + 1)
remaining = 0
incomings = null
}
}
async function connect() {
terminated = false
backendParameters = {}
socket || (socket = await createSocket())
if (!socket)
return
connectTimer.start()
if (options.socket)
return ssl ? secure() : connected()
socket.on('connect', ssl ? secure : connected)
if (options.path)
return socket.connect(options.path)
socket.ssl = ssl
socket.connect(port[hostIndex], host[hostIndex])
socket.host = host[hostIndex]
socket.port = port[hostIndex]
hostIndex = (hostIndex + 1) % port.length
}
function reconnect() {
setTimeout(connect, closedTime ? Math.max(0, closedTime + delay - performance.now()) : 0)
}
function connected() {
try {
statements = {}
needsTypes = options.fetch_types
statementId = Math.random().toString(36).slice(2)
statementCount = 1
lifeTimer.start()
socket.on('data', data)
keep_alive && socket.setKeepAlive && socket.setKeepAlive(true, 1000 * keep_alive)
const s = StartupMessage()
write(s)
} catch (err) {
error(err)
}
}
function error(err) {
if (connection.queue === queues.connecting && options.host[retries + 1])
return
errored(err)
while (sent.length)
queryError(sent.shift(), err)
}
function errored(err) {
stream && (stream.destroy(err), stream = null)
query && queryError(query, err)
initial && (queryError(initial, err), initial = null)
}
function queryError(query, err) {
if (query.reserve)
return query.reject(err)
if (!err || typeof err !== 'object')
err = new Error(err)
'query' in err || 'parameters' in err || Object.defineProperties(err, {
stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug },
query: { value: query.string, enumerable: options.debug },
parameters: { value: query.parameters, enumerable: options.debug },
args: { value: query.args, enumerable: options.debug },
types: { value: query.statement && query.statement.types, enumerable: options.debug }
})
query.reject(err)
}
function end() {
return ending || (
!connection.reserved && onend(connection),
!connection.reserved && !initial && !query && sent.length === 0
? (terminate(), new Promise(r => socket && socket.readyState !== 'closed' ? socket.once('close', r) : r()))
: ending = new Promise(r => ended = r)
)
}
function terminate() {
terminated = true
if (stream || query || initial || sent.length)
error(Errors.connection('CONNECTION_DESTROYED', options))
clearImmediate(nextWriteTimer)
if (socket) {
socket.removeListener('data', data)
socket.removeListener('connect', connected)
socket.readyState === 'open' && socket.end(b().X().end())
}
ended && (ended(), ending = ended = null)
}
async function closed(hadError) {
incoming = Buffer.alloc(0)
remaining = 0
incomings = null
clearImmediate(nextWriteTimer)
socket.removeListener('data', data)
socket.removeListener('connect', connected)
idleTimer.cancel()
lifeTimer.cancel()
connectTimer.cancel()
socket.removeAllListeners()
socket = null
if (initial)
return reconnect()
!hadError && (query || sent.length) && error(Errors.connection('CONNECTION_CLOSED', options, socket))
closedTime = performance.now()
hadError && options.shared.retries++
delay = (typeof backoff === 'function' ? backoff(options.shared.retries) : backoff) * 1000
onclose(connection, Errors.connection('CONNECTION_CLOSED', options, socket))
}
/* Handlers */
function handle(xs, x = xs[0]) {
(
x === 68 ? DataRow : // D
x === 100 ? CopyData : // d
x === 65 ? NotificationResponse : // A
x === 83 ? ParameterStatus : // S
x === 90 ? ReadyForQuery : // Z
x === 67 ? CommandComplete : // C
x === 50 ? BindComplete : // 2
x === 49 ? ParseComplete : // 1
x === 116 ? ParameterDescription : // t
x === 84 ? RowDescription : // T
x === 82 ? Authentication : // R
x === 110 ? NoData : // n
x === 75 ? BackendKeyData : // K
x === 69 ? ErrorResponse : // E
x === 115 ? PortalSuspended : // s
x === 51 ? CloseComplete : // 3
x === 71 ? CopyInResponse : // G
x === 78 ? NoticeResponse : // N
x === 72 ? CopyOutResponse : // H
x === 99 ? CopyDone : // c
x === 73 ? EmptyQueryResponse : // I
x === 86 ? FunctionCallResponse : // V
x === 118 ? NegotiateProtocolVersion : // v
x === 87 ? CopyBothResponse : // W
/* c8 ignore next */
UnknownMessage
)(xs)
}
function DataRow(x) {
let index = 7
let length
let column
let value
const row = query.isRaw ? new Array(query.statement.columns.length) : {}
for (let i = 0; i < query.statement.columns.length; i++) {
column = query.statement.columns[i]
length = x.readInt32BE(index)
index += 4
value = length === -1
? null
: query.isRaw === true
? x.subarray(index, index += length)
: column.parser === undefined
? x.toString('utf8', index, index += length)
: column.parser.array === true
? column.parser(x.toString('utf8', index + 1, index += length))
: column.parser(x.toString('utf8', index, index += length))
query.isRaw
? (row[i] = query.isRaw === true
? value
: transform.value.from ? transform.value.from(value, column) : value)
: (row[column.name] = transform.value.from ? transform.value.from(value, column) : value)
}
query.forEachFn
? query.forEachFn(transform.row.from ? transform.row.from(row) : row, result)
: (result[rows++] = transform.row.from ? transform.row.from(row) : row)
}
function ParameterStatus(x) {
const [k, v] = x.toString('utf8', 5, x.length - 1).split(b.N)
backendParameters[k] = v
if (options.parameters[k] !== v) {
options.parameters[k] = v
onparameter && onparameter(k, v)
}
}
function ReadyForQuery(x) {
if (query) {
if (errorResponse) {
query.retried
? errored(query.retried)
: query.prepared && retryRoutines.has(errorResponse.routine)
? retry(query, errorResponse)
: errored(errorResponse)
} else {
query.resolve(results || result)
}
} else if (errorResponse) {
errored(errorResponse)
}
query = results = errorResponse = null
result = new Result()
connectTimer.cancel()
if (initial) {
if (target_session_attrs) {
if (!backendParameters.in_hot_standby || !backendParameters.default_transaction_read_only)
return fetchState()
else if (tryNext(target_session_attrs, backendParameters))
return terminate()
}
if (needsTypes) {
initial.reserve && (initial = null)
return fetchArrayTypes()
}
initial && !initial.reserve && execute(initial)
options.shared.retries = retries = 0
initial = null
return
}
while (sent.length && (query = sent.shift()) && (query.active = true, query.cancelled))
Connection(options).cancel(query.state, query.cancelled.resolve, query.cancelled.reject)
if (query)
return // Consider opening if able and sent.length < 50
connection.reserved
? !connection.reserved.release && x[5] === 73 // I
? ending
? terminate()
: (connection.reserved = null, onopen(connection))
: connection.reserved()
: ending
? terminate()
: onopen(connection)
}
function CommandComplete(x) {
rows = 0
for (let i = x.length - 1; i > 0; i--) {
if (x[i] === 32 && x[i + 1] < 58 && result.count === null)
result.count = +x.toString('utf8', i + 1, x.length - 1)
if (x[i - 1] >= 65) {
result.command = x.toString('utf8', 5, i)
result.state = backend
break
}
}
final && (final(), final = null)
if (result.command === 'BEGIN' && max !== 1 && !connection.reserved)
return errored(Errors.generic('UNSAFE_TRANSACTION', 'Only use sql.begin, sql.reserved or max: 1'))
if (query.options.simple)
return BindComplete()
if (query.cursorFn) {
result.count && query.cursorFn(result)
write(Sync)
}
}
function ParseComplete() {
query.parsing = false
}
function BindComplete() {
!result.statement && (result.statement = query.statement)
result.columns = query.statement.columns
}
function ParameterDescription(x) {
const length = x.readUInt16BE(5)
for (let i = 0; i < length; ++i)
!query.statement.types[i] && (query.statement.types[i] = x.readUInt32BE(7 + i * 4))
query.prepare && (statements[query.signature] = query.statement)
query.describeFirst && !query.onlyDescribe && (write(prepared(query)), query.describeFirst = false)
}
function RowDescription(x) {
if (result.command) {
results = results || [result]
results.push(result = new Result())
result.count = null
query.statement.columns = null
}
const length = x.readUInt16BE(5)
let index = 7
let start
query.statement.columns = Array(length)
for (let i = 0; i < length; ++i) {
start = index
while (x[index++] !== 0);
const table = x.readUInt32BE(index)
const number = x.readUInt16BE(index + 4)
const type = x.readUInt32BE(index + 6)
query.statement.columns[i] = {
name: transform.column.from
? transform.column.from(x.toString('utf8', start, index - 1))
: x.toString('utf8', start, index - 1),
parser: parsers[type],
table,
number,
type
}
index += 18
}
result.statement = query.statement
if (query.onlyDescribe)
return (query.resolve(query.statement), write(Sync))
}
async function Authentication(x, type = x.readUInt32BE(5)) {
(
type === 3 ? AuthenticationCleartextPassword :
type === 5 ? AuthenticationMD5Password :
type === 10 ? SASL :
type === 11 ? SASLContinue :
type === 12 ? SASLFinal :
type !== 0 ? UnknownAuth :
noop
)(x, type)
}
/* c8 ignore next 5 */
async function AuthenticationCleartextPassword() {
const payload = await Pass()
write(
b().p().str(payload).z(1).end()
)
}
async function AuthenticationMD5Password(x) {
const payload = 'md5' + (
await md5(
Buffer.concat([
Buffer.from(await md5((await Pass()) + user)),
x.subarray(9)
])
)
)
write(
b().p().str(payload).z(1).end()
)
}
async function SASL() {
nonce = (await crypto.randomBytes(18)).toString('base64')
b().p().str('SCRAM-SHA-256' + b.N)
const i = b.i
write(b.inc(4).str('n,,n=*,r=' + nonce).i32(b.i - i - 4, i).end())
}
async function SASLContinue(x) {
const res = x.toString('utf8', 9).split(',').reduce((acc, x) => (acc[x[0]] = x.slice(2), acc), {})
const saltedPassword = await crypto.pbkdf2Sync(
await Pass(),
Buffer.from(res.s, 'base64'),
parseInt(res.i), 32,
'sha256'
)
const clientKey = await hmac(saltedPassword, 'Client Key')
const auth = 'n=*,r=' + nonce + ','
+ 'r=' + res.r + ',s=' + res.s + ',i=' + res.i
+ ',c=biws,r=' + res.r
serverSignature = (await hmac(await hmac(saltedPassword, 'Server Key'), auth)).toString('base64')
const payload = 'c=biws,r=' + res.r + ',p=' + xor(
clientKey, Buffer.from(await hmac(await sha256(clientKey), auth))
).toString('base64')
write(
b().p().str(payload).end()
)
}
function SASLFinal(x) {
if (x.toString('utf8', 9).split(b.N, 1)[0].slice(2) === serverSignature)
return
/* c8 ignore next 5 */
errored(Errors.generic('SASL_SIGNATURE_MISMATCH', 'The server did not return the correct signature'))
socket.destroy()
}
function Pass() {
return Promise.resolve(typeof options.pass === 'function'
? options.pass()
: options.pass
)
}
function NoData() {
result.statement = query.statement
result.statement.columns = []
if (query.onlyDescribe)
return (query.resolve(query.statement), write(Sync))
}
function BackendKeyData(x) {
backend.pid = x.readUInt32BE(5)
backend.secret = x.readUInt32BE(9)
}
async function fetchArrayTypes() {
needsTypes = false
const types = await new Query([`
select b.oid, b.typarray
from pg_catalog.pg_type a
left join pg_catalog.pg_type b on b.oid = a.typelem
where a.typcategory = 'A'
group by b.oid, b.typarray
order by b.oid
`], [], execute)
types.forEach(({ oid, typarray }) => addArrayType(oid, typarray))
}
function addArrayType(oid, typarray) {
if (!!options.parsers[typarray] && !!options.serializers[typarray]) return
const parser = options.parsers[oid]
options.shared.typeArrayMap[oid] = typarray
options.parsers[typarray] = (xs) => arrayParser(xs, parser, typarray)
options.parsers[typarray].array = true
options.serializers[typarray] = (xs) => arraySerializer(xs, options.serializers[oid], options, typarray)
}
function tryNext(x, xs) {
return (
(x === 'read-write' && xs.default_transaction_read_only === 'on') ||
(x === 'read-only' && xs.default_transaction_read_only === 'off') ||
(x === 'primary' && xs.in_hot_standby === 'on') ||
(x === 'standby' && xs.in_hot_standby === 'off') ||
(x === 'prefer-standby' && xs.in_hot_standby === 'off' && options.host[retries])
)
}
function fetchState() {
const query = new Query([`
show transaction_read_only;
select pg_catalog.pg_is_in_recovery()
`], [], execute, null, { simple: true })
query.resolve = ([[a], [b]]) => {
backendParameters.default_transaction_read_only = a.transaction_read_only
backendParameters.in_hot_standby = b.pg_is_in_recovery ? 'on' : 'off'
}
query.execute()
}
function ErrorResponse(x) {
if (query) {
(query.cursorFn || query.describeFirst) && write(Sync)
errorResponse = Errors.postgres(parseError(x))
} else {
errored(Errors.postgres(parseError(x)))
}
}
function retry(q, error) {
delete statements[q.signature]
q.retried = error
execute(q)
}
function NotificationResponse(x) {
if (!onnotify)
return
let index = 9
while (x[index++] !== 0);
onnotify(
x.toString('utf8', 9, index - 1),
x.toString('utf8', index, x.length - 1)
)
}
async function PortalSuspended() {
try {
const x = await Promise.resolve(query.cursorFn(result))
rows = 0
x === CLOSE
? write(Close(query.portal))
: (result = new Result(), write(Execute('', query.cursorRows)))
} catch (err) {
write(Sync)
query.reject(err)
}
}
function CloseComplete() {
result.count && query.cursorFn(result)
query.resolve(result)
}
function CopyInResponse() {
stream = new Stream.Writable({
autoDestroy: true,
write(chunk, encoding, callback) {
socket.write(b().d().raw(chunk).end(), callback)
},
destroy(error, callback) {
callback(error)
socket.write(b().f().str(error + b.N).end())
stream = null
},
final(callback) {
socket.write(b().c().end())
final = callback
stream = null
}
})
query.resolve(stream)
}
function CopyOutResponse() {
stream = new Stream.Readable({
read() { socket.resume() }
})
query.resolve(stream)
}
/* c8 ignore next 3 */
function CopyBothResponse() {
stream = new Stream.Duplex({
autoDestroy: true,
read() { socket.resume() },
/* c8 ignore next 11 */
write(chunk, encoding, callback) {
socket.write(b().d().raw(chunk).end(), callback)
},
destroy(error, callback) {
callback(error)
socket.write(b().f().str(error + b.N).end())
stream = null
},
final(callback) {
socket.write(b().c().end())
final = callback
}
})
query.resolve(stream)
}
function CopyData(x) {
stream && (stream.push(x.subarray(5)) || socket.pause())
}
function CopyDone() {
stream && stream.push(null)
stream = null
}
function NoticeResponse(x) {
onnotice
? onnotice(parseError(x))
: console.log(parseError(x)) // eslint-disable-line
}
/* c8 ignore next 3 */
function EmptyQueryResponse() {
/* noop */
}
/* c8 ignore next 3 */
function FunctionCallResponse() {
errored(Errors.notSupported('FunctionCallResponse'))
}
/* c8 ignore next 3 */
function NegotiateProtocolVersion() {
errored(Errors.notSupported('NegotiateProtocolVersion'))
}
/* c8 ignore next 3 */
function UnknownMessage(x) {
console.error('Postgres.js : Unknown Message:', x[0]) // eslint-disable-line
}
/* c8 ignore next 3 */
function UnknownAuth(x, type) {
console.error('Postgres.js : Unknown Auth:', type) // eslint-disable-line
}
/* Messages */
function Bind(parameters, types, statement = '', portal = '') {
let prev
, type
b().B().str(portal + b.N).str(statement + b.N).i16(0).i16(parameters.length)
parameters.forEach((x, i) => {
if (x === null)
return b.i32(0xFFFFFFFF)
type = types[i]
parameters[i] = x = type in options.serializers
? options.serializers[type](x)
: '' + x
prev = b.i
b.inc(4).str(x).i32(b.i - prev - 4, prev)
})
b.i16(0)
return b.end()
}
function Parse(str, parameters, types, name = '') {
b().P().str(name + b.N).str(str + b.N).i16(parameters.length)
parameters.forEach((x, i) => b.i32(types[i] || 0))
return b.end()
}
function Describe(x, name = '') {
return b().D().str(x).str(name + b.N).end()
}
function Execute(portal = '', rows = 0) {
return Buffer.concat([
b().E().str(portal + b.N).i32(rows).end(),
Flush
])
}
function Close(portal = '') {
return Buffer.concat([
b().C().str('P').str(portal + b.N).end(),
b().S().end()
])
}
function StartupMessage() {
return cancelMessage || b().inc(4).i16(3).z(2).str(
Object.entries(Object.assign({
user,
database,
client_encoding: 'UTF8'
},
options.connection
)).filter(([, v]) => v).map(([k, v]) => k + b.N + v).join(b.N)
).z(2).end(0)
}
}
function parseError(x) {
const error = {}
let start = 5
for (let i = 5; i < x.length - 1; i++) {
if (x[i] === 0) {
error[errorFields[x[start]]] = x.toString('utf8', start + 1, i)
start = i + 1
}
}
return error
}
function md5(x) {
return crypto.createHash('md5').update(x).digest('hex')
}
function hmac(key, x) {
return crypto.createHmac('sha256', key).update(x).digest()
}
function sha256(x) {
return crypto.createHash('sha256').update(x).digest()
}
function xor(a, b) {
const length = Math.max(a.length, b.length)
const buffer = Buffer.allocUnsafe(length)
for (let i = 0; i < length; i++)
buffer[i] = a[i] ^ b[i]
return buffer
}
function timer(fn, seconds) {
seconds = typeof seconds === 'function' ? seconds() : seconds
if (!seconds)
return { cancel: noop, start: noop }
let timer
return {
cancel() {
timer && (clearTimeout(timer), timer = null)
},
start() {
timer && clearTimeout(timer)
timer = setTimeout(done, seconds * 1000, arguments)
}
}
function done(args) {
fn.apply(null, args)
timer = null
}
}
================================================
FILE: cjs/src/errors.js
================================================
const PostgresError = module.exports.PostgresError = class PostgresError extends Error {
constructor(x) {
super(x.message)
this.name = this.constructor.name
Object.assign(this, x)
}
}
const Errors = module.exports.Errors = {
connection,
postgres,
generic,
notSupported
}
function connection(x, options, socket) {
const { host, port } = socket || options
const error = Object.assign(
new Error(('write ' + x + ' ' + (options.path || (host + ':' + port)))),
{
code: x,
errno: x,
address: options.path || host
}, options.path ? {} : { port: port }
)
Error.captureStackTrace(error, connection)
return error
}
function postgres(x) {
const error = new PostgresError(x)
Error.captureStackTrace(error, postgres)
return error
}
function generic(code, message) {
const error = Object.assign(new Error(code + ': ' + message), { code })
Error.captureStackTrace(error, generic)
return error
}
/* c8 ignore next 10 */
function notSupported(x) {
const error = Object.assign(
new Error(x + ' (B) is not supported'),
{
code: 'MESSAGE_NOT_SUPPORTED',
name: x
}
)
Error.captureStackTrace(error, notSupported)
return error
}
================================================
FILE: cjs/src/index.js
================================================
const os = require('os')
const fs = require('fs')
const {
mergeUserTypes,
inferType,
Parameter,
Identifier,
Builder,
toPascal,
pascal,
toCamel,
camel,
toKebab,
kebab,
fromPascal,
fromCamel,
fromKebab
} = require('./types.js')
const Connection = require('./connection.js')
const { Query, CLOSE } = require('./query.js')
const Queue = require('./queue.js')
const { Errors, PostgresError } = require('./errors.js')
const Subscribe = require('./subscribe.js')
const largeObject = require('./large.js')
Object.assign(Postgres, {
PostgresError,
toPascal,
pascal,
toCamel,
camel,
toKebab,
kebab,
fromPascal,
fromCamel,
fromKebab,
BigInt: {
to: 20,
from: [20],
parse: x => BigInt(x), // eslint-disable-line
serialize: x => x.toString()
}
})
module.exports = Postgres
function Postgres(a, b) {
const options = parseOptions(a, b)
, subscribe = options.no_subscribe || Subscribe(Postgres, { ...options })
let ending = false
const queries = Queue()
, connecting = Queue()
, reserved = Queue()
, closed = Queue()
, ended = Queue()
, open = Queue()
, busy = Queue()
, full = Queue()
, queues = { connecting, reserved, closed, ended, open, busy, full }
const connections = [...Array(options.max)].map(() => Connection(options, queues, { onopen, onend, onclose }))
const sql = Sql(handler)
Object.assign(sql, {
get parameters() { return options.parameters },
largeObject: largeObject.bind(null, sql),
subscribe,
CLOSE,
END: CLOSE,
PostgresError,
options,
reserve,
listen,
begin,
close,
end
})
return sql
function Sql(handler) {
handler.debug = options.debug
Object.entries(options.types).reduce((acc, [name, type]) => {
acc[name] = (x) => new Parameter(x, type.to)
return acc
}, typed)
Object.assign(sql, {
types: typed,
typed,
unsafe,
notify,
array,
json,
file
})
return sql
function typed(value, type) {
return new Parameter(value, type)
}
function sql(strings, ...args) {
const query = strings && Array.isArray(strings.raw)
? new Query(strings, args, handler, cancel)
: typeof strings === 'string' && !args.length
? new Identifier(options.transform.column.to ? options.transform.column.to(strings) : strings)
: new Builder(strings, args)
return query
}
function unsafe(string, args = [], options = {}) {
arguments.length === 2 && !Array.isArray(args) && (options = args, args = [])
const query = new Query([string], args, handler, cancel, {
prepare: false,
...options,
simple: 'simple' in options ? options.simple : args.length === 0
})
return query
}
function file(path, args = [], options = {}) {
arguments.length === 2 && !Array.isArray(args) && (options = args, args = [])
const query = new Query([], args, (query) => {
fs.readFile(path, 'utf8', (err, string) => {
if (err)
return query.reject(err)
query.strings = [string]
handler(query)
})
}, cancel, {
...options,
simple: 'simple' in options ? options.simple : args.length === 0
})
return query
}
}
async function listen(name, fn, onlisten) {
const listener = { fn, onlisten }
const sql = listen.sql || (listen.sql = Postgres({
...options,
max: 1,
idle_timeout: null,
max_lifetime: null,
fetch_types: false,
onclose() {
Object.entries(listen.channels).forEach(([name, { listeners }]) => {
delete listen.channels[name]
Promise.all(listeners.map(l => listen(name, l.fn, l.onlisten).catch(() => { /* noop */ })))
})
},
onnotify(c, x) {
c in listen.channels && listen.channels[c].listeners.forEach(l => l.fn(x))
}
}))
const channels = listen.channels || (listen.channels = {})
, exists = name in channels
if (exists) {
channels[name].listeners.push(listener)
const result = await channels[name].result
listener.onlisten && listener.onlisten()
return { state: result.state, unlisten }
}
channels[name] = { result: sql`listen ${
sql.unsafe('"' + name.replace(/"/g, '""') + '"')
}`, listeners: [listener] }
const result = await channels[name].result
listener.onlisten && listener.onlisten()
return { state: result.state, unlisten }
async function unlisten() {
if (name in channels === false)
return
channels[name].listeners = channels[name].listeners.filter(x => x !== listener)
if (channels[name].listeners.length)
return
delete channels[name]
return sql`unlisten ${
sql.unsafe('"' + name.replace(/"/g, '""') + '"')
}`
}
}
async function notify(channel, payload) {
return await sql`select pg_notify(${ channel }, ${ '' + payload })`
}
async function reserve() {
const queue = Queue()
const c = open.length
? open.shift()
: await new Promise((resolve, reject) => {
const query = { reserve: resolve, reject }
queries.push(query)
closed.length && connect(closed.shift(), query)
})
move(c, reserved)
c.reserved = () => queue.length
? c.execute(queue.shift())
: move(c, reserved)
c.reserved.release = true
const sql = Sql(handler)
sql.release = () => {
c.reserved = null
onopen(c)
}
return sql
function handler(q) {
c.queue === full
? queue.push(q)
: c.execute(q) || move(c, full)
}
}
async function begin(options, fn) {
!fn && (fn = options, options = '')
const queries = Queue()
let savepoints = 0
, connection
, prepare = null
try {
await sql.unsafe('begin ' + options.replace(/[^a-z ]/ig, ''), [], { onexecute }).execute()
return await Promise.race([
scope(connection, fn),
new Promise((_, reject) => connection.onclose = reject)
])
} catch (error) {
throw error
}
async function scope(c, fn, name) {
const sql = Sql(handler)
sql.savepoint = savepoint
sql.prepare = x => prepare = x.replace(/[^a-z0-9$-_. ]/gi)
let uncaughtError
, result
name && await sql`savepoint ${ sql(name) }`
try {
result = await new Promise((resolve, reject) => {
const x = fn(sql)
Promise.resolve(Array.isArray(x) ? Promise.all(x) : x).then(resolve, reject)
})
if (uncaughtError)
throw uncaughtError
} catch (e) {
await (name
? sql`rollback to ${ sql(name) }`
: sql`rollback`
)
throw e instanceof PostgresError && e.code === '25P02' && uncaughtError || e
}
if (!name) {
prepare
? await sql`prepare transaction '${ sql.unsafe(prepare) }'`
: await sql`commit`
}
return result
function savepoint(name, fn) {
if (name && Array.isArray(name.raw))
return savepoint(sql => sql.apply(sql, arguments))
arguments.length === 1 && (fn = name, name = null)
return scope(c, fn, 's' + savepoints++ + (name ? '_' + name : ''))
}
function handler(q) {
q.catch(e => uncaughtError || (uncaughtError = e))
c.queue === full
? queries.push(q)
: c.execute(q) || move(c, full)
}
}
function onexecute(c) {
connection = c
move(c, reserved)
c.reserved = () => queries.length
? c.execute(queries.shift())
: move(c, reserved)
}
}
function move(c, queue) {
c.queue.remove(c)
queue.push(c)
c.queue = queue
queue === open
? c.idleTimer.start()
: c.idleTimer.cancel()
return c
}
function json(x) {
return new Parameter(x, 3802)
}
function array(x, type) {
if (!Array.isArray(x))
return array(Array.from(arguments))
return new Parameter(x, type || (x.length ? inferType(x) || 25 : 0), options.shared.typeArrayMap)
}
function handler(query) {
if (ending)
return query.reject(Errors.connection('CONNECTION_ENDED', options, options))
if (open.length)
return go(open.shift(), query)
if (closed.length)
return connect(closed.shift(), query)
busy.length
? go(busy.shift(), query)
: queries.push(query)
}
function go(c, query) {
return c.execute(query)
? move(c, busy)
: move(c, full)
}
function cancel(query) {
return new Promise((resolve, reject) => {
query.state
? query.active
? Connection(options).cancel(query.state, resolve, reject)
: query.cancelled = { resolve, reject }
: (
queries.remove(query),
query.cancelled = true,
query.reject(Errors.generic('57014', 'canceling statement due to user request')),
resolve()
)
})
}
async function end({ timeout = null } = {}) {
if (ending)
return ending
await 1
let timer
return ending = Promise.race([
new Promise(r => timeout !== null && (timer = setTimeout(destroy, timeout * 1000, r))),
Promise.all(connections.map(c => c.end()).concat(
listen.sql ? listen.sql.end({ timeout: 0 }) : [],
subscribe.sql ? subscribe.sql.end({ timeout: 0 }) : []
))
]).then(() => clearTimeout(timer))
}
async function close() {
await Promise.all(connections.map(c => c.end()))
}
async function destroy(resolve) {
await Promise.all(connections.map(c => c.terminate()))
while (queries.length)
queries.shift().reject(Errors.connection('CONNECTION_DESTROYED', options))
resolve()
}
function connect(c, query) {
move(c, connecting)
c.connect(query)
return c
}
function onend(c) {
move(c, ended)
}
function onopen(c) {
if (queries.length === 0)
return move(c, open)
let max = Math.ceil(queries.length / (connecting.length + 1))
, ready = true
while (ready && queries.length && max-- > 0) {
const query = queries.shift()
if (query.reserve)
return query.reserve(c)
ready = c.execute(query)
}
ready
? move(c, busy)
: move(c, full)
}
function onclose(c, e) {
move(c, closed)
c.reserved = null
c.onclose && (c.onclose(e), c.onclose = null)
options.onclose && options.onclose(c.id)
queries.length && connect(c, queries.shift())
}
}
function parseOptions(a, b) {
if (a && a.shared)
return a
const env = process.env // eslint-disable-line
, o = (!a || typeof a === 'string' ? b : a) || {}
, { url, multihost } = parseUrl(a)
, query = [...url.searchParams].reduce((a, [b, c]) => (a[b] = c, a), {})
, host = o.hostname || o.host || multihost || url.hostname || env.PGHOST || 'localhost'
, port = o.port || url.port || env.PGPORT || 5432
, user = o.user || o.username || url.username || env.PGUSERNAME || env.PGUSER || osUsername()
o.no_prepare && (o.prepare = false)
query.sslmode && (query.ssl = query.sslmode, delete query.sslmode)
'timeout' in o && (console.log('The timeout option is deprecated, use idle_timeout instead'), o.idle_timeout = o.timeout) // eslint-disable-line
query.sslrootcert === 'system' && (query.ssl = 'verify-full')
const ints = ['idle_timeout', 'connect_timeout', 'max_lifetime', 'max_pipeline', 'backoff', 'keep_alive']
const defaults = {
max : globalThis.Cloudflare ? 3 : 10,
ssl : false,
sslnegotiation : null,
idle_timeout : null,
connect_timeout : 30,
max_lifetime : max_lifetime,
max_pipeline : 100,
backoff : backoff,
keep_alive : 60,
prepare : true,
debug : false,
fetch_types : true,
publications : 'alltables',
target_session_attrs: null
}
return {
host : Array.isArray(host) ? host : host.split(',').map(x => x.split(':')[0]),
port : Array.isArray(port) ? port : host.split(',').map(x => parseInt(x.split(':')[1] || port)),
path : o.path || host.indexOf('/') > -1 && host + '/.s.PGSQL.' + port,
database : o.database || o.db || (url.pathname || '').slice(1) || env.PGDATABASE || user,
user : user,
pass : o.pass || o.password || url.password || env.PGPASSWORD || '',
...Object.entries(defaults).reduce(
(acc, [k, d]) => {
const value = k in o ? o[k] : k in query
? (query[k] === 'disable' || query[k] === 'false' ? false : query[k])
: env['PG' + k.toUpperCase()] || d
acc[k] = typeof value === 'string' && ints.includes(k)
? +value
: value
return acc
},
{}
),
connection : {
application_name: env.PGAPPNAME || 'postgres.js',
...o.connection,
...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {})
},
types : o.types || {},
target_session_attrs: tsa(o, url, env),
onnotice : o.onnotice,
onnotify : o.onnotify,
onclose : o.onclose,
onparameter : o.onparameter,
socket : o.socket,
transform : parseTransform(o.transform || { undefined: undefined }),
parameters : {},
shared : { retries: 0, typeArrayMap: {} },
...mergeUserTypes(o.types)
}
}
function tsa(o, url, env) {
const x = o.target_session_attrs || url.searchParams.get('target_session_attrs') || env.PGTARGETSESSIONATTRS
if (!x || ['read-write', 'read-only', 'primary', 'standby', 'prefer-standby'].includes(x))
return x
throw new Error('target_session_attrs ' + x + ' is not supported')
}
function backoff(retries) {
return (0.5 + Math.random() / 2) * Math.min(3 ** retries / 100, 20)
}
function max_lifetime() {
return 60 * (30 + Math.random() * 30)
}
function parseTransform(x) {
return {
undefined: x.undefined,
column: {
from: typeof x.column === 'function' ? x.column : x.column && x.column.from,
to: x.column && x.column.to
},
value: {
from: typeof x.value === 'function' ? x.value : x.value && x.value.from,
to: x.value && x.value.to
},
row: {
from: typeof x.row === 'function' ? x.row : x.row && x.row.from,
to: x.row && x.row.to
}
}
}
function parseUrl(url) {
if (!url || typeof url !== 'string')
return { url: { searchParams: new Map() } }
let host = url
host = host.slice(host.indexOf('://') + 3).split(/[?/]/)[0]
host = decodeURIComponent(host.slice(host.indexOf('@') + 1))
const urlObj = new URL(url.replace(host, host.split(',')[0]))
return {
url: {
username: decodeURIComponent(urlObj.username),
password: decodeURIComponent(urlObj.password),
host: urlObj.host,
hostname: urlObj.hostname,
port: urlObj.port,
pathname: urlObj.pathname,
searchParams: urlObj.searchParams
},
multihost: host.indexOf(',') > -1 && host
}
}
function osUsername() {
try {
return os.userInfo().username // eslint-disable-line
} catch (_) {
return process.env.USERNAME || process.env.USER || process.env.LOGNAME // eslint-disable-line
}
}
================================================
FILE: cjs/src/large.js
================================================
const Stream = require('stream')
module.exports = largeObject;function largeObject(sql, oid, mode = 0x00020000 | 0x00040000) {
return new Promise(async(resolve, reject) => {
await sql.begin(async sql => {
let finish
!oid && ([{ oid }] = await sql`select lo_creat(-1) as oid`)
const [{ fd }] = await sql`select lo_open(${ oid }, ${ mode }) as fd`
const lo = {
writable,
readable,
close : () => sql`select lo_close(${ fd })`.then(finish),
tell : () => sql`select lo_tell64(${ fd })`,
read : (x) => sql`select loread(${ fd }, ${ x }) as data`,
write : (x) => sql`select lowrite(${ fd }, ${ x })`,
truncate : (x) => sql`select lo_truncate64(${ fd }, ${ x })`,
seek : (x, whence = 0) => sql`select lo_lseek64(${ fd }, ${ x }, ${ whence })`,
size : () => sql`
select
lo_lseek64(${ fd }, location, 0) as position,
seek.size
from (
select
lo_lseek64($1, 0, 2) as size,
tell.location
from (select lo_tell64($1) as location) tell
) seek
`
}
resolve(lo)
return new Promise(async r => finish = r)
async function readable({
highWaterMark = 2048 * 8,
start = 0,
end = Infinity
} = {}) {
let max = end - start
start && await lo.seek(start)
return new Stream.Readable({
highWaterMark,
async read(size) {
const l = size > max ? size - max : size
max -= size
const [{ data }] = await lo.read(l)
this.push(data)
if (data.length < size)
this.push(null)
}
})
}
async function writable({
highWaterMark = 2048 * 8,
start = 0
} = {}) {
start && await lo.seek(start)
return new Stream.Writable({
highWaterMark,
write(chunk, encoding, callback) {
lo.write(chunk).then(() => callback(), callback)
}
})
}
}).catch(reject)
})
}
================================================
FILE: cjs/src/query.js
================================================
const originCache = new Map()
, originStackCache = new Map()
, originError = Symbol('OriginError')
const CLOSE = module.exports.CLOSE = {}
const Query = module.exports.Query = class Query extends Promise {
constructor(strings, args, handler, canceller, options = {}) {
let resolve
, reject
super((a, b) => {
resolve = a
reject = b
})
this.tagged = Array.isArray(strings.raw)
this.strings = strings
this.args = args
this.handler = handler
this.canceller = canceller
this.options = options
this.state = null
this.statement = null
this.resolve = x => (this.active = false, resolve(x))
this.reject = x => (this.active = false, reject(x))
this.active = false
this.cancelled = null
this.executed = false
this.signature = ''
this[originError] = this.handler.debug
? new Error()
: this.tagged && cachedError(this.strings)
}
get origin() {
return (this.handler.debug
? this[originError].stack
: this.tagged && originStackCache.has(this.strings)
? originStackCache.get(this.strings)
: originStackCache.set(this.strings, this[originError].stack).get(this.strings)
) || ''
}
static get [Symbol.species]() {
return Promise
}
cancel() {
return this.canceller && (this.canceller(this), this.canceller = null)
}
simple() {
this.options.simple = true
this.options.prepare = false
return this
}
async readable() {
this.simple()
this.streaming = true
return this
}
async writable() {
this.simple()
this.streaming = true
return this
}
cursor(rows = 1, fn) {
this.options.simple = false
if (typeof rows === 'function') {
fn = rows
rows = 1
}
this.cursorRows = rows
if (typeof fn === 'function')
return (this.cursorFn = fn, this)
let prev
return {
[Symbol.asyncIterator]: () => ({
next: () => {
if (this.executed && !this.active)
return { done: true }
prev && prev()
const promise = new Promise((resolve, reject) => {
this.cursorFn = value => {
resolve({ value, done: false })
return new Promise(r => prev = r)
}
this.resolve = () => (this.active = false, resolve({ done: true }))
this.reject = x => (this.active = false, reject(x))
})
this.execute()
return promise
},
return() {
prev && prev(CLOSE)
return { done: true }
}
})
}
}
describe() {
this.options.simple = false
this.onlyDescribe = this.options.prepare = true
return this
}
stream() {
throw new Error('.stream has been renamed to .forEach')
}
forEach(fn) {
gitextract_gowjwbrv/
├── .eslintrc.json
├── .github/
│ └── workflows/
│ └── test.yml
├── CHANGELOG.md
├── README.md
├── UNLICENSE
├── cf/
│ ├── polyfills.js
│ ├── src/
│ │ ├── bytes.js
│ │ ├── connection.js
│ │ ├── errors.js
│ │ ├── index.js
│ │ ├── large.js
│ │ ├── query.js
│ │ ├── queue.js
│ │ ├── result.js
│ │ ├── subscribe.js
│ │ └── types.js
│ └── test.js
├── cjs/
│ ├── package.json
│ ├── src/
│ │ ├── bytes.js
│ │ ├── connection.js
│ │ ├── errors.js
│ │ ├── index.js
│ │ ├── large.js
│ │ ├── query.js
│ │ ├── queue.js
│ │ ├── result.js
│ │ ├── subscribe.js
│ │ └── types.js
│ └── tests/
│ ├── bootstrap.js
│ ├── copy.csv
│ ├── index.js
│ ├── pg_hba.conf
│ ├── select-param.sql
│ ├── select.sql
│ └── test.js
├── deno/
│ ├── README.md
│ ├── mod.js
│ ├── package.json
│ ├── polyfills.js
│ ├── src/
│ │ ├── bytes.js
│ │ ├── connection.js
│ │ ├── errors.js
│ │ ├── index.js
│ │ ├── large.js
│ │ ├── query.js
│ │ ├── queue.js
│ │ ├── result.js
│ │ ├── subscribe.js
│ │ └── types.js
│ ├── tests/
│ │ ├── bootstrap.js
│ │ ├── copy.csv
│ │ ├── index.js
│ │ ├── pg_hba.conf
│ │ ├── select-param.sql
│ │ ├── select.sql
│ │ └── test.js
│ └── types/
│ └── index.d.ts
├── package.json
├── src/
│ ├── bytes.js
│ ├── connection.js
│ ├── errors.js
│ ├── index.js
│ ├── large.js
│ ├── query.js
│ ├── queue.js
│ ├── result.js
│ ├── subscribe.js
│ └── types.js
├── tests/
│ ├── bootstrap.js
│ ├── copy.csv
│ ├── index.js
│ ├── pg_hba.conf
│ ├── select-param.sql
│ ├── select.sql
│ └── test.js
├── transpile.cf.js
├── transpile.cjs
├── transpile.deno.js
└── types/
├── index.d.ts
├── package.json
└── tsconfig.json
SYMBOL INDEX (505 symbols across 57 files)
FILE: cf/polyfills.js
method userInfo (line 93) | userInfo() {
method readFile (line 99) | readFile() {
method connect (line 112) | connect({ socket: tcp, servername }) {
function Socket (line 132) | function Socket() {
function setImmediate (line 219) | function setImmediate(fn) {
function clearImmediate (line 231) | function clearImmediate(id) {
FILE: cf/src/bytes.js
method inc (line 18) | inc(x) {
method str (line 22) | str(x) {
method i16 (line 28) | i16(x) {
method i32 (line 34) | i32(x, i) {
method z (line 44) | z(x) {
method raw (line 50) | raw(x) {
method end (line 55) | end(at = 1) {
function fit (line 66) | function fit(x) {
function reset (line 76) | function reset() {
FILE: cf/src/connection.js
function Connection (line 54) | function Connection(options, queues = {}, { onopen = noop, onend = noop,...
function parseError (line 1012) | function parseError(x) {
function md5 (line 1024) | function md5(x) {
function hmac (line 1028) | function hmac(key, x) {
function sha256 (line 1032) | function sha256(x) {
function xor (line 1036) | function xor(a, b) {
function timer (line 1044) | function timer(fn, seconds) {
FILE: cf/src/errors.js
class PostgresError (line 1) | class PostgresError extends Error {
method constructor (line 2) | constructor(x) {
function connection (line 16) | function connection(x, options, socket) {
function postgres (line 30) | function postgres(x) {
function generic (line 36) | function generic(code, message) {
function notSupported (line 43) | function notSupported(x) {
FILE: cf/src/index.js
function Postgres (line 50) | function Postgres(a, b) {
function parseOptions (line 431) | function parseOptions(a, b) {
function tsa (line 504) | function tsa(o, url, env) {
function backoff (line 512) | function backoff(retries) {
function max_lifetime (line 516) | function max_lifetime() {
function parseTransform (line 520) | function parseTransform(x) {
function parseUrl (line 538) | function parseUrl(url) {
function osUsername (line 562) | function osUsername() {
FILE: cf/src/large.js
function largeObject (line 3) | function largeObject(sql, oid, mode = 0x00020000 | 0x00040000) {
FILE: cf/src/query.js
constant CLOSE (line 5) | const CLOSE = {}
class Query (line 6) | class Query extends Promise {
method constructor (line 7) | constructor(strings, args, handler, canceller, options = {}) {
method origin (line 39) | get origin() {
method cancel (line 52) | cancel() {
method simple (line 56) | simple() {
method readable (line 62) | async readable() {
method writable (line 68) | async writable() {
method cursor (line 74) | cursor(rows = 1, fn) {
method describe (line 113) | describe() {
method stream (line 119) | stream() {
method forEach (line 123) | forEach(fn) {
method raw (line 129) | raw() {
method values (line 134) | values() {
method handle (line 139) | async handle() {
method execute (line 143) | execute() {
method then (line 148) | then() {
method catch (line 153) | catch() {
method finally (line 158) | finally() {
method [Symbol.species] (line 48) | static get [Symbol.species]() {
function cachedError (line 164) | function cachedError(xs) {
FILE: cf/src/queue.js
function Queue (line 3) | function Queue(initial = []) {
FILE: cf/src/result.js
class Result (line 1) | class Result extends Array {
method constructor (line 2) | constructor() {
method [Symbol.species] (line 13) | static get [Symbol.species]() {
FILE: cf/src/subscribe.js
function Subscribe (line 4) | function Subscribe(postgres, options) {
function Time (line 144) | function Time(x) {
function parse (line 148) | function parse(x, state, parsers, handle, transform) {
function tuples (line 235) | function tuples(x, columns, xi, transform) {
function parseEvent (line 267) | function parseEvent(x) {
FILE: cf/src/types.js
class NotTagged (line 43) | class NotTagged { then() { notTagged() } catch() { notTagged() } finally...
method then (line 43) | then() { notTagged() }
method catch (line 43) | catch() { notTagged() }
method finally (line 43) | finally() { notTagged() }
class Identifier (line 45) | class Identifier extends NotTagged {
method constructor (line 46) | constructor(value) {
class Parameter (line 52) | class Parameter extends NotTagged {
method constructor (line 53) | constructor(value, type, array) {
class Builder (line 61) | class Builder extends NotTagged {
method constructor (line 62) | constructor(first, rest) {
method build (line 68) | build(before, parameters, types, options) {
function handleValue (line 76) | function handleValue(x, parameters, types, options) {
function stringify (line 99) | function stringify(q, string, value, parameters, types, options) { // es...
function stringifyValue (line 108) | function stringifyValue(string, value, parameters, types, o) {
function fragment (line 118) | function fragment(q, parameters, types, options) {
function valuesBuilder (line 123) | function valuesBuilder(first, parameters, types, columns, options) {
function values (line 131) | function values(first, rest, parameters, types, options) {
function select (line 137) | function select(first, rest, parameters, types, options) {
method update (line 165) | update(first, rest, parameters, types, options) {
method insert (line 172) | insert(first, rest, parameters, types, options) {
function notTagged (line 179) | function notTagged() {
constant END (line 186) | const END = {}
function firstIsString (line 188) | function firstIsString(x) {
function typeHandlers (line 202) | function typeHandlers(types) {
function escapeIdentifiers (line 213) | function escapeIdentifiers(xs, { transform: { column } }) {
function arrayEscape (line 236) | function arrayEscape(x) {
function arrayParserLoop (line 282) | function arrayParserLoop(s, x, parser, typarray) {
function createJsonTransform (line 339) | function createJsonTransform(fn) {
FILE: cf/test.js
constant DATABASE_URL (line 6) | const DATABASE_URL = ''
method fetch (line 9) | async fetch() {
FILE: cjs/src/bytes.js
method inc (line 17) | inc(x) {
method str (line 21) | str(x) {
method i16 (line 27) | i16(x) {
method i32 (line 33) | i32(x, i) {
method z (line 43) | z(x) {
method raw (line 49) | raw(x) {
method end (line 54) | end(at = 1) {
function fit (line 65) | function fit(x) {
function reset (line 75) | function reset() {
FILE: cjs/src/connection.js
function Connection (line 52) | function Connection(options, queues = {}, { onopen = noop, onend = noop,...
function parseError (line 1010) | function parseError(x) {
function md5 (line 1022) | function md5(x) {
function hmac (line 1026) | function hmac(key, x) {
function sha256 (line 1030) | function sha256(x) {
function xor (line 1034) | function xor(a, b) {
function timer (line 1042) | function timer(fn, seconds) {
FILE: cjs/src/errors.js
method constructor (line 2) | constructor(x) {
function connection (line 16) | function connection(x, options, socket) {
function postgres (line 30) | function postgres(x) {
function generic (line 36) | function generic(code, message) {
function notSupported (line 43) | function notSupported(x) {
FILE: cjs/src/index.js
function Postgres (line 49) | function Postgres(a, b) {
function parseOptions (line 430) | function parseOptions(a, b) {
function tsa (line 503) | function tsa(o, url, env) {
function backoff (line 511) | function backoff(retries) {
function max_lifetime (line 515) | function max_lifetime() {
function parseTransform (line 519) | function parseTransform(x) {
function parseUrl (line 537) | function parseUrl(url) {
function osUsername (line 561) | function osUsername() {
FILE: cjs/src/large.js
function largeObject (line 3) | function largeObject(sql, oid, mode = 0x00020000 | 0x00040000) {
FILE: cjs/src/query.js
constant CLOSE (line 5) | const CLOSE = module.exports.CLOSE = {}
method constructor (line 7) | constructor(strings, args, handler, canceller, options = {}) {
method origin (line 39) | get origin() {
method [Symbol.species] (line 48) | static get [Symbol.species]() {
method cancel (line 52) | cancel() {
method simple (line 56) | simple() {
method readable (line 62) | async readable() {
method writable (line 68) | async writable() {
method cursor (line 74) | cursor(rows = 1, fn) {
method describe (line 113) | describe() {
method stream (line 119) | stream() {
method forEach (line 123) | forEach(fn) {
method raw (line 129) | raw() {
method values (line 134) | values() {
method handle (line 139) | async handle() {
method execute (line 143) | execute() {
method then (line 148) | then() {
method catch (line 153) | catch() {
method finally (line 158) | finally() {
function cachedError (line 164) | function cachedError(xs) {
FILE: cjs/src/queue.js
function Queue (line 3) | function Queue(initial = []) {
FILE: cjs/src/result.js
method constructor (line 2) | constructor() {
method [Symbol.species] (line 13) | static get [Symbol.species]() {
FILE: cjs/src/subscribe.js
function Subscribe (line 3) | function Subscribe(postgres, options) {
function Time (line 143) | function Time(x) {
function parse (line 147) | function parse(x, state, parsers, handle, transform) {
function tuples (line 234) | function tuples(x, columns, xi, transform) {
function parseEvent (line 266) | function parseEvent(x) {
FILE: cjs/src/types.js
class NotTagged (line 42) | class NotTagged { then() { notTagged() } catch() { notTagged() } finally...
method then (line 42) | then() { notTagged() }
method catch (line 42) | catch() { notTagged() }
method finally (line 42) | finally() { notTagged() }
method constructor (line 45) | constructor(value) {
method constructor (line 52) | constructor(value, type, array) {
method constructor (line 61) | constructor(first, rest) {
method build (line 67) | build(before, parameters, types, options) {
function handleValue (line 75) | function handleValue(x, parameters, types, options) {
function stringify (line 98) | function stringify(q, string, value, parameters, types, options) { // es...
function stringifyValue (line 107) | function stringifyValue(string, value, parameters, types, o) {
function fragment (line 117) | function fragment(q, parameters, types, options) {
function valuesBuilder (line 122) | function valuesBuilder(first, parameters, types, columns, options) {
function values (line 130) | function values(first, rest, parameters, types, options) {
function select (line 136) | function select(first, rest, parameters, types, options) {
method update (line 164) | update(first, rest, parameters, types, options) {
method insert (line 171) | insert(first, rest, parameters, types, options) {
function notTagged (line 178) | function notTagged() {
constant END (line 185) | const END = module.exports.END = {}
function firstIsString (line 187) | function firstIsString(x) {
function typeHandlers (line 201) | function typeHandlers(types) {
function escapeIdentifiers (line 212) | function escapeIdentifiers(xs, { transform: { column } }) {
function arrayEscape (line 235) | function arrayEscape(x) {
function arrayParserLoop (line 281) | function arrayParserLoop(s, x, parser, typarray) {
function createJsonTransform (line 338) | function createJsonTransform(fn) {
FILE: cjs/tests/bootstrap.js
function exec (line 21) | function exec(cmd, args) {
function execAsync (line 27) | async function execAsync(cmd, args) { // eslint-disable-line
FILE: cjs/tests/index.js
function yo (line 1611) | function yo() {
function wat (line 1615) | function wat() {
method valueOf (line 1700) | valueOf() { return first ? (first = false, 0.0001) : 1 }
FILE: cjs/tests/test.js
function test (line 18) | async function test(o, name, options, fn) {
function exit (line 62) | function exit() {
FILE: deno/polyfills.js
class Socket (line 8) | class Socket {
method constructor (line 9) | constructor() {
function createSocket (line 14) | function createSocket() {
method createServer (line 144) | createServer() {
method connect (line 164) | connect({ socket, ...options }) {
FILE: deno/src/bytes.js
method inc (line 18) | inc(x) {
method str (line 22) | str(x) {
method i16 (line 28) | i16(x) {
method i32 (line 34) | i32(x, i) {
method z (line 44) | z(x) {
method raw (line 50) | raw(x) {
method end (line 55) | end(at = 1) {
function fit (line 66) | function fit(x) {
function reset (line 76) | function reset() {
FILE: deno/src/connection.js
function Connection (line 55) | function Connection(options, queues = {}, { onopen = noop, onend = noop,...
function parseError (line 1013) | function parseError(x) {
function md5 (line 1025) | function md5(x) {
function hmac (line 1029) | function hmac(key, x) {
function sha256 (line 1033) | function sha256(x) {
function xor (line 1037) | function xor(a, b) {
function timer (line 1045) | function timer(fn, seconds) {
FILE: deno/src/errors.js
class PostgresError (line 1) | class PostgresError extends Error {
method constructor (line 2) | constructor(x) {
function connection (line 16) | function connection(x, options, socket) {
function postgres (line 30) | function postgres(x) {
function generic (line 36) | function generic(code, message) {
function notSupported (line 43) | function notSupported(x) {
FILE: deno/src/index.js
function Postgres (line 50) | function Postgres(a, b) {
function parseOptions (line 431) | function parseOptions(a, b) {
function tsa (line 504) | function tsa(o, url, env) {
function backoff (line 512) | function backoff(retries) {
function max_lifetime (line 516) | function max_lifetime() {
function parseTransform (line 520) | function parseTransform(x) {
function parseUrl (line 538) | function parseUrl(url) {
function osUsername (line 562) | function osUsername() {
FILE: deno/src/large.js
function largeObject (line 3) | function largeObject(sql, oid, mode = 0x00020000 | 0x00040000) {
FILE: deno/src/query.js
constant CLOSE (line 5) | const CLOSE = {}
class Query (line 6) | class Query extends Promise {
method constructor (line 7) | constructor(strings, args, handler, canceller, options = {}) {
method origin (line 39) | get origin() {
method cancel (line 52) | cancel() {
method simple (line 56) | simple() {
method readable (line 62) | async readable() {
method writable (line 68) | async writable() {
method cursor (line 74) | cursor(rows = 1, fn) {
method describe (line 113) | describe() {
method stream (line 119) | stream() {
method forEach (line 123) | forEach(fn) {
method raw (line 129) | raw() {
method values (line 134) | values() {
method handle (line 139) | async handle() {
method execute (line 143) | execute() {
method then (line 148) | then() {
method catch (line 153) | catch() {
method finally (line 158) | finally() {
method [Symbol.species] (line 48) | static get [Symbol.species]() {
function cachedError (line 164) | function cachedError(xs) {
FILE: deno/src/queue.js
function Queue (line 3) | function Queue(initial = []) {
FILE: deno/src/result.js
class Result (line 1) | class Result extends Array {
method constructor (line 2) | constructor() {
method [Symbol.species] (line 13) | static get [Symbol.species]() {
FILE: deno/src/subscribe.js
function Subscribe (line 4) | function Subscribe(postgres, options) {
function Time (line 144) | function Time(x) {
function parse (line 148) | function parse(x, state, parsers, handle, transform) {
function tuples (line 235) | function tuples(x, columns, xi, transform) {
function parseEvent (line 267) | function parseEvent(x) {
FILE: deno/src/types.js
class NotTagged (line 43) | class NotTagged { then() { notTagged() } catch() { notTagged() } finally...
method then (line 43) | then() { notTagged() }
method catch (line 43) | catch() { notTagged() }
method finally (line 43) | finally() { notTagged() }
class Identifier (line 45) | class Identifier extends NotTagged {
method constructor (line 46) | constructor(value) {
class Parameter (line 52) | class Parameter extends NotTagged {
method constructor (line 53) | constructor(value, type, array) {
class Builder (line 61) | class Builder extends NotTagged {
method constructor (line 62) | constructor(first, rest) {
method build (line 68) | build(before, parameters, types, options) {
function handleValue (line 76) | function handleValue(x, parameters, types, options) {
function stringify (line 99) | function stringify(q, string, value, parameters, types, options) { // es...
function stringifyValue (line 108) | function stringifyValue(string, value, parameters, types, o) {
function fragment (line 118) | function fragment(q, parameters, types, options) {
function valuesBuilder (line 123) | function valuesBuilder(first, parameters, types, columns, options) {
function values (line 131) | function values(first, rest, parameters, types, options) {
function select (line 137) | function select(first, rest, parameters, types, options) {
method update (line 165) | update(first, rest, parameters, types, options) {
method insert (line 172) | insert(first, rest, parameters, types, options) {
function notTagged (line 179) | function notTagged() {
constant END (line 186) | const END = {}
function firstIsString (line 188) | function firstIsString(x) {
function typeHandlers (line 202) | function typeHandlers(types) {
function escapeIdentifiers (line 213) | function escapeIdentifiers(xs, { transform: { column } }) {
function arrayEscape (line 236) | function arrayEscape(x) {
function arrayParserLoop (line 282) | function arrayParserLoop(s, x, parser, typarray) {
function createJsonTransform (line 339) | function createJsonTransform(fn) {
FILE: deno/tests/bootstrap.js
function ignore (line 21) | function ignore(cmd, args) {
function exec (line 27) | async function exec(cmd, args) { // eslint-disable-line
FILE: deno/tests/index.js
function yo (line 1613) | function yo() {
function wat (line 1617) | function wat() {
method valueOf (line 1702) | valueOf() { return first ? (first = false, 0.0001) : 1 }
FILE: deno/tests/test.js
function test (line 19) | async function test(o, name, options, fn) {
function exit (line 63) | function exit() {
FILE: deno/types/index.d.ts
type BaseOptions (line 28) | interface BaseOptions<T extends Record<string, postgres.PostgresType>> {
class NotAPromise (line 132) | class NotAPromise {
type UnwrapPromiseArray (line 152) | type UnwrapPromiseArray<T> = T extends any[] ? {
type Keys (line 156) | type Keys = string
type SerializableObject (line 158) | type SerializableObject<T, K extends readonly any[], TT> =
type First (line 162) | type First<T, K extends readonly any[], TT> =
type Rest (line 178) | type Rest<T> =
type Return (line 195) | type Return<T, K extends readonly any[]> =
class PostgresError (line 203) | class PostgresError extends Error {
type PostgresType (line 321) | interface PostgresType<T = any> {
type ConnectionParameters (line 328) | interface ConnectionParameters {
type Options (line 348) | interface Options<T extends Record<string, postgres.PostgresType>> exten...
type ParsedOptions (line 380) | interface ParsedOptions<T extends Record<string, unknown> = {}> extends ...
type Transform (line 393) | interface Transform {
type Notice (line 416) | interface Notice {
type Parameter (line 420) | interface Parameter<T = SerializableParameter> extends NotAPromise {
type ArrayParameter (line 435) | interface ArrayParameter<T extends readonly any[] = readonly any[]> exte...
type ConnectionError (line 439) | interface ConnectionError extends globalThis.Error {
type NotSupportedError (line 450) | interface NotSupportedError extends globalThis.Error {
type GenericError (line 455) | interface GenericError extends globalThis.Error {
type AuthNotImplementedError (line 466) | interface AuthNotImplementedError extends globalThis.Error {
type Error (line 472) | type Error = never
type ColumnInfo (line 479) | interface ColumnInfo {
type RelationInfo (line 487) | interface RelationInfo {
type ReplicationEvent (line 494) | type ReplicationEvent =
type SubscriptionHandle (line 499) | interface SubscriptionHandle {
type LargeObject (line 503) | interface LargeObject {
type EscapableArray (line 523) | type EscapableArray = (string | number)[]
type Serializable (line 525) | type Serializable = never
type SerializableParameter (line 533) | type SerializableParameter<T = never> = never
type JSONValue (line 541) | type JSONValue = // using a dedicated type to detect symbols, bigints, a...
type Row (line 556) | interface Row {
type MaybeRow (line 560) | type MaybeRow = Row | undefined;
type Column (line 562) | interface Column<T extends string> {
type ColumnList (line 570) | type ColumnList<T> = (T extends string ? Column<T> : never)[];
type State (line 572) | interface State {
type Statement (line 578) | interface Statement {
type ResultMeta (line 588) | interface ResultMeta<T extends number | null> {
type ResultQueryMeta (line 595) | interface ResultQueryMeta<T extends number | null, U> extends ResultMeta...
type ExecutionResult (line 599) | type ExecutionResult<T> = [] & ResultQueryMeta<number, keyof NonNullable...
type ValuesRowList (line 600) | type ValuesRowList<T extends readonly any[]> = T[number][keyof T[number]...
type RawRowList (line 601) | type RawRowList<T extends readonly any[]> = Buffer[][] & Iterable<Buffer...
type RowList (line 602) | type RowList<T extends readonly any[]> = T & Iterable<NonNullable<T[numb...
type PendingQueryModifiers (line 604) | interface PendingQueryModifiers<TRow extends readonly any[]> {
type PendingDescribeQuery (line 624) | interface PendingDescribeQuery extends Promise<Statement> {
type PendingValuesQuery (line 627) | interface PendingValuesQuery<TRow extends readonly MaybeRow[]> extends P...
type PendingRawQuery (line 631) | interface PendingRawQuery<TRow extends readonly MaybeRow[]> extends Prom...
type PendingQuery (line 634) | interface PendingQuery<TRow extends readonly MaybeRow[]> extends Promise...
type PendingRequest (line 640) | interface PendingRequest extends Promise<[] & ResultMeta<null>> { }
type ListenRequest (line 642) | interface ListenRequest extends Promise<ListenMeta> { }
type ListenMeta (line 643) | interface ListenMeta extends ResultMeta<null> {
type Helper (line 647) | interface Helper<T, U extends readonly any[] = T[]> extends NotAPromise {
type Fragment (line 652) | type Fragment = PendingQuery<any>
type ParameterOrJSON (line 654) | type ParameterOrJSON<T> =
type ParameterOrFragment (line 658) | type ParameterOrFragment<T> =
type Sql (line 663) | interface Sql<TTypes extends Record<string, unknown> = {}> {
type UnsafeQueryOptions (line 712) | interface UnsafeQueryOptions {
type TransactionSql (line 720) | interface TransactionSql<TTypes extends Record<string, unknown> = {}> ex...
type ReservedSql (line 740) | interface ReservedSql<TTypes extends Record<string, unknown> = {}> exten...
FILE: src/bytes.js
method inc (line 17) | inc(x) {
method str (line 21) | str(x) {
method i16 (line 27) | i16(x) {
method i32 (line 33) | i32(x, i) {
method z (line 43) | z(x) {
method raw (line 49) | raw(x) {
method end (line 54) | end(at = 1) {
function fit (line 65) | function fit(x) {
function reset (line 75) | function reset() {
FILE: src/connection.js
function Connection (line 52) | function Connection(options, queues = {}, { onopen = noop, onend = noop,...
function parseError (line 1010) | function parseError(x) {
function md5 (line 1022) | function md5(x) {
function hmac (line 1026) | function hmac(key, x) {
function sha256 (line 1030) | function sha256(x) {
function xor (line 1034) | function xor(a, b) {
function timer (line 1042) | function timer(fn, seconds) {
FILE: src/errors.js
class PostgresError (line 1) | class PostgresError extends Error {
method constructor (line 2) | constructor(x) {
function connection (line 16) | function connection(x, options, socket) {
function postgres (line 30) | function postgres(x) {
function generic (line 36) | function generic(code, message) {
function notSupported (line 43) | function notSupported(x) {
FILE: src/index.js
function Postgres (line 49) | function Postgres(a, b) {
function parseOptions (line 430) | function parseOptions(a, b) {
function tsa (line 503) | function tsa(o, url, env) {
function backoff (line 511) | function backoff(retries) {
function max_lifetime (line 515) | function max_lifetime() {
function parseTransform (line 519) | function parseTransform(x) {
function parseUrl (line 537) | function parseUrl(url) {
function osUsername (line 561) | function osUsername() {
FILE: src/large.js
function largeObject (line 3) | function largeObject(sql, oid, mode = 0x00020000 | 0x00040000) {
FILE: src/query.js
constant CLOSE (line 5) | const CLOSE = {}
class Query (line 6) | class Query extends Promise {
method constructor (line 7) | constructor(strings, args, handler, canceller, options = {}) {
method origin (line 39) | get origin() {
method cancel (line 52) | cancel() {
method simple (line 56) | simple() {
method readable (line 62) | async readable() {
method writable (line 68) | async writable() {
method cursor (line 74) | cursor(rows = 1, fn) {
method describe (line 113) | describe() {
method stream (line 119) | stream() {
method forEach (line 123) | forEach(fn) {
method raw (line 129) | raw() {
method values (line 134) | values() {
method handle (line 139) | async handle() {
method execute (line 143) | execute() {
method then (line 148) | then() {
method catch (line 153) | catch() {
method finally (line 158) | finally() {
method [Symbol.species] (line 48) | static get [Symbol.species]() {
function cachedError (line 164) | function cachedError(xs) {
FILE: src/queue.js
function Queue (line 3) | function Queue(initial = []) {
FILE: src/result.js
class Result (line 1) | class Result extends Array {
method constructor (line 2) | constructor() {
method [Symbol.species] (line 13) | static get [Symbol.species]() {
FILE: src/subscribe.js
function Subscribe (line 3) | function Subscribe(postgres, options) {
function Time (line 143) | function Time(x) {
function parse (line 147) | function parse(x, state, parsers, handle, transform) {
function tuples (line 234) | function tuples(x, columns, xi, transform) {
function parseEvent (line 266) | function parseEvent(x) {
FILE: src/types.js
class NotTagged (line 42) | class NotTagged { then() { notTagged() } catch() { notTagged() } finally...
method then (line 42) | then() { notTagged() }
method catch (line 42) | catch() { notTagged() }
method finally (line 42) | finally() { notTagged() }
class Identifier (line 44) | class Identifier extends NotTagged {
method constructor (line 45) | constructor(value) {
class Parameter (line 51) | class Parameter extends NotTagged {
method constructor (line 52) | constructor(value, type, array) {
class Builder (line 60) | class Builder extends NotTagged {
method constructor (line 61) | constructor(first, rest) {
method build (line 67) | build(before, parameters, types, options) {
function handleValue (line 75) | function handleValue(x, parameters, types, options) {
function stringify (line 98) | function stringify(q, string, value, parameters, types, options) { // es...
function stringifyValue (line 107) | function stringifyValue(string, value, parameters, types, o) {
function fragment (line 117) | function fragment(q, parameters, types, options) {
function valuesBuilder (line 122) | function valuesBuilder(first, parameters, types, columns, options) {
function values (line 130) | function values(first, rest, parameters, types, options) {
function select (line 136) | function select(first, rest, parameters, types, options) {
method update (line 164) | update(first, rest, parameters, types, options) {
method insert (line 171) | insert(first, rest, parameters, types, options) {
function notTagged (line 178) | function notTagged() {
constant END (line 185) | const END = {}
function firstIsString (line 187) | function firstIsString(x) {
function typeHandlers (line 201) | function typeHandlers(types) {
function escapeIdentifiers (line 212) | function escapeIdentifiers(xs, { transform: { column } }) {
function arrayEscape (line 235) | function arrayEscape(x) {
function arrayParserLoop (line 281) | function arrayParserLoop(s, x, parser, typarray) {
function createJsonTransform (line 338) | function createJsonTransform(fn) {
FILE: tests/bootstrap.js
function exec (line 21) | function exec(cmd, args) {
function execAsync (line 27) | async function execAsync(cmd, args) { // eslint-disable-line
FILE: tests/index.js
function yo (line 1611) | function yo() {
function wat (line 1615) | function wat() {
method valueOf (line 1700) | valueOf() { return first ? (first = false, 0.0001) : 1 }
FILE: tests/test.js
function test (line 18) | async function test(o, name, options, fn) {
function exit (line 62) | function exit() {
FILE: transpile.cf.js
function transpile (line 18) | function transpile(x) {
FILE: transpile.cjs
function transpile (line 32) | function transpile(x) {
FILE: transpile.deno.js
function transpile (line 49) | function transpile(x, name, folder) {
FILE: types/index.d.ts
type BaseOptions (line 26) | interface BaseOptions<T extends Record<string, postgres.PostgresType>> {
class NotAPromise (line 130) | class NotAPromise {
type UnwrapPromiseArray (line 150) | type UnwrapPromiseArray<T> = T extends any[] ? {
type Keys (line 154) | type Keys = string
type SerializableObject (line 156) | type SerializableObject<T, K extends readonly any[], TT> =
type First (line 160) | type First<T, K extends readonly any[], TT> =
type Rest (line 176) | type Rest<T> =
type Return (line 193) | type Return<T, K extends readonly any[]> =
class PostgresError (line 201) | class PostgresError extends Error {
type PostgresType (line 319) | interface PostgresType<T = any> {
type ConnectionParameters (line 326) | interface ConnectionParameters {
type Options (line 346) | interface Options<T extends Record<string, postgres.PostgresType>> exten...
type ParsedOptions (line 378) | interface ParsedOptions<T extends Record<string, unknown> = {}> extends ...
type Transform (line 391) | interface Transform {
type Notice (line 414) | interface Notice {
type Parameter (line 418) | interface Parameter<T = SerializableParameter> extends NotAPromise {
type ArrayParameter (line 433) | interface ArrayParameter<T extends readonly any[] = readonly any[]> exte...
type ConnectionError (line 437) | interface ConnectionError extends globalThis.Error {
type NotSupportedError (line 448) | interface NotSupportedError extends globalThis.Error {
type GenericError (line 453) | interface GenericError extends globalThis.Error {
type AuthNotImplementedError (line 464) | interface AuthNotImplementedError extends globalThis.Error {
type Error (line 470) | type Error = never
type ColumnInfo (line 477) | interface ColumnInfo {
type RelationInfo (line 485) | interface RelationInfo {
type ReplicationEvent (line 492) | type ReplicationEvent =
type SubscriptionHandle (line 497) | interface SubscriptionHandle {
type LargeObject (line 501) | interface LargeObject {
type EscapableArray (line 521) | type EscapableArray = (string | number)[]
type Serializable (line 523) | type Serializable = never
type SerializableParameter (line 531) | type SerializableParameter<T = never> = never
type JSONValue (line 539) | type JSONValue = // using a dedicated type to detect symbols, bigints, a...
type Row (line 554) | interface Row {
type MaybeRow (line 558) | type MaybeRow = Row | undefined;
type Column (line 560) | interface Column<T extends string> {
type ColumnList (line 568) | type ColumnList<T> = (T extends string ? Column<T> : never)[];
type State (line 570) | interface State {
type Statement (line 576) | interface Statement {
type ResultMeta (line 586) | interface ResultMeta<T extends number | null> {
type ResultQueryMeta (line 593) | interface ResultQueryMeta<T extends number | null, U> extends ResultMeta...
type ExecutionResult (line 597) | type ExecutionResult<T> = [] & ResultQueryMeta<number, keyof NonNullable...
type ValuesRowList (line 598) | type ValuesRowList<T extends readonly any[]> = T[number][keyof T[number]...
type RawRowList (line 599) | type RawRowList<T extends readonly any[]> = Buffer[][] & Iterable<Buffer...
type RowList (line 600) | type RowList<T extends readonly any[]> = T & Iterable<NonNullable<T[numb...
type PendingQueryModifiers (line 602) | interface PendingQueryModifiers<TRow extends readonly any[]> {
type PendingDescribeQuery (line 622) | interface PendingDescribeQuery extends Promise<Statement> {
type PendingValuesQuery (line 625) | interface PendingValuesQuery<TRow extends readonly MaybeRow[]> extends P...
type PendingRawQuery (line 629) | interface PendingRawQuery<TRow extends readonly MaybeRow[]> extends Prom...
type PendingQuery (line 632) | interface PendingQuery<TRow extends readonly MaybeRow[]> extends Promise...
type PendingRequest (line 638) | interface PendingRequest extends Promise<[] & ResultMeta<null>> { }
type ListenRequest (line 640) | interface ListenRequest extends Promise<ListenMeta> { }
type ListenMeta (line 641) | interface ListenMeta extends ResultMeta<null> {
type Helper (line 645) | interface Helper<T, U extends readonly any[] = T[]> extends NotAPromise {
type Fragment (line 650) | type Fragment = PendingQuery<any>
type ParameterOrJSON (line 652) | type ParameterOrJSON<T> =
type ParameterOrFragment (line 656) | type ParameterOrFragment<T> =
type Sql (line 661) | interface Sql<TTypes extends Record<string, unknown> = {}> {
type UnsafeQueryOptions (line 710) | interface UnsafeQueryOptions {
type TransactionSql (line 718) | interface TransactionSql<TTypes extends Record<string, unknown> = {}> ex...
type ReservedSql (line 738) | interface ReservedSql<TTypes extends Record<string, unknown> = {}> exten...
Condensed preview — 81 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (724K chars).
[
{
"path": ".eslintrc.json",
"chars": 6027,
"preview": "{\n \"root\": true,\n \"env\": {\n \"es2020\": true,\n \"node\": true\n },\n \"parserOptions\": {\n \"ecmaVersion\": 2020,\n "
},
{
"path": ".github/workflows/test.yml",
"chars": 2614,
"preview": "name: test\n\non: [push, pull_request]\n\njobs:\n test:\n name: Node v${{ matrix.node }} on PostgreSQL v${{ matrix.postgre"
},
{
"path": "CHANGELOG.md",
"chars": 6470,
"preview": "# Changelog\n\n## v3.2.4 - 25 May 2022\n- Allow setting keep_alive: false bee62f3\n- Fix support for null in arrays - fixes"
},
{
"path": "README.md",
"chars": 50564,
"preview": "<img align=\"left\" width=\"440\" height=\"180\" alt=\"Fastest full PostgreSQL nodejs client\" src=\"https://raw.githubuserconten"
},
{
"path": "UNLICENSE",
"chars": 1212,
"preview": "This is free and unencumbered software released into the public domain.\n\nAnyone is free to copy, modify, publish, use, c"
},
{
"path": "cf/polyfills.js",
"chars": 5484,
"preview": "import { EventEmitter } from 'node:events'\nimport { Buffer } from 'node:buffer'\n\nconst Crypto = globalThis.crypto\n\nlet i"
},
{
"path": "cf/src/bytes.js",
"chars": 1399,
"preview": "import { Buffer } from 'node:buffer'\nconst size = 256\nlet buffer = Buffer.allocUnsafe(size)\n\nconst messages = 'BCcDdEFfH"
},
{
"path": "cf/src/connection.js",
"chars": 28845,
"preview": "import { Buffer } from 'node:buffer'\nimport { setImmediate, clearImmediate } from '../polyfills.js'\nimport { net } from "
},
{
"path": "cf/src/errors.js",
"chars": 1155,
"preview": "export class PostgresError extends Error {\n constructor(x) {\n super(x.message)\n this.name = this.constructor.name"
},
{
"path": "cf/src/index.js",
"chars": 15504,
"preview": "import { process } from '../polyfills.js'\nimport { os } from '../polyfills.js'\nimport { fs } from '../polyfills.js'\n\nimp"
},
{
"path": "cf/src/large.js",
"chars": 2138,
"preview": "import Stream from 'node:stream'\n\nexport default function largeObject(sql, oid, mode = 0x00020000 | 0x00040000) {\n retu"
},
{
"path": "cf/src/query.js",
"chars": 3600,
"preview": "const originCache = new Map()\n , originStackCache = new Map()\n , originError = Symbol('OriginError')\n\nexport const"
},
{
"path": "cf/src/queue.js",
"chars": 541,
"preview": "export default Queue\n\nfunction Queue(initial = []) {\n let xs = initial.slice()\n let index = 0\n\n return {\n get leng"
},
{
"path": "cf/src/result.js",
"chars": 416,
"preview": "export default class Result extends Array {\n constructor() {\n super()\n Object.defineProperties(this, {\n coun"
},
{
"path": "cf/src/subscribe.js",
"chars": 7663,
"preview": "import { Buffer } from 'node:buffer'\nconst noop = () => { /* noop */ }\n\nexport default function Subscribe(postgres, opti"
},
{
"path": "cf/src/types.js",
"chars": 10972,
"preview": "import { Buffer } from 'node:buffer'\nimport { Query } from './query.js'\nimport { Errors } from './errors.js'\n\nexport con"
},
{
"path": "cf/test.js",
"chars": 656,
"preview": "// Add your database url and run this file with the below two commands to test pages and workers\n// npx wrangler@latest "
},
{
"path": "cjs/package.json",
"chars": 19,
"preview": "{\"type\":\"commonjs\"}"
},
{
"path": "cjs/src/bytes.js",
"chars": 1364,
"preview": "const size = 256\nlet buffer = Buffer.allocUnsafe(size)\n\nconst messages = 'BCcDdEFfHPpQSX'.split('').reduce((acc, x) => {"
},
{
"path": "cjs/src/connection.js",
"chars": 28747,
"preview": "const net = require('net')\nconst tls = require('tls')\nconst crypto = require('crypto')\nconst Stream = require('stream')\n"
},
{
"path": "cjs/src/errors.js",
"chars": 1218,
"preview": "const PostgresError = module.exports.PostgresError = class PostgresError extends Error {\n constructor(x) {\n super(x."
},
{
"path": "cjs/src/index.js",
"chars": 15475,
"preview": "const os = require('os')\nconst fs = require('fs')\n\nconst {\n mergeUserTypes,\n inferType,\n Parameter,\n Identifier,\n B"
},
{
"path": "cjs/src/large.js",
"chars": 2152,
"preview": "const Stream = require('stream')\n\nmodule.exports = largeObject;function largeObject(sql, oid, mode = 0x00020000 | 0x0004"
},
{
"path": "cjs/src/query.js",
"chars": 3646,
"preview": "const originCache = new Map()\n , originStackCache = new Map()\n , originError = Symbol('OriginError')\n\nconst CLOSE "
},
{
"path": "cjs/src/queue.js",
"chars": 543,
"preview": "module.exports = Queue\n\nfunction Queue(initial = []) {\n let xs = initial.slice()\n let index = 0\n\n return {\n get le"
},
{
"path": "cjs/src/result.js",
"chars": 418,
"preview": "module.exports = class Result extends Array {\n constructor() {\n super()\n Object.defineProperties(this, {\n co"
},
{
"path": "cjs/src/subscribe.js",
"chars": 7638,
"preview": "const noop = () => { /* noop */ }\n\nmodule.exports = Subscribe;function Subscribe(postgres, options) {\n const subscriber"
},
{
"path": "cjs/src/types.js",
"chars": 11476,
"preview": "const { Query } = require('./query.js')\nconst { Errors } = require('./errors.js')\n\nconst types = module.exports.types = "
},
{
"path": "cjs/tests/bootstrap.js",
"chars": 1666,
"preview": "const { spawnSync } = require('child_process')\n\nexec('dropdb', ['postgres_js_test'])\n\nexec('psql', ['-c', 'alter system "
},
{
"path": "cjs/tests/copy.csv",
"chars": 12,
"preview": "1\t2\t3\n4\t5\t6\n"
},
{
"path": "cjs/tests/index.js",
"chars": 68455,
"preview": "const { exec } = require('./bootstrap.js')\n\nconst { t, nt, ot } = require('./test.js') // eslint-disable-line\nconst net "
},
{
"path": "cjs/tests/pg_hba.conf",
"chars": 376,
"preview": "local all all trust\nhost all postgres "
},
{
"path": "cjs/tests/select-param.sql",
"chars": 15,
"preview": "select $1 as x\n"
},
{
"path": "cjs/tests/select.sql",
"chars": 14,
"preview": "select 1 as x\n"
},
{
"path": "cjs/tests/test.js",
"chars": 2325,
"preview": "/* eslint no-console: 0 */\n\nconst util = require('util')\n\nlet done = 0\nlet only = false\nlet ignored = 0\nlet failed = fal"
},
{
"path": "deno/README.md",
"chars": 50541,
"preview": "<img align=\"left\" width=\"440\" height=\"180\" alt=\"Fastest full PostgreSQL nodejs client\" src=\"https://raw.githubuserconten"
},
{
"path": "deno/mod.js",
"chars": 77,
"preview": "// @deno-types=\"./types/index.d.ts\"\nexport { default } from './src/index.js'\n"
},
{
"path": "deno/package.json",
"chars": 19,
"preview": "{\"type\":\"commonjs\"}"
},
{
"path": "deno/polyfills.js",
"chars": 4509,
"preview": "/* global Deno */\n\nimport { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'\nimport { isIP } from 'https://d"
},
{
"path": "deno/src/bytes.js",
"chars": 1432,
"preview": "import { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'\nconst size = 256\nlet buffer = Buffer.allocUnsafe(s"
},
{
"path": "deno/src/connection.js",
"chars": 28946,
"preview": "import { HmacSha256 } from 'https://deno.land/std@0.132.0/hash/sha256.ts'\nimport { Buffer } from 'https://deno.land/std@"
},
{
"path": "deno/src/errors.js",
"chars": 1155,
"preview": "export class PostgresError extends Error {\n constructor(x) {\n super(x.message)\n this.name = this.constructor.name"
},
{
"path": "deno/src/index.js",
"chars": 15572,
"preview": "import process from 'https://deno.land/std@0.132.0/node/process.ts'\nimport os from 'https://deno.land/std@0.132.0/node/o"
},
{
"path": "deno/src/large.js",
"chars": 2171,
"preview": "import Stream from 'https://deno.land/std@0.132.0/node/stream.ts'\n\nexport default function largeObject(sql, oid, mode = "
},
{
"path": "deno/src/query.js",
"chars": 3600,
"preview": "const originCache = new Map()\n , originStackCache = new Map()\n , originError = Symbol('OriginError')\n\nexport const"
},
{
"path": "deno/src/queue.js",
"chars": 541,
"preview": "export default Queue\n\nfunction Queue(initial = []) {\n let xs = initial.slice()\n let index = 0\n\n return {\n get leng"
},
{
"path": "deno/src/result.js",
"chars": 416,
"preview": "export default class Result extends Array {\n constructor() {\n super()\n Object.defineProperties(this, {\n coun"
},
{
"path": "deno/src/subscribe.js",
"chars": 7696,
"preview": "import { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'\nconst noop = () => { /* noop */ }\n\nexport default "
},
{
"path": "deno/src/types.js",
"chars": 11005,
"preview": "import { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'\nimport { Query } from './query.js'\nimport { Errors"
},
{
"path": "deno/tests/bootstrap.js",
"chars": 1762,
"preview": "import { spawn } from 'https://deno.land/std@0.132.0/node/child_process.ts'\n\nawait exec('dropdb', ['postgres_js_test'])\n"
},
{
"path": "deno/tests/copy.csv",
"chars": 12,
"preview": "1\t2\t3\n4\t5\t6\n"
},
{
"path": "deno/tests/index.js",
"chars": 68722,
"preview": "import { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'\nimport process from 'https://deno.land/std@0.132.0"
},
{
"path": "deno/tests/pg_hba.conf",
"chars": 376,
"preview": "local all all trust\nhost all postgres "
},
{
"path": "deno/tests/select-param.sql",
"chars": 15,
"preview": "select $1 as x\n"
},
{
"path": "deno/tests/select.sql",
"chars": 14,
"preview": "select 1 as x\n"
},
{
"path": "deno/tests/test.js",
"chars": 2388,
"preview": "import process from 'https://deno.land/std@0.132.0/node/process.ts'\n/* eslint no-console: 0 */\n\nimport util from 'https:"
},
{
"path": "deno/types/index.d.ts",
"chars": 25063,
"preview": "import { Buffer } from 'https://deno.land/std@0.132.0/node/buffer.ts'\nimport process from 'https://deno.land/std@0.132.0"
},
{
"path": "package.json",
"chars": 1814,
"preview": "{\n \"name\": \"postgres\",\n \"version\": \"3.4.8\",\n \"description\": \"Fastest full featured PostgreSQL client for Node.js\",\n "
},
{
"path": "src/bytes.js",
"chars": 1362,
"preview": "const size = 256\nlet buffer = Buffer.allocUnsafe(size)\n\nconst messages = 'BCcDdEFfHPpQSX'.split('').reduce((acc, x) => {"
},
{
"path": "src/connection.js",
"chars": 28690,
"preview": "import net from 'net'\nimport tls from 'tls'\nimport crypto from 'crypto'\nimport Stream from 'stream'\nimport { performance"
},
{
"path": "src/errors.js",
"chars": 1155,
"preview": "export class PostgresError extends Error {\n constructor(x) {\n super(x.message)\n this.name = this.constructor.name"
},
{
"path": "src/index.js",
"chars": 15428,
"preview": "import os from 'os'\nimport fs from 'fs'\n\nimport {\n mergeUserTypes,\n inferType,\n Parameter,\n Identifier,\n Builder,\n "
},
{
"path": "src/large.js",
"chars": 2133,
"preview": "import Stream from 'stream'\n\nexport default function largeObject(sql, oid, mode = 0x00020000 | 0x00040000) {\n return ne"
},
{
"path": "src/query.js",
"chars": 3600,
"preview": "const originCache = new Map()\n , originStackCache = new Map()\n , originError = Symbol('OriginError')\n\nexport const"
},
{
"path": "src/queue.js",
"chars": 541,
"preview": "export default Queue\n\nfunction Queue(initial = []) {\n let xs = initial.slice()\n let index = 0\n\n return {\n get leng"
},
{
"path": "src/result.js",
"chars": 416,
"preview": "export default class Result extends Array {\n constructor() {\n super()\n Object.defineProperties(this, {\n coun"
},
{
"path": "src/subscribe.js",
"chars": 7626,
"preview": "const noop = () => { /* noop */ }\n\nexport default function Subscribe(postgres, options) {\n const subscribers = new Map("
},
{
"path": "src/types.js",
"chars": 10935,
"preview": "import { Query } from './query.js'\nimport { Errors } from './errors.js'\n\nexport const types = {\n string: {\n to: 25,\n"
},
{
"path": "tests/bootstrap.js",
"chars": 1641,
"preview": "import { spawnSync } from 'child_process'\n\nexec('dropdb', ['postgres_js_test'])\n\nexec('psql', ['-c', 'alter system set s"
},
{
"path": "tests/copy.csv",
"chars": 12,
"preview": "1\t2\t3\n4\t5\t6\n"
},
{
"path": "tests/index.js",
"chars": 68418,
"preview": "import { exec } from './bootstrap.js'\n\nimport { t, nt, ot } from './test.js' // eslint-disable-line\nimport net from 'net"
},
{
"path": "tests/pg_hba.conf",
"chars": 376,
"preview": "local all all trust\nhost all postgres "
},
{
"path": "tests/select-param.sql",
"chars": 15,
"preview": "select $1 as x\n"
},
{
"path": "tests/select.sql",
"chars": 14,
"preview": "select 1 as x\n"
},
{
"path": "tests/test.js",
"chars": 2282,
"preview": "/* eslint no-console: 0 */\n\nimport util from 'util'\n\nlet done = 0\nlet only = false\nlet ignored = 0\nlet failed = false\nle"
},
{
"path": "transpile.cf.js",
"chars": 1418,
"preview": "import fs from 'fs'\nimport path from 'path'\n\nconst empty = x => fs.readdirSync(x).forEach(f => fs.unlinkSync(path.join(x"
},
{
"path": "transpile.cjs",
"chars": 1690,
"preview": "const fs = require('fs')\n , path = require('path')\n\nconst empty = x => fs.readdirSync(x).forEach(f => fs.unlinkSync(p"
},
{
"path": "transpile.deno.js",
"chars": 3072,
"preview": "import fs from 'fs'\nimport path from 'path'\n\nconst std = 'https://deno.land/std@0.132.0/'\n , empty = x => fs.readdirS"
},
{
"path": "types/index.d.ts",
"chars": 24892,
"preview": "import { Readable, Writable } from 'node:stream'\n\n/**\n * Establish a connection to a PostgreSQL server.\n * @param option"
},
{
"path": "types/package.json",
"chars": 56,
"preview": "{\n \"devDependencies\": {\n \"@types/node\": \"^16\"\n }\n}\n"
},
{
"path": "types/tsconfig.json",
"chars": 215,
"preview": "{\n \"compilerOptions\": {\n \"lib\": [\n \"ES2015\"\n ],\n \"types\": [\n \"node\"\n ],\n \"esModuleInterop\": tr"
}
]
About this extraction
This page contains the full source code of the porsager/postgres GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 81 files (678.4 KB), approximately 189.5k tokens, and a symbol index with 505 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.