Skip to main content
Version: 0.8.0

Timestamps

Timestamp fields are output-only fields, automatically populated by a schema-level resolver when a record is created or updated.

  • #[created_at] -- set once, at creation.
  • #[updated_at] -- set at creation and on every update.
  • #[optional_updated_at] -- like #[updated_at], but typed Option<T> and only ever set once an update actually happens; stays None until then.
  • A schema may declare zero or one of each. Both use the same shared, synchronous resolver, declared once via #[timestamps(|| ...)] (or a bare function path) on an anonymous const item.
  • Requires output(...) on the schema, since timestamps never appear on the input struct.

Example​

#[updated_at] is set on create and on every update; #[optional_updated_at] stays None until the first update:

main.rs
use ivo::ivo_schema;
use std::sync::atomic::{AtomicU64, Ordering};

// a clock that ticks once per call, so the example's output is predictable (use e.g.
// `chrono::Utc::now` in a real schema)
static CLOCK: AtomicU64 = AtomicU64::new(0);

fn now() -> u64 {
CLOCK.fetch_add(1, Ordering::SeqCst) + 1
}

#[ivo_schema(
input(PostInput, ivo_attrs(derive(Debug, Clone, PartialEq))),
output(Post, ivo_attrs(derive(Debug, Clone, PartialEq)))
)]
mod post_schema {
struct Fields {
#[lax(String::new())]
pub title: String,

#[created_at]
pub created_at: u64,

// set on create, and on every update
#[updated_at]
pub updated_at: u64,
}

#[timestamps(super::now)]
const _: () = ();
}

#[ivo_schema(
input(NoteInput, ivo_attrs(derive(Debug, Clone, PartialEq))),
output(Note, ivo_attrs(derive(Debug, Clone, PartialEq)))
)]
mod note_schema {
struct Fields {
#[lax(String::new())]
pub text: String,

#[created_at]
pub created_at: u64,

// `None` until the first update
#[optional_updated_at]
pub updated_at: Option<u64>,
}

#[timestamps(super::now)]
const _: () = ();
}

fn main() {
use post_schema::{PartialPostInput, PostModel};

let post = PostModel.create(PartialPostInput { title: Some("Hello".into()) }).unwrap();
println!("{:?}", post); // Post { title: "Hello", created_at: 1, updated_at: 1 }

let updated = PostModel
.update(post, PartialPostInput { title: Some("Hello, world".into()) })
.unwrap();
println!("{:?}", updated); // PartialPost { title: Some("Hello, world"), created_at: None, updated_at: Some(2) }

use note_schema::{NoteModel, PartialNoteInput};

let note = NoteModel.create(PartialNoteInput { text: Some("Hi".into()) }).unwrap();
println!("{:?}", note); // Note { text: "Hi", created_at: 3, updated_at: None }

let updated = NoteModel.update(note, PartialNoteInput { text: Some("Hi!".into()) }).unwrap();
println!("{:?}", updated); // PartialNote { text: Some("Hi!"), created_at: None, updated_at: Some(Some(4)) }
}

Example: custom field names, optional updated_at​

Timestamp fields can be named anything -- the attribute, not the field name, is what matters. Use #[optional_updated_at] when "never updated yet" should be a real, distinct None state rather than defaulting to the creation timestamp:

struct Fields {
#[lax("default_username".to_string())]
pub username: String,

#[created_at]
pub inserted_at: Timestamp,

#[optional_updated_at]
pub modified_at: Option<Timestamp>,
}

modified_at is None on the freshly created record, and only becomes Some(...) after the first update.

More examples​

Try it in the browser​

username is a lax field with a default. created_at and updated_at are populated automatically from the timestamp resolver.

Loading playground…