Skip to content

How relationships work? #8

@pnewell

Description

@pnewell

How are relationships supposed to work? I have setup a simple example and it doesn't appear that they are actually linked. If I updated a subdocument (linked via HasOne), should any document that has that document id as a child also reference this updated version?

struct Item: Papyrus {
  let id: String
  @HasOne
  var subitem: SubItem
  
  init(id: String = UUID().uuidString, subitem: SubItem) {
    self.id = id
    self.subitem = subitem
  }
}

struct SubItem: Papyrus {
  let id: String
  var name: String
  
  init(id: String = UUID().uuidString, name: String) {
    self.id = id
    self.name = name
  }
}
@main
struct PapyrusExampleApp: App {
  @State var store: PapyrusStore = PapyrusStore()
  
    var body: some Scene {
        WindowGroup {
            ContentView()
            .task {
              var subitem = SubItem(name: "Tom")
              let item = Item(subitem: subitem)
              await store.save(item)
              subitem.name = "Sam"
              await store.save(subitem)
              let updatedItem: Item = try! await store.object(id: item.id).execute()
              let allitems = try! await store.objects(type: SubItem.self).execute()
              let allsubitems = try! await store.objects(type: SubItem.self).execute()
            }
        }
    }
}

With this example, all the SubItems are correctly renamed Sam as expected, but the fresh pull of the Items still show the original value of Tom.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions