diff --git a/docs/index.html b/docs/index.html index a0f7f59..7f21488 100644 --- a/docs/index.html +++ b/docs/index.html @@ -280,7 +280,7 @@

JOINs as nested JSON

"isLiving": true }, "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.995182+00:00" + "createdAt": "2024-01-08T11:50:21.508324+00:00" }, { "id": 1001, @@ -301,7 +301,7 @@

JOINs as nested JSON

"isLiving": true }, "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.996168+00:00" + "createdAt": "2024-01-08T11:50:21.50977+00:00" }, { "id": 1002, @@ -322,7 +322,7 @@

JOINs as nested JSON

"isLiving": true }, "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.996692+00:00" + "createdAt": "2024-01-08T11:50:21.510292+00:00" }, { "id": 1003, @@ -339,7 +339,7 @@

JOINs as nested JSON

"isLiving": true }, "authorId": 1001, - "createdAt": "2023-11-30T11:55:25.000887+00:00" + "createdAt": "2024-01-08T11:50:21.513693+00:00" }, { "id": 1004, @@ -356,7 +356,7 @@

JOINs as nested JSON

"isLiving": true }, "authorId": 1002, - "createdAt": "2023-11-30T11:55:25.002593+00:00" + "createdAt": "2024-01-08T11:50:21.514519+00:00" } ] @@ -680,10 +680,10 @@

sql tagged template stringsSELECT random()
[
   {
-    "random": 0.5511811017169812
+    "random": 0.4620805530200762
   }
 ]
-
0.5511811017169812
+
0.4620805530200762

Interpolations is never because nothing needs to be interpolated in this query, and the RunResult type says that the query will return one row comprising one numeric column, named random. The random TypeScript variable we initialize will of course be typed as a number.

If you’re happy to have your types tied down a little less tightly, it also works to wholly omit the type variables in this particular query, falling back on their defaults:

@@ -823,7 +823,7 @@

Whereable

"id": 1000, "authorId": 1000, "title": "Northern Lights", - "createdAt": "2023-11-30T11:55:24.995Z" + "createdAt": "2024-01-08T11:50:21.508Z" } ] @@ -849,7 +849,7 @@

Whereable

"id": 1000, "authorId": 1000, "title": "Northern Lights", - "createdAt": "2023-11-30T11:55:24.995Z" + "createdAt": "2024-01-08T11:50:21.508Z" } ] @@ -875,7 +875,7 @@

Whereable

"id": 1000, "authorId": 1000, "title": "Northern Lights", - "createdAt": "2023-11-30T11:55:24.995Z" + "createdAt": "2024-01-08T11:50:21.508Z" } ] @@ -990,7 +990,7 @@

runResultTransform: (qr: const dbNow = await dbNowQuery().run(pool); // dbNow is a Date: the result you can toggle below has come via JSON.stringify
SELECT now()
-
"2023-11-30T11:56:08.041Z"
+
"2024-01-08T11:51:14.463Z"

Note that the RunResult type variable on the sql template function (in this case, Date) must reflect the type of the transformed result, not what comes straight back from pg (which in this case is roughly { rows: [{ now: Date }] }).

If a SQLFragment does not have run called on it directly — for example, if it is instead interpolated into another SQLFragment, or given as the value of the lateral option to the select shortcut — then the runResultTransform function is never applied.

@@ -1016,7 +1016,7 @@

Manual joins using Postgres "id": 1000, "authorId": 1000, "title": "Northern Lights", - "createdAt": "2023-11-30T11:55:24.995Z", + "createdAt": "2024-01-08T11:50:21.508Z", "author": { "id": 1000, "name": "Philip Pullman", @@ -1027,7 +1027,7 @@

Manual joins using Postgres "id": 1001, "authorId": 1000, "title": "The Subtle Knife", - "createdAt": "2023-11-30T11:55:24.996Z", + "createdAt": "2024-01-08T11:50:21.509Z", "author": { "id": 1000, "name": "Philip Pullman", @@ -1038,7 +1038,7 @@

Manual joins using Postgres "id": 1002, "authorId": 1000, "title": "The Amber Spyglass", - "createdAt": "2023-11-30T11:55:24.996Z", + "createdAt": "2024-01-08T11:50:21.510Z", "author": { "id": 1000, "name": "Philip Pullman", @@ -1049,7 +1049,7 @@

Manual joins using Postgres "id": 1003, "authorId": 1001, "title": "The Curious Incident of the Dog in the Night-Time", - "createdAt": "2023-11-30T11:55:25.000Z", + "createdAt": "2024-01-08T11:50:21.513Z", "author": { "id": 1001, "name": "Mark Haddon", @@ -1060,7 +1060,7 @@

Manual joins using Postgres "id": 1004, "authorId": 1002, "title": "Holes", - "createdAt": "2023-11-30T11:55:25.002Z", + "createdAt": "2024-01-08T11:50:21.514Z", "author": { "id": 1002, "name": "Louis Sachar", @@ -1096,19 +1096,19 @@

Manual joins using Postgres "id": 1000, "title": "Northern Lights", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.995182+00:00" + "createdAt": "2024-01-08T11:50:21.508324+00:00" }, { "id": 1001, "title": "The Subtle Knife", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.996168+00:00" + "createdAt": "2024-01-08T11:50:21.50977+00:00" }, { "id": 1002, "title": "The Amber Spyglass", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.996692+00:00" + "createdAt": "2024-01-08T11:50:21.510292+00:00" } ] }, @@ -1121,7 +1121,7 @@

Manual joins using Postgres "id": 1003, "title": "The Curious Incident of the Dog in the Night-Time", "authorId": 1001, - "createdAt": "2023-11-30T11:55:25.000887+00:00" + "createdAt": "2024-01-08T11:50:21.513693+00:00" } ] }, @@ -1134,7 +1134,7 @@

Manual joins using Postgres "id": 1004, "title": "Holes", "authorId": 1002, - "createdAt": "2023-11-30T11:55:25.002593+00:00" + "createdAt": "2024-01-08T11:50:21.514519+00:00" } ] } @@ -1172,19 +1172,19 @@

