The FilesManager
class is responsible for managing file operations in conjunction with the Konecty platform. It handles uploading, deleting, and reordering files associated with a specific record AND a specific field in a Konecty collection. So each managed file field in a record must have its own FilesManager instance.
The class is designed to work both in browser environments and in Node.js.
The class can be instantiated via a Konecty Module instance.
konectyClientOpts
: An object of typeKonectyClientOptions
containing options for the Konecty client, with added files stuff.files
: An array of file configurations (KonFiles.FileConfig[]
) to manage. (The same stored in Konecty)recordData
: An object of typeKonFiles.RecordData
containing metadata about the record associated with the files.
const konectyClientOpts = {
endpoint: 'http://konecty.com',
accessKey: 'your-access-key',
fileManager: {
providerUrl: 'http://konecty.com',
origin: 'http://konecty.com',
},
};
const ProdModule = new ProductModule(konectyClientOpts);
const myProduct = await fetchMyProduct();
const picturesManager = ProdModule.filesManager(
{
recordId: myProduct._id,
fieldName: 'pictures',
},
myProduct.pictures,
);
Uploads all files present in the provided FormData
. On the browser, this should be a native FormData
object, while in Node.js, it should be compatible with the form-data
package.
formData
: An instance ofFormData
(browser) orNodeFormData
(Node.js) containing files to upload.
- A promise that resolves to a
KonectyResult<KonFiles.FileConfig>
containing the uploaded file's configuration.
const formData = new FormData();
formData.append('file', yourFile);
const result = await filesManager.upload(formData);
if (result.success) {
console.log('File uploaded successfully:', result.data);
} else {
console.error('Upload failed:', result.errors);
}
Deletes a file by its name from the Konecty record.
fileName
: The name of the file to delete.
- A promise that resolves to a
KonectyResult<'no-data'>
.
const result = await filesManager.deleteFile('file1');
if (result.success) {
console.log('File deleted successfully');
} else {
console.error('Deletion failed:', result.errors);
}
reorder(fileName: string, newPosition: number, reorderMode?: 'swap' | 'push'): Promise<KonectyResult<'no-data'>>
Reorders a single file in the list.
fileName
: The name of the file to reorder.newPosition
: The new position of the file in the list.reorderMode
: (Optional) The mode of reordering, either'swap'
or'push'
. Defaults to'push'
.
- A promise that resolves to a
KonectyResult<'no-data'>
.
await filesManager.reorder('file2', 0, 'push');
Reorders multiple files based on a new order of positions.
positions
: An array of file names representing the new order of files.
- A promise that resolves to a
KonectyResult<'no-data'>
.
await filesManager.reorder(['file3', 'file1', 'file2']);
Converts the list of managed files to a JSON-compatible array of KonFiles.FileConfig
objects.
- An array of
KonFiles.FileConfig
objects.
const filesJson = filesManager.toJson();
console.log(filesJson);
// Initialize FilesManager
const filesManager = new FilesManager(konectyClientOpts, files, recordData);
// Upload a file
const uploadResult = await filesManager.upload(formData);
// Delete a file
const deleteResult = await filesManager.deleteFile('file1');
// Reorder files
await filesManager.reorder('file2', 0, 'swap');
// Get files as JSON
const filesJson = filesManager.toJson();