Building production-ready web applications requires efficient database interaction, flexible query construction, and well-structured data access layers. We examine how these technologies work together to simplify database operations and support the development of scalable backend applications. SeaQuery provides a dynamic SQL query builder, SQLx offers asynchronous and compile-time-checked database interactions, while ModQL helps simplify query and model-related operations. Through this practical development session, we explore how Rust developers can combine these tools to build maintainable, efficient, and production-oriented web applications. Whether you are developing enterprise applications, SaaS platforms, REST APIs, or complex backend systems, this episode provides valuable insights into modern Rust database architecture. What you'll learn:
Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.
Transcript
From the show. Tap a time to play from there.
Speaker 1CQuery for production coding. In this second episode of our production coding series, we're going to upgrade our code to CQuery SQLX with ModQL. This will bring us multi-database support for Postgres, MySQL, SQLite, and a lot of power on the filtering side with composable filters. Three chapters. In our first chapter, we will update our CRUD implementations by preparing the data with ModQL, build the query with CQuery, and
Speaker 1execute the query with SQLX. Then in the second chapter, we're going to upgrade our CRUD generic list function to build the CQuery conditions from our ModQL filters and apply the list options. And that will allow us in chapter three to update our RPC list function so that when the params property is empty, we get everything. We can add list options with some other buys, for example, descending ID,
Speaker 1and we will be able to add filters. For example, we want task with titles that end in BB and have the done flag false. And our code will also support combining filters into an array to express an OR. For example, we want task that ends with BB as well as a task that have some ID. And we can also use custom operators on numbers. So for example, we can have the operator in and give the array
Speaker 1of values. And thanks to SQL support for multi database drivers, this will work with our progress, but can also support MySQL and SQLite with or without SQLX. And that is pure power. Okay, enough talking, let's start coding. So the way it's structured in our Git is that we're going to check out the E02-start, the tag, and then there's one commit per chapter. So this way, this will allow
Speaker 1you to follow along as we're coding, but also to check with the corresponding chapter Git commit. So we bring a terminal. We do our git clone, russ10x, russwebapp. We clone the code. We're going to get into the russwebapp folder. We're going to do a git checkout b and my underscore e02. And then we're going to check out the e02-start tag. And we can just do a code, a VS code or whatever editor you are using. And that
Speaker 1will start VS code. And you will have a structure like that. And a good first step is to make sure that everything is working fine. So if we go to our readme.md, there's an example of a Docker command to start the PostgreSQL database. And usually I do that in an external terminal and I let it run because we don't need to restart it because our code is recreating the database all the time. So we already took care of that. Then we're bringing another terminal and usually I do it within VS Code. And then we're going
Speaker 1to do a cargo test. So we're making sure that everything is working well. Now that we have that, we're going to do our first chapter, which is going to be upgrading our model layer with the modql, the cquery, and the sqlx. And most of the code will actually be in the base.rs. And first, I would like to say a big thanks to all of my patrons. I made the jump to be full-time on this production coding. So any help is big help. And big thanks to
Speaker 1Crab Nebula for the sponsorship. Okay. So now the first thing that we're going to do is go to our cargo2ml, remove SQLB. We're going to put C query, make sure to put the latest. And then the way it works is we put a C query binder with SQLX progress, and then with a UID similar to what we did in SQLX. And then we're going to put our mode query. So that is a library that I'm maintaining. And we are going to add the with C query. So the
Speaker 1great news about that, it would give us great power on the filters, as well as bringing some of the SQLBs that are not in C query, like fields and so on. So we won't lose any feature. We will just gain on power and flexibility. So if we bring back our file explorer, that is obviously what breaks now, because that was using the SQLB. And the first thing that we're going to do is go to our base.rs. So now the first thing we're going to see is that SQLB is not there anymore. Now the cool thing
Speaker 1is I brought everything in SQLB that is not in C Query in ModQL. So what we can do is just change SQLB by ModQL and the field part is part of the sub-module field. And so now we have the has fields. And then we're going to use another ModQL little utility and that is for C Query. We're going to see why later. And now we can use a C query, some of the types that we're going to use, and the SQL X binder. That is the way it works. So now the first thing that we're
Speaker 1going to do to make it work well with C query is in our DB BMC, we're going to still have our table as a static STR because that is very flexible. But we're going to create a default function, table ref, that will return the C query table ref. And for that, we're just going to create a new table ref from cQuery. It's a table. And that is where we're going to use our static identifier and we're giving our static string. That is what we have over there.
Speaker 1And then we are putting into in them. So this way, we don't have to change anything upstream. And now we can call table ref in our code below when we want to have a cQuery table reference. So that is a little bit the best of both worlds. Then we're going to go to our create And the first things we can see is that our hasFields is fine because now it's coming from ModQL. And that will give us this method, which is the not known fields. And the
Speaker 1reason why we have that is because all of our types task have the fields derived from ModQL. So in this case here for the create, we have the task for create that also had the fields derived. So that is the way it works. And then we have that also for task for update. So because we have that, that everything passes. And obviously we do not have our SQLB here. So we're going to delete that. And what we're going to do is we're going to have three steps,
Speaker 1which the first one is going to extract the fields, prep the data, then drill the query, we see query and then execute the query with SQLX. So we will organize our code like that because it's clean. and it's very clear of the responsibility of each block. Now, when we're going to extract the fields, that will give us the name CQuery values expression, but we can get it in a CQuery convenient way, and we're going to see in the build, and for that, we're going to do a let colons and C values, and we're going to
Speaker 1have our fields, and then there's a method which is for C insert. So the nice thing about that is that it will reformat the fields array into what CQuery expect for an insert. So if we toggle our types, we see that the not known fields come because we have the has fields and that will return the modeQL fields. And then we call our for C insert and that will return those types for CQuery that then we can
Speaker 1use very simply when we build our query. Now that we have that, we're going to build the query with cQuery. The builder pattern in cQuery is a ref mute builder pattern. So the way I like to do it is to have the first line capturing the mute of the builder query insert. And so that will return our insert statement for cQuery. And then we can do a query and chain all our builders over there. So the first one is going to be into table. That is where we're
Speaker 1going to use the MC, which is a generic that we had above. And we're going to use a function that we just created, table ref. That will build the query for this table. Then we're going to add the columns. And that is where we're just going to pass what the 4C insert gives us. So that is a vector of CRC and so on. And then, We're going to have our values and we're just going to pass our C values. I'm going to put a question mark and then we'll add the error later. And then we're going
Speaker 1to have a returning because we're not changing the contract of the method. And that would be a query returning and the way it works in C Query is you do like that, or you do a columns. And then we're going to do our common identifier that we're going to create an ID. So this case, we're going to follow the pattern. our C query. So if we go back on top of the same file, we're going to define a name, common item, which is the identifier, which in C query is the columns in a way, or the table names and so on. And so we're going
Speaker 1to have ID for now. And then we just derive from item. And so that comes from C query. And so now we can use it like that. And we don't have the error anymore. The question mark should show an error, but why now it doesn't show because there's other ones, but we're going to fix that later. So now to execute our query, we're going to have our SQL and our values, and that will be the SQLX values. And SQL has a similar pattern to get from SQL
Speaker 1to SQLX, and that is with a build SQLX, you give your database, and that will give you the SQL as a string and the values as a sqlx values. So now we're just going to execute sqlx the sqlx way. So it's a tuple for id and then we do a sqlx query as with, with our generic and our tuple for the type And then we give our reference to SQL and then the values. And then we're going to fetch one in this case, and we pass our
Speaker 1DBs that we had above. That is our SQLX connection pool. And then obviously we do an await. Now, again, here, this error doesn't show because I think we have other errors everywhere, but we know we're going to have to add it to our model error because we don't have SQL there yet. So for that, we're going to add a variant, cQuery. We're going to have our cQuery error, and then we're going to do our seredit display from str as everything else, such as we can serialize it
Speaker 1for request logline. And then we are adding the attribute from. That is something that I added since the episode one, and that come from derive more from. So this way we can just add this attribute on the variant, and then it will do a from the error type below. And that prevent us to have a lot of boilerplate over there. And that's it. That is actually fixed now. So what we did, if we bring back our old code, we are preparing the data. And that is now those
Speaker 1two lines. We're building the query. So now we're doing that with sqlquery before we were doing that with sqlb. And then we are executing the query with sqlx. So before sqlx was embedded into SQLB in a way, so we could make it more concise. So now it's a little bit these two steps, but there's a lot of power that goes from this separation of concern. And one of the great thing of C query is that it works across database drivers. So you can actually use it
Speaker 1for Tokyo Postgres or Ruslight or whatever. So it's a little bit more verbose, but it's exactly the same concept and it's much more powerful. Now that we have that, We can check our get. And it's the same thing. We don't want our SQLB anymore. We're going to build the query. We don't really need to prep any data. And then we're going to execute the query. So on the build side, we're going to follow the pattern here. Let mute query, query select. And then from the table,
Speaker 1same thing, the table ref. And then the columns, the beauty here is it has fields, has a convenient one, which is fields colon ref. And that is because our E implement has fields. And so it provides this field colon ref. So now we just do an anywhere. And the way it works with eQueries, you do an expression. We're going to have our colon. And that is going to be our common identifier ID. And then equal ID,
Speaker 1which is a value that we have over there. And then once we have that, we're going to execute the query with sqlX. So similar as before, and then we would work query as fetch optional. We do the await and now we're going to do the OK all. And that is exactly the same error that we had before. So the entity will be the MC table. So that is just a static string. So very simple. And then we have our ID. Because when we do a get, we want to fail if we don't find it. So
Speaker 1when we do a first or a find prefix in our function, that is optional in a way. But if we do a get, if it doesn't find it, it complains. Now the cool thing about that is that we didn't change the signature of the function or the contract of the function. It's exactly the same contract. Now we're going to fix our list. So again, here, we don't need the SQLB. We're going to build the query and execute the query. So now for the build query, the same pattern, query from table ref, the columns, and then we execute
Speaker 1our SQLX. And the way to work with SQLX is we do a query as with, we can give the E, fetch all, and await DB. And then for our update, we're going to remove these two blocks. And then we're going to have the prep data, the build query, the exec query, and then we're going to check the result to make sure that it was found. So that is a second block we deleted. And if we get to the function signature, we can see that our data is E and that E implement has fields. So because we
Speaker 1have that, like in our create, we can do a let fields, data, not none fields, that will return our fields. And then we're going to shadow this variable with a let fields, fields for C update, because C query has a different structure for the update. So that will return an iterator, which will be C query friendly. And now to build the query is as simple as doing a mute query, query update, that gives us an update statement.
Speaker 1And then we do a query table. We give our table. values and now for the values we can give the fields that is the way it works in sql update and mod ql is giving us in the right format and then obviously we want to update only the one that match the id we're going to do an unware and we're going to have the same expression that we have above because that is the ids that we have over there and now that we have that we're going to execute the query with SQLX, same as before. So
Speaker 1we're just going to do an execute, our database, await, and then the rule affected. And then in the check result is a code that we had before. So if the count is zero, we are going to return the error. Otherwise it's okay. Now you might want to check if it's greater than one, but for now, since ID is a primary key, it's okay. It's something that we can fine tune later. So now we have the update fixed and now it's just a delete. And the delete is going to be very simple. We're just going to build the query and execute the query and check the result.
Speaker 1And again, the result is exactly what we had before. You might want to put that in a function or somewhere, but right now that is good enough. And so to build the query, we're going to have our mute query. We do our query delete and inside our query from table again, and where, and then the expression with the ID. And then we just execute with SQLX, with query, our SQL and values, execute DB, await, row affected. And then we press save, and then everything should compile. Now we
Speaker 1can go back up. I'm going to change this one as prep data, just for symmetry with the rest. And then that's it. We fixed all of our base.rs error. So that was relatively easy, and we didn't lose any functionality. And we actually, with ModQL and cQuery, are gaining a lot of functionalities. We're going to see that later. So now we still have these two files that have errors. So let's go and fix task. And on this one is going to be so trivial because we're just going
Speaker 1to select sqlb and do a mod qr colon, colon field. And that will give us our fields. Press save. And then that's it. That fixed this one. Now let's fix the user.rs. So this error, very simple again, we're going to do the modeQL field, and that will fix our problem. We see that we're down to two errors, and that is because we have two methods of our user BMC, which is the first by username and the update password
Speaker 1that are doing their own queries. So if we go back to the file, and if we go down, we're going to see that our user BMC get is fine because it's just a pass-through to our base get. But our first by username is still using SQLB. So let's fix this one. And in this case, we're going to use a username identifier, the column name. So what we're going to do is we're going to go on top of the same file and in our user type section down there, we're going to add enum user in them.
Speaker 1And that is where we're going to put the custom user identifiers that we're using in our code. So we're going to have ID. We could use a common identifier, but we're just going to have it there. Username, and then the password as well. Those are the three things that we are going to use in some of the custom methods. And now we're going to derive with cQueryIdent. That will allow us to use those as column names. And again, here we don't need to put all of the properties of the user because we have the modql
Speaker 1fields that will give us all of the columns anyway when we're using it in base. But when we use them manually in some specific query building, then we're going to put them there. Such as again, we don't have strings everywhere. So now we're going to have the build query and execute query section. On the build query is going to be a select from self table ref. And then we have the columns which come from the E. And that is because E is bound to user by
Speaker 1and user by trait, which is a marker trait, implement the has fields. That is why we have the fields in them from ModQL. So now we can do the and where, that is where we're going to put our expression on user-ident username. And then we give the username that we got from above, making sure to import C query expression and C query query. And then we're going to execute it, very similar. We get the user, we do the SQLX, we're giving the E, which also implements
Speaker 1from row, from the market trade. And then we do a fetch optional, await, making sure to do the imports. And then that's it. This one is fixed. Now the next one is the update password. And so we get the ID and the password in clear. If we open the function block, this first section on top is actually prep password. We're going to delete our SQLB update. We'll build a query and exec the query. In the build query section, we're going to have a query update from
Speaker 1the table. And then the value, and in this case, we are going to go without ModQL, we don't need to use it. So we're going to do user and then pwd, and then our simple expression from pwd. So that is API of ZQuery when you want to add manually column value. And you can do it also with multiple values. And then we do similar than before, we do the unware on the user IDEN ID with the ID, making sure to import simple expression. And then we're just doing our execute. This one, we're going to get the count,
Speaker 1but we're not going to use it. There will be an argument that that should be like the delete, the update, and the get, and throw an error if you didn't find the user, but that we can do that offline. So now we have our model layer completely fixed, and the beauty of our architecture is because model is the only one that access the database. Everything should work very nicely. So if we go back on top, we're going to bring our readme, couple of terminals. We're going to take the docker run, If you already have it, you don't need to run it twice. And then we're just going
Speaker 1to do a cargo test. And then everything works. And now we can bring another terminal to do a cargo run to run our server. And then in the terminal below, we could do a cargo run, our example, quick dev. And now we have all our APIs that work exactly as before. So that is pretty cool. And that is concluding our chapter one. By the way, subscribes and likes is always appreciated. So the chapter two is going to be about updating our base list function with the condition
Speaker 1from filters. So that will take the modeQL filters and make it as a C query filter. And then we will take the modeQL list options and then we will apply it to the C query query. So first, we're going to actually update our SQL, our schema, to make it a little bit more interesting. We're just going to add another property to our task table, which is going to be done, going to be a Boolean, and we're going to do a not null and default to false.
Speaker 1So usually in database, if you can avoid the nulls, it's always better. And for Boolean, it's often a good technique. And if you need a three-state Boolean type of property, probably better to do a database anyway. So now that we have that in place, we're going to update our task types. So for task type, we're going to add our done Boolean. For create, we're going to leave it as is. So we're going to require colon update. You can obviously change the strategy. And then in this case,
Speaker 1you will take an option of Boolean probably. And then for task for update, we're going to have our done. And now we do our option of Boolean. Now down there, we have some unit tests and that will fail because we didn't set the Boolean. So one good pattern here will be to add the default derive, which by default, everything will be done. And now in our unit test, we can do the dot dot default and default. So everything compiles nicely and we can add our task filter.
Speaker 1And that is where the magic is going to happen. So first, we're going to have all of the properties that we want to filter by. So the first one is going to be ID. And the trick here is to be option, opvars, and int64. That will be coming from our ModQL filter module. Then I'm going to add an empty line. I like to have the keys on top and then an empty line and then the other properties. That's just a personal preference. And then we have the title, option, and that will be an opvars string from the same
Speaker 1module filter and then we have done, which is an option and you guessed it, opvarls bool. The reason why there are different types and not just a generic on the same opvarls is because they have different operators. The string has a little bit more operators and the bool is the one that has the least obviously. And now we're going to add our derive and the first one is going to be the filter nodes from modql-filter. And that will turn this type into a list of filter
Speaker 1nodes that then can be composed and move into a C query. Then we're going to add the deserialize. So for all of these types, there is a deserialization that we saw at the beginning to have the dollar sign, so on, like MongoDB notation. So everything is already implemented for that. And then we're just going to have our default and the debug for convenience. And now we're going to go to our list We're going to add filter, and that will be an option of task filter. For now,
Speaker 1we're just going to have one, and later we're going to see how we can have a vector. And then we have our list options, and that will be an option of list options. And that is a type that comes from ModQL. You just have offset, limit, and order bys. And obviously, you can have your own list options if you want. So now that we have that, we have a couple of things that we need to fix. First, in the test, we're just going to add non-none for now, such as it compiles. The next one is in our
Speaker 1web RPC task RPC, which is calling the task BMC list. We're obviously missing these two arguments, so we're going to put non-none for now. So now the beauty of this system is that everything can be done at the base generic list function. So we're going to pass our filter and we're going to pass our list options. And that obviously won't compile. So we're going to go to our base. And then if we go to our base list function,
Speaker 1we're going to add these two arguments. But now it will be an option of F. And the generic F is going to be bound to into filter groups from modQL filter. So filter nodes are set up in a group. and groups can be composed together. We're going to see why later. So by doing that now, the monomorphization of first will make sure that we can pass any types as filters that implement the filter nodes, because the filter nodes implement into filter groups. So now we're going to
Speaker 1add our list options, and that is simpler because it's a concrete type. So it's just going to be list options from our ModQL filter list options. So now if we press save, We're going to have a compile error at the task.rs. And the reason is because a function expects three generics that we have in the base list, but our list are giving only two generic. So we're missing one. So what we're going to do is add the one and the compiler can infer it. So we can just put underscore. And now that will compile. Now
Speaker 1we could put rather than underscore, we could put task filter, press save, and that will compile as well. But I prefer to let the compiler do the work because then after if we change it, everything will still work as long as the trade bound is still valid. So we can close that. And that is where things become extremely simple and very powerful. We're going to add our condition from filter and then our list options. Now on the filter side, we're going to say if we have some filter,
Speaker 1so we're going to get a F. We are going to have our filters, which is going to be a filter groups, and we're going to do filter into. So that will take a filter. Actually later, we're going to see that it can take a vector filter as well, but it will do a into filter groups. And then we can just do a condition, and that is a C query condition. And now we can just do filters, try into the question mark. And so obviously we have an error here because we have the into C error
Speaker 1that is not implemented for modQL. So we can do that very quickly. We're going to have the modQL into C variant, modQL filter into C error. And now we're going to add our notation from. And then the CID has the display from string to make sure that everything can get serialized to JSON for request logline. And now that we have that in place, we have our C query condition, and we can just do a query condition where, and that will take condition so we just add our
Speaker 1condition and then that's it now for the list option is even simpler we do if we have a list options we will take our list options and we have a method which is apply to cquery that will take one argument which is a mutable reference of select statement so we give our mute reference of query and that's it that compiles and this is pretty cool so now If we go to our task and we can go to our unit test we are going to have actually
Speaker 1two lists one which is a list all so we're going to rename this test list okay to test list all okay making sure to rename the string as well to follow the pattern and then we're going to take that paste it And then rename this one test list by filter okay. Press save, everything should work pretty nicely. And now to really make sure that we can test the filters, we're going to create a little bit more task. So the first one will be 1A, 1B, we're going
Speaker 1to have the 2A, 2B, and we're just going to have a 03. And now we're just going to go down. And one thing that I like to do when I do my development is my test is part of my development cycles. So I actually remove the check for now, and I'm just going to do debug print, and then I'm just going to have a task and then a pretty debug. So now if I run cargo watch dash q quiet dash clear dash execute, and then I do
Speaker 1a test, and then I give just my test function. And then very important, the dash dash dash dash no capture. So this way, we see the print. Sometimes my test is also dash quiet as well. Now, if we run that, we get all of the data. So everything makes sense. Now, in our exec side, what we're going to do is we're going to create a filter and we're going to use cdjson. You can create it also by hand with the obvars, but right now we're just going to do
Speaker 1from JSON. And so we're going to have our cdjson macro and we want a filter on title and we're just going to take our first title over there and then put it here. We don't forget to put a question mark and now we just take the filter over here and we put some filter. Bring back the terminal, you press save and now you should see only one. That's pretty cool already. And the reason why that works is because we have our task filters with our ID, title and done. So for example, we can add done
Speaker 1equal to true And if we press save, we'll have nothing because all of our tasks have false. Now we're going to remove that, go back to square one. And these obvals, string and int and so on, they come with a set of operators that we can have. So when we just put a string like name equal general is the same thing as the $ EQ operator. And then we have all of these operators. And for numbers, is all of them except the contains, the starts and the ends.
Speaker 1So now what we can do is we can take that and the way we put the operators is by putting a JSON object and with, and we're going to have the. a. Press save and then boom, we have two. And now the same property can have all sorts of conditions. They are all going to be end. We're going to see how to do all later. But in this case, we want to ends with. a and contains zero one. If we press save, we're back to one. That is
Speaker 1pretty powerful. But then if we look at the list of our operators, we're going to see that we have a contains any, and that will take an array. So we can say contains any in this array of zero one or zero two. And that give us a two back. And then we could also add the other property of done and do the same things and everything will be a and between them. So now we can take care of our list options. So we're going to do a set it from JSON. We're going to use the same pattern.
Speaker 1We're going to use our from value of JSON. And basically we have limit, which we can put a limit one, for example. We don't forget to put a question mark. And now we're going to give our sum list options, press save, and that will limit the result to one. And then we can also add another property, this is limit offset and order bys, where we can give one or multiple orders. And here we are going to do a descending with exclamation mark, ID. Now if we press save,
Speaker 1we get one, but from the top. Now we can remove our limit one, and then we get our two result, order by higher to lower ID. Now we can also add another property because we have the title, the id, and the done. So we're going to add done true, but that will return an empty list because it's an and between title and done. And in this case, all the tasks are false, so we get an empty list. So let's go back to this one, such as we can get our two tasks. Okay, so that is already
Speaker 1pretty cool, but we can even do more. So let's say that we want everything that has a. a and contains 01 or 02. or anything that has 03, regardless of having a dot A or not. The current way we couldn't because all of that is AND. So what we really would like is to put a vector of task filter and having each task filter being a OR. We'll rename filters to feel good. And then we'll put
Speaker 1our JSON into an array. And then we'll have another task filter with title contains 03. We obviously have a compiler in our task BMC list. So if we go there, the only thing that we need to do is make our option of task filter an option of vect of task filter. We're going to rename filter by filters to feel good. And now the compiler is gone. Now the question is, why does base list still work now that
Speaker 1we changed the type? And that is because our filters is a vector filters And if we look at our base RS list, we see that the only requirement for F is to implement the into filter groups. And it happened that when you annotate your struct with filter nodes, like we did for task filter, then he implements the into filter groups for the task filter, as well as the into filter groups for the vector of task filter.
Speaker 1And each item of vector is interpreted as a all. So we can rename the filter with filters to feel good. And now that we have that, we can press save. And then voila, we have our task. All those are right way. And we have our task zero three. And then we're going to do our assert. So we remove our debug print and we're going to do simple asserts, making sure that the vector is of three, that the
Speaker 1first one ends with zero three, 02.a, 01.a. So we could be fancier, but that would be good enough for now. And so now if we press save, we have everything that works well. I did the dash Q on the test, so that's why we don't see the test name. And then in the clean, we can make it a little bit better, which now we're going to do a list. And for our filter, we're going to have title and start with, and that is a prefix that we are using to all of the titles of this unit test. And we're going to give a known name, For our
Speaker 1list options, we do the await. We're making sure that the length is 5. That is just an extra check. And then we'll have the delete below. Now we press save. Everything should pass. And then a good practice is you stop the cargo watch. And then we're just going to do a cargo test to make sure that we didn't break anything. And everything is good. So now there's another little thing here, which is important for production actually. And that is this list options. The problem here is that we're just executing it only if a list option is
Speaker 1given to us, which means that our select statement will be unbounded because we don't have any limit. So what we want to have is something that will at least give us a default or a maximum list limit. So what we're going to do is we're going to remove the if let In this block, we'll always have a list options and we're going to create our custom function, which is going to be finalized list options. And we're going to give our list options, which this one is the option of list options. And then we're going to
Speaker 1put a question mark. I'm going to show why later. Obviously it doesn't compile. We're going to go on top and we're going to create. our pub finalizeListOptions. We're going to make it public because perhaps other specific BMCs would like to use that as well. And we're going to take our ListOptions, which will be an option of ListOptions, and that will return a result of ListOptions. So now we're going to add a couple of const. One is
Speaker 1going to be our list limit default as 64, and we're going to say 300, and then The other one will be our cost, list limit, max, and that will be a thousand, for example. So if nothing is given, 300 will be given back. But if someone gives more than a thousand, we will complain back. That's why we have the result. If we go back to our function, we're going to have two blocks, when we have some or when we have none. So when we have none,
Speaker 1it's going to be relatively easy. We're going to return the list options with our limit, the offset, will be none. And then we're going to have a default order bys of ID. That again, is just a personal preference here that might not work as a default for everybody, but right now we can put this in. And then if we have a list options, we just need to validate the limit. And rather to change the limit to the max, we're going to complain back to educate the API users of what is allowed or not. And so we're going
Speaker 1to have to create this error later. And then if the list option provided do not have a limit, we're going to put the least limit default. That is the way it works. And then at the end, we're going to do our okay list options. There's probably other way here to write this function. It doesn't really matter. Write it with your style. Now, if we press save, our least limit over max does not exist. And so we're going to add that. And again, I'm following my pattern here, which is reserved to have tuples. I like to have
Speaker 1name properties such as When we are doing a debug or we serialize, we see exactly what's going on and we don't need to have a documentation or even we don't need to have formatting when it's only for the developer. And then we could do a quick cargo test. Everything is working. I've done it. And that will conclude our chapter two. So now we have done the hardest. In chapter three, we just need to update our RPC layer and have some fun with our quick dev. So first, let's go to our web RPC layer and we're going
Speaker 1to go to our params.rs. Those are the types for the JSON RPC params. And so we have the params for create, when we want to create an entity. We have the params for update, that takes the ID and the data. We have the params ID, so that is for the get or for the delete, when we just need to have the ID. And now we're going to create our params list. We're going to have the deserialize because it will come from our JSON RPC calls. So
Speaker 1it has to be deserialized from JSON. Then we're going to add our list options. And that is a simplest because it's a concrete type. So that will be option of list options from modQL filter. And that already implement deserialize. So we are all okay. And then we're going to add our filters property. And that would be an option of vect of We could put task filter, but that will defeat the purpose because in this file, it has
Speaker 1to be generic because we don't know which entity. So we're going to do a vector of F and we're going to declare our generic F over there. And now there's one thing that will be very cool. When we had our parents filters, we want to make sure that we support a single object like that, a single filter, or We want also to make sure that filters can take an array, which would mean a vector of filters. So to do that, there's a little trick in SerD,
Speaker 1and that is by using the SerD as from SerD with, which is an awesome crate for SerD. And then we're going to add a SerD as for filters, property, deserialize as, option, one or many. That is a trick. Now we need to do the import. So first we are going to import CDAs from CDWith. Then we're going to see this error, which is basically cannot find one or many, and we cannot import it automatically because it's a text. So what
Speaker 1we're going to do is we're going to copy it, go on top of the same file and where we have the CDAs, we're going to add one or many. Then we press save and we have one last error. which is a famous deserialize lifetime business. And when you have that, the simplest way is to have f deserialize on from CRD. And that's it. So this one compiles. Now these two properties, we are going to put them public because we want to make sure that we can
Speaker 1get the value out in other modules. And then the next step is to go to our task RPC and where we do a list. Now, like the other RPC functions, We can add params list, the one we just created, and then type will be task filter. That would be the concrete type because we're on the concrete module. And then in our task BMC list, we're going to pass the params filters and the params list options. Press save.
Speaker 1And now we just have few errors in the mode.rs. So we're going to go there. And if we open our routes, we're going to see that we're going to have some cryptic errors over there. So that is what happened with dynamic handlers. Sometimes you get some cryptic ones, but you have to go down and look at the handlers and usually it becomes obvious. So if we go down and we're going to go down directly to the underscore RPC handler, which is called from the RPC handler, we're going to see that where we do the routing
Speaker 1on this task with our little macro over there, We didn't give the RPC params and obviously this function requires one so it doesn't compile. So now we're going to add the RPC params. And by the way, in the next episode, it's going to be very cool. We're going to do a RPC router based on the same pattern than Axiom and BV with a dynamic handler binding. So it's going to be pretty cool. But right now, this works pretty well for now. And now that's it. So we can have some fun in our examples. So we're going to go to our quick dev. And the first
Speaker 1thing we're going to do is we're going to go to our list where we make the list call and make sure to add a params empty. Because right now it has to have a params. We're going to solve that in the next episode with our dynamic RPC router. But right now, this is a little tricky. And now we're going to bring our first terminal and we're going to run cargo watch, quiet, clear. And we're going to watch two folders, the source and the. cargo for the environment variables. And then we execute run quiet. That will
Speaker 1run our server. And every time we press save on our source file, it will reload the server. So that's the way I'm using it, such as I can reload very quickly without going to terminals. And we can let that running on the background. And then we're going to do the same thing with our cargo watch, but now for examples. So we're just going to watch the example folder and we're going to run quiet example quick dev. We press enter and then boom, we get everything. And for now, we just have one because that is what
Speaker 1we had from our last episode where we created only one and then we are renaming it. And if you press save multiple times, it will continue to add. So we can make it a little bit more interesting is we can go back up and we're going to actually create multiple tasks. We're going to keep track of the IDs on task IDs vector. We're going to put our create task call to a loop. of five element starting at zero. And then in the title, we're just going to do a format and we're just going to add
Speaker 1the index. So like this, we can keep track. Then there's another cool stuff of HTTPC, which is now we can just take the result and now HTTPC supports the JSON value with with JSON pointer spec, which is supported by CRD, so it's just a pass-through, and that will return the I64. So that will keep track of all of the IDs that we're creating in this call. Then, in the update, we're just going to update the first task, and now we know exactly the ID, so we don't
Speaker 1need to hard-code it anymore. We're going to say task IDs, zero. For the delete, we're going to delete the second task, And now we don't need to hard code the ID. We can do task IDs one. So that will be our second task. And then we can do our list task with filters. And so that is where we're going to have some fun. So now if we press save in one of our source file, and then in the quick dev, such as we reset everything from scratch, we're
Speaker 1going to get our four elements. And that is because at the middle, we deleted one. Now, the first thing we can notice is the first one is ID is 10002. The last one is 1000. And then we have 10003, 10004 at the middle. So the order is all over the place. So one thing we can add is a list options. And we are going to do our order by descending with exclamation mark ID. Press save in the source file and in the quick dev. And then we
Speaker 1have all of the data. We still have all our list, but ordered the right way. Now we can have fun with our filters. And the first one is going to be our title. And we're going to do and with bb. Press save. And that will give us one task bb. By the way, if you press save on the quick dev and not on your source file first, it will keep adding because that is a way we have the code running. Now we could do actually a loop below to do a for loop of deleting now that we
Speaker 1have the IDs. That would be OK as well. But so far, that is working pretty well. So we're going to press save on the source file, save on quick dev, and now we are reset to square one. And now the real power is we can select our filter, making it an array, and then on the second task filter, so now that will be our OR between each one, we're going to do ID, and we're going to give our third ID. Press save, and then we have our task, 1002,
Speaker 1in addition of the task BB. And as we showed at the beginning, we can select that and make that even an operator in, and we can put that in an array. and we're going to do the fourth one press save and then we have our three items and that is pure power And in the next production coding episode, we're going to upgrade our little RPC routing macro to a full-fledged dynamic RPC routing using the same dynamic handler
Speaker 1pattern used in Tower, Axum, or BV. And that is going to be awesome. Don't forget to like and subscribe. Until next one, happy coding.
In this episode, we continue our exploration of nine essential Rust programming best practices, focusing on utilities, practical examples, and software testing. Building reliable Rust applications requires more than...
In this comprehensive tutorial, we explore how to build database-driven applications using Rust and SQLite, covering essential database concepts and practical implementation techniques. We take a hands-on approach to...
Join Jeremy Chone for a live-stream session dedicated to Rust programming, software development, and practical engineering discussions. This session provides an opportunity to explore Rust development through a live...
In this episode, we explore data access in Rust web application development, focusing on how backend applications interact with databases to store, retrieve, and manage information. Data access is a critical component...