Manual joins using Postgres "id": 1000, "authorId": 1000, "title": "Northern Lights", - "createdAt": "2023-11-30T11:55:24.995182+00:00" + "createdAt": "2024-01-08T11:50:21.508324+00:00" }, { "id": 1001, "authorId": 1000, "title": "The Subtle Knife", - "createdAt": "2023-11-30T11:55:24.996168+00:00" + "createdAt": "2024-01-08T11:50:21.50977+00:00" }, { "id": 1002, "authorId": 1000, "title": "The Amber Spyglass", - "createdAt": "2023-11-30T11:55:24.996692+00:00" + "createdAt": "2024-01-08T11:50:21.510292+00:00" } ] }, @@ -1197,7 +1197,7 @@

Manual joins using Postgres "id": 1003, "authorId": 1001, "title": "The Curious Incident of the Dog in the Night-Time", - "createdAt": "2023-11-30T11:55:25.000887+00:00" + "createdAt": "2024-01-08T11:50:21.513693+00:00" } ] }, @@ -1210,7 +1210,7 @@

Manual joins using Postgres "id": 1004, "authorId": 1002, "title": "Holes", - "createdAt": "2023-11-30T11:55:25.002593+00:00" + "createdAt": "2024-01-08T11:50:21.514519+00:00" } ] }, @@ -1314,13 +1314,13 @@

insert

"id": 1, "title": "A Brief History of Time", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.395795+00:00" + "createdAt": "2024-01-08T11:51:15.452677+00:00" }, { "id": 2, "title": "My Brief History", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.395795+00:00" + "createdAt": "2024-01-08T11:51:15.452677+00:00" } ]
INSERT INTO "tags" ("bookId", "tag")
@@ -1415,7 +1415,7 @@ 

update

[
   {
     "email": "me@privacy.net",
-    "lastFailedLogin": "2023-11-30T11:56:10.736952+00:00",
+    "lastFailedLogin": "2024-01-08T11:51:16.550739+00:00",
     "consecutiveFailedLogins": 1
   }
 ]
@@ -1563,7 +1563,7 @@
INSERT ... ON CONFLICT ... DO NOT
{
   "code": "XYE953ZVU767",
   "$action": "INSERT",
-  "redeemedAt": "2023-11-30T11:56:11.609362+00:00"
+  "redeemedAt": "2024-01-08T11:51:17.298226+00:00"
 }
INSERT INTO "usedVoucherCodes" ("code")
   VALUES ($1)
@@ -1697,43 +1697,43 @@ 

