Custom SwiftData DataStore

In the sample code below I am reading and writing ProductRecord model objects to PostgreSQL using a custom DataStore.

However it is unclear to me how I can set this up such that I am able to also write other model object types to the same custom DataStore.

Currently the ProductPostgresSnapshot is specific to the ProductRecord and the PostgresSwiftDataStore Snapshot refers to this ProductPostgresSnapshot.

Any idea how I would change this to accommodate different model object types such as SupplierRecord ?

Full console program shown below.

OK I have made some progress by creating a snapshot that allows different payload types and this seems to work for creating model objects but fails when fetching objects.

I seem to have found a solution for the problem by customising the snapshots encode(to:) function.

Hope it saves someone some time as I wasn't able to find any working examples.

See attached code

Custom SwiftData DataStore
 
 
Q