Getting Started with Turbosql: Your Easy Data Persistence Layer

Jul 16, 2024 | Programming

Turbosql is a versatile and straightforward local data persistence layer backed by SQLite, designed specifically for Rust developers. With features such as schema auto-definition by your Rust structs, automatic schema migrations, and simple basic CRUD (Create, Read, Update, Delete) operations, Turbosql streamlines the way you handle data in your applications.

How to Use Turbosql

Let’s dive right into using Turbosql. Below is a step-by-step guide showcasing its core functionality.

Setting Up Your Rust Struct

First, you need to define a struct that will represent your data. For example, let’s create a `Person` struct:

rust
use turbosql::Turbosql;

#[derive(Turbosql, Default)]
struct Person {
    rowid: Option, // Required member, enforced at compile time
    name: Option,
    age: Option,
    image_jpg: Option>,
}

Think of structuring your data like building a house. Each room is a field in your struct, and Turbosql ensures that no essential pieces are missing from your architectural plan.

Basic Operations

Inserting Data

Inserting a new record into the database is quite simple:

rust
fn main() -> Result<(), Box> {
    let name = "Joe";

    // Insert a row
    let rowid = Person {
        name: Some(name.to_string()),
        age: Some(42),
        ..Default::default()
    }
    .insert()?;

Reading Data

To fetch data, you can use various select methods:

rust
    // SELECT all rows
    let people = select!(Vec)?;

    // SELECT multiple rows with a predicate
    let people = select!(Vec 21)?;

    // SELECT a single row with a predicate
    let mut person = select!(Person WHERE name = "Joe")?;

Updating Data

Updating records is also straightforward:

rust
    // Update based on rowid
    person.age = Some(43);
    person.update()?;

    // Update with manual SQL
    execute!(UPDATE person SET age = 44 WHERE name = "Joe")?;

Deleting Data

Deletion can be done with a simple command:

rust
    execute!(DELETE FROM person WHERE rowid = 1)?;

Automatic Schema Migrations

When you modify a struct, Turbosql automatically creates migration SQL statements to update your database schema. Each time you run the command, the existing schema adapts to reflect your changes:

rust
#[derive(turbosql::Turbosql, Default)]
struct Person {
    rowid: Option,
    name: Option,
}

The structural changes are recorded in a `migrations.toml` file, making your project more maintainable over time.

Troubleshooting Common Issues

Where’s My Data?

If you can’t find your SQLite database file, remember that it is created in a directory returned by directories_next::ProjectDirs::data_dir(). It typically resolves to the appropriate location based on your operating system.

Seeing Unexpected Compiler Errors?

Sometimes re-compiling can help if you’re experiencing weird compiler messages. There are times when the order in which the procedural macros run can throw things out of sync after schema alterations. A quick re-compile often does the trick.

General Tips

  • Your schema migrations are one-way and append-only, so plan your changes accordingly.
  • Avoid deleting any previously applied migrations unless you intend to completely rebuild your database file.
  • If you’re encountering locking issues, check the SQLite busy timeout, which is set to 3 seconds by default.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

Turbosql is an essential tool for any Rust developer looking for an efficient way to manage local data storage with SQLite. With its automatic schema migrations and concise operations, it offers both simplicity and flexibility.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox