Skip to content

Commit f5a08bd

Browse files
committed
Update readme
1 parent 0574fa8 commit f5a08bd

File tree

1 file changed

+44
-38
lines changed

1 file changed

+44
-38
lines changed

README.md

+44-38
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ You can use either a postgres:// url connection string or the options to define
4242
```js
4343

4444
const sql = postgres('postgres://user:pass@host:port/database', {
45-
host : // or hostname
46-
port : // Postgres server port
47-
path : // unix socket path (usually /tmp)
48-
database : // Database to connect to
49-
username : // or username
50-
password : // or password
45+
host : '', // or hostname
46+
port : 5432, // Postgres server port
47+
path : '', // unix socket path (usually /tmp)
48+
database : '', // Database to connect to
49+
username : '', // or username
50+
password : '', // or password
5151
ssl : false, // True, or an object with options to tls.connect
5252
max : 10, // Max number of connections
5353
timeout : 0, // Idle connection timeout in seconds
5454
types : [], // Custom types, see section below
55-
onconnect : null, // Runs before any queries on each connect
55+
onconnect : null, // Runs on each single connection
5656
onnotice : console.log // Any NOTICE the db sends will be posted here
5757
})
5858

@@ -133,52 +133,51 @@ sql.notify('news', JSON.stringify({ no: 'this', is: 'news' }))
133133

134134
```
135135

136-
## Query Helpers
136+
## Dynamic query helpers `sql()`
137137

138-
Due to the nature of sql and Postgres types various helpers are available to simplify queries.
138+
Postgres.js has a safe, ergonomic way to aid you in writing queries. This makes it easier to write dynamic inserts, selects, updates and where queries.
139+
140+
#### Insert
139141

140-
#### Object to row `row(Object, ...columns)`
141-
Sometimes the number of columns can be quite large, so this is shorter.
142142

143143
```js
144144

145-
const data = {
146-
user: {
147-
name: 'Murray'
148-
}
145+
const user = {
146+
name: 'Murray',
147+
age: 68
149148
}
150149

151-
const [user] = sql`
152-
insert into users (
153-
name, age
154-
) values ${
155-
sql.row(data.user, 'name', 'age')
150+
sql`
151+
insert into users ${
152+
sql(data)
156153
}
157154
`
158155

159156
```
160157

161-
#### Array of objects to rows `sql.rows(Array, ...columns)`
162-
If you need to insert multiple rows at the same time it's much faster to do it with a single `insert`. This is easily done using `sql.rows`
163-
```js
158+
Is translated into a safe query like this:
164159

165-
const data = {
166-
users: [{
167-
name: 'Murray',
168-
age: 68
169-
}, {
170-
name: 'Walter',
171-
age: 78
172-
}]
173-
}
160+
```sql
161+
insert into users (name, age) values ($1, $2)
162+
```
174163

175-
const users = sql`
176-
insert into users (
177-
name, age
178-
) values ${
179-
sql.rows(data.users, 'name', 'age')
164+
#### Multiple inserts in one query
165+
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()`.
166+
```js
167+
168+
const users = [{
169+
name: 'Murray',
170+
age: 68,
171+
garbage: 'ignore'
172+
}, {
173+
name: 'Walter',
174+
age: 78
175+
}]
176+
177+
sql`
178+
insert into users ${
179+
sql(users, 'name', 'age')
180180
}
181-
returning *
182181
`
183182

184183
```
@@ -225,6 +224,13 @@ const [{ json }] = await sql`
225224
// json = { hello: 'postgres' }
226225
```
227226

227+
## Unsafe
228+
229+
230+
231+
## File
232+
233+
228234

229235
## Transactions
230236

0 commit comments

Comments
 (0)