Hello everyone!
# spicedb
m
Hello everyone! We're diving into SpiceDb and currently focused on syncing relationships from various data sources. Our approach involves an initial "full sync" from relevant databases, creating relationships. Subsequently, we aim to perform continuous updates by handling single new relationships as they occur in our databases. However, we're encountering a challenge during the full sync, where fetching data from an API and creating relationships works fine initially. Still, on subsequent triggers, it fails due to attempting to create an already existing relationship. To address this, we've devised a strategy to fetch all existing relationships of a specific type and then appropriately skip existing ones, delete, or create single relationships. We are currently getting stuck at just reading all the existing relationships of a type… Here's an overview: We've defined two entities in our schema -
team
and
employee
. The
team
has a "member" relation with
employee
. Our goal is to read relationships for a specific resource, where the relation is "member," essentially fetching all employees that are members of a particular team. We've crafted a function utilizing the SpiceDb API to read relationships. The function, responsible for reading existing relationships, follows these steps: - Defining an
ObjectReference
for the entity using the
v1.ObjectReference.create
method. - Creating a
ReadRelationshipsRequest
specifying details about the relationship to be read. It includes the
ObjectReference
and sets the relation to "member" - gRPC Invoking
client.readRelationships
method with the defined
readRelationshipsRequest
. Currently, we get the error: "Cannot read properties of undefined (reading 'create')." Is there any documentation available on the correct procedure for this, or do you have suggestions on where I might be going wrong? It's worth mentioning that I'm working with Node.js. Thank you!
v
have you considered using
TOUCH
instead of
CREATE
in
WriteRelationships
?
m
RelationshipUpdate_Operation.TOUCH
works like a charm!! 😅 Thanks! I am still eager to learn more about reading already existing relationships. Do you have any best practices on how to achieve that?
v
The ReadRelationships API supports being provided with a RelationshipFilter (https://buf.build/authzed/api/docs/main:authzed.api.v1#authzed.api.v1.RelationshipFilter). You can basically read relations after any relation in your schema. I see we don't have examples for read relationships in the node client - feel free to open an issue: https://github.com/authzed/authzed-node/blob/main/examples/v1/example.js The filter that you pass would have to look roughly like this: - resource_type:
team
- optional_resource_id:
<the_id_of_the_team>
- optional_relation:
member
- optional_subject_filter: { subject_type:
employee
}