select, select "id": 1000, "title": "Northern Lights", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.995182+00:00" + "createdAt": "2024-01-08T11:50:21.508324+00:00" }, { "id": 1001, "title": "The Subtle Knife", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.996168+00:00" + "createdAt": "2024-01-08T11:50:21.50977+00:00" }, { "id": 1002, "title": "The Amber Spyglass", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.996692+00:00" + "createdAt": "2024-01-08T11:50:21.510292+00:00" }, { "id": 1003, "title": "The Curious Incident of the Dog in the Night-Time", "authorId": 1001, - "createdAt": "2023-11-30T11:55:25.000887+00:00" + "createdAt": "2024-01-08T11:50:21.513693+00:00" }, { "id": 1, "title": "A Brief History of Time", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.395795+00:00" + "createdAt": "2024-01-08T11:51:15.452677+00:00" }, { "id": 2, "title": "My Brief History", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.395795+00:00" + "createdAt": "2024-01-08T11:51:15.452677+00:00" }, { "id": 3, "title": "The Universe in a Nutshell", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.400222+00:00" + "createdAt": "2024-01-08T11:51:15.457539+00:00" } ]

@@ -1751,19 +1751,19 @@

select, select "id": 1000, "title": "Northern Lights", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.995182+00:00" + "createdAt": "2024-01-08T11:50:21.508324+00:00" }, { "id": 1001, "title": "The Subtle Knife", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.996168+00:00" + "createdAt": "2024-01-08T11:50:21.50977+00:00" }, { "id": 1002, "title": "The Amber Spyglass", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.996692+00:00" + "createdAt": "2024-01-08T11:50:21.510292+00:00" } ]

@@ -1817,7 +1817,7 @@

select, select "id": 1003, "title": "The Curious Incident of the Dog in the Night-Time", "authorId": 1001, - "createdAt": "2023-11-30T11:55:25.000887+00:00" + "createdAt": "2024-01-08T11:50:21.513693+00:00" } ] @@ -1840,7 +1840,7 @@

select, select "id": 1003, "title": "The Curious Incident of the Dog in the Night-Time", "authorId": 1001, - "createdAt": "2023-11-30T11:55:25.000887+00:00" + "createdAt": "2024-01-08T11:50:21.513693+00:00" } ] @@ -1862,7 +1862,7 @@

select, select "id": 1, "title": "A Brief History of Time", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.395795+00:00" + "createdAt": "2024-01-08T11:51:15.452677+00:00" } ] @@ -1930,7 +1930,7 @@

order, limit and "id": 1, "title": "A Brief History of Time", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.395795+00:00" + "createdAt": "2024-01-08T11:51:15.452677+00:00" } ] @@ -1950,7 +1950,7 @@
order, limit and "id": 1, "title": "A Brief History of Time", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.395795+00:00" + "createdAt": "2024-01-08T11:51:15.452677+00:00" }

The { limit: 1 } option is now applied automatically. And the return type following await needs no destructuring and is now, correctly, JSONSelectable | undefined.

@@ -2003,7 +2003,7 @@
lateral property maps
"isLiving": true }, "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.995182+00:00" + "createdAt": "2024-01-08T11:50:21.508324+00:00" }, { "id": 1001, @@ -2024,7 +2024,7 @@
lateral property maps
"isLiving": true }, "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.996168+00:00" + "createdAt": "2024-01-08T11:50:21.50977+00:00" }, { "id": 1002, @@ -2045,7 +2045,7 @@
lateral property maps
"isLiving": true }, "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.996692+00:00" + "createdAt": "2024-01-08T11:50:21.510292+00:00" }, { "id": 1003, @@ -2062,7 +2062,7 @@
lateral property maps
"isLiving": true }, "authorId": 1001, - "createdAt": "2023-11-30T11:55:25.000887+00:00" + "createdAt": "2024-01-08T11:50:21.513693+00:00" }, { "id": 1, @@ -2079,7 +2079,7 @@
lateral property maps
"isLiving": false }, "authorId": 5, - "createdAt": "2023-11-30T11:56:09.395795+00:00" + "createdAt": "2024-01-08T11:51:15.452677+00:00" }, { "id": 2, @@ -2100,7 +2100,7 @@
lateral property maps
"isLiving": false }, "authorId": 5, - "createdAt": "2023-11-30T11:56:09.395795+00:00" + "createdAt": "2024-01-08T11:51:15.452677+00:00" }, { "id": 3, @@ -2112,7 +2112,7 @@
lateral property maps
"isLiving": false }, "authorId": 5, - "createdAt": "2023-11-30T11:56:09.400222+00:00" + "createdAt": "2024-01-08T11:51:15.457539+00:00" } ]
@@ -2163,7 +2163,7 @@
lateral property maps
], "title": "Northern Lights", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.995182+00:00" + "createdAt": "2024-01-08T11:50:21.508324+00:00" }, { "id": 1001, @@ -2177,7 +2177,7 @@
lateral property maps
], "title": "The Subtle Knife", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.996168+00:00" + "createdAt": "2024-01-08T11:50:21.50977+00:00" }, { "id": 1002, @@ -2191,7 +2191,7 @@
lateral property maps
], "title": "The Amber Spyglass", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.996692+00:00" + "createdAt": "2024-01-08T11:50:21.510292+00:00" } ], "isLiving": true @@ -2209,7 +2209,7 @@
lateral property maps
], "title": "The Curious Incident of the Dog in the Night-Time", "authorId": 1001, - "createdAt": "2023-11-30T11:55:25.000887+00:00" + "createdAt": "2024-01-08T11:50:21.513693+00:00" } ], "isLiving": true @@ -2257,7 +2257,7 @@
lateral property maps
], "title": "A Brief History of Time", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.395795+00:00" + "createdAt": "2024-01-08T11:51:15.452677+00:00" }, { "id": 2, @@ -2271,14 +2271,14 @@
lateral property maps
], "title": "My Brief History", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.395795+00:00" + "createdAt": "2024-01-08T11:51:15.452677+00:00" }, { "id": 3, "tags": [], "title": "The Universe in a Nutshell", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.400222+00:00" + "createdAt": "2024-01-08T11:51:15.457539+00:00" } ], "isLiving": false @@ -2752,46 +2752,46 @@
distinct
FROM "books") AS "sq_books"
[
   {
-    "id": 1,
-    "title": "A Brief History of Time",
-    "authorId": 5,
-    "createdAt": "2023-11-30T11:56:09.395795+00:00"
+    "id": 1002,
+    "title": "The Amber Spyglass",
+    "authorId": 1000,
+    "createdAt": "2024-01-08T11:50:21.510292+00:00"
   },
   {
     "id": 1001,
     "title": "The Subtle Knife",
     "authorId": 1000,
-    "createdAt": "2023-11-30T11:55:24.996168+00:00"
+    "createdAt": "2024-01-08T11:50:21.50977+00:00"
   },
   {
     "id": 1003,
     "title": "The Curious Incident of the Dog in the Night-Time",
     "authorId": 1001,
-    "createdAt": "2023-11-30T11:55:25.000887+00:00"
+    "createdAt": "2024-01-08T11:50:21.513693+00:00"
   },
   {
     "id": 2,
     "title": "My Brief History",
     "authorId": 5,
-    "createdAt": "2023-11-30T11:56:09.395795+00:00"
+    "createdAt": "2024-01-08T11:51:15.452677+00:00"
   },
   {
-    "id": 1002,
-    "title": "The Amber Spyglass",
+    "id": 1000,
+    "title": "Northern Lights",
     "authorId": 1000,
-    "createdAt": "2023-11-30T11:55:24.996692+00:00"
+    "createdAt": "2024-01-08T11:50:21.508324+00:00"
   },
   {
-    "id": 3,
-    "title": "The Universe in a Nutshell",
+    "id": 1,
+    "title": "A Brief History of Time",
     "authorId": 5,
-    "createdAt": "2023-11-30T11:56:09.400222+00:00"
+    "createdAt": "2024-01-08T11:51:15.452677+00:00"
   },
   {
-    "id": 1000,
-    "title": "Northern Lights",
-    "authorId": 1000,
-    "createdAt": "2023-11-30T11:55:24.995182+00:00"
+    "id": 3,
+    "title": "The Universe in a Nutshell",
+    "authorId": 5,
+    "createdAt": "2024-01-08T11:51:15.457539+00:00"
   }
 ]
