-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for foreign keys #8
Comments
I'm not quite sure what you're imagining, is it something like the following data model: struct Project {
owner: User,
name: String
}
struct User {
id: String,
name: String,
} And you would like to reconcile this into a document with something like the following structure: {
"projects": {
"id1": {
"name": "Some project",
"owner": "user1"
}
},
"users": {
"user1": {
"name": "Some user",
"id": "user1"
}
}
} I.e. the reconciliation for the user within the |
Indeed. |
So you can achieve this on reconciliation with the current API: use automerge_test::{assert_doc, map, list};
use autosurgeon::{Reconcile, Reconciler, reconcile_prop};
#[derive(Reconcile)]
struct Project {
#[autosurgeon(reconcile="reconcile_userid")]
owner: User,
name: String
}
fn reconcile_userid<R: Reconciler>(u: &User, mut reconciler: R) -> Result<(), R::Error> {
reconciler.str(&u.id)
}
#[derive(Clone, Reconcile)]
struct User {
id: String,
name: String,
}
fn main() {
let mut doc = automerge::AutoCommit::new();
let users = vec![User{
id: "user1".to_string(),
name: "some user".to_string(),
}];
let projects = vec![Project{
owner: users[0].clone(),
name: "some project".to_string(),
}];
reconcile_prop(&mut doc, automerge::ROOT, "projects", &projects).unwrap();
reconcile_prop(&mut doc, automerge::ROOT, "users", &users).unwrap();
assert_doc!(
doc.document(),
map!{
"projects" => { list! {
{ map! {
"name" => { "some project" },
"owner" => { "user1" },
}}
}},
"users" => { list! {
{ map! {
"id" => { "user1" },
"name" => { "some user" },
}}
}}
}
);
} This is great but you can't |
But this means no transitive / cascading persistence, which I guess might not be such a problem...
That of course makes the whole thing less ergonomic to use, and we still end up kicking the problem to the application layer, which seems worse to me... |
My initial design goals for this library were not to support arbitrary mappings of domain logic to an automerge document, but to make expressing the semantics of automerge data types in Rust easy. Much as It seems to me that what you are interested in are properties of the entire document rather than of individual types. I think it would be interesting to explore how we might express those but my gut says that it would be better to think about doing that in some other trait (I'm imagining a function similar to What do you think? Do you have an idea of what an API for describing these constraints should look like? |
Thanks for your insight.
Unfortunately I don't think I'm at the level of understanding to be able to give any constructive input here. |
If I understand correctly, there are plans (or it's at least being considered) to allow storing the If so, I wonder if it's possible to reuse that Or perhaps would it even be possible to support foreign keys in |
I'd like to be able to reconcile / hydrate a related object as a foreign key in the document.
Currently, I'm running into a roadblock in the API:
In
Reconcile::reconcile
I'd need to be able to access thedoc
, to be able to determine theObjId
for the related object, in order to callreconcile_prop
for it.For example:
The text was updated successfully, but these errors were encountered: