update grid4 & heroledger models
This commit is contained in:
15
heromodels/examples/heroledger_example/README.md
Normal file
15
heromodels/examples/heroledger_example/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# Heroledger Postgres Example
|
||||
|
||||
This example demonstrates how to use the Heroledger `User` model against Postgres using the `heromodels::db::postgres` backend.
|
||||
|
||||
- Connects to Postgres with user `postgres` and password `test123` on `localhost:5432`.
|
||||
- Creates the table and indexes automatically on first use.
|
||||
- Shows basic CRUD and an index lookup on `username`.
|
||||
|
||||
Run it:
|
||||
|
||||
```bash
|
||||
cargo run -p heromodels --example heroledger_example
|
||||
```
|
||||
|
||||
Make sure Postgres is running locally and accessible with the credentials above.
|
54
heromodels/examples/heroledger_example/example.rs
Normal file
54
heromodels/examples/heroledger_example/example.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use heromodels::db::postgres::{Config, Postgres};
|
||||
use heromodels::db::{Collection, Db};
|
||||
use heromodels::models::heroledger::user::user_index::username;
|
||||
use heromodels::models::heroledger::user::{SecretBox, User};
|
||||
|
||||
fn main() {
|
||||
let db = Postgres::new(
|
||||
Config::new()
|
||||
.user(Some("postgres".into()))
|
||||
.password(Some("test123".into()))
|
||||
.host(Some("localhost".into()))
|
||||
.port(Some(5432)),
|
||||
)
|
||||
.expect("Can connect to Postgres");
|
||||
|
||||
println!("Heroledger User - Postgres Example");
|
||||
println!("==================================");
|
||||
|
||||
let users = db.collection::<User>().expect("open user collection");
|
||||
|
||||
// Clean
|
||||
if let Ok(existing) = users.get_all() {
|
||||
for u in existing {
|
||||
let _ = users.delete_by_id(u.get_id());
|
||||
}
|
||||
}
|
||||
|
||||
let sb = SecretBox::new().data(vec![1, 2, 3]).nonce(vec![9, 9, 9]).build();
|
||||
|
||||
let u = User::new(0)
|
||||
.username("alice")
|
||||
.pubkey("PUBKEY_A")
|
||||
.add_email("alice@example.com")
|
||||
.add_userprofile(sb)
|
||||
.build();
|
||||
|
||||
let (id, stored) = users.set(&u).expect("store user");
|
||||
println!("Stored user id={id} username={} pubkey={}", stored.username, stored.pubkey);
|
||||
|
||||
let by_idx = users.get::<username, _>("alice").expect("by username");
|
||||
println!("Found {} user(s) with username=alice", by_idx.len());
|
||||
|
||||
let fetched = users.get_by_id(id).expect("get by id").expect("exists");
|
||||
println!("Fetched by id={} username={} emails={:?}", id, fetched.username, fetched.email);
|
||||
|
||||
// Update
|
||||
let updated = fetched.clone().add_email("work@alice.example");
|
||||
let (_, back) = users.set(&updated).expect("update user");
|
||||
println!("Updated emails = {:?}", back.email);
|
||||
|
||||
// Delete
|
||||
users.delete_by_id(id).expect("delete user");
|
||||
println!("Deleted user id={id}");
|
||||
}
|
Reference in New Issue
Block a user