Attachments: Copy, Move, Rename
-
Attachments can be copied, moved, or renamed using built-in session methods.
-
All of those actions are executed when
saveChanges
is called and take place on the server-side,
removing the need to transfer the entire attachment binary data over the network in order to perform the action. -
In this page:
Copy attachment
Use session.advanced.attachments.copy
to copy an attachment from one document to another.
// Load entities
const employee1 = await session.load("employees/1-A");
const employee2 = await session.load("employees/2-A");
// Call method 'copy'
// Copy attachment from employee1 to employee2
session.advanced.attachments.copy(employee1, "photo.jpg", employee2, "photo-copy.jpg");
// Attachment will be copied on the server-side only when saveChanges is called
await session.saveChanges();
// Call method 'copy'
// Copy attachment from "employees/1-A" to "employees/2-A"
session.advanced.attachments.copy("employees/1-A", "photo.jpg", "employees/2-A", "photo-copy.jpg");
// Attachment will be copied on the server-side only when saveChanges is called
await session.saveChanges();
Move attachment
Use session.advanced.attachments.move
to move an attachment from one document to another.
// Load entities
const employee1 = await session.load("employees/1-A");
const employee2 = await session.load("employees/2-A");
// Call method 'move'
// Move attachment from employee1 to employee2
session.advanced.attachments.move(employee1, "photo.jpg", employee2, "photo.jpg");
// Attachment will be moved on the server-side only when saveChanges is called
await session.saveChanges();
// Call method 'move'
// Move attachment from "employees/1-A" to "employees/2-A"
session.advanced.attachments.move("employees/1-A", "photo.jpg", "employees/2-A", "photo.jpg");
// Attachment will be moved on the server-side only when saveChanges is called
await session.saveChanges();
Rename attachment
Use session.advanced.attachments.rename
to rename an attachment.
// Load entity
const employee = await session.load("employees/1-A");
// Call method 'rename'
// Rename "photo.jpg"
session.advanced.attachments.rename(employee, "photo.jpg", "photo-new.jpg");
// Attachment will be renamed on the server-side only when saveChanges is called
await session.saveChanges();
// Call method 'rename'
// Rename "photo.jpg"
session.advanced.attachments.rename("employees/1-A", "photo.jpg", "photo-new.jpg");
// Attachment will be renamed on the server-side only when saveChanges is called
await session.saveChanges();
Syntax
// Copy - available overloads:
// ===========================
copy(sourceEntity, sourceName, destinationEntity, destinationName);
copy(sourceDocumentId, sourceName, destinationDocumentId, destinationName);
// Move - a vailable overloads:
// ============================
move(sourceEntity, sourceName, destinationEntity, destinationName);
move(sourceDocumentId, sourceName, destinationDocumentId, destinationName);
Parameter | Type | Description |
---|---|---|
sourceEntity | object |
Source entity |
destinationEntity | object |
Destination entity |
sourceDocumentId | string | Source document Id |
destinationDocumentId | string | Destination document Id |
sourceName | string | Source attachment name |
destinationName | string | Destination attachment name |
// Rename - available overloads:
// =============================
rename(entity, name, newName);
rename(documentId, name, newName);
Parameter | Type | Description |
---|---|---|
entity | object |
The document entity |
documentId | string | The document Id |
name | string | Current name of attachment |
newName | string | The new name for the attachment |