Skip to content

Commit

Permalink
build
Browse files Browse the repository at this point in the history
  • Loading branch information
porsager committed Mar 20, 2024
1 parent 3e28f3a commit 6f20f3f
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 20 deletions.
8 changes: 3 additions & 5 deletions cjs/src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
lifeTimer.cancel()
connectTimer.cancel()

if (socket.encrypted) {
socket.removeAllListeners()
socket = null
}
socket.removeAllListeners()
socket = null

if (initial)
return reconnect()
Expand Down Expand Up @@ -790,7 +788,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
const error = Errors.postgres(parseError(x))
query && query.retried
? errored(query.retried)
: query && retryRoutines.has(error.routine)
: query && query.prepare && retryRoutines.has(error.routine)
? retry(query, error)
: errored(error)
}
Expand Down
7 changes: 5 additions & 2 deletions cjs/src/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = Subscribe;function Subscribe(postgres, options) {

return subscribe

async function subscribe(event, fn, onsubscribe = noop) {
async function subscribe(event, fn, onsubscribe = noop, onerror = noop) {
event = parseEvent(event)

if (!connection)
Expand All @@ -66,6 +66,7 @@ module.exports = Subscribe;function Subscribe(postgres, options) {
return connection.then(x => {
connected(x)
onsubscribe()
stream && stream.on('error', onerror)
return { unsubscribe, state, sql }
})
}
Expand Down Expand Up @@ -109,8 +110,10 @@ module.exports = Subscribe;function Subscribe(postgres, options) {
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])
else if (x[0] === 0x6b && x[17]) {
state.lsn = x.subarray(1, 9)
pong()
}
}

function handle(a, b) {
Expand Down
15 changes: 15 additions & 0 deletions cjs/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,21 @@ t('Recreate prepared statements on RevalidateCachedQuery error', async() => {
]
})

t('Properly throws routing error on not prepared statements', async() => {
await sql`create table x (x text[])`
const { routine } = await sql.unsafe(`insert into x(x) values (('a', 'b'))`).catch(e => e)

return ['transformAssignedExpr', routine, await sql`drop table x`]
})

t('Properly throws routing error on not prepared statements in transaction', async() => {
const { routine } = await sql.begin(sql => [
sql`create table x (x text[])`,
sql`insert into x(x) values (('a', 'b'))`,
]).catch(e => e)

return ['transformAssignedExpr', routine]
})

t('Catches connection config errors', async() => {
const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' })
Expand Down
10 changes: 5 additions & 5 deletions deno/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ const users = [
]

await sql`
update users set name = update_data.name, (age = update_data.age)::int
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
Expand All @@ -286,7 +286,7 @@ const users = await sql`

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

### .count

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 ...`.
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

Expand Down Expand Up @@ -1099,10 +1099,10 @@ export default async fetch(req: Request, env: Env, ctx: ExecutionContext) {
}
```

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

```toml
node_compat = true # required for database drivers to function
compatibility_flags = ["nodejs_compat"]
```

### Auto fetching of array types
Expand Down
8 changes: 3 additions & 5 deletions deno/src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
lifeTimer.cancel()
connectTimer.cancel()

if (socket.encrypted) {
socket.removeAllListeners()
socket = null
}
socket.removeAllListeners()
socket = null

if (initial)
return reconnect()
Expand Down Expand Up @@ -793,7 +791,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
const error = Errors.postgres(parseError(x))
query && query.retried
? errored(query.retried)
: query && retryRoutines.has(error.routine)
: query && query.prepare && retryRoutines.has(error.routine)
? retry(query, error)
: errored(error)
}
Expand Down
7 changes: 5 additions & 2 deletions deno/src/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function Subscribe(postgres, options) {

return subscribe

async function subscribe(event, fn, onsubscribe = noop) {
async function subscribe(event, fn, onsubscribe = noop, onerror = noop) {
event = parseEvent(event)

if (!connection)
Expand All @@ -67,6 +67,7 @@ export default function Subscribe(postgres, options) {
return connection.then(x => {
connected(x)
onsubscribe()
stream && stream.on('error', onerror)
return { unsubscribe, state, sql }
})
}
Expand Down Expand Up @@ -110,8 +111,10 @@ export default function Subscribe(postgres, options) {
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])
else if (x[0] === 0x6b && x[17]) {
state.lsn = x.subarray(1, 9)
pong()
}
}

function handle(a, b) {
Expand Down
15 changes: 15 additions & 0 deletions deno/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,21 @@ t('Recreate prepared statements on RevalidateCachedQuery error', async() => {
]
})

t('Properly throws routing error on not prepared statements', async() => {
await sql`create table x (x text[])`
const { routine } = await sql.unsafe(`insert into x(x) values (('a', 'b'))`).catch(e => e)

return ['transformAssignedExpr', routine, await sql`drop table x`]
})

t('Properly throws routing error on not prepared statements in transaction', async() => {
const { routine } = await sql.begin(sql => [
sql`create table x (x text[])`,
sql`insert into x(x) values (('a', 'b'))`,
]).catch(e => e)

return ['transformAssignedExpr', routine]
})

t('Catches connection config errors', async() => {
const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' })
Expand Down
3 changes: 2 additions & 1 deletion deno/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ declare namespace postgres {
type RowList<T extends readonly any[]> = T & Iterable<NonNullable<T[number]>> & ResultQueryMeta<T['length'], keyof T[number]>;

interface PendingQueryModifiers<TRow extends readonly any[]> {
simple(): this;
readable(): Promise<Readable>;
writable(): Promise<Writable>;

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

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

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

Expand Down

0 comments on commit 6f20f3f

Please sign in to comment.