TypeORM是否包含一些功能以避免这种情况:
let contraption = await thingRepository.findOne({ name : "Contraption" });
if(!contraption) // Create if not exist
{
let newThing = new Thing();
newThing.name = "Contraption"
await thingRepository.save(newThing);
contraption = newThing;
}
Run Code Online (Sandbox Code Playgroud)
就像是 :
let contraption = await thingRepository.upsert({ name : "Contraption" });
Run Code Online (Sandbox Code Playgroud)