Module zino_core::orm

source ·
Available on crate feature orm only.
Expand description

Database schema and ORM.

§Supported database drivers

The following optional features are available:

Feature flagDescriptionDefault?
orm-mariadbEnables the MariaDB database driver.No
orm-mysqlEnables the MySQL database driver.No
orm-postgresEnables the PostgreSQL database driver.No
orm-sqliteEnables the SQLite database driver.No
orm-tidbEnables the TiDB database driver.No

§Design references

The design of our ORM is inspired by Mongoose, Prisma, TypeORM and PostgREST.

use zino_core::{model::{Mutation, Query}, orm::Schema, Map, Record};

// Constructs a model `Query` with JSON expressions.
let query = Query::new(json!({
    "$or": [
        {
            "roles": "worker",
            "visibility": "Public",
        },
        {
            "roles": { "$in": ["admin", "auditor"] },
            "visibility": { "$ne": "Public" },
        },
    ],
    "status": { "$nin": ["Deleted", "Locked"] },
}));

// Constructs a model `Mutation` with JSON expressions.
let mut mutation = Mutation::new(json!({
    "status": "Active",
    "refreshed_at": DateTime::now(),
    "$inc": { "refresh_count": 1 },
}));

// Updates the models using `update_many` provided by the `Schema` trait.
let ctx = User::update_many(&query, &mut mutation).await?;
ctx.emit_metrics("user_refresh");

// Constructs a model `Query` with projection fields.
let mut query = Query::new(json!({
    "project.start_date": { "$le": "2023-10-07" },
    "project.end_date": { "$ge": "2023-10-01" },
    "task.status": "Completed",
}));
query.allow_fields(&[
    "task.id",
    "task.name",
    "task.status",
    "task.project_id",
    "project.start_date",
    "project.end_date",
]);
query.order_desc("task.updated_at");

// Performs a LEFT OUTER JOIN using `lookup` provided by the `Schema` trait.
let entries = Task::lookup::<Project, Map>(&query, &[("project_id", "id")]).await?;

// Executes the raw SQL with interpolations `${param}` and argument bindings `#{param}`.
let sql =
    "SELECT u.id, u.name, u.tags, t.id, t.name \
        FROM ${user_table} u INNER JOIN ${tag_table} t \
            ON t.id = ANY(u.tags) AND t.category = #{category};";
let params = json!({
    "user_table": User::table_name(),
    "tag_table": Tag::table_name(),
    "category": "Rustacean",
});
let records = User::query::<Record>(sql, params.as_object()).await?;

§Query operators

NameMySQLPostgreSQLSQLite
$andANDANDAND
$orOROROR
$notNOTNOTNOT
$randrand()random()abs(random())
$textmatch() against()to_tsvector()MATCH
$ovlpoverlaps()OVERLAPSN/A
$eq===
$ne<><><>
$lt<<<
$le<=<=<=
$gt>>>
$ge>=>=>=
$inINININ
$ninNOT INNOT INNOT IN
$betwBETWEEN ANDBETWEEN ANDBETWEEN AND
$likeLIKELIKELIKE
$ilikeILIKEILIKEN/A
$rlikeRLIKE~*REGEXP
$globN/AN/AGLOB
$isISISIS
$sizejson_length()array_length()json_array_length()

Structs§

Traits§

Functions§

  • decodeorm-sqlx
    Decodes a single value as T for the field in a row.
  • decode_arrayorm-sqlx
    Decodes a single value as Vec<T> for the field in a row.
  • Decodes a single value as Decimal for the field in a row.
  • decode_uuidorm-sqlx
    Decodes a single value as Uuid for the field in a row.

Type Aliases§