如何从sequelize中选择插入

Ste*_*orf 8 insert sequelize.js

我们在节点js上使用sequelize我有资源文件.文件具有UserId属性.需要:将相同的文件(通过filesId)复制到用户.如果使用sql那么需要做:

INSERT INTO files (name, link, userId) SELECT name, link, 5 FROM files WHERE id in (1,2,3,4,10)
Run Code Online (Sandbox Code Playgroud)

但是如何对sequelize我没有任何想法...只有自定义sql查询.

Rud*_*chi -1

尝试这样的事情:

File.findAll({
  where: {id: {$in: [1,2,3,4,10]}}
}).then(function(filesSeq)){
  //make file objects with UserId=5
  var files = filesSeq.map(function(fileSeq){
    var file = fileSeq.toJSON();
    file['UserId'] = 5;
    return file;
  });
  return File.bulkCreate(files)
}).then(function(files){
   //do something with created files
});
Run Code Online (Sandbox Code Playgroud)