Preview
Preview Feature — This feature is currently in preview and under active development. APIs and functionality may change. We recommend testing thoroughly before using in production.
Working with Relationships
Learn how to create, query, update, and delete relationships between nodes in your graph database. Relationships define connections and enable powerful graph traversals.
Quick Example
- TypeScript
- Java
- Go
- .NET
// Create a relationship
const friendship = await ductape.graph.createRelationship({
type: 'FRIENDS_WITH',
startNodeId: aliceId,
endNodeId: bobId,
properties: {
since: 2020,
closeness: 'high',
},
});
// Find relationships
const friendships = await ductape.graph.findRelationships({
type: 'FRIENDS_WITH',
startNodeId: aliceId,
});
// Update a relationship
await ductape.graph.updateRelationship({
id: friendship.relationship.id,
properties: { closeness: 'very high', lastContact: new Date() },
});
// Create a relationship
Map<String, Object> friendship = ductape.graph.createRelationship(Map.of(
"type", "FRIENDS_WITH",
startNodeId: aliceId,
endNodeId: bobId,
properties: Map.of(
"since", 2020,
"closeness", "high"
)
));
// Find relationships
Map<String, Object> friendships = ductape.graph.findRelationships(Map.of(
"type", "FRIENDS_WITH",
startNodeId: aliceId
));
// Update a relationship
ductape.graph.updateRelationship(Map.of(
id: friendship.relationship.id,
properties: Map.of( "closeness", "very high", lastContact: Instant.now() )
));
// Create a relationship
friendship := client.graph.createRelationship({
"type": "FRIENDS_WITH",
startNodeId: aliceId,
endNodeId: bobId,
properties: {
"since": 2020,
"closeness": "high",
},
});
// Find relationships
friendships := client.graph.findRelationships({
"type": "FRIENDS_WITH",
startNodeId: aliceId,
});
// Update a relationship
client.graph.updateRelationship({
id: friendship.relationship.id,
properties: { "closeness": "very high", lastContact: new Date() },
});
// Create a relationship
var friendship = await ductape.graph.createRelationship({
["type"] = "FRIENDS_WITH",
startNodeId: aliceId,
endNodeId: bobId,
properties: {
["since"] = 2020,
["closeness"] = "high",
},
});
// Find relationships
var friendships = await ductape.graph.findRelationships({
["type"] = "FRIENDS_WITH",
startNodeId: aliceId,
});
// Update a relationship
await ductape.graph.updateRelationship({
id: friendship.relationship.id,
properties: { ["closeness"] = "very high", lastContact: DateTime.UtcNow },
});
Creating Relationships
Basic Relationship Creation
Connect two nodes with a typed relationship:
- TypeScript
- Java
- Go
- .NET
const result = await ductape.graph.createRelationship({
type: 'WORKS_WITH',
startNodeId: aliceId,
endNodeId: bobId,
properties: {
team: 'Engineering',
since: 2023,
role: 'colleague',
},
});
console.log('Created relationship ID:', result.relationship.id);
console.log('Type:', result.relationship.type);
console.log('Properties:', result.relationship.properties);
Map<String, Object> result = ductape.graph.createRelationship(Map.of(
"type", "WORKS_WITH",
startNodeId: aliceId,
endNodeId: bobId,
properties: Map.of(
"team", "Engineering",
"since", 2023,
"role", "colleague"
)
));
System.out.println('Created relationship "ID", ", result.relationship.id);
System.out.println(""Type", ", result.relationship.type);
System.out.println("Properties:', result.relationship.properties);
result := client.graph.createRelationship({
"type": "WORKS_WITH",
startNodeId: aliceId,
endNodeId: bobId,
properties: {
"team": "Engineering",
"since": 2023,
"role": "colleague",
},
});
fmt.Println('Created relationship "ID": ", result.relationship.id);
fmt.Println(""Type": ", result.relationship.type);
fmt.Println("Properties:', result.relationship.properties);
var result = await ductape.graph.createRelationship({
["type"] = "WORKS_WITH",
startNodeId: aliceId,
endNodeId: bobId,
properties: {
["team"] = "Engineering",
["since"] = 2023,
["role"] = "colleague",
},
});
Console.WriteLine('Created relationship ["ID"] = ", result.relationship.id);
Console.WriteLine("["Type"] = ", result.relationship.type);
Console.WriteLine("Properties:', result.relationship.properties);
Relationship Types
Use descriptive, UPPERCASE names for relationship types:
- TypeScript
- Java
- Go
- .NET
// Social relationships
await ductape.graph.createRelationship({
type: 'FRIENDS_WITH',
startNodeId: user1Id,
endNodeId: user2Id,
properties: { since: 2020 },
});
// Hierarchical relationships
await ductape.graph.createRelationship({
type: 'MANAGES',
startNodeId: managerId,
endNodeId: employeeId,
properties: { since: new Date('2023-01-01') },
});
// Ownership relationships
await ductape.graph.createRelationship({
type: 'OWNS',
startNodeId: userId,
endNodeId: productId,
properties: { purchasedAt: new Date(), price: 299.99 },
});
// Action relationships
await ductape.graph.createRelationship({
type: 'LIKED',
startNodeId: userId,
endNodeId: postId,
properties: { timestamp: new Date() },
});
// Social relationships
ductape.graph.createRelationship(Map.of(
"type", "FRIENDS_WITH",
startNodeId: user1Id,
endNodeId: user2Id,
properties: Map.of( "since", 2020 )
));
// Hierarchical relationships
ductape.graph.createRelationship(Map.of(
"type", "MANAGES",
startNodeId: managerId,
endNodeId: employeeId,
properties: Map.of( since: new Date('2023-01-01') )
));
// Ownership relationships
ductape.graph.createRelationship(Map.of(
"type", "OWNS",
startNodeId: userId,
endNodeId: productId,
properties: Map.of( purchasedAt: Instant.now(), "price", 299.99 )
));
// Action relationships
ductape.graph.createRelationship(Map.of(
"type", "LIKED",
startNodeId: userId,
endNodeId: postId,
properties: Map.of( timestamp: Instant.now() )
));
// Social relationships
client.graph.createRelationship({
"type": "FRIENDS_WITH",
startNodeId: user1Id,
endNodeId: user2Id,
properties: { "since": 2020 },
});
// Hierarchical relationships
client.graph.createRelationship({
"type": "MANAGES",
startNodeId: managerId,
endNodeId: employeeId,
properties: { since: new Date('2023-01-01') },
});
// Ownership relationships
client.graph.createRelationship({
"type": "OWNS",
startNodeId: userId,
endNodeId: productId,
properties: { purchasedAt: new Date(), "price": 299.99 },
});
// Action relationships
client.graph.createRelationship({
"type": "LIKED",
startNodeId: userId,
endNodeId: postId,
properties: { timestamp: new Date() },
});
// Social relationships
await ductape.graph.createRelationship({
["type"] = "FRIENDS_WITH",
startNodeId: user1Id,
endNodeId: user2Id,
properties: { ["since"] = 2020 },
});
// Hierarchical relationships
await ductape.graph.createRelationship({
["type"] = "MANAGES",
startNodeId: managerId,
endNodeId: employeeId,
properties: { since: new Date('2023-01-01') },
});
// Ownership relationships
await ductape.graph.createRelationship({
["type"] = "OWNS",
startNodeId: userId,
endNodeId: productId,
properties: { purchasedAt: DateTime.UtcNow, ["price"] = 299.99 },
});
// Action relationships
await ductape.graph.createRelationship({
["type"] = "LIKED",
startNodeId: userId,
endNodeId: postId,
properties: { timestamp: DateTime.UtcNow },
});
Relationships with Properties
Relationships can store rich metadata:
- TypeScript
- Java
- Go
- .NET
const result = await ductape.graph.createRelationship({
type: 'PURCHASED',
startNodeId: customerId,
endNodeId: productId,
properties: {
// Transaction details
orderId: 'ORD-2024-12345',
quantity: 2,
price: 599.98,
discount: 50,
finalPrice: 549.98,
// Timestamps
purchasedAt: new Date(),
deliveredAt: null,
// Status
status: 'pending',
paymentMethod: 'credit_card',
// Additional metadata
notes: 'Gift wrapped',
giftMessage: 'Happy Birthday!',
},
});
Map<String, Object> result = ductape.graph.createRelationship(Map.of(
"type", "PURCHASED",
startNodeId: customerId,
endNodeId: productId,
properties: Map.of(
// Transaction details
"orderId", "ORD-2024-12345",
"quantity", 2,
"price", 599.98,
"discount", 50,
"finalPrice", 549.98,
// Timestamps
purchasedAt: Instant.now(),
deliveredAt: null,
// Status
"status", "pending",
"paymentMethod", "credit_card",
// Additional metadata
"notes", "Gift wrapped",
"giftMessage", "Happy Birthday!"
)
));
result := client.graph.createRelationship({
"type": "PURCHASED",
startNodeId: customerId,
endNodeId: productId,
properties: {
// Transaction details
"orderId": "ORD-2024-12345",
"quantity": 2,
"price": 599.98,
"discount": 50,
"finalPrice": 549.98,
// Timestamps
purchasedAt: new Date(),
deliveredAt: null,
// Status
"status": "pending",
"paymentMethod": "credit_card",
// Additional metadata
"notes": "Gift wrapped",
"giftMessage": "Happy Birthday!",
},
});
var result = await ductape.graph.createRelationship({
["type"] = "PURCHASED",
startNodeId: customerId,
endNodeId: productId,
properties: {
// Transaction details
["orderId"] = "ORD-2024-12345",
["quantity"] = 2,
["price"] = 599.98,
["discount"] = 50,
["finalPrice"] = 549.98,
// Timestamps
purchasedAt: DateTime.UtcNow,
deliveredAt: null,
// Status
["status"] = "pending",
["paymentMethod"] = "credit_card",
// Additional metadata
["notes"] = "Gift wrapped",
["giftMessage"] = "Happy Birthday!",
},
});
Finding Relationships
Find All Relationships of a Type
- TypeScript
- Java
- Go
- .NET
const result = await ductape.graph.findRelationships({
type: 'FRIENDS_WITH',
});
console.log('Found relationships:', result.relationships.length);
Map<String, Object> result = ductape.graph.findRelationships(Map.of(
"type", "FRIENDS_WITH"
));
System.out.println('Found relationships:', result.relationships.length);
result := client.graph.findRelationships({
"type": "FRIENDS_WITH",
});
fmt.Println('Found relationships:', result.relationships.length);
var result = await ductape.graph.findRelationships({
["type"] = "FRIENDS_WITH",
});
Console.WriteLine('Found relationships:', result.relationships.length);
Find Outgoing Relationships
Find relationships starting from a specific node:
- TypeScript
- Java
- Go
- .NET
const result = await ductape.graph.findRelationships({
type: 'FOLLOWS',
startNodeId: userId,
});
console.log(`User follows ${result.relationships.length} people`);
Map<String, Object> result = ductape.graph.findRelationships(Map.of(
"type", "FOLLOWS",
startNodeId: userId
));
System.out.println(`User follows $Map.of(result.relationships.length) people`);
result := client.graph.findRelationships({
"type": "FOLLOWS",
startNodeId: userId,
});
fmt.Println(`User follows ${result.relationships.length} people`);
var result = await ductape.graph.findRelationships({
["type"] = "FOLLOWS",
startNodeId: userId,
});
Console.WriteLine(`User follows ${result.relationships.length} people`);
Find Incoming Relationships
Find relationships ending at a specific node:
- TypeScript
- Java
- Go
- .NET
const result = await ductape.graph.findRelationships({
type: 'FOLLOWS',
endNodeId: userId,
});
console.log(`User has ${result.relationships.length} followers`);
Map<String, Object> result = ductape.graph.findRelationships(Map.of(
"type", "FOLLOWS",
endNodeId: userId
));
System.out.println(`User has $Map.of(result.relationships.length) followers`);
result := client.graph.findRelationships({
"type": "FOLLOWS",
endNodeId: userId,
});
fmt.Println(`User has ${result.relationships.length} followers`);
var result = await ductape.graph.findRelationships({
["type"] = "FOLLOWS",
endNodeId: userId,
});
Console.WriteLine(`User has ${result.relationships.length} followers`);
Find All Relationships of a Node
Find both incoming and outgoing:
- TypeScript
- Java
- Go
- .NET
const outgoing = await ductape.graph.findRelationships({
startNodeId: userId,
});
const incoming = await ductape.graph.findRelationships({
endNodeId: userId,
});
const total = outgoing.relationships.length + incoming.relationships.length;
console.log(`User has ${total} total relationships`);
Map<String, Object> outgoing = ductape.graph.findRelationships(Map.of(
startNodeId: userId
));
Map<String, Object> incoming = ductape.graph.findRelationships(Map.of(
endNodeId: userId
));
Map<String, Object> total = outgoing.relationships.length + incoming.relationships.length;
System.out.println(`User has $Map.of(total) total relationships`);
outgoing := client.graph.findRelationships({
startNodeId: userId,
});
incoming := client.graph.findRelationships({
endNodeId: userId,
});
total := outgoing.relationships.length + incoming.relationships.length;
fmt.Println(`User has ${total} total relationships`);
var outgoing = await ductape.graph.findRelationships({
startNodeId: userId,
});
var incoming = await ductape.graph.findRelationships({
endNodeId: userId,
});
var total = outgoing.relationships.length + incoming.relationships.length;
Console.WriteLine(`User has ${total} total relationships`);
Find with Filters
Filter relationships by properties:
- TypeScript
- Java
- Go
- .NET
const result = await ductape.graph.findRelationships({
type: 'PURCHASED',
startNodeId: customerId,
where: {
status: 'completed',
finalPrice: { $gt: 100 },
purchasedAt: { $gte: new Date('2024-01-01') },
},
});
Map<String, Object> result = ductape.graph.findRelationships(Map.of(
"type", "PURCHASED",
startNodeId: customerId,
where: Map.of(
"status", "completed",
finalPrice: Map.of( $"gt", 100 ),
purchasedAt: Map.of( $gte: new Date('2024-01-01') )
)
));
result := client.graph.findRelationships({
"type": "PURCHASED",
startNodeId: customerId,
where: {
"status": "completed",
finalPrice: { $"gt": 100 },
purchasedAt: { $gte: new Date('2024-01-01') },
},
});
var result = await ductape.graph.findRelationships({
["type"] = "PURCHASED",
startNodeId: customerId,
where: {
["status"] = "completed",
finalPrice: { $["gt"] = 100 },
purchasedAt: { $gte: new Date('2024-01-01') },
},
});
Find Multiple Relationship Types
- TypeScript
- Java
- Go
- .NET
const result = await ductape.graph.findRelationships({
type: ['FRIENDS_WITH', 'WORKS_WITH', 'LIVES_NEAR'],
startNodeId: userId,
});
Map<String, Object> result = ductape.graph.findRelationships(Map.of(
type: ['FRIENDS_WITH', 'WORKS_WITH', 'LIVES_NEAR'],
startNodeId: userId
));
result := client.graph.findRelationships({
type: ['FRIENDS_WITH', 'WORKS_WITH', 'LIVES_NEAR'],
startNodeId: userId,
});
var result = await ductape.graph.findRelationships({
type: ['FRIENDS_WITH', 'WORKS_WITH', 'LIVES_NEAR'],
startNodeId: userId,
});
Finding Relationships by ID
Single Relationship Lookup
- TypeScript
- Java
- Go
- .NET
const relationship = await ductape.graph.findRelationshipById('rel-id-123');
if (relationship) {
console.log('Type:', relationship.type);
console.log('From:', relationship.startNodeId);
console.log('To:', relationship.endNodeId);
console.log('Properties:', relationship.properties);
}
Map<String, Object> relationship = ductape.graph.findRelationshipById('rel-id-123');
if (relationship) Map.of(
System.out.println('"Type", ", relationship.type);
System.out.println(""From", ", relationship.startNodeId);
System.out.println(""To", ", relationship.endNodeId);
System.out.println("Properties:', relationship.properties);
)
relationship := client.graph.findRelationshipById('rel-id-123');
if (relationship) {
fmt.Println('"Type": ", relationship.type);
fmt.Println(""From": ", relationship.startNodeId);
fmt.Println(""To": ", relationship.endNodeId);
fmt.Println("Properties:', relationship.properties);
}
var relationship = await ductape.graph.findRelationshipById('rel-id-123');
if (relationship) {
Console.WriteLine('["Type"] = ", relationship.type);
Console.WriteLine("["From"] = ", relationship.startNodeId);
Console.WriteLine("["To"] = ", relationship.endNodeId);
Console.WriteLine("Properties:', relationship.properties);
}
With Type Safety
- TypeScript
- Java
- Go
- .NET
interface PurchaseProperties {
orderId: string;
quantity: number;
price: number;
purchasedAt: Date;
}
const purchase = await ductape.graph.findRelationshipById<PurchaseProperties>('rel-id-123');
if (purchase) {
console.log(`Order ${purchase.properties.orderId} - Qty: ${purchase.properties.quantity}`);
}
interface PurchaseProperties Map.of(
orderId: string;
quantity: number;
price: number;
purchasedAt: Date;
)
Map<String, Object> purchase = ductape.graph.findRelationshipById<PurchaseProperties>('rel-id-123');
if (purchase) Map.of(
System.out.println(`Order $Map.of(purchase.properties.orderId) - Qty: $Map.of(purchase.properties.quantity)`);
)
interface PurchaseProperties {
orderId: string;
quantity: number;
price: number;
purchasedAt: Date;
}
purchase := client.graph.findRelationshipById<PurchaseProperties>('rel-id-123');
if (purchase) {
fmt.Println(`Order ${purchase.properties.orderId} - Qty: ${purchase.properties.quantity}`);
}
interface PurchaseProperties {
orderId: string;
quantity: number;
price: number;
purchasedAt: Date;
}
var purchase = await ductape.graph.findRelationshipById<PurchaseProperties>('rel-id-123');
if (purchase) {
Console.WriteLine(`Order ${purchase.properties.orderId} - Qty: ${purchase.properties.quantity}`);
}
Updating Relationships
Update Properties
Add or modify properties on an existing relationship:
- TypeScript
- Java
- Go
- .NET
const result = await ductape.graph.updateRelationship({
id: relationshipId,
properties: {
closeness: 'very high',
lastContact: new Date(),
meetingCount: { $INCREMENT: 1 },
},
});
console.log('Updated:', result.relationship.properties);
Map<String, Object> result = ductape.graph.updateRelationship(Map.of(
id: relationshipId,
properties: Map.of(
"closeness", "very high",
lastContact: Instant.now(),
meetingCount: Map.of( $"INCREMENT", 1 )
)
));
System.out.println('Updated:', result.relationship.properties);
result := client.graph.updateRelationship({
id: relationshipId,
properties: {
"closeness": "very high",
lastContact: new Date(),
meetingCount: { $"INCREMENT": 1 },
},
});
fmt.Println('Updated:', result.relationship.properties);
var result = await ductape.graph.updateRelationship({
id: relationshipId,
properties: {
["closeness"] = "very high",
lastContact: DateTime.UtcNow,
meetingCount: { $["INCREMENT"] = 1 },
},
});
Console.WriteLine('Updated:', result.relationship.properties);
Update by Filter
Update multiple relationships matching criteria:
- TypeScript
- Java
- Go
- .NET
const result = await ductape.graph.updateRelationship({
type: 'PURCHASED',
where: {
status: 'pending',
purchasedAt: { $lt: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000) },
},
properties: {
status: 'expired',
},
});
console.log('Updated relationships:', result.updatedCount);
Map<String, Object> result = ductape.graph.updateRelationship(Map.of(
"type", "PURCHASED",
where: Map.of(
"status", "pending",
purchasedAt: Map.of( $lt: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000) )
),
properties: Map.of(
"status", "expired"
)
));
System.out.println('Updated relationships:', result.updatedCount);
result := client.graph.updateRelationship({
"type": "PURCHASED",
where: {
"status": "pending",
purchasedAt: { $lt: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000) },
},
properties: {
"status": "expired",
},
});
fmt.Println('Updated relationships:', result.updatedCount);
var result = await ductape.graph.updateRelationship({
["type"] = "PURCHASED",
where: {
["status"] = "pending",
purchasedAt: { $lt: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000) },
},
properties: {
["status"] = "expired",
},
});
Console.WriteLine('Updated relationships:', result.updatedCount);
Increment Counters
- TypeScript
- Java
- Go
- .NET
// Track interaction frequency
await ductape.graph.updateRelationship({
id: relationshipId,
properties: {
interactions: { $INCREMENT: 1 },
lastInteraction: new Date(),
},
});
// Track interaction frequency
ductape.graph.updateRelationship(Map.of(
id: relationshipId,
properties: Map.of(
interactions: Map.of( $"INCREMENT", 1 ),
lastInteraction: Instant.now()
)
));
// Track interaction frequency
client.graph.updateRelationship({
id: relationshipId,
properties: {
interactions: { $"INCREMENT": 1 },
lastInteraction: new Date(),
},
});
// Track interaction frequency
await ductape.graph.updateRelationship({
id: relationshipId,
properties: {
interactions: { $["INCREMENT"] = 1 },
lastInteraction: DateTime.UtcNow,
},
});
Deleting Relationships
Delete by ID
- TypeScript
- Java
- Go
- .NET
const result = await ductape.graph.deleteRelationship({
id: relationshipId,
});
console.log('Deleted:', result.deleted);
Map<String, Object> result = ductape.graph.deleteRelationship(Map.of(
id: relationshipId
));
System.out.println('Deleted:', result.deleted);
result := client.graph.deleteRelationship({
id: relationshipId,
});
fmt.Println('Deleted:', result.deleted);
var result = await ductape.graph.deleteRelationship({
id: relationshipId,
});
Console.WriteLine('Deleted:', result.deleted);
Delete by Type and Nodes
Delete specific relationship between two nodes:
- TypeScript
- Java
- Go
- .NET
const result = await ductape.graph.deleteRelationship({
type: 'FRIENDS_WITH',
startNodeId: aliceId,
endNodeId: bobId,
});
Map<String, Object> result = ductape.graph.deleteRelationship(Map.of(
"type", "FRIENDS_WITH",
startNodeId: aliceId,
endNodeId: bobId
));
result := client.graph.deleteRelationship({
"type": "FRIENDS_WITH",
startNodeId: aliceId,
endNodeId: bobId,
});
var result = await ductape.graph.deleteRelationship({
["type"] = "FRIENDS_WITH",
startNodeId: aliceId,
endNodeId: bobId,
});
Delete All Relationships of a Type
- TypeScript
- Java
- Go
- .NET
const result = await ductape.graph.deleteRelationship({
type: 'TEMP_LINK',
where: {
createdAt: { $lt: new Date(Date.now() - 24 * 60 * 60 * 1000) },
},
});
console.log('Deleted relationships:', result.deletedCount);
Map<String, Object> result = ductape.graph.deleteRelationship(Map.of(
"type", "TEMP_LINK",
where: Map.of(
createdAt: Map.of( $lt: new Date(Date.now() - 24 * 60 * 60 * 1000) )
)
));
System.out.println('Deleted relationships:', result.deletedCount);
result := client.graph.deleteRelationship({
"type": "TEMP_LINK",
where: {
createdAt: { $lt: new Date(Date.now() - 24 * 60 * 60 * 1000) },
},
});
fmt.Println('Deleted relationships:', result.deletedCount);
var result = await ductape.graph.deleteRelationship({
["type"] = "TEMP_LINK",
where: {
createdAt: { $lt: new Date(Date.now() - 24 * 60 * 60 * 1000) },
},
});
Console.WriteLine('Deleted relationships:', result.deletedCount);
Delete All Relationships of a Node
- TypeScript
- Java
- Go
- .NET
// Delete all outgoing relationships
await ductape.graph.deleteRelationship({
startNodeId: userId,
});
// Delete all incoming relationships
await ductape.graph.deleteRelationship({
endNodeId: userId,
});
// Delete all outgoing relationships
ductape.graph.deleteRelationship(Map.of(
startNodeId: userId
));
// Delete all incoming relationships
ductape.graph.deleteRelationship(Map.of(
endNodeId: userId
));
// Delete all outgoing relationships
client.graph.deleteRelationship({
startNodeId: userId,
});
// Delete all incoming relationships
client.graph.deleteRelationship({
endNodeId: userId,
});
// Delete all outgoing relationships
await ductape.graph.deleteRelationship({
startNodeId: userId,
});
// Delete all incoming relationships
await ductape.graph.deleteRelationship({
endNodeId: userId,
});
Merge Relationships
Merge creates a relationship if it doesn't exist, or updates it if it does.
Basic Merge
- TypeScript
- Java
- Go
- .NET
const result = await ductape.graph.mergeRelationship({
type: 'FRIENDS_WITH',
startNodeId: aliceId,
endNodeId: bobId,
onCreate: {
since: new Date(),
interactions: 1,
},
onMatch: {
interactions: { $INCREMENT: 1 },
lastInteraction: new Date(),
},
});
if (result.created) {
console.log('Created new friendship');
} else {
console.log('Updated existing friendship');
}
Map<String, Object> result = ductape.graph.mergeRelationship(Map.of(
"type", "FRIENDS_WITH",
startNodeId: aliceId,
endNodeId: bobId,
onCreate: Map.of(
since: Instant.now(),
"interactions", 1
),
onMatch: Map.of(
interactions: Map.of( $"INCREMENT", 1 ),
lastInteraction: Instant.now()
)
));
if (result.created) Map.of(
System.out.println('Created new friendship');
) else Map.of(
System.out.println('Updated existing friendship');
)
result := client.graph.mergeRelationship({
"type": "FRIENDS_WITH",
startNodeId: aliceId,
endNodeId: bobId,
onCreate: {
since: new Date(),
"interactions": 1,
},
onMatch: {
interactions: { $"INCREMENT": 1 },
lastInteraction: new Date(),
},
});
if (result.created) {
fmt.Println('Created new friendship');
} else {
fmt.Println('Updated existing friendship');
}
var result = await ductape.graph.mergeRelationship({
["type"] = "FRIENDS_WITH",
startNodeId: aliceId,
endNodeId: bobId,
onCreate: {
since: DateTime.UtcNow,
["interactions"] = 1,
},
onMatch: {
interactions: { $["INCREMENT"] = 1 },
lastInteraction: DateTime.UtcNow,
},
});
if (result.created) {
Console.WriteLine('Created new friendship');
} else {
Console.WriteLine('Updated existing friendship');
}
Idempotent Operations
Merge is perfect for operations that should be idempotent:
- TypeScript
- Java
- Go
- .NET
// This can be called multiple times safely
async function followUser(followerId: string, followedId: string) {
const result = await ductape.graph.mergeRelationship({
type: 'FOLLOWS',
startNodeId: followerId,
endNodeId: followedId,
onCreate: {
followedAt: new Date(),
active: true,
},
onMatch: {
// Ensure it's active (in case it was unfollowed before)
active: true,
refollowedAt: new Date(),
},
});
return result;
}
// This can be called multiple times safely
async function followUser(followerId: string, followedId: string) Map.of(
Map<String, Object> result = ductape.graph.mergeRelationship(Map.of(
"type", "FOLLOWS",
startNodeId: followerId,
endNodeId: followedId,
onCreate: Map.of(
followedAt: Instant.now(),
"active", true
),
onMatch: Map.of(
// Ensure it's active (in case it was unfollowed before)
"active", true,
refollowedAt: Instant.now()
)
));
return result;
)
// This can be called multiple times safely
async function followUser(followerId: string, followedId: string) {
result := client.graph.mergeRelationship({
"type": "FOLLOWS",
startNodeId: followerId,
endNodeId: followedId,
onCreate: {
followedAt: new Date(),
"active": true,
},
onMatch: {
// Ensure it's active (in case it was unfollowed before)
"active": true,
refollowedAt: new Date(),
},
});
return result;
}
// This can be called multiple times safely
async function followUser(followerId: string, followedId: string) {
var result = await ductape.graph.mergeRelationship({
["type"] = "FOLLOWS",
startNodeId: followerId,
endNodeId: followedId,
onCreate: {
followedAt: DateTime.UtcNow,
["active"] = true,
},
onMatch: {
// Ensure it's active (in case it was unfollowed before)
["active"] = true,
refollowedAt: DateTime.UtcNow,
},
});
return result;
}