Papyrus icon indicating copy to clipboard operation
Papyrus copied to clipboard

How relationships work?

Open pnewell opened this issue 2 years ago • 0 comments

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.

pnewell avatar Jul 17 '23 02:07 pnewell