SELECT coalesce(jsonb_agg(result), '[]') AS result
@@ -2803,43 +2803,43 @@ 
distinct
"id": 1, "title": "A Brief History of Time", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.395795+00:00" + "createdAt": "2024-01-08T11:51:15.452677+00:00" }, { "id": 2, "title": "My Brief History", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.395795+00:00" + "createdAt": "2024-01-08T11:51:15.452677+00:00" }, { "id": 1000, "title": "Northern Lights", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.995182+00:00" + "createdAt": "2024-01-08T11:50:21.508324+00:00" }, { "id": 1002, "title": "The Amber Spyglass", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.996692+00:00" + "createdAt": "2024-01-08T11:50:21.510292+00:00" }, { "id": 1003, "title": "The Curious Incident of the Dog in the Night-Time", "authorId": 1001, - "createdAt": "2023-11-30T11:55:25.000887+00:00" + "createdAt": "2024-01-08T11:50:21.513693+00:00" }, { "id": 1001, "title": "The Subtle Knife", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.996168+00:00" + "createdAt": "2024-01-08T11:50:21.50977+00:00" }, { "id": 3, "title": "The Universe in a Nutshell", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.400222+00:00" + "createdAt": "2024-01-08T11:51:15.457539+00:00" } ]
SELECT coalesce(jsonb_agg(result), '[]') AS result
@@ -2851,43 +2851,43 @@ 
distinct
"id": 1, "title": "A Brief History of Time", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.395795+00:00" + "createdAt": "2024-01-08T11:51:15.452677+00:00" }, { "id": 2, "title": "My Brief History", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.395795+00:00" + "createdAt": "2024-01-08T11:51:15.452677+00:00" }, { "id": 1000, "title": "Northern Lights", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.995182+00:00" + "createdAt": "2024-01-08T11:50:21.508324+00:00" }, { "id": 1002, "title": "The Amber Spyglass", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.996692+00:00" + "createdAt": "2024-01-08T11:50:21.510292+00:00" }, { "id": 1003, "title": "The Curious Incident of the Dog in the Night-Time", "authorId": 1001, - "createdAt": "2023-11-30T11:55:25.000887+00:00" + "createdAt": "2024-01-08T11:50:21.513693+00:00" }, { "id": 1001, "title": "The Subtle Knife", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.996168+00:00" + "createdAt": "2024-01-08T11:50:21.50977+00:00" }, { "id": 3, "title": "The Universe in a Nutshell", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.400222+00:00" + "createdAt": "2024-01-08T11:51:15.457539+00:00" } ]
SELECT coalesce(jsonb_agg(result), '[]') AS result
@@ -2899,43 +2899,43 @@ 
distinct
"id": 1, "title": "A Brief History of Time", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.395795+00:00" + "createdAt": "2024-01-08T11:51:15.452677+00:00" }, { "id": 2, "title": "My Brief History", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.395795+00:00" + "createdAt": "2024-01-08T11:51:15.452677+00:00" }, { "id": 1000, "title": "Northern Lights", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.995182+00:00" + "createdAt": "2024-01-08T11:50:21.508324+00:00" }, { "id": 1002, "title": "The Amber Spyglass", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.996692+00:00" + "createdAt": "2024-01-08T11:50:21.510292+00:00" }, { "id": 1003, "title": "The Curious Incident of the Dog in the Night-Time", "authorId": 1001, - "createdAt": "2023-11-30T11:55:25.000887+00:00" + "createdAt": "2024-01-08T11:50:21.513693+00:00" }, { "id": 1001, "title": "The Subtle Knife", "authorId": 1000, - "createdAt": "2023-11-30T11:55:24.996168+00:00" + "createdAt": "2024-01-08T11:50:21.50977+00:00" }, { "id": 3, "title": "The Universe in a Nutshell", "authorId": 5, - "createdAt": "2023-11-30T11:56:09.400222+00:00" + "createdAt": "2024-01-08T11:51:15.457539+00:00" } ]
@@ -3094,7 +3094,7 @@

JSONSelectable

s1: '2012-06-01T12:34:00.000Z', s2: '2012-06-01T00:00:00.000', s3: '2012-06-01', - s4: null + s4: '2012-10-09T02:34:00.000Z' } -

As is implied above, for REPEATABLE READ or SYNCHRONIZED isolation modes the callback could be called several times. It’s therefore important that it doesn’t have any non-database-related side-effects (i.e. don’t, say, bill your customer’s credit card from this function).

+

As is implied above, for REPEATABLE READ or SERIALIZABLE isolation modes the callback could be called several times. It’s therefore important that it doesn’t have any non-database-related side-effects (i.e. don’t, say, bill your customer’s credit card from this function).

We already saw one transaction example. Here’s another, adapted from CockroachDB’s write-up on SERIALIZABLE.

We have a table of doctors, and a table of their assigned shifts.

CREATE TABLE "doctors"
@@ -3420,7 +3448,7 @@ 

transaction

Transaction 1
ROLLBACK
Transaction 1
-
Transaction rollback (code 40001) on attempt 1 of 5, retrying in 76ms
+
Transaction rollback (code 40001) on attempt 1 of 5, retrying in 151ms
Transaction 1
Retrying transaction, attempt 2 of 5
Transaction 1