Expand description
A read-only implementation of reading Realm database files in Rust.
Note that not all features, and in particular all column types, are supported. But for the cases where you only need to read data from a Realm file in Rust, within the current limitations of this library, it may be a more ergonomic alternative than interacting with the C++ SDK.
§Loading a Realm file
use realm_db_reader::{Group, Realm};
let realm = Realm::open("my-database.realm").unwrap();
let group = realm.into_group().unwrap();
At this point, you have a Group
instance, which is the root of the Realm
database. You can use it to access the tables and columns within the loaded file.
§Reading data from tables
let table = group.get_table_by_name("col_MyObjects").unwrap();
// or:
let table = group.get_table(my_objects_table_index).unwrap();
// Tables allow you to load all rows, a given row based on its index, or
// look up a row by an indexed column.
// Here, we'll check the total number of rows in the table, and load the
// middle one.
let row_count = table.row_count().unwrap();
let middle_row = table.get_row(row_count / 2).unwrap();
let row = table.get_row(0).unwrap();
dbg!(row);
As mentioned, if the table you’re interacting with has an indexed column, you can find a row by a known value:
let table = group.get_table_by_name("col_MyObjects").unwrap();
let row = table.find_row_from_indexed_column("id", &some_id.into()).unwrap();
dbg!(row);
Here, row
, will be the first row matching the given value. If no rows
match, Option::None
will be returned.
§Mapping rows to structs
Row
values are relatively low-level, so you may want to map them to your
struct for convenience. You can use the realm_model
macro for this.
struct MyObject {
id: String,
name: String,
}
realm_model!(MyObject => id, name);
let my_object: MyObject = row.try_into().unwrap();
Check the macro documentation for more details.
Macros§
- realm_
model - Macro to implement conversion from a Row to a Realm model struct. This allows for easy creation of your own struct instances, based on data retrieved from a Realm database.
Structs§
- Backlink
- A backlink to one or more rows in a given table. This is the opposite end of
a
Link
. Note thatrow_numbers
is guaranteed to be non-empty. An empty backlink would be represented asValue::None
. - Group
- The group is the central root of a Realm database. It contains all the tables and their names.
- Link
- A link to a single row in a given table.
- Realm
- A reference to a Realm database.
- Row
- A single row in a Realm table. This allows you to either extract
Value
s manually, or userealm_model!
to convert them into your own structs. - Table
- A view into a single Realm database table.
Enums§
- Value
- A single value from a Realm database. Represents one row in one column.
Traits§
- Column
- A column for a table.