Skip to content

Commit

Permalink
refactor: move search implementation out of strategies store (#5642)
Browse files Browse the repository at this point in the history
This is first step of refactoring. Next steps follow with possibly a
query builder, or atleast using some reusable methods.
  • Loading branch information
sjaanus authored Dec 14, 2023
1 parent fbb5dd9 commit fa087fb
Show file tree
Hide file tree
Showing 11 changed files with 569 additions and 402 deletions.
2 changes: 2 additions & 0 deletions src/lib/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { ImportTogglesStore } from '../features/export-import-toggles/import-tog
import PrivateProjectStore from '../features/private-project/privateProjectStore';
import { DependentFeaturesStore } from '../features/dependent-features/dependent-features-store';
import LastSeenStore from '../services/client-metrics/last-seen/last-seen-store';
import FeatureSearchStore from '../features/feature-search/feature-search-store';

export const createStores = (
config: IUnleashConfig,
Expand Down Expand Up @@ -139,6 +140,7 @@ export const createStores = (
privateProjectStore: new PrivateProjectStore(db, getLogger),
dependentFeaturesStore: new DependentFeaturesStore(db),
lastSeenStore: new LastSeenStore(db, eventBus, getLogger),
featureSearchStore: new FeatureSearchStore(db, eventBus, getLogger),
};
};

Expand Down
13 changes: 6 additions & 7 deletions src/lib/features/feature-search/createFeatureSearchService.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
import { Db } from '../../db/db';
import { IUnleashConfig } from '../../types';

import FeatureStrategiesStore from '../feature-toggle/feature-toggle-strategies-store';
import { FeatureSearchService } from './feature-search-service';
import FakeFeatureStrategiesStore from '../feature-toggle/fakes/fake-feature-strategies-store';
import FakeFeatureSearchStore from './fake-feature-search-store';
import FeatureSearchStore from './feature-search-store';

export const createFeatureSearchService =
(config: IUnleashConfig) =>
(db: Db): FeatureSearchService => {
const { getLogger, eventBus, flagResolver } = config;
const featureStrategiesStore = new FeatureStrategiesStore(
const featureSearchStore = new FeatureSearchStore(
db,
eventBus,
getLogger,
flagResolver,
);

return new FeatureSearchService(
{ featureStrategiesStore: featureStrategiesStore },
{ featureSearchStore: featureSearchStore },
config,
);
};

export const createFakeFeatureSearchService = (
config: IUnleashConfig,
): FeatureSearchService => {
const fakeFeatureStrategiesStore = new FakeFeatureStrategiesStore();
const fakeFeatureSearchStore = new FakeFeatureSearchStore();

return new FeatureSearchService(
{
featureStrategiesStore: fakeFeatureStrategiesStore,
featureSearchStore: fakeFeatureSearchStore,
},
config,
);
Expand Down
15 changes: 15 additions & 0 deletions src/lib/features/feature-search/fake-feature-search-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { IFeatureOverview } from 'lib/types';
import {
IFeatureSearchParams,
IQueryParam,
} from '../feature-toggle/types/feature-toggle-strategies-store-type';
import { IFeatureSearchStore } from './feature-search-store-type';

export default class FakeFeatureSearchStore implements IFeatureSearchStore {
searchFeatures(
params: IFeatureSearchParams,
queryParams: IQueryParam[],
): Promise<{ features: IFeatureOverview[]; total: number }> {
throw new Error('Method not implemented.');
}
}
13 changes: 5 additions & 8 deletions src/lib/features/feature-search/feature-search-service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Logger } from '../../logger';
import {
IFeatureStrategiesStore,
IFeatureSearchStore,
IUnleashConfig,
IUnleashStores,
serializeDates,
} from '../../types';
import {
IFeatureSearchParams,
Expand All @@ -12,22 +11,20 @@ import {
} from '../feature-toggle/types/feature-toggle-strategies-store-type';

export class FeatureSearchService {
private featureStrategiesStore: IFeatureStrategiesStore;
private featureSearchStore: IFeatureSearchStore;
private logger: Logger;
constructor(
{
featureStrategiesStore,
}: Pick<IUnleashStores, 'featureStrategiesStore'>,
{ featureSearchStore }: Pick<IUnleashStores, 'featureSearchStore'>,
{ getLogger }: Pick<IUnleashConfig, 'getLogger'>,
) {
this.featureStrategiesStore = featureStrategiesStore;
this.featureSearchStore = featureSearchStore;
this.logger = getLogger('services/feature-search-service.ts');
}

async search(params: IFeatureSearchParams) {
const queryParams = this.convertToQueryParams(params);
const { features, total } =
await this.featureStrategiesStore.searchFeatures(
await this.featureSearchStore.searchFeatures(
{
...params,
limit: params.limit,
Expand Down
15 changes: 15 additions & 0 deletions src/lib/features/feature-search/feature-search-store-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
IFeatureSearchParams,
IQueryParam,
} from '../feature-toggle/types/feature-toggle-strategies-store-type';
import { IFeatureOverview } from '../../types';

export interface IFeatureSearchStore {
searchFeatures(
params: IFeatureSearchParams,
queryParams: IQueryParam[],
): Promise<{
features: IFeatureOverview[];
total: number;
}>;
}
Loading

0 comments on commit fa087fb

Please sign in to comment.