mirror of
https://github.com/jbranchaud/til
synced 2026-01-20 15:38:02 +00:00
Compare commits
1 Commits
8d68852d18
...
33153b6688
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
33153b6688 |
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||
|
||||
_1433 TILs and counting..._
|
||||
_1430 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -734,7 +734,6 @@ _1433 TILs and counting..._
|
||||
- [Generate Series Of Numbers](postgres/generate-series-of-numbers.md)
|
||||
- [Generating UUIDs With pgcrypto](postgres/generating-uuids-with-pgcrypto.md)
|
||||
- [Get A Quick Approximate Count Of A Table](postgres/get-a-quick-approximate-count-of-a-table.md)
|
||||
- [Get Row Count For Most Recent Query](postgres/get-row-count-for-most-recent-query.md)
|
||||
- [Get The Size On Disk of An Index](postgres/get-the-size-on-disk-of-an-index.md)
|
||||
- [Get The Size Of A Database](postgres/get-the-size-of-a-database.md)
|
||||
- [Get The Size Of A Table](postgres/get-the-size-of-a-table.md)
|
||||
@@ -830,7 +829,6 @@ _1433 TILs and counting..._
|
||||
- [Execute A Raw SQL Query](prisma/execute-a-raw-sql-query.md)
|
||||
- [Grab A Limited Set Of Records](prisma/grab-a-limited-set-of-records.md)
|
||||
- [Open Connections To Multiple Databases](prisma/open-connections-to-multiple-databases.md)
|
||||
- [Override Table Name For Prisma Model](prisma/override-table-name-for-prisma-model.md)
|
||||
- [Specify Alternate Location For Prisma Schema](prisma/specify-alternate-location-for-prisma-schema.md)
|
||||
|
||||
### Python
|
||||
@@ -1378,7 +1376,6 @@ _1433 TILs and counting..._
|
||||
- [Find Files With fd](unix/find-files-with-fd.md)
|
||||
- [Find Newer Files](unix/find-newer-files.md)
|
||||
- [Find Occurrences Of Multiple Values With Ripgrep](unix/find-occurrences-of-multiple-values-with-ripgrep.md)
|
||||
- [Find Top-Level Directories Matching A Pattern](unix/find-top-level-directories-matching-a-pattern.md)
|
||||
- [Fix Unlinked Node Binaries With asdf](unix/fix-unlinked-node-binaries-with-asdf.md)
|
||||
- [Forward Multiple Ports Over SSH](unix/forward-multiple-ports-over-ssh.md)
|
||||
- [Generate A SAML Key And Certificate Pair](unix/generate-a-saml-key-and-certificate-pair.md)
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
# Get Row Count For Most Recent Query
|
||||
|
||||
Anytime you execute a query in `psql`, there is a _row count_ associated with
|
||||
that query. This is most naturally understood with a `select` query where a
|
||||
discreet number of rows are returned. We typically see the row count (e.g. `(19
|
||||
rows)`) right below the result set.
|
||||
|
||||
You can always reference the row count of the most recent query with [the
|
||||
`:ROW_COUNT`
|
||||
variable](https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-VARIABLES-ROW-COUNT).
|
||||
Here we use `\echo` to print it out.
|
||||
|
||||
```sql
|
||||
> select generate_series(2,20);
|
||||
generate_series
|
||||
-----------------
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
10
|
||||
11
|
||||
12
|
||||
13
|
||||
14
|
||||
15
|
||||
16
|
||||
17
|
||||
18
|
||||
19
|
||||
20
|
||||
(19 rows)
|
||||
|
||||
Time: 12.338 ms
|
||||
> \echo :ROW_COUNT
|
||||
19
|
||||
```
|
||||
|
||||
For some queries, like one that induces a pager (e.g. `less`) to be used,
|
||||
you'll lose track of the row count once the pager closes. This is where being
|
||||
able to reference the row count without rerunning the query is most useful.
|
||||
|
||||
```sql
|
||||
> select generate_series(2,2000);
|
||||
Time: 9.815 ms
|
||||
> \echo :ROW_COUNT
|
||||
1999
|
||||
```
|
||||
|
||||
Notice, we can also get a row count from other kinds of queries like this
|
||||
`insert` statement.
|
||||
|
||||
```sql
|
||||
> insert into users (id) values (50001), (50002), (50003);
|
||||
INSERT 0 3
|
||||
Time: 2.804 ms
|
||||
> \echo :ROW_COUNT
|
||||
3
|
||||
```
|
||||
|
||||
[source](https://postgresql.verite.pro/blog/2024/05/13/advanced-psql-coproc.html)
|
||||
@@ -1,42 +0,0 @@
|
||||
# Override Table Name For Prisma Model
|
||||
|
||||
When defining your Prisma schema, you'll add models to your
|
||||
`prisma/schema.prisma` file that look something like this:
|
||||
|
||||
```
|
||||
model Book {
|
||||
id BigInt @id @default(autoincrement()) @db.BigInt
|
||||
title String
|
||||
author String
|
||||
publication_year Int
|
||||
created_at DateTime @default(now())
|
||||
updated_at DateTime @updatedAt
|
||||
}
|
||||
```
|
||||
|
||||
The prisma client (ORM-layer) that gets generated will have a `Book` type and
|
||||
you'll be able to reference the model to, for instance, create a record with
|
||||
`prisma.book.create(...)`. Both of these things are derived from the model
|
||||
name: `Book`.
|
||||
|
||||
The other thing that is derived from the model name is the name given to the
|
||||
underlying database table. So you end up with a table called `Book`. You may,
|
||||
however, prefer a table naming convention where this one would be named `books`
|
||||
(snake_case and pluralized).
|
||||
|
||||
To achieve that, you have to manually override the table name with [the `@@map`
|
||||
directive](https://www.prisma.io/docs/orm/reference/prisma-schema-reference#map-1).
|
||||
Add it toward the bottom of the model like so:
|
||||
|
||||
```
|
||||
model Book {
|
||||
id BigInt @id @default(autoincrement()) @db.BigInt
|
||||
title String
|
||||
author String
|
||||
publication_year Int
|
||||
created_at DateTime @default(now())
|
||||
updated_at DateTime @updatedAt
|
||||
|
||||
@@map("books")
|
||||
}
|
||||
```
|
||||
@@ -1,36 +0,0 @@
|
||||
# Find Top-Level Directories Matching A Pattern
|
||||
|
||||
I like using [`fd`](https://github.com/sharkdp/fd) as an alternative to `find`.
|
||||
In my experience it is more intuitive to use. For instance, I wanted to find
|
||||
all the top-level directories in my current directory that contained the word
|
||||
`next`. I was able to get the command mostly right by guessing the flags, only
|
||||
checking the man page once.
|
||||
|
||||
On my first attempt, it prompted me with a suggestion for a flag that wasn't
|
||||
quite right. I tried `--depth`, but it should have been `--maxdepth`.
|
||||
|
||||
```bash
|
||||
$ fd --depth 0 next ./
|
||||
error: Found argument '--depth' which wasn't expected, or isn't valid in this context
|
||||
Did you mean --maxdepth?
|
||||
```
|
||||
|
||||
Then I checked the man page for how to specify the file type as _directory_ --
|
||||
using `-t` or `--type` with `d`.
|
||||
|
||||
And here is the command that gets me all top-level directories matching `next`
|
||||
in my current directory:
|
||||
|
||||
```bash
|
||||
$ fd --maxdepth 1 --type d next ./
|
||||
|
||||
bookshelf-nextjs-prisma-postgres
|
||||
bookshelf-prisma-nextjs-planetscale
|
||||
my-next-app
|
||||
next-bookshelf
|
||||
next-personal-site
|
||||
next-sanity-v3-example
|
||||
try-trpc-next
|
||||
```
|
||||
|
||||
See `man fd` for more details.
|
||||
Reference in New Issue
Block a user