Add logging to lowdb storage service (#188)
This commit is contained in:
parent
d84d6da7f7
commit
4cd20f0fa8
|
@ -3,6 +3,7 @@ import * as lowdb from 'lowdb';
|
||||||
import * as FileSync from 'lowdb/adapters/FileSync';
|
import * as FileSync from 'lowdb/adapters/FileSync';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
|
|
||||||
|
import { LogService } from '../abstractions/log.service';
|
||||||
import { StorageService } from '../abstractions/storage.service';
|
import { StorageService } from '../abstractions/storage.service';
|
||||||
|
|
||||||
import { NodeUtils } from '../misc/nodeUtils';
|
import { NodeUtils } from '../misc/nodeUtils';
|
||||||
|
@ -13,28 +14,37 @@ export class LowdbStorageService implements StorageService {
|
||||||
private defaults: any;
|
private defaults: any;
|
||||||
private dataFilePath: string;
|
private dataFilePath: string;
|
||||||
|
|
||||||
constructor(defaults?: any, dir?: string, private allowCache = false) {
|
constructor(private logService: LogService, defaults?: any, dir?: string, private allowCache = false) {
|
||||||
this.defaults = defaults;
|
this.defaults = defaults;
|
||||||
|
|
||||||
|
this.logService.info('Initializing lowdb storage service.');
|
||||||
let adapter: lowdb.AdapterSync<any>;
|
let adapter: lowdb.AdapterSync<any>;
|
||||||
if (Utils.isNode && dir != null) {
|
if (Utils.isNode && dir != null) {
|
||||||
if (!fs.existsSync(dir)) {
|
if (!fs.existsSync(dir)) {
|
||||||
|
this.logService.warning(`Could not find dir, "${dir}"; creating it instead.`);
|
||||||
NodeUtils.mkdirpSync(dir, '700');
|
NodeUtils.mkdirpSync(dir, '700');
|
||||||
|
this.logService.info(`Created dir "${dir}".`);
|
||||||
}
|
}
|
||||||
this.dataFilePath = path.join(dir, 'data.json');
|
this.dataFilePath = path.join(dir, 'data.json');
|
||||||
if (!fs.existsSync(this.dataFilePath)) {
|
if (!fs.existsSync(this.dataFilePath)) {
|
||||||
|
this.logService.warning(`Could not find data file, "${this.dataFilePath}"; creating it instead.`);
|
||||||
fs.writeFileSync(this.dataFilePath, '', { mode: 0o600 });
|
fs.writeFileSync(this.dataFilePath, '', { mode: 0o600 });
|
||||||
fs.chmodSync(this.dataFilePath, 0o600);
|
fs.chmodSync(this.dataFilePath, 0o600);
|
||||||
|
this.logService.info(`Created data file "${this.dataFilePath}" with chmod 600.`);
|
||||||
}
|
}
|
||||||
adapter = new FileSync(this.dataFilePath);
|
adapter = new FileSync(this.dataFilePath);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
this.logService.info('Attempting to create lowdb storage adapter.');
|
||||||
this.db = lowdb(adapter);
|
this.db = lowdb(adapter);
|
||||||
|
this.logService.info('Successfuly created lowdb storage adapter.');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof SyntaxError) {
|
if (e instanceof SyntaxError) {
|
||||||
|
this.logService.warning(`Error creating lowdb storage adapter, "${e.message}"; emptying data file.`);
|
||||||
adapter.write({});
|
adapter.write({});
|
||||||
this.db = lowdb(adapter);
|
this.db = lowdb(adapter);
|
||||||
} else {
|
} else {
|
||||||
|
this.logService.error(`Error creating lowdb storage adapter, "${e.message}".`);
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,8 +52,10 @@ export class LowdbStorageService implements StorageService {
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
if (this.defaults != null) {
|
if (this.defaults != null) {
|
||||||
|
this.logService.info('Writing defaults.');
|
||||||
this.readForNoCache();
|
this.readForNoCache();
|
||||||
this.db.defaults(this.defaults).write();
|
this.db.defaults(this.defaults).write();
|
||||||
|
this.logService.info('Successfully wrote defaults to db.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue