Module wither::model [] [src]

Interface for defining & using data models.

Model is the central type in this crate. The entire purpose of this create is to simplify the process of interfacing with MongoDB in Rust for patterns which should be simple. This system allows you to define a data model using a normal struct, and then interact with your MongoDB database collections using that struct.

Implementing Model for your custom structs is quite simple.

  • define an associated constant COLLECTION_NAME in your impl which will be the name of the collection where the corresponding model's data will be read from & written to.
  • provide an implementation for the id & set_id methods.

That's it! Now you can easliy perform standard CRUD operations on MongoDB using your models.

sync

Model::sync is an integral component of this system & allows you to delegate a majority of your database administration tasks to your services which are actually using the database. Both of the indexes & migrations systems rely upon this system to function.

This routine should be called once per model, early on at boottime. It will synchronize any indexes defined on this model with the backend & will execute any active migrations against the model's collection.

This routine will destroy any indexes found on this model's collection which are not defined on this model (barring the default index on _id).

indexes

Any collection that you actually plan on reading data from will need some indexes. These are very simple to define in your Model implementation.

use mongodb::coll::options::IndexModel;

// snip ...

// Define any indexes which need to be maintained for your model here.
// Remember to `use mongodb::coll::options::IndexModel;`.
fn indexes() -> Vec<IndexModel> {
    return vec![
        IndexModel{
            keys: doc!{"email" => 1},
            // Args are: name, background, unique, ttl, sparse.
            options: wither::basic_index_options("unique-email", true, Some(true), None, None),
        },
    ];
}

// snip ...

Whenever Model::sync is called, it will synchronize any indexes defined in this method with the database. Any indexes which do not exist in the model definition will be removed (barring the default index on _id).

migrations

See the documentation on the migration module.

Constants

DEFAULT_INDEX

The name of the default index created by MongoDB.

Traits

Model

Model provides data modeling behaviors for interacting with MongoDB database collections.

Functions

basic_index_options

A convenience function for basic index options. Everything else will default to None.