All files / src/datasource datasource.ts

44.3% Statements 35/79
17.64% Branches 3/17
53.84% Functions 28/52
44.3% Lines 35/79

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306                                      4x         33x         75x 75x 75x   75x 75x 75x       135x       1x       42x       21x       1551x             1x       2x                                   1x                                 68x       7x       461x       5x                                                                           63x               218x                         66x                                                         349x                         61x               10x                         66x               495x                         69x               22x       3x       1x         61x       406x       4x                                        
import type {
  ParsedFeature,
  Segment,
  Attribute,
  Group,
  FeatureKey,
  Test,
  EnvironmentKey,
  ExistingState,
  SegmentKey,
  AttributeKey,
  DatafileContent,
  EntityType,
  SchemaKey,
  Schema,
  Target,
  TargetKey,
} from "@featurevisor/types";
 
import { getProjectConfigForSet, ProjectConfig } from "../config";
import type { CustomParser } from "@featurevisor/parsers";
 
import { Adapter, DatafileFile, DatafileOptions } from "./adapter";
 
export class Datasource {
  private adapter: Adapter;
  private rootConfig: ProjectConfig;
 
  constructor(
    private config: ProjectConfig,
    private rootDirectoryPath?: string,
    private set?: string,
  ) {
    this.rootConfig = config;
    this.config = set ? getProjectConfigForSet(this.rootConfig, set) : config;
    this.adapter = new this.config.adapter(this.config, rootDirectoryPath);
  }
 
  getConfig() {
    return this.config;
  }
 
  getSet() {
    return this.set;
  }
 
  forSet(set: string) {
    return new Datasource(this.rootConfig, this.rootDirectoryPath, set);
  }
 
  listSets() {
    return this.adapter.listSets();
  }
 
  getExtension() {
    return (this.config.parser as CustomParser).extension;
  }
 
  /**
   * State
   */
  readState(environment: EnvironmentKey | false): Promise<ExistingState> {
    return this.adapter.readState(environment);
  }
 
  writeState(environment: EnvironmentKey | false, existingState: ExistingState) {
    return this.adapter.writeState(environment, existingState);
  }
 
  /**
   * Revision
   */
  readRevision() {
    return this.adapter.readRevision();
  }
 
  writeRevision(revision: string) {
    return this.adapter.writeRevision(revision);
  }
 
  /**
   * Datafile
   */
  listDatafiles(): Promise<DatafileFile[]> {
    return this.adapter.listDatafiles ? this.adapter.listDatafiles() : Promise.resolve([]);
  }
 
  readDatafile(options: DatafileOptions) {
    return this.adapter.readDatafile(options);
  }
 
  writeDatafile(datafileContent: DatafileContent, options: DatafileOptions) {
    return this.adapter.writeDatafile(datafileContent, options);
  }
 
  /**
   * Entity specific methods
   */
 
  // features
  listFeatures() {
    return this.adapter.listEntities("feature");
  }
 
  featureExists(featureKey: FeatureKey) {
    return this.adapter.entityExists("feature", featureKey);
  }
 
  readFeature(featureKey: FeatureKey) {
    return this.adapter.readEntity<ParsedFeature>("feature", featureKey);
  }
 
  writeFeature(featureKey: FeatureKey, feature: ParsedFeature) {
    return this.adapter.writeEntity<ParsedFeature>("feature", featureKey, feature);
  }
 
  deleteFeature(featureKey: FeatureKey) {
    return this.adapter.deleteEntity("feature", featureKey);
  }
 
  async getRequiredFeaturesChain(
    featureKey: FeatureKey,
    chain = new Set<FeatureKey>(),
  ): Promise<Set<FeatureKey>> {
    chain.add(featureKey);
 
    if (!this.adapter.entityExists("feature", featureKey)) {
      throw new Error(`Feature not found: ${featureKey}`);
    }
 
    const feature = await this.readFeature(featureKey);
 
    if (!feature.required) {
      return chain;
    }
 
    for (const r of feature.required) {
      const requiredKey = typeof r === "string" ? r : r.key;
 
      if (chain.has(requiredKey)) {
        throw new Error(`Circular dependency detected: ${chain.toString()}`);
      }
 
      await this.getRequiredFeaturesChain(requiredKey, chain);
    }
 
    return chain;
  }
 
  // segments
  listSegments() {
    return this.adapter.listEntities("segment");
  }
 
