Skip to content

Commit

Permalink
fix: async/await for all stores
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarconr committed Apr 28, 2020
1 parent 924edc8 commit 3be3274
Showing 1 changed file with 51 additions and 52 deletions.
103 changes: 51 additions & 52 deletions lib/db/client-applications-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,66 +44,68 @@ class ClientApplicationsDb {
this.eventBus = eventBus;
}

updateRow(details, prev) {
// eslint-disable-next-line no-param-reassign
details.updatedAt = 'now()';
return this.db(TABLE)
.where('app_name', details.appName)
.update(remapRow(details, prev))
.then(
metricsHelper.wrapTimer(this.eventBus, DB_TIME, {
store: 'applications',
action: 'updateRow',
}),
);
_createTimer(action) {
return metricsHelper.wrapTimer(this.eventBus, DB_TIME, {
store: 'applications',
action,
});
}

insertNewRow(details) {
async updateRow(details, prev) {
const timer = this._createTimer('updateRow');

const data = { ...details, updatedAt: 'now()' };

await this.db(TABLE)
.where('app_name', data.appName)
.update(remapRow(data, prev));

timer();
}

async insertNewRow(details) {
return this.db(TABLE).insert(remapRow(details));
}

upsert(data) {
async upsert(data) {
const timer = this._createTimer('upsert');
if (!data) {
throw new Error('Missing data to add / update');
}
return this.db(TABLE)

const result = await this.db(TABLE)
.select(COLUMNS)
.where('app_name', data.appName)
.then(result => {
if (result && result[0]) {
return this.updateRow(data, result[0]);
}
return this.insertNewRow(data);
})
.then(
metricsHelper.wrapTimer(this.eventBus, DB_TIME, {
store: 'applications',
action: 'upsert',
}),
);
.where('app_name', data.appName);

if (result && result[0]) {
this.updateRow(data, result[0]);
} else {
this.insertNewRow(data);
}

timer();
}

getAll() {
return this.db
async getAll() {
const timer = this._createTimer('getAll');

const rows = await this.db
.select(COLUMNS)
.from(TABLE)
.orderBy('app_name', 'asc')
.map(mapRow)
.then(
metricsHelper.wrapTimer(this.eventBus, DB_TIME, {
store: 'applications',
action: 'getAll',
}),
);
.orderBy('app_name', 'asc');

const applications = rows.map(mapRow);
timer();
return applications;
}

getApplication(appName) {
return this.db
.select(COLUMNS)
async getApplication(appName) {
const item = await this.db
.first(COLUMNS)
.where('app_name', appName)
.from(TABLE)
.map(mapRow)
.then(list => list[0]);
.from(TABLE);

return mapRow(item);
}

/**
Expand All @@ -115,17 +117,14 @@ class ClientApplicationsDb {
* ) as foo
* WHERE foo.strategyName = '"other"';
*/
getAppsForStrategy(strategyName) {
return this.db
.select(COLUMNS)
.from(TABLE)
async getAppsForStrategy(strategyName) {
const rows = await this.db.select(COLUMNS).from(TABLE);
return rows
.map(mapRow)
.then(apps =>
apps.filter(app => app.strategies.includes(strategyName)),
);
.filter(app => app.strategies.includes(strategyName));
}

getApplications(filter) {
async getApplications(filter) {
return filter && filter.strategyName
? this.getAppsForStrategy(filter.strategyName)
: this.getAll();
Expand Down

0 comments on commit 3be3274

Please sign in to comment.