-
-
Notifications
You must be signed in to change notification settings - Fork 720
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: read model for dependent features (#4846)
- Loading branch information
Showing
10 changed files
with
111 additions
and
50 deletions.
There are no files selected for viewing
19 changes: 18 additions & 1 deletion
19
src/lib/features/dependent-features/createDependentFeaturesService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,27 @@ | ||
import { Db } from '../../db/db'; | ||
import { DependentFeaturesService } from './dependent-features-service'; | ||
import { DependentFeaturesStore } from './dependent-features-store'; | ||
import { DependentFeaturesReadModel } from './dependent-features-read-model'; | ||
import { FakeDependentFeaturesStore } from './fake-dependent-features-store'; | ||
import { FakeDependentFeaturesReadModel } from './fake-dependent-features-read-model'; | ||
|
||
export const createDependentFeaturesService = ( | ||
db: Db, | ||
): DependentFeaturesService => { | ||
const dependentFeaturesStore = new DependentFeaturesStore(db); | ||
return new DependentFeaturesService(dependentFeaturesStore); | ||
const dependentFeaturesReadModel = new DependentFeaturesReadModel(db); | ||
return new DependentFeaturesService( | ||
dependentFeaturesStore, | ||
dependentFeaturesReadModel, | ||
); | ||
}; | ||
|
||
export const createFakeDependentFeaturesService = | ||
(): DependentFeaturesService => { | ||
const dependentFeaturesStore = new FakeDependentFeaturesStore(); | ||
const dependentFeaturesReadModel = new FakeDependentFeaturesReadModel(); | ||
return new DependentFeaturesService( | ||
dependentFeaturesStore, | ||
dependentFeaturesReadModel, | ||
); | ||
}; |
5 changes: 5 additions & 0 deletions
5
src/lib/features/dependent-features/dependent-features-read-model-type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface IDependentFeaturesReadModel { | ||
getChildren(parent: string): Promise<string[]>; | ||
getParents(child: string): Promise<string[]>; | ||
getParentOptions(child: string): Promise<string[]>; | ||
} |
42 changes: 42 additions & 0 deletions
42
src/lib/features/dependent-features/dependent-features-read-model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Db } from '../../db/db'; | ||
import { IDependentFeaturesReadModel } from './dependent-features-read-model-type'; | ||
|
||
export class DependentFeaturesReadModel implements IDependentFeaturesReadModel { | ||
private db: Db; | ||
|
||
constructor(db: Db) { | ||
this.db = db; | ||
} | ||
|
||
async getChildren(parent: string): Promise<string[]> { | ||
const rows = await this.db('dependent_features').where( | ||
'parent', | ||
parent, | ||
); | ||
|
||
return rows.map((row) => row.child); | ||
} | ||
|
||
async getParents(child: string): Promise<string[]> { | ||
const rows = await this.db('dependent_features').where('child', child); | ||
|
||
return rows.map((row) => row.parent); | ||
} | ||
|
||
async getParentOptions(child: string): Promise<string[]> { | ||
const result = await this.db('features as f') | ||
.where('f.name', child) | ||
.select('f.project'); | ||
if (result.length === 0) { | ||
return []; | ||
} | ||
const rows = await this.db('features as f') | ||
.leftJoin('dependent_features as df', 'f.name', 'df.child') | ||
.where('f.project', result[0].project) | ||
.andWhere('f.name', '!=', child) | ||
.andWhere('df.child', null) | ||
.select('f.name'); | ||
|
||
return rows.map((item) => item.name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/lib/features/dependent-features/fake-dependent-features-read-model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { IDependentFeaturesReadModel } from './dependent-features-read-model-type'; | ||
|
||
export class FakeDependentFeaturesReadModel | ||
implements IDependentFeaturesReadModel | ||
{ | ||
getChildren(): Promise<string[]> { | ||
return Promise.resolve([]); | ||
} | ||
|
||
getParents(): Promise<string[]> { | ||
return Promise.resolve([]); | ||
} | ||
|
||
getParentOptions(): Promise<string[]> { | ||
return Promise.resolve([]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters