mirror of
https://github.com/Unleash/unleash.git
synced 2024-12-22 19:07:54 +01:00
Fix/upgrade knex (#630)
This commit is contained in:
parent
7ac134312f
commit
8ac4a243b2
@ -1,5 +1,9 @@
|
||||
# Changelog
|
||||
|
||||
## 3.6.0
|
||||
|
||||
- feat: upgrade knex to latest
|
||||
|
||||
## 3.5.4
|
||||
|
||||
- fix: helmet wap csp in quotes
|
||||
|
@ -2,9 +2,6 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
const metricsHelper = require('../metrics-helper');
|
||||
const { DB_TIME } = require('../events');
|
||||
|
||||
const COLUMNS = [
|
||||
'app_name',
|
||||
'created_at',
|
||||
@ -44,25 +41,19 @@ class ClientApplicationsDb {
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
updateRow(details, prev) {
|
||||
async 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',
|
||||
}),
|
||||
);
|
||||
.update(remapRow(details, prev));
|
||||
}
|
||||
|
||||
insertNewRow(details) {
|
||||
async insertNewRow(details) {
|
||||
return this.db(TABLE).insert(remapRow(details));
|
||||
}
|
||||
|
||||
upsert(data) {
|
||||
async upsert(data) {
|
||||
if (!data) {
|
||||
throw new Error('Missing data to add / update');
|
||||
}
|
||||
@ -74,36 +65,26 @@ class ClientApplicationsDb {
|
||||
return this.updateRow(data, result[0]);
|
||||
}
|
||||
return this.insertNewRow(data);
|
||||
})
|
||||
.then(
|
||||
metricsHelper.wrapTimer(this.eventBus, DB_TIME, {
|
||||
store: 'applications',
|
||||
action: 'upsert',
|
||||
}),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
getAll() {
|
||||
return this.db
|
||||
async 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');
|
||||
|
||||
return rows.map(mapRow);
|
||||
}
|
||||
|
||||
getApplication(appName) {
|
||||
return this.db
|
||||
async getApplication(appName) {
|
||||
const row = await this.db
|
||||
.select(COLUMNS)
|
||||
.where('app_name', appName)
|
||||
.from(TABLE)
|
||||
.map(mapRow)
|
||||
.then(list => list[0]);
|
||||
.first();
|
||||
|
||||
return mapRow(row);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -115,17 +96,17 @@ 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 =>
|
||||
.filter(apps =>
|
||||
apps.filter(app => app.strategies.includes(strategyName)),
|
||||
);
|
||||
}
|
||||
|
||||
getApplications(filter) {
|
||||
async getApplications(filter) {
|
||||
return filter && filter.strategyName
|
||||
? this.getAppsForStrategy(filter.strategyName)
|
||||
: this.getAll();
|
||||
|
@ -31,21 +31,27 @@ class ClientInstanceStore {
|
||||
this.db = db;
|
||||
this.eventBus = eventBus;
|
||||
this.logger = getLogger('client-instance-store.js');
|
||||
this.timer = action =>
|
||||
metricsHelper.wrapTimer(eventBus, DB_TIME, {
|
||||
store: 'instance',
|
||||
action,
|
||||
});
|
||||
const clearer = () => this._removeInstancesOlderThanTwoDays();
|
||||
setTimeout(clearer, 10).unref();
|
||||
setInterval(clearer, ONE_DAY).unref();
|
||||
}
|
||||
|
||||
_removeInstancesOlderThanTwoDays() {
|
||||
this.db(TABLE)
|
||||
async _removeInstancesOlderThanTwoDays() {
|
||||
const rows = await this.db(TABLE)
|
||||
.whereRaw("created_at < now() - interval '2 days'")
|
||||
.del()
|
||||
.then(
|
||||
res => res > 0 && this.logger.debug(`Deleted ${res} instances`),
|
||||
);
|
||||
.del();
|
||||
|
||||
if (rows > 0) {
|
||||
this.logger.debug(`Deleted ${rows} instances`);
|
||||
}
|
||||
}
|
||||
|
||||
updateRow(details) {
|
||||
async updateRow(details) {
|
||||
return this.db(TABLE)
|
||||
.where('app_name', details.appName)
|
||||
.where('instance_id', details.instanceId)
|
||||
@ -56,7 +62,7 @@ class ClientInstanceStore {
|
||||
});
|
||||
}
|
||||
|
||||
insertNewRow(details) {
|
||||
async insertNewRow(details) {
|
||||
return this.db(TABLE).insert({
|
||||
app_name: details.appName,
|
||||
instance_id: details.instanceId,
|
||||
@ -65,56 +71,61 @@ class ClientInstanceStore {
|
||||
});
|
||||
}
|
||||
|
||||
insert(details) {
|
||||
return this.db(TABLE)
|
||||
async insert(details) {
|
||||
const stopTimer = this.timer('insert');
|
||||
|
||||
const result = await this.db(TABLE)
|
||||
.count('*')
|
||||
.where('app_name', details.appName)
|
||||
.where('instance_id', details.instanceId)
|
||||
.map(row => ({ count: row.count }))
|
||||
.then(rows => {
|
||||
if (rows[0].count > 0) {
|
||||
return this.updateRow(details);
|
||||
}
|
||||
return this.insertNewRow(details);
|
||||
})
|
||||
.then(
|
||||
metricsHelper.wrapTimer(this.eventBus, DB_TIME, {
|
||||
store: 'instance',
|
||||
action: 'insert',
|
||||
}),
|
||||
);
|
||||
.first();
|
||||
|
||||
let item;
|
||||
|
||||
if (Number(result.count) > 0) {
|
||||
item = await this.updateRow(details);
|
||||
} else {
|
||||
item = await this.insertNewRow(details);
|
||||
}
|
||||
|
||||
stopTimer();
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
getAll() {
|
||||
return this.db
|
||||
async getAll() {
|
||||
const stopTimer = this.timer('getAll');
|
||||
|
||||
const rows = await this.db
|
||||
.select(COLUMNS)
|
||||
.from(TABLE)
|
||||
.orderBy('last_seen', 'desc')
|
||||
.map(mapRow)
|
||||
.then(
|
||||
metricsHelper.wrapTimer(this.eventBus, DB_TIME, {
|
||||
store: 'instance',
|
||||
action: 'getAll',
|
||||
}),
|
||||
);
|
||||
.orderBy('last_seen', 'desc');
|
||||
|
||||
const toggles = rows.map(mapRow);
|
||||
|
||||
stopTimer();
|
||||
|
||||
return toggles;
|
||||
}
|
||||
|
||||
getByAppName(appName) {
|
||||
return this.db
|
||||
async getByAppName(appName) {
|
||||
const rows = await this.db
|
||||
.select()
|
||||
.from(TABLE)
|
||||
.where('app_name', appName)
|
||||
.orderBy('last_seen', 'desc')
|
||||
.map(mapRow);
|
||||
.orderBy('last_seen', 'desc');
|
||||
|
||||
return rows.map(mapRow);
|
||||
}
|
||||
|
||||
getApplications() {
|
||||
return this.db
|
||||
async getApplications() {
|
||||
const rows = await this.db
|
||||
.distinct('app_name')
|
||||
.select(['app_name'])
|
||||
.from(TABLE)
|
||||
.orderBy('app_name', 'desc')
|
||||
.map(mapRow);
|
||||
.orderBy('app_name', 'desc');
|
||||
|
||||
return rows.map(mapRow);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,38 +22,43 @@ class ClientMetricsDb {
|
||||
setInterval(clearer, ONE_MINUTE).unref();
|
||||
}
|
||||
|
||||
removeMetricsOlderThanOneHour() {
|
||||
this.db(TABLE)
|
||||
async removeMetricsOlderThanOneHour() {
|
||||
const rows = await this.db(TABLE)
|
||||
.whereRaw("created_at < now() - interval '1 hour'")
|
||||
.del()
|
||||
.then(res => res > 0 && this.logger.info(`Deleted ${res} metrics`));
|
||||
.del();
|
||||
|
||||
if (rows > 0) {
|
||||
this.logger.debug(`Deleted ${rows} metrics`);
|
||||
}
|
||||
}
|
||||
|
||||
// Insert new client metrics
|
||||
insert(metrics) {
|
||||
async insert(metrics) {
|
||||
return this.db(TABLE).insert({ metrics });
|
||||
}
|
||||
|
||||
// Used at startup to load all metrics last week into memory!
|
||||
getMetricsLastHour() {
|
||||
return this.db
|
||||
async getMetricsLastHour() {
|
||||
const result = await this.db
|
||||
.select(METRICS_COLUMNS)
|
||||
.from(TABLE)
|
||||
.limit(2000)
|
||||
.whereRaw("created_at > now() - interval '1 hour'")
|
||||
.orderBy('created_at', 'asc')
|
||||
.map(mapRow);
|
||||
.orderBy('created_at', 'asc');
|
||||
|
||||
return result.map(mapRow);
|
||||
}
|
||||
|
||||
// Used to poll for new metrics
|
||||
getNewMetrics(lastKnownId) {
|
||||
return this.db
|
||||
async getNewMetrics(lastKnownId) {
|
||||
const result = await this.db
|
||||
.select(METRICS_COLUMNS)
|
||||
.from(TABLE)
|
||||
.limit(1000)
|
||||
.where('id', '>', lastKnownId)
|
||||
.orderBy('created_at', 'asc')
|
||||
.map(mapRow);
|
||||
.orderBy('created_at', 'asc');
|
||||
|
||||
return result.map(mapRow);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,12 @@ class ClientMetricsStore extends EventEmitter {
|
||||
this.eventBus = eventBus;
|
||||
this.highestIdSeen = 0;
|
||||
|
||||
this.startTimer = action =>
|
||||
metricsHelper.wrapTimer(eventBus, DB_TIME, {
|
||||
store: 'metrics',
|
||||
action,
|
||||
});
|
||||
|
||||
this._init(pollInterval);
|
||||
}
|
||||
|
||||
@ -47,13 +53,12 @@ class ClientMetricsStore extends EventEmitter {
|
||||
}
|
||||
|
||||
// Insert new client metrics
|
||||
insert(metrics) {
|
||||
return this.metricsDb.insert(metrics).then(
|
||||
metricsHelper.wrapTimer(this.eventBus, DB_TIME, {
|
||||
store: 'metrics',
|
||||
action: 'insert',
|
||||
}),
|
||||
);
|
||||
async insert(metrics) {
|
||||
const stopTimer = this.startTimer('insert');
|
||||
|
||||
await this.metricsDb.insert(metrics);
|
||||
|
||||
stopTimer();
|
||||
}
|
||||
|
||||
destroy() {
|
||||
|
@ -69,15 +69,16 @@ class ContextFieldStore {
|
||||
};
|
||||
}
|
||||
|
||||
getAll() {
|
||||
return this.db
|
||||
async getAll() {
|
||||
const rows = await this.db
|
||||
.select(COLUMNS)
|
||||
.from(TABLE)
|
||||
.orderBy('name', 'asc')
|
||||
.map(mapRow);
|
||||
.orderBy('name', 'asc');
|
||||
|
||||
return rows.map(mapRow);
|
||||
}
|
||||
|
||||
get(name) {
|
||||
async get(name) {
|
||||
return this.db
|
||||
.first(COLUMNS)
|
||||
.from(TABLE)
|
||||
@ -85,7 +86,7 @@ class ContextFieldStore {
|
||||
.then(mapRow);
|
||||
}
|
||||
|
||||
_createContextField(contextField) {
|
||||
async _createContextField(contextField) {
|
||||
return this.db(TABLE)
|
||||
.insert(this.fieldToRow(contextField))
|
||||
.catch(err =>
|
||||
@ -96,7 +97,7 @@ class ContextFieldStore {
|
||||
);
|
||||
}
|
||||
|
||||
_updateContextField(data) {
|
||||
async _updateContextField(data) {
|
||||
return this.db(TABLE)
|
||||
.where({ name: data.name })
|
||||
.update(this.fieldToRow(data))
|
||||
@ -108,7 +109,7 @@ class ContextFieldStore {
|
||||
);
|
||||
}
|
||||
|
||||
_deleteContextField({ name }) {
|
||||
async _deleteContextField({ name }) {
|
||||
return this.db(TABLE)
|
||||
.where({ name })
|
||||
.del()
|
||||
|
@ -11,27 +11,27 @@ class EventStore extends EventEmitter {
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
store(event) {
|
||||
return this.db('events')
|
||||
.insert({
|
||||
type: event.type,
|
||||
async store(event) {
|
||||
await this.db('events').insert({
|
||||
type: event.type,
|
||||
created_by: event.createdBy, // eslint-disable-line
|
||||
data: event.data,
|
||||
})
|
||||
.then(() => this.emit(event.type, event));
|
||||
data: event.data,
|
||||
});
|
||||
this.emit(event.type, event);
|
||||
}
|
||||
|
||||
getEvents() {
|
||||
return this.db
|
||||
async getEvents() {
|
||||
const rows = await this.db
|
||||
.select(EVENT_COLUMNS)
|
||||
.from('events')
|
||||
.limit(100)
|
||||
.orderBy('created_at', 'desc')
|
||||
.map(this.rowToEvent);
|
||||
.orderBy('created_at', 'desc');
|
||||
|
||||
return rows.map(this.rowToEvent);
|
||||
}
|
||||
|
||||
getEventsFilterByName(name) {
|
||||
return this.db
|
||||
async getEventsFilterByName(name) {
|
||||
const rows = await this.db
|
||||
.select(EVENT_COLUMNS)
|
||||
.from('events')
|
||||
.limit(100)
|
||||
@ -44,8 +44,9 @@ class EventStore extends EventEmitter {
|
||||
.from('events')
|
||||
.where({ type: DROP_FEATURES }),
|
||||
)
|
||||
.orderBy('created_at', 'desc')
|
||||
.map(this.rowToEvent);
|
||||
.orderBy('created_at', 'desc');
|
||||
|
||||
return rows.map(this.rowToEvent);
|
||||
}
|
||||
|
||||
rowToEvent(row) {
|
||||
|
@ -1,5 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const metricsHelper = require('../metrics-helper');
|
||||
const { DB_TIME } = require('../events');
|
||||
const {
|
||||
FEATURE_CREATED,
|
||||
FEATURE_UPDATED,
|
||||
@ -23,9 +25,16 @@ const FEATURE_COLUMNS = [
|
||||
const TABLE = 'features';
|
||||
|
||||
class FeatureToggleStore {
|
||||
constructor(db, eventStore, getLogger) {
|
||||
constructor(db, eventStore, eventBus, getLogger) {
|
||||
this.db = db;
|
||||
this.getLogger = getLogger('client-toggle-store.js');
|
||||
this.getLogger = getLogger('feature-toggle-store.js');
|
||||
|
||||
this.timer = action =>
|
||||
metricsHelper.wrapTimer(eventBus, DB_TIME, {
|
||||
store: 'feature-toggle',
|
||||
action,
|
||||
});
|
||||
|
||||
eventStore.on(FEATURE_CREATED, event =>
|
||||
this._createFeature(event.data),
|
||||
);
|
||||
@ -42,16 +51,21 @@ class FeatureToggleStore {
|
||||
eventStore.on(DROP_FEATURES, () => this._dropFeatures());
|
||||
}
|
||||
|
||||
getFeatures() {
|
||||
return this.db
|
||||
async getFeatures() {
|
||||
const stopTimer = this.timer('getAll');
|
||||
|
||||
const rows = await this.db
|
||||
.select(FEATURE_COLUMNS)
|
||||
.from(TABLE)
|
||||
.where({ archived: 0 })
|
||||
.orderBy('name', 'asc')
|
||||
.map(this.rowToFeature);
|
||||
.orderBy('name', 'asc');
|
||||
|
||||
stopTimer();
|
||||
|
||||
return rows.map(this.rowToFeature);
|
||||
}
|
||||
|
||||
count() {
|
||||
async count() {
|
||||
return this.db
|
||||
.count('*')
|
||||
.from(TABLE)
|
||||
@ -59,7 +73,7 @@ class FeatureToggleStore {
|
||||
.then(res => Number(res[0].count));
|
||||
}
|
||||
|
||||
getFeature(name) {
|
||||
async getFeature(name) {
|
||||
return this.db
|
||||
.first(FEATURE_COLUMNS)
|
||||
.from(TABLE)
|
||||
@ -67,7 +81,7 @@ class FeatureToggleStore {
|
||||
.then(this.rowToFeature);
|
||||
}
|
||||
|
||||
hasFeature(name) {
|
||||
async hasFeature(name) {
|
||||
return this.db
|
||||
.first('name', 'archived')
|
||||
.from(TABLE)
|
||||
@ -83,13 +97,13 @@ class FeatureToggleStore {
|
||||
});
|
||||
}
|
||||
|
||||
getArchivedFeatures() {
|
||||
return this.db
|
||||
async getArchivedFeatures() {
|
||||
const rows = await this.db
|
||||
.select(FEATURE_COLUMNS)
|
||||
.from(TABLE)
|
||||
.where({ archived: 1 })
|
||||
.orderBy('name', 'asc')
|
||||
.map(this.rowToFeature);
|
||||
.orderBy('name', 'asc');
|
||||
return rows.map(this.rowToFeature);
|
||||
}
|
||||
|
||||
rowToFeature(row) {
|
||||
@ -122,60 +136,65 @@ class FeatureToggleStore {
|
||||
};
|
||||
}
|
||||
|
||||
_createFeature(data) {
|
||||
return this.db(TABLE)
|
||||
.insert(this.eventDataToRow(data))
|
||||
.catch(err =>
|
||||
this.logger.error('Could not insert feature, error: ', err),
|
||||
);
|
||||
async _createFeature(data) {
|
||||
try {
|
||||
await this.db(TABLE).insert(this.eventDataToRow(data));
|
||||
} catch (err) {
|
||||
this.logger.error('Could not insert feature, error: ', err);
|
||||
}
|
||||
}
|
||||
|
||||
_updateFeature(data) {
|
||||
return this.db(TABLE)
|
||||
.where({ name: data.name })
|
||||
.update(this.eventDataToRow(data))
|
||||
.catch(err =>
|
||||
this.logger.error('Could not update feature, error: ', err),
|
||||
);
|
||||
async _updateFeature(data) {
|
||||
try {
|
||||
await this.db(TABLE)
|
||||
.where({ name: data.name })
|
||||
.update(this.eventDataToRow(data));
|
||||
} catch (err) {
|
||||
this.logger.error('Could not update feature, error: ', err);
|
||||
}
|
||||
}
|
||||
|
||||
_archiveFeature({ name }) {
|
||||
return this.db(TABLE)
|
||||
.where({ name })
|
||||
.update({ archived: 1, enabled: 0 })
|
||||
.catch(err => {
|
||||
this.logger.error('Could not archive feature, error: ', err);
|
||||
});
|
||||
async _archiveFeature({ name }) {
|
||||
try {
|
||||
await this.db(TABLE)
|
||||
.where({ name })
|
||||
.update({ archived: 1, enabled: 0 });
|
||||
} catch (err) {
|
||||
this.logger.error('Could not archive feature, error: ', err);
|
||||
}
|
||||
}
|
||||
|
||||
_reviveFeature({ name }) {
|
||||
return this.db(TABLE)
|
||||
.where({ name })
|
||||
.update({ archived: 0, enabled: 0 })
|
||||
.catch(err =>
|
||||
this.logger.error('Could not archive feature, error: ', err),
|
||||
);
|
||||
async _reviveFeature({ name }) {
|
||||
try {
|
||||
await this.db(TABLE)
|
||||
.where({ name })
|
||||
.update({ archived: 0, enabled: 0 });
|
||||
} catch (err) {
|
||||
this.logger.error('Could not archive feature, error: ', err);
|
||||
}
|
||||
}
|
||||
|
||||
_importFeature(data) {
|
||||
async _importFeature(data) {
|
||||
const rowData = this.eventDataToRow(data);
|
||||
return this.db(TABLE)
|
||||
.where({ name: rowData.name })
|
||||
.update(rowData)
|
||||
.then(result =>
|
||||
result === 0 ? this.db(TABLE).insert(rowData) : result,
|
||||
)
|
||||
.catch(err =>
|
||||
this.logger.error('Could not import feature, error: ', err),
|
||||
);
|
||||
try {
|
||||
const result = await this.db(TABLE)
|
||||
.where({ name: rowData.name })
|
||||
.update(rowData);
|
||||
|
||||
if (result === 0) {
|
||||
await this.db(TABLE).insert(rowData);
|
||||
}
|
||||
} catch (err) {
|
||||
this.logger.error('Could not import feature, error: ', err);
|
||||
}
|
||||
}
|
||||
|
||||
_dropFeatures() {
|
||||
return this.db(TABLE)
|
||||
.delete()
|
||||
.catch(err =>
|
||||
this.logger.error('Could not drop features, error: ', err),
|
||||
);
|
||||
async _dropFeatures() {
|
||||
try {
|
||||
await this.db(TABLE).delete();
|
||||
} catch (err) {
|
||||
this.logger.error('Could not drop features, error: ', err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,11 +9,9 @@ class FeatureToggleStore {
|
||||
this.getLogger = getLogger('feature-type-store.js');
|
||||
}
|
||||
|
||||
getAll() {
|
||||
return this.db
|
||||
.select(COLUMNS)
|
||||
.from(TABLE)
|
||||
.map(this.rowToFeatureType);
|
||||
async getAll() {
|
||||
const rows = await this.db.select(COLUMNS).from(TABLE);
|
||||
return rows.map(this.rowToFeatureType);
|
||||
}
|
||||
|
||||
rowToFeatureType(row) {
|
||||
|
@ -22,7 +22,12 @@ module.exports.createStores = (config, eventBus) => {
|
||||
return {
|
||||
db,
|
||||
eventStore,
|
||||
featureToggleStore: new FeatureToggleStore(db, eventStore, getLogger),
|
||||
featureToggleStore: new FeatureToggleStore(
|
||||
db,
|
||||
eventStore,
|
||||
eventBus,
|
||||
getLogger,
|
||||
),
|
||||
featureTypeStore: new FeatureTypeStore(db, getLogger),
|
||||
strategyStore: new StrategyStore(db, eventStore, getLogger),
|
||||
clientApplicationsStore: new ClientApplicationsStore(
|
||||
|
@ -10,7 +10,7 @@ class SettingStore {
|
||||
this.logger = getLogger('settings-store.js');
|
||||
}
|
||||
|
||||
updateRow(name, content) {
|
||||
async updateRow(name, content) {
|
||||
return this.db(TABLE)
|
||||
.where('name', name)
|
||||
.update({
|
||||
@ -18,21 +18,20 @@ class SettingStore {
|
||||
});
|
||||
}
|
||||
|
||||
insertNewRow(name, content) {
|
||||
async insertNewRow(name, content) {
|
||||
return this.db(TABLE).insert({ name, content });
|
||||
}
|
||||
|
||||
insert(name, content) {
|
||||
return this.db(TABLE)
|
||||
async insert(name, content) {
|
||||
const result = await this.db(TABLE)
|
||||
.count('*')
|
||||
.where('name', name)
|
||||
.map(row => ({ count: row.count }))
|
||||
.then(rows => {
|
||||
if (rows[0].count > 0) {
|
||||
return this.updateRow(name, content);
|
||||
}
|
||||
return this.insertNewRow(name, content);
|
||||
});
|
||||
.first();
|
||||
|
||||
if (Number(result.count) > 0) {
|
||||
return this.updateRow(name, content);
|
||||
}
|
||||
return this.insertNewRow(name, content);
|
||||
}
|
||||
|
||||
async get(name) {
|
||||
|
@ -31,24 +31,25 @@ class StrategyStore {
|
||||
eventStore.on(DROP_STRATEGIES, () => this._dropStrategies());
|
||||
}
|
||||
|
||||
getStrategies() {
|
||||
return this.db
|
||||
async getStrategies() {
|
||||
const rows = await this.db
|
||||
.select(STRATEGY_COLUMNS)
|
||||
.from(TABLE)
|
||||
.orderBy('name', 'asc')
|
||||
.map(this.rowToStrategy);
|
||||
.orderBy('name', 'asc');
|
||||
|
||||
return rows.map(this.rowToStrategy);
|
||||
}
|
||||
|
||||
getEditableStrategies() {
|
||||
return this.db
|
||||
async getEditableStrategies() {
|
||||
const rows = await this.db
|
||||
.select(STRATEGY_COLUMNS)
|
||||
.from(TABLE)
|
||||
.where({ built_in: 0 }) // eslint-disable-line
|
||||
.orderBy('name', 'asc')
|
||||
.map(this.rowToEditableStrategy);
|
||||
.orderBy('name', 'asc');
|
||||
return rows.map(this.rowToEditableStrategy);
|
||||
}
|
||||
|
||||
getStrategy(name) {
|
||||
async getStrategy(name) {
|
||||
return this.db
|
||||
.first(STRATEGY_COLUMNS)
|
||||
.from(TABLE)
|
||||
@ -87,7 +88,7 @@ class StrategyStore {
|
||||
};
|
||||
}
|
||||
|
||||
_createStrategy(data) {
|
||||
async _createStrategy(data) {
|
||||
this.db(TABLE)
|
||||
.insert(this.eventDataToRow(data))
|
||||
.catch(err =>
|
||||
@ -95,7 +96,7 @@ class StrategyStore {
|
||||
);
|
||||
}
|
||||
|
||||
_updateStrategy(data) {
|
||||
async _updateStrategy(data) {
|
||||
this.db(TABLE)
|
||||
.where({ name: data.name })
|
||||
.update(this.eventDataToRow(data))
|
||||
@ -104,7 +105,7 @@ class StrategyStore {
|
||||
);
|
||||
}
|
||||
|
||||
_deleteStrategy({ name }) {
|
||||
async _deleteStrategy({ name }) {
|
||||
return this.db(TABLE)
|
||||
.where({ name })
|
||||
.del()
|
||||
@ -113,7 +114,7 @@ class StrategyStore {
|
||||
});
|
||||
}
|
||||
|
||||
_importStrategy(data) {
|
||||
async _importStrategy(data) {
|
||||
const rowData = this.eventDataToRow(data);
|
||||
return this.db(TABLE)
|
||||
.where({ name: rowData.name, built_in: 0 }) // eslint-disable-line
|
||||
@ -126,7 +127,7 @@ class StrategyStore {
|
||||
);
|
||||
}
|
||||
|
||||
_dropStrategies() {
|
||||
async _dropStrategies() {
|
||||
return this.db(TABLE)
|
||||
.where({ built_in: 0 }) // eslint-disable-line
|
||||
.delete()
|
||||
|
@ -77,7 +77,7 @@
|
||||
"helmet": "^4.1.0",
|
||||
"joi": "^17.2.0",
|
||||
"js-yaml": "^3.14.0",
|
||||
"knex": "0.20.10",
|
||||
"knex": "0.21.5",
|
||||
"log4js": "^6.0.0",
|
||||
"mime": "^2.4.2",
|
||||
"moment": "^2.24.0",
|
||||
|
78
yarn.lock
78
yarn.lock
@ -698,7 +698,7 @@ bintrees@1.0.1:
|
||||
resolved "https://registry.yarnpkg.com/bintrees/-/bintrees-1.0.1.tgz#0e655c9b9c2435eaab68bf4027226d2b55a34524"
|
||||
integrity sha1-DmVcm5wkNeqraL9AJyJtK1WjRSQ=
|
||||
|
||||
bluebird@^3.1.1, bluebird@^3.7.2:
|
||||
bluebird@^3.1.1:
|
||||
version "3.7.2"
|
||||
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.2.tgz#9f229c15be272454ffa973ace0dbee79a1b0c36f"
|
||||
integrity sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==
|
||||
@ -1025,10 +1025,10 @@ color-name@~1.1.4:
|
||||
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
|
||||
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==
|
||||
|
||||
colorette@1.1.0:
|
||||
version "1.1.0"
|
||||
resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.1.0.tgz#1f943e5a357fac10b4e0f5aaef3b14cdc1af6ec7"
|
||||
integrity sha512-6S062WDQUXi6hOfkO/sBPVwE5ASXY4G2+b4atvhJfSsuUUhIaUKlkjLe9692Ipyt5/a+IPF5aVTu3V5gvXq5cg==
|
||||
colorette@1.2.1:
|
||||
version "1.2.1"
|
||||
resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.2.1.tgz#4d0b921325c14faf92633086a536db6e89564b1b"
|
||||
integrity sha512-puCDz0CzydiSYOrnXpz/PKd69zRrribezjtE9yd4zvytoRc8+RY/KJPvtPFKZS3E3wP6neGyMe0vOTlHO5L3Pw==
|
||||
|
||||
colors@1.0.x:
|
||||
version "1.0.3"
|
||||
@ -1047,11 +1047,6 @@ combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6:
|
||||
dependencies:
|
||||
delayed-stream "~1.0.0"
|
||||
|
||||
commander@^4.1.1:
|
||||
version "4.1.1"
|
||||
resolved "https://registry.yarnpkg.com/commander/-/commander-4.1.1.tgz#9fd602bd936294e9e9ef46a3f4d6964044b18068"
|
||||
integrity sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==
|
||||
|
||||
commander@^5.1.0:
|
||||
version "5.1.0"
|
||||
resolved "https://registry.yarnpkg.com/commander/-/commander-5.1.0.tgz#46abbd1652f8e059bddaef99bbdcb2ad9cf179ae"
|
||||
@ -2748,7 +2743,7 @@ inquirer@^7.0.0:
|
||||
strip-ansi "^6.0.0"
|
||||
through "^2.3.6"
|
||||
|
||||
interpret@^2.0.0:
|
||||
interpret@^2.2.0:
|
||||
version "2.2.0"
|
||||
resolved "https://registry.yarnpkg.com/interpret/-/interpret-2.2.0.tgz#1a78a0b5965c40a5416d007ad6f50ad27c417df9"
|
||||
integrity sha512-Ju0Bz/cEia55xDwUWEa8+olFpCiQoypjnQySseKtmjNrnps3P+xfpUmGr90T7yjlVJmOtybRvPXhKMbHr+fWnw==
|
||||
@ -3268,27 +3263,26 @@ kind-of@^6.0.0, kind-of@^6.0.2, kind-of@^6.0.3:
|
||||
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-6.0.3.tgz#07c05034a6c349fa06e24fa35aa76db4580ce4dd"
|
||||
integrity sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw==
|
||||
|
||||
knex@0.20.10:
|
||||
version "0.20.10"
|
||||
resolved "https://registry.yarnpkg.com/knex/-/knex-0.20.10.tgz#48c22abe38fdc6856b5e9d948685ca009b3fbd77"
|
||||
integrity sha512-07D6fvY5NdvrfRPmkLLG+OrHvmAy55OX7eXkN8TMiOOI5lWJh1dC2zKjeEQJqUILMOsTnZCGqTKGaRm4t1E9xg==
|
||||
knex@0.21.5:
|
||||
version "0.21.5"
|
||||
resolved "https://registry.yarnpkg.com/knex/-/knex-0.21.5.tgz#c4be1958488f348aed3510aa4b7115639ee1bd01"
|
||||
integrity sha512-cQj7F2D/fu03eTr6ZzYCYKdB9w7fPYlvTiU/f2OeXay52Pq5PwD+NAkcf40WDnppt/4/4KukROwlMOaE7WArcA==
|
||||
dependencies:
|
||||
bluebird "^3.7.2"
|
||||
colorette "1.1.0"
|
||||
commander "^4.1.1"
|
||||
colorette "1.2.1"
|
||||
commander "^5.1.0"
|
||||
debug "4.1.1"
|
||||
esm "^3.2.25"
|
||||
getopts "2.2.5"
|
||||
inherits "~2.0.4"
|
||||
interpret "^2.0.0"
|
||||
interpret "^2.2.0"
|
||||
liftoff "3.1.0"
|
||||
lodash "^4.17.15"
|
||||
mkdirp "^0.5.1"
|
||||
pg-connection-string "2.1.0"
|
||||
tarn "^2.0.0"
|
||||
lodash "^4.17.20"
|
||||
mkdirp "^1.0.4"
|
||||
pg-connection-string "2.3.0"
|
||||
tarn "^3.0.0"
|
||||
tildify "2.0.0"
|
||||
uuid "^3.4.0"
|
||||
v8flags "^3.1.3"
|
||||
uuid "^7.0.3"
|
||||
v8flags "^3.2.0"
|
||||
|
||||
latest-version@^5.0.0:
|
||||
version "5.1.0"
|
||||
@ -3428,6 +3422,11 @@ lodash@^4.14.0, lodash@^4.17.11, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.
|
||||
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.19.tgz#e48ddedbe30b3321783c5b4301fbd353bc1e4a4b"
|
||||
integrity sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==
|
||||
|
||||
lodash@^4.17.20:
|
||||
version "4.17.20"
|
||||
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52"
|
||||
integrity sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==
|
||||
|
||||
log-driver@^1.2.7:
|
||||
version "1.2.7"
|
||||
resolved "https://registry.yarnpkg.com/log-driver/-/log-driver-1.2.7.tgz#63b95021f0702fedfa2c9bb0a24e7797d71871d8"
|
||||
@ -3656,6 +3655,11 @@ mkdirp@0.x.x, mkdirp@^0.5.1, mkdirp@~0.5.0:
|
||||
dependencies:
|
||||
minimist "^1.2.5"
|
||||
|
||||
mkdirp@^1.0.4:
|
||||
version "1.0.4"
|
||||
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
|
||||
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==
|
||||
|
||||
module-not-found-error@^1.0.1:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/module-not-found-error/-/module-not-found-error-1.0.1.tgz#cf8b4ff4f29640674d6cdd02b0e3bc523c2bbdc0"
|
||||
@ -4228,12 +4232,7 @@ performance-now@^2.1.0:
|
||||
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
|
||||
integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=
|
||||
|
||||
pg-connection-string@2.1.0:
|
||||
version "2.1.0"
|
||||
resolved "https://registry.yarnpkg.com/pg-connection-string/-/pg-connection-string-2.1.0.tgz#e07258f280476540b24818ebb5dca29e101ca502"
|
||||
integrity sha512-bhlV7Eq09JrRIvo1eKngpwuqKtJnNhZdpdOlvrPrA4dxqXPjxSrbNrfnIDmTpwMyRszrcV4kU5ZA4mMsQUrjdg==
|
||||
|
||||
pg-connection-string@^2.3.0:
|
||||
pg-connection-string@2.3.0, pg-connection-string@^2.3.0:
|
||||
version "2.3.0"
|
||||
resolved "https://registry.yarnpkg.com/pg-connection-string/-/pg-connection-string-2.3.0.tgz#c13fcb84c298d0bfa9ba12b40dd6c23d946f55d6"
|
||||
integrity sha512-ukMTJXLI7/hZIwTW7hGMZJ0Lj0S2XQBCJ4Shv4y1zgQ/vqVea+FLhzywvPj0ujSuofu+yA4MYHGZPTsgjBgJ+w==
|
||||
@ -5383,10 +5382,10 @@ table@^5.2.3:
|
||||
slice-ansi "^2.1.0"
|
||||
string-width "^3.0.0"
|
||||
|
||||
tarn@^2.0.0:
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/tarn/-/tarn-2.0.0.tgz#c68499f69881f99ae955b4317ca7d212d942fdee"
|
||||
integrity sha512-7rNMCZd3s9bhQh47ksAQd92ADFcJUjjbyOvyFjNLwTPpGieFHMC84S+LOzw0fx1uh6hnDz/19r8CPMnIjJlMMA==
|
||||
tarn@^3.0.0:
|
||||
version "3.0.0"
|
||||
resolved "https://registry.yarnpkg.com/tarn/-/tarn-3.0.0.tgz#a4082405216c0cce182b8b4cb2639c52c1e870d4"
|
||||
integrity sha512-PKUnlDFODZueoA8owLehl8vLcgtA8u4dRuVbZc92tspDYZixjJL6TqYOmryf/PfP/EBX+2rgNcrj96NO+RPkdQ==
|
||||
|
||||
tdigest@^0.1.1:
|
||||
version "0.1.1"
|
||||
@ -5704,17 +5703,22 @@ utils-merge@1.0.1:
|
||||
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
|
||||
integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=
|
||||
|
||||
uuid@^3.3.2, uuid@^3.3.3, uuid@^3.4.0:
|
||||
uuid@^3.3.2, uuid@^3.3.3:
|
||||
version "3.4.0"
|
||||
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
|
||||
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==
|
||||
|
||||
uuid@^7.0.3:
|
||||
version "7.0.3"
|
||||
resolved "https://registry.yarnpkg.com/uuid/-/uuid-7.0.3.tgz#c5c9f2c8cf25dc0a372c4df1441c41f5bd0c680b"
|
||||
integrity sha512-DPSke0pXhTZgoF/d+WSt2QaKMCFSfx7QegxEWT+JOuHF5aWrKEn0G+ztjuJg/gG8/ItK+rbPCD/yNv8yyih6Cg==
|
||||
|
||||
v8-compile-cache@^2.0.3:
|
||||
version "2.1.1"
|
||||
resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.1.1.tgz#54bc3cdd43317bca91e35dcaf305b1a7237de745"
|
||||
integrity sha512-8OQ9CL+VWyt3JStj7HX7/ciTL2V3Rl1Wf5OL+SNTm0yK1KvtReVulksyeRnCANHHuUxHlQig+JJDlUhBt1NQDQ==
|
||||
|
||||
v8flags@^3.1.3:
|
||||
v8flags@^3.2.0:
|
||||
version "3.2.0"
|
||||
resolved "https://registry.yarnpkg.com/v8flags/-/v8flags-3.2.0.tgz#b243e3b4dfd731fa774e7492128109a0fe66d656"
|
||||
integrity sha512-mH8etigqMfiGWdeXpaaqGfs6BndypxusHHcv2qSHyZkGEznCd/qAXCWWRzeowtL54147cktFOC4P5y+kl8d8Jg==
|
||||
|
Loading…
Reference in New Issue
Block a user