Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
entity = JSON.stringify(entity);
} catch (error) {
entity = entity.toString();
}
}
try {
await fs.writeFile(filePath, entity, {flag: 'wx'});
return primaryKey;
} catch (error) {
if (error.code === 'ENOENT') {
await fs.outputFile(filePath, entity);
return primaryKey;
} else if (error.code === 'EEXIST') {
const message = `Record "${primaryKey}" already exists in "${tableName}". You need to delete the record first if you want to overwrite it.`;
throw new RecordAlreadyExistsError(message);
}
throw error;
}
} else {
const message = `Record "${primaryKey}" cannot be saved in "${tableName}" because it's "undefined" or "null".`;
throw new RecordTypeError(message);
}
}