2023-09-27 14:33:51 +02:00
|
|
|
import { Db } from '../../db/db';
|
|
|
|
import { IDependentFeaturesReadModel } from './dependent-features-read-model-type';
|
2023-09-27 15:07:20 +02:00
|
|
|
import { IDependency } from '../../types';
|
2023-09-27 14:33:51 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-09-27 15:07:20 +02:00
|
|
|
async getParents(child: string): Promise<IDependency[]> {
|
2023-09-27 14:33:51 +02:00
|
|
|
const rows = await this.db('dependent_features').where('child', child);
|
|
|
|
|
2023-09-27 15:07:20 +02:00
|
|
|
return rows.map((row) => ({
|
|
|
|
feature: row.parent,
|
|
|
|
enabled: row.enabled,
|
|
|
|
variants: row.variants,
|
|
|
|
}));
|
2023-09-27 14:33:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|