RelatePromise provides chainable methods for configuring RELATE operations for graph relationships.
The RelatePromise class provides a chainable interface for configuring RELATE operations to create graph relationships (edges) between records. It extends Promise, allowing you to await it directly or chain configuration methods.
J - Boolean indicating if result is JSON (default: false)
Configuration methods
.unique()
Enforce a unique relationship constraint (only one edge between the same nodes).
Method Syntax
relatePromise.unique()
Returns
RelatePromise<T, J> - Chainable promise
Example
// Only allow one 'likes' edge between user and post constedge=awaitdb.relate( newRecordId('users','john'), newTable('likes'), newRecordId('posts','1') ).unique(); // If edge already exists, this won't create a duplicate
// Use specific ID for the edge constedge=awaitdb.relate( newRecordId('users','john'), newRecordId('likes','specific-edge-id'), newRecordId('posts','1'), {strength:10} );
Fan-out relationships
// One user follows many constedges=awaitdb.relate( newRecordId('users','john'), newTable('follows'), [ newRecordId('users','alice'), newRecordId('users','bob'), newRecordId('users','carol') ] );
// Avoid: Allowing duplicates awaitdb.relate(from,newTable('likes'),to); // May create multiple edges
2. Include metadata
// Good: Track when relationship was created awaitdb.relate(from,edge,to,{ created_at:DateTime.now(), source:'web-app' });
// Basic: No metadata awaitdb.relate(from,edge,to);
3. Use specific edge IDs for updates
// Good: use a specific ID so you can update the edge later constedgeId=newRecordId('likes',[from.toString(),to.toString()]); awaitdb.relate(from,edgeId,to,metadata);
// Later: update by record ID awaitdb.update(edgeId).merge({updated_at:DateTime.now()});
As of SurrealDB 3.1.5, when the edge ID already exists, INSERT RELATION returns an error unless you use ON DUPLICATE KEY UPDATE. See Explicit edge record IDs for more details.