Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: clone() to clone a record #748

Open
urakagi opened this issue May 13, 2020 · 5 comments
Open

Feature request: clone() to clone a record #748

urakagi opened this issue May 13, 2020 · 5 comments

Comments

@urakagi
Copy link

urakagi commented May 13, 2020

A method for FileCursor instance or Collection.clone(<_id>) will be good for applications that have duplication needs.

For example, wekan can copy cards, so when I copy a card the corresponded attachments also need to be copied. In this case, a clone method will be convenient.

It will also be good if we can pass an option that defines something that will not be cloned but use the passed parameters instead.

@tomtom87
Copy link

tomtom87 commented Jun 3, 2020

+1 A copy / duplicate function would be very useful so it can either straight up copy or have an option to rename.

@fracz
Copy link

fracz commented Mar 27, 2022

Is there any workaround for now? I.e. how to duplicate existing files in FielsCollection now?

@dr-dimitru
Copy link
Member

dr-dimitru commented Mar 27, 2022

@fracz at the moment you would need to:

  1. Read MongoDB record of the existing file
  2. Write the same record to MongoDB omitting _id field
  3. After new record inserted into collection grab newly created entry _id
  4. Replace old _id in path, versions.original.path, and in custom meta fields (if any)
  5. .copyFile() to actually duplicate a file using path from the old and new mongodb entries

Hope that helps

@fracz
Copy link

fracz commented Mar 27, 2022

Ended up with the following code for duplicating certain files. Could you take a look, please?

// collection definition
const Attachments = new FilesCollection({...}).

// inside some meteor method:
if (Meteor.isServer) {
  Attachments.find(someFilters).forEach((attachment) => {
    const buffer = fs.readFileSync(attachment.path);
    Attachments.write(buffer, {
        fileName: attachment.name,
        type: attachment.type,
        size: attachment.size,
        userId: this.userId,
        meta: attachment.meta,
    });
  });
}

@dr-dimitru
Copy link
Member

@fracz lgtm, don't forget to pass a callback to .write method to make sure you have controls over potential errors and exceptions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants