How to Retrieve Existing StreamID for a Model Instance Without Using createInstance?

Hello @spencer, one question from your reply here and opening new post as it can be useful for others.

I’m using Ceramic One and I’m able to successfully create models and instances.

For models with accountRelation set to "single" or "set", I understand that each user can create one instance (or one per unique set of fields, in the case of "set"). When creating an instance with modelInstanceClient.createInstance, I get a streamID—even if the instance already exists. This is useful for me, because I could get the updated instance/document information easily having this streamID by using getDocumentState method.

Is there a recommended way to retrieve the existing streamID for a model instance without calling createInstance again?

Because I know that calling createInstance will return the streamID which is what I want, but I would expect different method than createInstance, maybe something like getInstance?

Or I must use flight-sql api to construct SQL query and retrieve the information? (using createInstance I don’t need to use flight-sql query to get the streamID, so is faster and simpler at first sight from my point of view, but maybe I am wrong at some point).

Just want to make sure I’m not misunderstanding or misusing the API.

Thanks a lot!

I don’t think the SDK has a function for this currently, but with single and set streams it’s possible to calculate the StreamID client-side without any interaction to the server based entirely on the initial content.

This function should give you a decent idea of how this could work for an instance of a stream with the single account relation: rust-ceramic/sdk/packages/model-instance-client/src/client.ts at 7e928abfcf4cd7f99d8acdd408afe291b5238e05 · ceramicnetwork/rust-ceramic · GitHub

You could just replace the line const cid = await this.ceramic.postEventType(InitEventPayload, event) with a line that calculates the CID client-side from the event payload, which you should be able to do using the cid library and the DAG_JOSE codec.

1 Like