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
Copy file name to clipboardExpand all lines: docs/pg-typed.md
+79-11Lines changed: 79 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,7 +61,7 @@ export async function initialize() {
61
61
favorite_color: `blue`,
62
62
});
63
63
64
-
awaitdb.tx(asyncdb=> {
64
+
awaitdb.tx(async(db)=> {
65
65
// These 2 queries are run in the same transaction
66
66
awaitusers(db).find().all();
67
67
awaitusers(db).insert({
@@ -143,6 +143,28 @@ export async function setFavoriteColor(
143
143
}
144
144
```
145
145
146
+
By default, all the columns will be updated when a conflict is encountered. You can alternatively specify exactly which columns to update. For example:
This is similar to `insertOrUpdate`, except that when a conflict is encountered, it simply ignores the record. Only the records that were successfully inserted are returned.
@@ -299,6 +321,27 @@ import db, {users} from './database';
299
321
users(db).tableName; // 'users'
300
322
```
301
323
324
+
### conditionToSql(whereValues, tableAlias)
325
+
326
+
If you want to build parts of the query yourself, you can still use pg-typed to help you construct the condition in your where clause. This can be useful for join statements. In the following example the condition is very simple, so it might have been easier to simply write that SQL by hand, but in more complex conditions the type safety can still be helpful.
INNER JOIN blog_posts AS b ON u.id=b.created_by_id
340
+
WHERE ${users(db).conditionToSql({active: true}, `u`)}
341
+
`);
342
+
}
343
+
```
344
+
302
345
### bulkFind(options)
303
346
304
347
This is like the regular `.find(condition)` API, but it lets you specify multiple distinct conditions that are efficiently or'ed together. Once you've started a query using `bulkFind` you can call `.orderByAsc`/`.orderByDesc`/`.select`/etc. just like you could if you started a query with a call to `find`. You can find more details on how this API works in [@databases/pg-bulk](pg-bulk.md).
@@ -320,11 +363,11 @@ async function getPosts() {
320
363
321
364
### bulkInsert(options)
322
365
323
-
To insert thousands of records at a time, you can use the bulk insert API. This requires you to specify any optional fields that you want to pass in. Any required (i.e. `NOT NULL` and no default value) fields are automatically expected. You can find more details on how this API works in [@databases/pg-bulk](pg-bulk.md). `bulkInsert` also returns the inserted records.
366
+
To insert thousands of records at a time, you can use the bulk insert API. This requires you to specify any optional columns that you want to pass in. Any required (i.e. `NOT NULL` and no default value) columns are automatically expected. You can find more details on how this API works in [@databases/pg-bulk](pg-bulk.md). `bulkInsert` also returns the inserted records.
324
367
325
368
```typescript
326
369
asyncfunction insertUsers() {
327
-
// This example assumes that `email` is a non-nullable field
370
+
// This example assumes that `email` is a non-nullable column
328
371
awaittables.users(db).bulkInsert({
329
372
columnsToInsert: [`favorite_color`],
330
373
records: [
@@ -367,7 +410,7 @@ Updating multiple records in one go, where each record needs to be updated to a
367
410
368
411
```typescript
369
412
asyncfunction updateUsers() {
370
-
// This example assumes that `email` is a non-nullable field
413
+
// This example assumes that `email` is a non-nullable column
371
414
awaittables.users(db).bulkUpdate({
372
415
whereColumnNames: [`email`],
373
416
setColumnNames: [`favorite_color`],
@@ -403,7 +446,7 @@ This will delete results that match: `(org_id=1 AND user_id=10) OR (org_id=2 AND
403
446
404
447
## SelectQuery
405
448
406
-
A `SelectQuery` is a query for records within a table. The actual query is sent when you call one of the methods that returns a `Promise`, i.e. `all()`, `first()` or `limit(count)`.
449
+
A `SelectQuery` is a query for records within a table. The actual query is sent when you call one of the methods that returns a `Promise`, i.e. `all()`, `one()`, `oneRequired()`, `first()` or `limit(count)`.
407
450
408
451
### andWhere(condition)
409
452
@@ -428,9 +471,13 @@ export async function getPostsSince(since: Date) {
428
471
}
429
472
```
430
473
431
-
### select(...fields)
474
+
### distinct(...columns)
475
+
476
+
Only return distinct results for the given columns. If you call `.distinct()` without passing any column names, the entire row is checked. You cannot specify a sort order using `.orderByAsc`/`.orderByDesc` if you are using `.distinct` and you cannot rely on the data being returned in any particular order. If you need to only return distinct rows and sort your data, you should use `.orderByAscDistinct` or `.orderByDescDistinct` instead.
432
477
433
-
Only return the provided fields. This can be useful if you have database records with many fields or where some fields are very large, and you typically only care about a small subset of the fields. The default is to return all fields, i.e. `*`.
478
+
### select(...columns)
479
+
480
+
Only return the provided columns. This can be useful if you have database records with many columns or where some columns are very large, and you typically only care about a small subset of the columns. The default is to return all columns, i.e. `*`.
434
481
435
482
```typescript
436
483
importdb, {users} from'./database';
@@ -441,6 +488,27 @@ export async function getEmails() {
441
488
}
442
489
```
443
490
491
+
### toSql()
492
+
493
+
If you want to run the query yourself, or perhaps use it as part of another more complex query, you can use the `toSql` method to return the SQLQuery
SELECT COUNT(*) AS row_count FROM (${distinctFirstNameQuery}) AS u
507
+
`);
508
+
returnparseInt(`${records[0].row_count}`, 10);
509
+
}
510
+
```
511
+
444
512
### orderByAsc(key) / orderByDesc(key)
445
513
446
514
Sort the records by the provided key. You can chain multiple calls to `orderByAsc` or `orderByDesc` with different keys to further sort records that have the same value for the provided key.
@@ -539,7 +607,7 @@ export async function getEmails() {
539
607
540
608
### one()
541
609
542
-
Return a single record (or null). If multiple records in the table match `whereValues`, an error is thrown. If no records match `whereValues`, `null` is returned. This is useful if you want to do `.findOne` but only need a sub-set of the fields.
610
+
Return a single record (or null). If multiple records in the table match `whereValues`, an error is thrown. If no records match `whereValues`, `null` is returned. This is useful if you want to do `.findOne` but only need a sub-set of the columns.
543
611
544
612
```typescript
545
613
importdb, {users} from'./database';
@@ -730,7 +798,7 @@ export async function getUsersWithValidPreference() {
730
798
}
731
799
```
732
800
733
-
### key(fieldName, whereClause)
801
+
### key(columnName, whereClause)
734
802
735
803
There is also a helper on the table itself to allow you to do `inQueryResults`, but in a type safe way:
736
804
@@ -756,7 +824,7 @@ export async function getPostsByUserEmail(email: string) {
756
824
757
825
## or(conditions) / and(conditions)
758
826
759
-
To `or`/`and` values/conditions for a single field, you can use `anyOf`/`allOf`, but the `or` utility helps if you want to have multiple distinct queries. If you anticipate many conditions in an or, you may be get better performance by using `.bulkFind`/`.bulkDelete` instead of `or`.
827
+
To `or`/`and` values/conditions for a single column, you can use `anyOf`/`allOf`, but the `or` utility helps if you want to have multiple distinct queries. If you anticipate many conditions in an or, you may be get better performance by using `.bulkFind`/`.bulkDelete` instead of `or`.
Copy file name to clipboardExpand all lines: docs/sql.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -141,6 +141,10 @@ db.query(sql`SELECT * FROM users WHERE dob = ${new DayDate(2018, 1, 20)};`);
141
141
// => {text: 'SELECT * FROM users WHERE dob = $1;', values: ['2018-01-20']}
142
142
```
143
143
144
+
### `sql.isSqlQuery(query)`
145
+
146
+
Use this to test if an unknown value is an SQLQuery. Returns `true` if `query` is an instance of `SQLQuery`, otherwise it returns `false`.
147
+
144
148
### `SQLQuery.format(config: FormatConfig)`
145
149
146
150
This returns an object with `{text: string, values: any[]}` where the `text` field contains the SQL formatted query, and values contains the parameters.
0 commit comments