You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
446
+
447
+
```js
448
+
449
+
sql.unsafe('select '+ danger +' from users where id = '+ dragons)
450
+
451
+
```
452
+
</details>
453
+
421
454
## Transactions
422
455
423
456
#### BEGIN / COMMIT `awaitsql.begin([options =''], fn) ->fn()`
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.
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.
511
-
512
-
```js
513
-
514
-
sql.unsafe('select '+ danger +' from users where id = '+ dragons)
515
-
516
-
```
517
-
</details>
518
-
519
-
## Custom Types
520
-
521
-
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_types` table.)_
522
-
523
-
Adding Query helpers is the cleanest approach which can be done like this:
524
-
525
-
```js
526
-
constsql=postgres({
527
-
types: {
528
-
rect: {
529
-
// The pg_types oid to pass to the db along with the serialized value.
530
-
to :1337,
531
-
532
-
// An array of pg_types oids to handle when parsing values coming from the db.
533
-
from : [1337],
534
-
535
-
//Function that transform values before sending them to the db.
536
-
serialize: ({ x, y, width, height }) => [x, y, width, height],
537
-
538
-
// Function that transforms values coming from the db.
539
-
parse: ([x, y, width, height]) => { x, y, width, height }
540
-
}
541
-
}
542
-
})
543
-
544
-
// Now you can use sql.typed.rect() as specified above
When you call `.listen`, a dedicated connection will be created to ensure that you receive notifications in real-time. This connection will be used for any further calls to `.listen`.
Postgres.js implements the logical replication protocol of PostgreSQL to support subscription to real-time updates of `insert`, `update` and `delete` operations.
587
561
588
562
> **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.
**`operation`** is one of ```*| insert | update |delete``` and defaults to `*`
613
587
@@ -617,7 +591,7 @@ You can subscribe to specific operations, tables, or even rows with primary keys
617
591
618
592
**`primary_key`** can be used to only subscribe to specific rows
619
593
620
-
#### Examples
594
+
### Examples
621
595
622
596
```js
623
597
sql.subscribe('*', () =>/* everything */ )
@@ -627,25 +601,6 @@ sql.subscribe('delete:users', () => /* all deletes on the public.users table
627
601
sql.subscribe('update:users=1', () =>/* all updates on the users row with a primary key = 1 */ )
628
602
```
629
603
630
-
## Teardown / Cleanup
631
-
632
-
To ensure proper teardown and cleanup on server restarts use `awaitsql.end()` before `process.exit()`.
633
-
634
-
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.
635
-
636
-
#### Sample shutdown using [Prexit](https://github.com/porsager/prexit)
637
-
638
-
```js
639
-
640
-
importprexitfrom'prexit'
641
-
642
-
prexit(async () => {
643
-
awaitsql.end({ timeout:5 })
644
-
awaitnewPromise(r=>server.close(r))
645
-
})
646
-
647
-
```
648
-
649
604
## Numbers, bigint, numeric
650
605
651
606
`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`.
@@ -665,7 +620,7 @@ const sql = postgres({
665
620
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).
666
621
667
622
668
-
## Connection options
623
+
## Connection details
669
624
670
625
### All Postgres options
671
626
@@ -792,6 +747,66 @@ const sql = postgres()
792
747
793
748
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 `no_prepare` option. For instance — this is useful when [using PGBouncer in `transaction mode`](https://github.com/porsager/postgres/issues/93).
794
749
750
+
## Custom Types
751
+
752
+
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_types` table.)_
753
+
754
+
Adding Query helpers is the cleanest approach which can be done like this:
755
+
756
+
```js
757
+
constsql=postgres({
758
+
types: {
759
+
rect: {
760
+
// The pg_types oid to pass to the db along with the serialized value.
761
+
to :1337,
762
+
763
+
// An array of pg_types oids to handle when parsing values coming from the db.
764
+
from : [1337],
765
+
766
+
//Function that transform values before sending them to the db.
767
+
serialize: ({ x, y, width, height }) => [x, y, width, height],
768
+
769
+
// Function that transforms values coming from the db.
770
+
parse: ([x, y, width, height]) => { x, y, width, height }
771
+
}
772
+
}
773
+
})
774
+
775
+
// Now you can use sql.typed.rect() as specified above
To ensure proper teardown and cleanup on server restarts use `awaitsql.end()` before `process.exit()`.
794
+
795
+
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.
796
+
797
+
#### Sample shutdown using [Prexit](https://github.com/porsager/prexit)
798
+
799
+
```js
800
+
801
+
importprexitfrom'prexit'
802
+
803
+
prexit(async () => {
804
+
awaitsql.end({ timeout:5 })
805
+
awaitnewPromise(r=>server.close(r))
806
+
})
807
+
808
+
```
809
+
795
810
## Error handling
796
811
797
812
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.
0 commit comments