![]() ![]() Using the SslMode enum, we opt to make create the connection over plain-text.I want to dynamically load the collection in meteor server when the user logs in, as shown below Accounts. We then create a Connection object that represents our connection to the Postgres database. Based on the Postgres connection information provided during the Preparation section, we can create a database url. We are now ready to setup our database connection. In the vast majority of cases, the name of the trait and the name of the trait we are deriving are the same. You can look to this commit for more details. In order for the rustc_serialize crate not to clash with the code still in the stdlib, the name we derive was modified. ![]() Note: If you are wondering why we derive RustcEncodable to automatically implement the Encoding trait, know that the rustc_serialize crate used to be part of the std library, was deprecated and migrated out to crates.io. If we had used a type that did not already implement Encodable, then the compiler would have thrown an error. As such, the compiler has enough information to automatically implement the RustcEncodable trait for the Order struct. The rustc_serialize crate has already implemeted Encodable for pretty much all the primitive types. The Order struct is just a shell around some primitive Rust types. The RustcEncodable trait will allow us to call json::encode() on our Order struct. Instead, we can use the # attribute and automatically generate the trait implementation for RustcEncodable. We could manually implement the ToJson trait that tells rustc_serialize how to convert an Order struct into json, but I do not want to write code unless I have to. Once the query result has been mapped into an Order struct, we want to serialize that into json. Using that information, we can create the Order struct with the correct types. Our platform offers a secure, collaborative environment for your organizations queries and data-driven insights. The postgres crate provides type correspondence documentation that maps each Postgres type to a Rust type. We need to create an Order struct to map each row to. Our database schema contains an orders table with an order id, an order total, the type of currency that was used and the status of the order. We will be querying the database for orders and then mapping the resulting rows into one more objects. We also will be using the rustc_serialize json module. We will be using the postgres Connection struct and the SslMode enum. Now we will alias which parts of the crates we want to use. The rustc-serialize crate is from early Rust days and the rules around crate names has changed. Also, notice that the crate name rustc-serialize (hyphen) is imported as rustc_serialize (underbar). These two crates are not exporting macros, so we can leave off the # attribute. We need to import the postgres and rustc_serialize crates. We now need to open up src/main.rs and start adding our dependencies. Due to this restriction, I have chosen to use rustc-serialize instead. Unfortunately, serde's ability to automatically serialize data structures is only available on Rust nightly (the version of Rust in active development). The future of json serialization is the serde_json crate. Note: The rustc-serialize crate works, but it is not being actively developed. Next time we run cargo build both crates will automatically be downloaded and made available to our webservice. We also need to add the rustc-serialize crate so we can serialize a native Rust data structure into json format. The first thing we need to do is update our Cargo.toml file with our postgres dependency. Crate DependenciesĪt this point we have a working database instance with an orders tables containing two rows. I also have a script called db-migrate.sh that will create the orders schema necessary to get this example working. I am using the default values of myapp for username, dbpass for the password and myapp for the database name. Simply clone the git repository and then vagrant up. I am using in combination with Vagrant to automatically provision a working Postgres instance. We have to get Postgres setup before we start writing Rust code. You can skip right to the TL DR for the final solution. This post goes into a fair amount of detail. That being said, the mysql crate looks well done too. There is a pure Rust PostresSQL driver written by Steven Fackler (sfackler) that I think is well done. ![]() I will be using PostgreSQL in this example. The webservice will accept a request for /orders, query the database for orders and return a json response. In this post we are going to hook our basic webservice up to a database. Use the following to set rust 1.18.0 up: $ cd /path/to/project He documents this in a letter to the President of the Royal Society in 1763 6. Note: This blog post does not work with rustc 1.19.0 or later due to a regression in rust 1.19.0. ![]()
0 Comments
Leave a Reply. |