unleash-server
Version:
Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.
64 lines • 2.07 kB
JavaScript
import metricsHelper from '../../util/metrics-helper.js';
import { DB_TIME } from '../../metric-events.js';
import { defaultToRow } from '../../db/crud/default-mappings.js';
const TABLE = 'jobs';
const toRow = (data) => defaultToRow(data);
export class JobStore {
constructor(db, config) {
this.db = db;
this.timer = (action) => metricsHelper.wrapTimer(config.eventBus, DB_TIME, {
store: TABLE,
action,
});
}
async acquireBucket(key, bucketLengthInMinutes) {
const endTimer = this.timer('acquireBucket');
const bucket = await this.db(TABLE)
.insert({
name: key,
// note: date_floor_round is a custom function defined in the DB
bucket: this.db.raw(`date_floor_round(now(), '${bucketLengthInMinutes} minutes')`),
stage: 'started',
})
.onConflict(['name', 'bucket'])
.ignore()
.returning(['name', 'bucket']);
endTimer();
return bucket[0];
}
async update(name, bucket, data) {
const rows = await this.db(TABLE)
.update(toRow(data))
.where({ name, bucket })
.returning('*');
return rows[0];
}
async get(pk) {
const rows = await this.db(TABLE).where(pk);
return rows[0];
}
async getAll(query) {
if (query) {
return this.db(TABLE).where(query);
}
return this.db(TABLE);
}
async exists(key) {
const result = await this.db.raw(`SELECT EXISTS(SELECT 1 FROM ${TABLE} WHERE name = ? AND bucket = ?) AS present`, [key.name, key.bucket]);
const { present } = result.rows[0];
return present;
}
async delete(key) {
await this.db(TABLE).where(key).delete();
}
async deleteAll() {
return this.db(TABLE).delete();
}
destroy() { }
async count() {
return this.db(TABLE)
.count()
.then((res) => Number(res[0].count));
}
}
//# sourceMappingURL=job-store.js.map