From 9441a91eb5242901e422fe9762386738176193ae Mon Sep 17 00:00:00 2001 From: James Pace Date: Fri, 16 Feb 2024 02:00:48 +0000 Subject: [PATCH] Publish sqlx article. --- {_drafts => posts}/sqlx-without-macros.md | 32 +++++++++++------------ 1 file changed, 16 insertions(+), 16 deletions(-) rename {_drafts => posts}/sqlx-without-macros.md (90%) diff --git a/_drafts/sqlx-without-macros.md b/posts/sqlx-without-macros.md similarity index 90% rename from _drafts/sqlx-without-macros.md rename to posts/sqlx-without-macros.md index 6df0603..e7e1b6d 100644 --- a/_drafts/sqlx-without-macros.md +++ b/posts/sqlx-without-macros.md @@ -9,7 +9,7 @@ for interacting with a SQL database. One of the more popular features of `sqlx` is that it does compile time checking of database queries (giving you some ORM-ish properties) while still allowing you to write straight SQL queries without the abstraction of an ORM though use -of their [`query!`](https://docs.rs/sqlx/0.5.5/sqlx/macro.query.html). +of their [`query!`](https://docs.rs/sqlx/0.5.5/sqlx/macro.query.html) macro. To make this work, `sqlx` requires: @@ -19,11 +19,11 @@ To make this work, `sqlx` requires: I don't particularly like either of those options, though I admittedly have not tried either of them long term. -Requring a running database at compile time would seem to make compilation +Requiring a running database at compile time would seem to make compilation a lot slower. -It also adds a unexpected step (run a container hosting a development database) +It also adds an unexpected step (run a container hosting a development database) before you can compile, which just feels wrong. -The configuation files are better in the sense they don't require a running +The configuration files are better in the sense they don't require a running database at compile time, but it's an extra thing that has to be kept in sync with the code. @@ -35,8 +35,8 @@ to improve in this post. The other thing I'll show in this post is working with JSON in Postgres. Postgres has native support for working with JSON documents, providing -a lot of the benefits of NoSQL databases, but in a SQL database. -`sqlx`'s API for json documents integrates well with serde, and is resonably +a lot of the benefits of NoSQL databases in a SQL database. +`sqlx`'s API for json documents integrates well with serde, and is reasonably easy to work with. # Pre-reqs @@ -73,8 +73,8 @@ The script: # Example -For the rest of this post, I will walk through our example (which can be found -[here](https://git.jpace121.net/public/sql-fun/src/branch/main/src/main.rs). +For the rest of this post, I will walk through an example (which can be found +[here](https://git.jpace121.net/public/sql-fun/src/branch/main/src/main.rs)). ``` rust use serde::{Deserialize, Serialize}; @@ -110,7 +110,7 @@ The first thing we do in main is to connect to the database by making a connecti The `connect` function takes a string argument to control what database by what method we want to connect to. Specifically, in the call above, we're going to connect as the user `postgres` using the -password `devpassword` to the server running on `localhost` with the default port, to the +password `devpassword` to the server running on `localhost` with the default port to the database `postgres`. ```rust @@ -133,7 +133,7 @@ in it. `query` returns a `Query` object which has member functions we call to actually do the database call. The two functions we'll use in this example are `execute` and `fetch_one`. -Both functions take the `pool` as an input an return a future with a result +Both functions take the `pool` as an input and return a future with a result that we need to await. What's in the non-Err part of the results depends on which function we call. For `execute` it is `()`, and thus we call `execute` when we're not expecting to @@ -157,7 +157,7 @@ query the output of our database query. let _ = sqlx::query(&james_add_query).execute(&pool).await?; ``` The next thing we do is add rows to table so we have something to query. -We start by instantiating and instance of our `Doc` struct, and then using +We start by instantiating an instance of our `Doc` struct, and then using `serde_json` to serialize it into a JSON string. We generate and execute the database query as before, with the exception that we make the string we're going to pass into `sqlx::query` in its own line. @@ -165,7 +165,7 @@ One thing of interest is we define the string using a "raw string literal" (the `r#` stuff). The official documentation for the "raw string literal"s is [here](https://doc.rust-lang.org/reference/tokens.html#raw-string-literals). -It's basically a special ysntax that allows us to avoid having to escape characters +It's basically a special syntax that allows us to avoid having to escape characters inside of the string. The next block of text is basically the same as above, but for our second user, @@ -200,12 +200,12 @@ There are three interesting new things in this block of code: 1. In the SQL query, we're going to use the special syntax `->>` to refer to elements inside the JSON object we added to the database. 2. Since we're trying to get one element from the database, instead of - call `execute` on the query, we use `fetch_one`. + calling `execute` on the query, we use `fetch_one`. 3. The result of `fetch_one` is a `sqlx::Row`. To get actual data out of the row we call `try_get`, which takes one - parameter, the index of the element of the row to get an elemet of. + parameter, the index of the element of the row to get an element of. There are two type parameters for `try_get`, the first the type of the - thing we're getting from the row and the second the type of index. + thing we're getting from the row and the second the type of the index. ```rust // Get name based on movie. @@ -216,7 +216,7 @@ There are three interesting new things in this block of code: ``` Our second example is similar to the first, but we use the `->>` syntax to pull -something out the json doc in the `WHERE` condition instead of in what we `SELECT`. +something out of the JSON doc in the `WHERE` condition instead of in what we `SELECT`. ```rust