  segmentExists(segmentKey: SegmentKey) {
    return this.adapter.entityExists("segment", segmentKey);
  }
 
  readSegment(segmentKey: SegmentKey) {
    return this.adapter.readEntity<Segment>("segment", segmentKey);
  }
 
  writeSegment(segmentKey: SegmentKey, segment: Segment) {
    return this.adapter.writeEntity<Segment>("segment", segmentKey, segment);
  }
 
  deleteSegment(segmentKey: SegmentKey) {
    return this.adapter.deleteEntity("segment", segmentKey);
  }
 
  // attributes
  listAttributes() {
    return this.adapter.listEntities("attribute");
  }
 
  async listFlattenedAttributes() {
    const attributes = await this.listAttributes();
    const result: string[] = [];
 
    for (const key of attributes) {
      const attribute = await this.readAttribute(key);
 
      result.push(key);
 
      if (attribute.type === "object") {
        // @NOTE: in future, this can be recursive
        const propertyKeys = Object.keys(attribute.properties || {});
        for (const propertyKey of propertyKeys) {
          result.push(`${key}.${propertyKey}`);
        }
      }
    }
 
    return result;
  }
 
  attributeExists(attributeKey: AttributeKey) {
    return this.adapter.entityExists("attribute", attributeKey);
  }
 
  readAttribute(attributeKey: AttributeKey) {
    return this.adapter.readEntity<Attribute>("attribute", attributeKey);
  }
 
  writeAttribute(attributeKey: AttributeKey, attribute: Attribute) {
    return this.adapter.writeEntity<Attribute>("attribute", attributeKey, attribute);
  }
 
  deleteAttribute(attributeKey: AttributeKey) {
    return this.adapter.deleteEntity("attribute", attributeKey);
  }
 
  // groups
  listGroups() {
    return this.adapter.listEntities("group");
  }
 
  groupExists(groupKey: string) {
    return this.adapter.entityExists("group", groupKey);
  }
 
  readGroup(groupKey: string) {
    return this.adapter.readEntity<Group>("group", groupKey);
  }
 
  writeGroup(groupKey: string, group: Group) {
    return this.adapter.writeEntity<Group>("group", groupKey, group);
  }
 
  deleteGroup(groupKey: string) {
    return this.adapter.deleteEntity("group", groupKey);
  }
 
  // schemas
  listSchemas() {
    return this.adapter.listEntities("schema");
  }
 
  schemaExists(schemaKey: SchemaKey) {
    return this.adapter.entityExists("schema", schemaKey);
  }
 
  readSchema(schemaKey: SchemaKey) {
    return this.adapter.readEntity<Schema>("schema", schemaKey);
  }
 
  writeSchema(schemaKey: SchemaKey, schema: Schema) {
    return this.adapter.writeEntity<Schema>("schema", schemaKey, schema);
  }
 
  deleteSchema(schemaKey: SchemaKey) {
    return this.adapter.deleteEntity("schema", schemaKey);
  }
 
  // targets
  listTargets() {
    return this.adapter.listEntities("target");
  }
 
  targetExists(targetKey: TargetKey) {
    return this.adapter.entityExists("target", targetKey);
  }
 
  readTarget(targetKey: TargetKey) {
    return this.adapter.readEntity<Target>("target", targetKey);
  }
 
  writeTarget(targetKey: TargetKey, target: Target) {
    return this.adapter.writeEntity<Target>("target", targetKey, target);
  }
 
  deleteTarget(targetKey: TargetKey) {
    return this.adapter.deleteEntity("target", targetKey);
  }
 
  // tests
  listTests() {
    return this.adapter.listEntities("test");
  }
 
  readTest(testKey: string) {
    return this.adapter.readEntity<Test>("test", testKey);
  }
 
  writeTest(testKey: string, test: Test) {
    return this.adapter.writeEntity<Test>("test", testKey, test);
  }
 
  deleteTest(testKey: string) {
    return this.adapter.deleteEntity("test", testKey);
  }
 
  getTestSpecName(testKey: string) {
    return `${testKey}.${this.getExtension()}`;
  }
 
  // history
  listHistoryEntries(entityType?: EntityType, entityKey?: string) {
    return this.adapter.listHistoryEntries(entityType, entityKey);
  }
 
  readCommit(commitHash: string, entityType?: EntityType, entityKey?: string) {
    return this.adapter.readCommit(commitHash, entityType, entityKey);
  }
}