Skip to content

Commit 8955aa1

Browse files
authored
Prep for 0.8 (#325)
1 parent ba06317 commit 8955aa1

File tree

5 files changed

+34
-41
lines changed

5 files changed

+34
-41
lines changed

Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ members = [
1818
edition = "2021"
1919
license = "MIT OR Apache-2.0"
2020
repository = "https://github.com/Electron100/butane"
21-
version = "0.7.0"
21+
version = "0.8.0"
2222

2323
[workspace.dependencies]
2424
async-trait = "0.1"
25-
butane = { version = "0.7", path = "butane" }
25+
butane = { version = "0.8", path = "butane" }
2626
butane_cli = { path = "butane_cli" }
27-
butane_core = { version = "0.7", path = "butane_core" }
28-
butane_codegen = { version = "0.7", path = "butane_codegen" }
27+
butane_core = { version = "0.8", path = "butane_core" }
28+
butane_codegen = { version = "0.8", path = "butane_codegen" }
2929
butane_test_helper = { path = "butane_test_helper", default-features = false }
3030
butane_test_macros = { path = "butane_test_macros" }
3131
cfg-if = "^1.0"

docs/getting-started.md

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ deliberately follow the same goal as [Diesel's getting-started
77
guide](https://diesel.rs/guides/getting-started/): building the
88
database 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+
1014
Let'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

2327
This 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
6569
the 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

8992
1. 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

10197
The `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
}
127121
impl 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

143137
Each 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

147145
The Tag model itself is trivial
148146

@@ -155,10 +153,7 @@ pub struct Tag {
155153
}
156154
impl 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

227222
The `butane::prelude::*` import brings some common Butane traits into
228223
scope. 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

232226
We don't need to create a new blog every time, if we have an existing
233227
one 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
504498
to 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();

examples/getting_started/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//! Common helpers for the getting_started example CLI.
22
3-
#![deny(missing_docs)]
4-
53
pub mod butane_migrations;
64
pub mod models;
75

86
use butane::db::{Connection, ConnectionSpec};
97
use butane::migrations::Migrations;
108
use butane::prelude::*;
9+
// If you prefer not to use the prelude, you could use the following instead.
10+
// use butane::{query::QueryOpsSync, DataObjectOpsSync, DataResult};
1111
use models::{Blog, Post};
1212

1313
/// Load a [Connection].

examples/getting_started/src/models.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Models for the getting_started example.
22
3-
use butane::AutoPk;
4-
use butane::{model, ForeignKey, Many};
3+
use butane::{model, AutoPk, ForeignKey, Many};
54

65
/// Blog metadata.
76
#[model]
@@ -16,8 +15,8 @@ impl Blog {
1615
/// Create a new Blog.
1716
pub fn new(name: impl Into<String>) -> Self {
1817
Blog {
18+
id: AutoPk::uninitialized(),
1919
name: name.into(),
20-
..Default::default()
2120
}
2221
}
2322
}

0 commit comments

Comments
 (0)