Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async append(tableName: string, primaryKey: string, additions: string): Promise {
const file = this.resolvePath(tableName, primaryKey);
let record = await this.read(tableName, primaryKey);
if (typeof record === 'string') {
record += additions;
} else {
const message = `Cannot append text to record "${primaryKey}" because it's not a string.`;
throw new RecordTypeError(message);
}
const updatedRecord = record;
await fs.outputFile(file, updatedRecord);
return primaryKey;
}
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);
}
}