Skip to content

Commit 6f20f3f

Browse files
committed
build
1 parent 3e28f3a commit 6f20f3f

File tree

8 files changed

+53
-20
lines changed

8 files changed

+53
-20
lines changed

cjs/src/connection.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
429429
lifeTimer.cancel()
430430
connectTimer.cancel()
431431

432-
if (socket.encrypted) {
433-
socket.removeAllListeners()
434-
socket = null
435-
}
432+
socket.removeAllListeners()
433+
socket = null
436434

437435
if (initial)
438436
return reconnect()
@@ -790,7 +788,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
790788
const error = Errors.postgres(parseError(x))
791789
query && query.retried
792790
? errored(query.retried)
793-
: query && retryRoutines.has(error.routine)
791+
: query && query.prepare && retryRoutines.has(error.routine)
794792
? retry(query, error)
795793
: errored(error)
796794
}

cjs/src/subscribe.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module.exports = Subscribe;function Subscribe(postgres, options) {
4747

4848
return subscribe
4949

50-
async function subscribe(event, fn, onsubscribe = noop) {
50+
async function subscribe(event, fn, onsubscribe = noop, onerror = noop) {
5151
event = parseEvent(event)
5252

5353
if (!connection)
@@ -66,6 +66,7 @@ module.exports = Subscribe;function Subscribe(postgres, options) {
6666
return connection.then(x => {
6767
connected(x)
6868
onsubscribe()
69+
stream && stream.on('error', onerror)
6970
return { unsubscribe, state, sql }
7071
})
7172
}
@@ -109,8 +110,10 @@ module.exports = Subscribe;function Subscribe(postgres, options) {
109110
function data(x) {
110111
if (x[0] === 0x77)
111112
parse(x.subarray(25), state, sql.options.parsers, handle, options.transform)
112-
else if (x[0] === 0x6b && x[17])
113+
else if (x[0] === 0x6b && x[17]) {
114+
state.lsn = x.subarray(1, 9)
113115
pong()
116+
}
114117
}
115118

116119
function handle(a, b) {

cjs/tests/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,21 @@ t('Recreate prepared statements on RevalidateCachedQuery error', async() => {
17891789
]
17901790
})
17911791

1792+
t('Properly throws routing error on not prepared statements', async() => {
1793+
await sql`create table x (x text[])`
1794+
const { routine } = await sql.unsafe(`insert into x(x) values (('a', 'b'))`).catch(e => e)
1795+
1796+
return ['transformAssignedExpr', routine, await sql`drop table x`]
1797+
})
1798+
1799+
t('Properly throws routing error on not prepared statements in transaction', async() => {
1800+
const { routine } = await sql.begin(sql => [
1801+
sql`create table x (x text[])`,
1802+
sql`insert into x(x) values (('a', 'b'))`,
1803+
]).catch(e => e)
1804+
1805+
return ['transformAssignedExpr', routine]
1806+
})
17921807

17931808
t('Catches connection config errors', async() => {
17941809
const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' })

deno/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ const users = [
266266
]
267267

268268
await sql`
269-
update users set name = update_data.name, (age = update_data.age)::int
269+
update users set name = update_data.name, age = (update_data.age)::int
270270
from (values ${sql(users)}) as update_data (id, name, age)
271271
where users.id = (update_data.id)::int
272272
returning users.id, users.name, users.age
@@ -286,7 +286,7 @@ const users = await sql`
286286

287287
or
288288
```js
289-
const [{ a, b, c }] => await sql`
289+
const [{ a, b, c }] = await sql`
290290
select
291291
*
292292
from (values ${ sql(['a', 'b', 'c']) }) as x(a, b, c)
@@ -913,7 +913,7 @@ The `Result` Array returned from queries is a custom array allowing for easy des
913913

914914
### .count
915915

916-
The `count` property is the number of affected rows returned by the database. This is usefull for insert, update and delete operations to know the number of rows since .length will be 0 in these cases if not using `RETURNING ...`.
916+
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 ...`.
917917

918918
### .command
919919

@@ -1099,10 +1099,10 @@ export default async fetch(req: Request, env: Env, ctx: ExecutionContext) {
10991099
}
11001100
```
11011101

1102-
In `wrangler.toml` you will need to enable `node_compat` to allow Postgres.js to operate in the Workers environment:
1102+
In `wrangler.toml` you will need to enable the `nodejs_compat` compatibility flag to allow Postgres.js to operate in the Workers environment:
11031103

11041104
```toml
1105-
node_compat = true # required for database drivers to function
1105+
compatibility_flags = ["nodejs_compat"]
11061106
```
11071107

11081108
### Auto fetching of array types

deno/src/connection.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
432432
lifeTimer.cancel()
433433
connectTimer.cancel()
434434

435-
if (socket.encrypted) {
436-
socket.removeAllListeners()
437-
socket = null
438-
}
435+
socket.removeAllListeners()
436+
socket = null
439437

440438
if (initial)
441439
return reconnect()
@@ -793,7 +791,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
793791
const error = Errors.postgres(parseError(x))
794792
query && query.retried
795793
? errored(query.retried)
796-
: query && retryRoutines.has(error.routine)
794+
: query && query.prepare && retryRoutines.has(error.routine)
797795
? retry(query, error)
798796
: errored(error)
799797
}

deno/src/subscribe.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default function Subscribe(postgres, options) {
4848

4949
return subscribe
5050

51-
async function subscribe(event, fn, onsubscribe = noop) {
51+
async function subscribe(event, fn, onsubscribe = noop, onerror = noop) {
5252
event = parseEvent(event)
5353

5454
if (!connection)
@@ -67,6 +67,7 @@ export default function Subscribe(postgres, options) {
6767
return connection.then(x => {
6868
connected(x)
6969
onsubscribe()
70+
stream && stream.on('error', onerror)
7071
return { unsubscribe, state, sql }
7172
})
7273
}
@@ -110,8 +111,10 @@ export default function Subscribe(postgres, options) {
110111
function data(x) {
111112
if (x[0] === 0x77)
112113
parse(x.subarray(25), state, sql.options.parsers, handle, options.transform)
113-
else if (x[0] === 0x6b && x[17])
114+
else if (x[0] === 0x6b && x[17]) {
115+
state.lsn = x.subarray(1, 9)
114116
pong()
117+
}
115118
}
116119

117120
function handle(a, b) {

deno/tests/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,21 @@ t('Recreate prepared statements on RevalidateCachedQuery error', async() => {
17911791
]
17921792
})
17931793

1794+
t('Properly throws routing error on not prepared statements', async() => {
1795+
await sql`create table x (x text[])`
1796+
const { routine } = await sql.unsafe(`insert into x(x) values (('a', 'b'))`).catch(e => e)
1797+
1798+
return ['transformAssignedExpr', routine, await sql`drop table x`]
1799+
})
1800+
1801+
t('Properly throws routing error on not prepared statements in transaction', async() => {
1802+
const { routine } = await sql.begin(sql => [
1803+
sql`create table x (x text[])`,
1804+
sql`insert into x(x) values (('a', 'b'))`,
1805+
]).catch(e => e)
1806+
1807+
return ['transformAssignedExpr', routine]
1808+
})
17941809

17951810
t('Catches connection config errors', async() => {
17961811
const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' })

deno/types/index.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ declare namespace postgres {
601601
type RowList<T extends readonly any[]> = T & Iterable<NonNullable<T[number]>> & ResultQueryMeta<T['length'], keyof T[number]>;
602602

603603
interface PendingQueryModifiers<TRow extends readonly any[]> {
604+
simple(): this;
604605
readable(): Promise<Readable>;
605606
writable(): Promise<Writable>;
606607

@@ -692,7 +693,7 @@ declare namespace postgres {
692693
listen(channel: string, onnotify: (value: string) => void, onlisten?: (() => void) | undefined): ListenRequest;
693694
notify(channel: string, payload: string): PendingRequest;
694695

695-
subscribe(event: string, cb: (row: Row | null, info: ReplicationEvent) => void, onsubscribe?: (() => void) | undefined): Promise<SubscriptionHandle>;
696+
subscribe(event: string, cb: (row: Row | null, info: ReplicationEvent) => void, onsubscribe?: (() => void), onerror?: (() => any)): Promise<SubscriptionHandle>;
696697

697698
largeObject(oid?: number | undefined, /** @default 0x00020000 | 0x00040000 */ mode?: number | undefined): Promise<LargeObject>;
698699

0 commit comments

Comments
 (0)