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 create(
tableName: string,
primaryKey: PrimaryKey,
entity: EntityType,
): Promise {
if (!entity) {
const message = `Record "${primaryKey}" cannot be saved in "${tableName}" because it's "undefined" or "null".`;
throw new StoreEngineError.RecordTypeError(message);
}
const internalPrimaryKey: any = this.createKey(tableName, primaryKey);
try {
await this.read(tableName, primaryKey);
const message = `Record "${primaryKey}" already exists in "${tableName}". You need to delete the record first if you want to overwrite it.`;
throw new StoreEngineError.RecordAlreadyExistsError(message);
} catch (error) {
if (!(error instanceof StoreEngineError.RecordNotFoundError)) {
throw error;
}
}
if (typeof entity === 'string') {
this.webStorage.setItem(`${internalPrimaryKey}`, entity);
} else {
this.webStorage.setItem(`${internalPrimaryKey}`, JSON.stringify(entity));
}
const keyWithoutPrefix = internalPrimaryKey.replace(this.createPrefix(tableName), '');
const numericKey = parseInt(keyWithoutPrefix, 10);
const returnKey = isNaN(numericKey) ? keyWithoutPrefix : numericKey;
let newEntity: EntityType | string = entity;
if (typeof entity === 'object') {
newEntity = JSON.stringify(entity);
}
try {
await fs.writeFile(filePath, newEntity, {flag: 'wx'});
return primaryKey;
} catch (error) {
if (error.code === 'ENOENT') {
await fs.outputFile(filePath, newEntity);
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 StoreEngineError.RecordAlreadyExistsError(message);
}
throw error;
}
} else {
const message = `Record "${primaryKey}" cannot be saved in "${tableName}" because it's "undefined" or "null".`;
throw new StoreEngineError.RecordTypeError(message);
}
}
private mapDatabaseError(error: Dexie.DexieError, tableName: string, primaryKey: string): Error {
const isAlreadyExisting = error instanceof Dexie.ConstraintError;
/** @see https://github.com/dfahlander/Dexie.js/issues/776 */
const hasNotEnoughDiskSpace =
error.name === Dexie.errnames.QuotaExceeded || (error.inner && error.inner.name === Dexie.errnames.QuotaExceeded);
if (isAlreadyExisting) {
const message = `Record "${primaryKey}" already exists in "${tableName}". You need to delete the record first if you want to overwrite it.`;
return new StoreEngineError.RecordAlreadyExistsError(message);
} else if (hasNotEnoughDiskSpace) {
const message = `Cannot save "${primaryKey}" in "${tableName}" because there is low disk space.`;
return new StoreEngineError.LowDiskSpaceError(message);
} else {
return error;
}
}
}
private mapDatabaseError(
error: Dexie.DexieError,
tableName: string,
primaryKey: PrimaryKey,
): Error {
const isAlreadyExisting = error instanceof Dexie.ConstraintError;
/** @see https://github.com/dfahlander/Dexie.js/issues/776 */
const hasNotEnoughDiskSpace =
error.name === Dexie.errnames.QuotaExceeded || error.inner?.name === Dexie.errnames.QuotaExceeded;
if (isAlreadyExisting) {
const message = `Record "${primaryKey}" already exists in "${tableName}". You need to delete the record first if you want to overwrite it.`;
return new StoreEngineError.RecordAlreadyExistsError(message);
} else if (hasNotEnoughDiskSpace) {
const message = `Cannot save "${primaryKey}" in "${tableName}" because there is low disk space.`;
return new StoreEngineError.LowDiskSpaceError(message);
} else {
return error;
}
}
if (!entity) {
const message = `Record "${primaryKey}" cannot be saved in "${tableName}" because it's "undefined" or "null".`;
throw new StoreEngineError.RecordTypeError(message);
}
if (primaryKey === undefined) {
primaryKey = (this.autoIncrementedPrimaryKey as unknown) as PrimaryKey;
this.autoIncrementedPrimaryKey += 1;
}
const filePath = this.createFilePath(tableName, primaryKey);
const isExistent = await fs.exists(filePath);
if (isExistent) {
const message = `Record "${primaryKey}" already exists in "${tableName}". You need to delete the record first if you want to overwrite it.`;
throw new StoreEngineError.RecordAlreadyExistsError(message);
} else {
let data: string;
try {
data = JSON.stringify(entity);
} catch (error) {
data = String(entity);
}
await fs.writeFile(filePath, data);
return primaryKey;
}
}