Comparing version 2.0.0-beta.5 to 2.0.0-beta.6
@@ -14,2 +14,3 @@ const { errors } = require('./errors.js') | ||
oncomplete, | ||
onerror, | ||
parsers, | ||
@@ -20,4 +21,3 @@ onauth, | ||
onnotice, | ||
onnotify, | ||
error | ||
onnotify | ||
}) { | ||
@@ -131,5 +131,3 @@ let rows = 0 | ||
function ErrorResponse(x) { | ||
backend.query | ||
? (backend.error = errors.postgres(parseError(x))) | ||
: error(errors.postgres(parseError(x))) | ||
onerror(errors.postgres(parseError(x))) | ||
} | ||
@@ -165,3 +163,3 @@ | ||
const type = x.readInt32BE(5) | ||
type !== 0 && onauth(type, x, error) | ||
type !== 0 && onauth(type, x, onerror) | ||
} | ||
@@ -168,0 +166,0 @@ |
@@ -55,2 +55,3 @@ const net = require('net') | ||
oncomplete, | ||
onerror, | ||
transform, | ||
@@ -65,7 +66,7 @@ parsers, | ||
function onsuspended(x) { | ||
new Promise(r => r(backend.query.cursor( | ||
function onsuspended(x, done) { | ||
new Promise(r => r(x.length && backend.query.cursor( | ||
backend.query.cursor.rows === 1 ? x[0] : x | ||
))).then(x => { | ||
x === END | ||
x === END || done | ||
? socket.write(frontend.Close()) | ||
@@ -80,5 +81,13 @@ : socket.write(frontend.Execute(backend.query.cursor.rows)) | ||
function oncomplete() { | ||
backend.query.cursor && socket.write(frontend.Close()) | ||
backend.query.cursor && onsuspended(backend.query.result, true) | ||
} | ||
function onerror(x) { | ||
if (!backend.query) | ||
return error(x) | ||
backend.error = x | ||
backend.query.cursor && socket.write(frontend.Sync) | ||
} | ||
function onparse() { | ||
@@ -327,3 +336,3 @@ if (backend.query && backend.query.statement.sig) | ||
x.host = options.host[i++] | ||
) | ||
).setKeepAlive(true, 1000 * 60) | ||
@@ -330,0 +339,0 @@ if (!options.ssl) |
@@ -8,2 +8,3 @@ const crypto = require('crypto') | ||
const empty = Buffer.alloc(0) | ||
const Sync = bytes.S().end() | ||
const execute = Buffer.concat([ | ||
@@ -44,2 +45,3 @@ bytes.D().str('P').str(N).end(), | ||
Bind, | ||
Sync, | ||
Parse, | ||
@@ -46,0 +48,0 @@ Query, |
@@ -70,3 +70,3 @@ const fs = require('fs') | ||
function postgres(xs) { | ||
return query({}, getConnection(), xs, Array.from(arguments).slice(1)) | ||
return query({ prepare: options.prepare }, getConnection(), xs, Array.from(arguments).slice(1)) | ||
} | ||
@@ -171,3 +171,3 @@ | ||
query.origin = options.debug ? new Error().stack : cachedError(xs) | ||
query.dynamic = query.dynamic || options.no_prepare | ||
query.prepare = 'prepare' in query ? query.prepare : options.prepare | ||
if (!query.raw && (!Array.isArray(xs) || !Array.isArray(xs.raw))) | ||
@@ -246,7 +246,7 @@ return nested(xs, args) | ||
new Promise((resolve, reject) => { | ||
send(connection, { resolve, reject, raw: true, dynamic: true }, ` | ||
send(connection, { resolve, reject, raw: true, prepare: false }, ` | ||
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' and b.typcategory != 'C' | ||
where a.typcategory = 'A' | ||
group by b.oid, b.typarray | ||
@@ -290,4 +290,5 @@ order by b.oid | ||
function unsafe(xs, args) { | ||
return query({ raw: true, simple: !args, dynamic: true }, connection || getConnection(), xs, args || []) | ||
function unsafe(xs, args, queryOptions) { | ||
const prepare = queryOptions && queryOptions.prepare || false | ||
return query({ raw: true, simple: !args, prepare }, connection || getConnection(), xs, args || []) | ||
} | ||
@@ -339,2 +340,3 @@ | ||
query.cursor = fn | ||
query.simple = false | ||
return promise | ||
@@ -422,3 +424,3 @@ } | ||
return { | ||
sig: !query.dynamic && types + str, | ||
sig: query.prepare && types + str, | ||
str, | ||
@@ -442,3 +444,3 @@ args: xargs | ||
return { | ||
sig: !query.dynamic && !xargs.dynamic && types + str, | ||
sig: query.prepare && !xargs.dynamic && types + str, | ||
str: str.trim(), | ||
@@ -566,6 +568,6 @@ args: xargs | ||
types : o.types || {}, | ||
ssl : o.ssl || url.query.sslmode || url.query.ssl || false, | ||
ssl : o.ssl || parseSSL(url.query.sslmode || url.query.ssl) || false, | ||
idle_timeout : o.idle_timeout || url.query.idle_timeout || env.PGIDLE_TIMEOUT || warn(o.timeout), | ||
connect_timeout : o.connect_timeout || url.query.connect_timeout || env.PGCONNECT_TIMEOUT || 30, | ||
no_prepare : o.no_prepare, | ||
prepare : 'prepare' in o ? o.prepare : 'no_prepare' in o ? !o.no_prepare : true, | ||
onnotice : o.onnotice, | ||
@@ -582,2 +584,6 @@ onparameter : o.onparameter, | ||
function parseSSL(x) { | ||
return x !== 'disabled' && x !== 'false' && x | ||
} | ||
function parseUrl(url) { | ||
@@ -584,0 +590,0 @@ if (typeof url !== 'string') |
@@ -69,3 +69,3 @@ const char = module.exports.char = (acc, [k, v]) => (acc[k.charCodeAt(0)] = v, acc) | ||
module.exports.escape = function escape(str) { | ||
return '"' + str.replace(/"/g, '""') + '"' | ||
return '"' + str.replace(/"/g, '""').replace(/\./g, '"."') + '"' | ||
} | ||
@@ -72,0 +72,0 @@ |
{ | ||
"name": "postgres", | ||
"version": "2.0.0-beta.5", | ||
"version": "2.0.0-beta.6", | ||
"description": "Fastest full featured PostgreSQL client for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -115,3 +115,3 @@ <img align="left" width="440" height="140" alt="Fastest full PostgreSQL nodejs client" src="https://raw.githubusercontent.com/porsager/postgres/master/postgresjs.svg?sanitize=true" /> | ||
| `idle_timeout` | `PGIDLE_TIMEOUT` | | ||
' `connect_timeout` | `PGCONNECT_TIMEOUT` | | ||
| `connect_timeout` | `PGCONNECT_TIMEOUT` | | ||
@@ -604,4 +604,10 @@ ## Query ```sql` ` -> Promise``` | ||
Connections will by default not close until `.end()` is called, but often it is useful to have them close when there is no activity or if using Postgres.js in eg. Lamdas / Serverless environments. This can be done using the `idle_timeout` option to specify the amount of seconds to wait before automatically closing an idle connection. | ||
By default, connections will not close until `.end()` is called. However, it may be useful to have them close automatically when: | ||
- there is no activity for some period of time | ||
- if using Postgres.js in Lamdas / Serverless environments | ||
- if using Postgres.js with a database service that automatically closes the connection after some time (see [`ECONNRESET` issue](https://github.com/porsager/postgres/issues/179)) | ||
This can be done using the `idle_timeout` option to specify the amount of seconds to wait before automatically closing an idle connection. | ||
For example, to close idle connections after 2 seconds: | ||
@@ -608,0 +614,0 @@ |
@@ -37,4 +37,12 @@ /** | ||
types: PostgresTypeList<T>; | ||
/** Disable prepared mode */ | ||
/** | ||
* Disable prepared mode | ||
* @deprecated use "prepare" option instead | ||
*/ | ||
no_prepare: boolean; | ||
/** | ||
* Enables prepare mode. | ||
* @default true | ||
*/ | ||
prepare: boolean; | ||
/** Defaults to console.log */ | ||
@@ -376,3 +384,3 @@ onnotice: (notice: postgres.Notice) => void; | ||
}; | ||
unsafe<T extends any[] = Row[]>(query: string, parameters?: SerializableParameter[]): PendingQuery<AsRowList<T>>; | ||
unsafe<T extends any[] = Row[]>(query: string, parameters?: SerializableParameter[], queryOptions?: UnsafeQueryOptions): PendingQuery<AsRowList<T>>; | ||
} | ||
@@ -387,2 +395,10 @@ | ||
interface UnsafeQueryOptions { | ||
/** | ||
* When executes query as prepared statement. | ||
* @default false | ||
*/ | ||
prepare?: boolean; | ||
} | ||
export = postgres; |
82377
1923
709