@@ -7,6 +7,10 @@ deliberately follow the same goal as [Diesel's getting-started
77guide] ( https://diesel.rs/guides/getting-started/ ) : building the
88database portions of a blog.
99
10+ This guide shows a synchronous example, but Butane now supports
11+ async. Code for an async equivalent can be found at
12+ [ examples/getting_started_async] ( https://github.com/Electron100/butane/tree/master/examples/getting_started_async )
13+
1014Let's begin by creating a new rust project
1115
1216``` shell
@@ -17,7 +21,7 @@ In `Cargo.toml`, add a dependency on Butane:
1721
1822``` toml
1923[dependencies ]
20- butane = { version = " 0.7 " , features =[" pg" , " sqlite" ] }
24+ butane = { version = " 0.8 " , features =[" pg" , " sqlite" ] }
2125```
2226
2327This guide will focus on using SQLite initially, and use "pg" for
@@ -65,8 +69,7 @@ yet. Let's define some _models_ for our blog objects (in `src/models.rs`). We'll
6569the Blog itself.
6670
6771``` rust
68- use butane :: prelude :: * ;
69- use butane :: {model, ForeignKey , Many , ObjectState };
72+ use butane :: {model, AutoPk , ForeignKey , Many , ObjectState };
7073
7174#[model]
7275#[derive(Debug , Default )]
@@ -88,14 +91,7 @@ The `#[model]` attribute does the heavy lifting here:
8891
89921 . It generates automatic ` impl ` s of [ ` butane::DataResult ` ] and
9093 [ ` butane::DataObject ` ] .
91- 2 . It adds an additional field ` state: butane::ObjectState ` used to
92- store internal Butane state information. In general, we can ignore
93- this field, but it must be initialized when the struct is
94- constructed and there may not be another field named ` state ` ,
95- although it is acceptable to manually include the `state:
96- ObjectState` field in the struct definition to make its presence
97- more obvious (and rust-analyzer happier).
98- 3 . It tells Butane that instances of this struct should be represented in the
94+ 2 . It tells Butane that instances of this struct should be represented in the
9995 database, recording migration info (more on this later).
10096
10197The ` id ` field is special -- it's the primary key. All models must
@@ -121,28 +117,30 @@ pub struct Post {
121117 pub blog : ForeignKey <Blog >,
122118 pub tags : Many <Tag >,
123119 pub byline : Option <String >,
124- // listed for clarity, generated automatically if omitted
125- state : butane :: ObjectState ,
126120}
127121impl Post {
128122 pub fn new (blog : & Blog , title : String , body : String ) -> Self {
129123 Post {
130- id : - 1 ,
124+ id : AutoPk :: uninitialized () ,
131125 title ,
132126 body ,
133127 published : false ,
134- blog : blog . into (),
135128 tags : Many :: default (),
129+ blog : blog . into (),
136130 byline : None ,
137- state : ObjectState :: default () ,
131+ likes : 0 ,
138132 }
139133 }
140134}
141135```
142136
143137Each post is associated with a single blog, represented by the
144- ` ForeignKey<Blog> ` . Posts and tags, however, have a many-to-many
145- relationship, represented here by ` Many<Tag> ` .
138+ ` ForeignKey<Blog> ` . Here we use ` blog.into() ` to construct the
139+ ` ForeignKey ` . If we had the ` id ` of the blog but not a ` Blog ` object
140+ itself, we could have used ` ForeignKey::from_pk ` instead.
141+
142+ Posts and tags, on the other hand, have a many-to-many relationship, represented
143+ here by ` Many<Tag> ` .
146144
147145The Tag model itself is trivial
148146
@@ -155,10 +153,7 @@ pub struct Tag {
155153}
156154impl Tag {
157155 pub fn new (tag : impl Into <String >) -> Self {
158- Tag {
159- tag : tag . into (),
160- .. Default :: default ()
161- }
156+ Tag { tag : tag . into () }
162157 }
163158}
164159```
@@ -226,8 +221,7 @@ pub fn create_post(conn: &Connection, blog: &Blog, title: String, body: String)
226221
227222The ` butane::prelude::* ` import brings some common Butane traits into
228223scope. If you'd prefer to avoid star-imports, you can import the
229- necessary traits explicitly (in this case `use butane::{DataObject,
230- DataResult};`)
224+ necessary traits explicitly (in this case ` use butane::{query::QueryOpsSync, DataObjectOpsSync, DataResult}; ` )
231225
232226We don't need to create a new blog every time, if we have an existing
233227one we want to reuse it (for simplicity we'll only add one blog in
@@ -504,9 +498,9 @@ Now compiling the code will include the migrations, however we need to update th
504498to use these migrations:
505499
506500``` rust
507- pub fn establish_connection () -> Connection {
508- use butane :: migrations :: Migrations ;
501+ use butane :: migrations :: Migrations ;
509502
503+ pub fn establish_connection () -> Connection {
510504 let mut connection = butane :: db :: connect (& ConnectionSpec :: load (" .butane/connection.json" ). unwrap ()). unwrap ();
511505 let migrations = butane_migrations :: get_migrations (). unwrap ();
512506 migrations . migrate (& mut connection ). unwrap ();
0 commit comments