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
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
139
141
140
-
#### Object to row `row(Object, ...columns)`
141
-
Sometimes the number of columns can be quite large, so this is shorter.
142
142
143
143
```js
144
144
145
-
constdata= {
146
-
user: {
147
-
name:'Murray'
148
-
}
145
+
constuser= {
146
+
name:'Murray',
147
+
age:68
149
148
}
150
149
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)
156
153
}
157
154
`
158
155
159
156
```
160
157
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:
164
159
165
-
constdata= {
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
+
```
174
163
175
-
constusers=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()`.
0 commit comments