如果文件名在数据库中已经存在(也基于记录),我想将一个数字递增到文件名。例如,如果我添加文件名为DOC的文件,它将检查DOC是否存在,因为DOC存在于下面的示例中,并且最新增量为1 (DOC-1),则文件名为DOC-2。如果我再次添加DOC,并且最新的增量是2,那么新的文件名将是DOC-3,依此类推。伙计们,你们知道吗?谢谢。
听起来我想要的是总是创建一个新的文件名(通过向文件名添加一个增量或数字),还可能在.future中定位多个文件名,这些文件名是相同数据的更新。
#搜索记录是否存在的代码(工作正常)
const file = await context.service.Model.findOne({
where: { employeeId: record.id, filename: data.filename },
paranoid: false,
});#更改文件名的代码(当前实现使用
if (file) {
//this is where we add number to filename
filename = getNumberedFileName(data.filename)
}#向文件名添加数字的代码
function getNumberedFileName(fileN) {
//These are value initializations to cope with the situation when the file does not have a .
var fileName = fileN;
var fileExtension = "";
var lastDotIndex = fileN.lastIndexOf(".");
if ((lastDotIndex > 0) && (lastDotIndex < fileN.length - 1)) { //We are not interested in file extensions for files without an extension hidden in UNIX systems, like .gitignore and we are not interested in file extensions if the file ends with a dot
fileName = fileN.substring(0, lastDotIndex);
fileExtension = "." + fileN.substring(lastDotIndex + 1);
}
var lastDashIndex = fileName.lastIndexOf("-");
if ((lastDashIndex > 0) && (lastDashIndex < fileName.length - 1)) {
var lastPart = fileName.substring(lastDashIndex + 1);
if (!isNaN(lastPart)) {
var index = parseInt(lastPart) + 1;
return fileName.substring(0, lastDashIndex) + "-" + index + fileExtension;
}
}
return fileName + "-1" + fileExtension;
}

发布于 2020-08-01 22:19:03
#建议
在我看来,您的逻辑可能会破坏ACID规则,您可以尝试添加numberOfFiles列,并使用像update set numberOfFiles = numberOfFiles + 1 where yourTable这样的update sql query
https://stackoverflow.com/questions/63206044
复制相似问题