unleash-server
Version:
Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.
74 lines • 2.59 kB
JavaScript
import NotFoundError from '../../lib/error/notfound-error.js';
import groupBy from 'lodash.groupby';
export default class FakeClientInstanceStore {
constructor() {
this.instances = [];
}
async bulkUpsert(instances) {
instances.forEach((i) => {
this.instances.push({ createdAt: new Date(), ...i });
});
}
async delete(key) {
this.instances.splice(this.instances.findIndex((i) => i.instanceId === key.instanceId &&
i.appName === key.appName), 1);
}
async getBySdkName(sdkName) {
return this.instances.filter((instance) => instance.sdkVersion?.startsWith(sdkName));
}
async groupApplicationsBySdk() {
return Object.entries(groupBy(this.instances, 'sdkVersion')).map(([sdkVersion, apps]) => ({
sdkVersion,
applications: apps.map((item) => item.appName),
}));
}
async groupApplicationsBySdkAndProject(_projectId) {
throw new Error('Not implemented in mock');
}
async deleteAll() {
this.instances = [];
}
async deleteForApplication(appName) {
this.instances = this.instances.filter((i) => i.appName !== appName);
}
destroy() { }
async exists(key) {
return this.instances.some((i) => i.appName === key.appName && i.instanceId === key.instanceId);
}
async get(key) {
const instance = this.instances.find((i) => i.appName === key.appName && i.instanceId === key.instanceId);
if (instance) {
return instance;
}
throw new NotFoundError(`Could not find instance with key: ${key}`);
}
async getAll() {
return this.instances;
}
async getByAppName(appName) {
return this.instances.filter((i) => i.appName === appName);
}
async getRecentByAppNameAndEnvironment(appName, environment) {
return this.instances
.filter((i) => i.appName === appName)
.filter((i) => i.environment === environment);
}
async getDistinctApplications() {
const apps = new Set();
this.instances.forEach((i) => {
apps.add(i.appName);
});
return Array.from(apps.values());
}
async getDistinctApplicationsCount() {
const apps = await this.getDistinctApplications();
return apps.length;
}
async upsert(details) {
this.instances.push({ createdAt: new Date(), ...details });
}
removeOldInstances() {
return Promise.resolve(undefined);
}
}
//# sourceMappingURL=fake-client-instance-store.js.map