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);
return this.db![tableName].get(primaryKey).then((record: any) => {
if (typeof record === 'string') {
record += additions;
} else {
const message = `Cannot append text to record "${primaryKey}" because it's not a string.`;
throw new StoreEngineError.RecordTypeError(message);
}
return this.updateOrCreate(tableName, primaryKey, record);
});
}
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);
}
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 {
public async append(
tableName: string,
primaryKey: PrimaryKey,
additions: string,
): Promise {
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 StoreEngineError.RecordTypeError(message);
}
const key = this.createKey(tableName, primaryKey);
this.webStorage.setItem(`${key}`, record);
return primaryKey;
}
public create(tableName: string, primaryKey: string, entity: T): Promise {
if (entity) {
return this.db![tableName].add(entity, primaryKey).catch((error: Dexie.DexieError) => {
throw this.mapDatabaseError(error, tableName, primaryKey);
});
}
const message = `Record "${primaryKey}" cannot be saved in "${tableName}" because it's "undefined" or "null".`;
return Promise.reject(new StoreEngineError.RecordTypeError(message));
}
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);
}
}
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);