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 | 3x 12x 12x 12x 428x 8x 3x 123x 1x 7x 66x 8x 103x 7x 3x 8x 180x 7x 141x | import type {
ParsedFeature,
Segment,
Attribute,
Group,
FeatureKey,
Test,
EnvironmentKey,
ExistingState,
SegmentKey,
AttributeKey,
DatafileContent,
EntityType,
SchemaKey,
Schema,
} from "@featurevisor/types";
import { ProjectConfig } from "../config";
import type { CustomParser } from "@featurevisor/parsers";
import {
Adapter,
DatafileOptions,
FeatureEnvironmentProperty,
FeatureSourcePaths,
} from "./adapter";
export class Datasource {
private adapter: Adapter;
constructor(
private config: ProjectConfig,
private rootDirectoryPath?: string,
) {
this.adapter = new config.adapter(config, rootDirectoryPath);
}
// @NOTE: only site generator needs it, find a way to get it out of here later
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
*/
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);
}
getFeatureSourcePaths(featureKey: FeatureKey): Promise<FeatureSourcePaths | undefined> {
return this.adapter.getFeatureSourcePaths(featureKey);
}
getFeaturePropertySourcePath(
featureKey: FeatureKey,
property: FeatureEnvironmentProperty,
environment?: string,
): Promise<string | undefined> {
return this.adapter.getFeaturePropertySourcePath(featureKey, property, environment);
}
async getRequiredFeaturesChain(
featureKey: FeatureKey,
chain = new Set<FeatureKey>(),
): Promise<Set<FeatureKey>> {
chain.add(featureKey);
Iif (!this.adapter.entityExists("feature", featureKey)) {
throw new Error(`Feature not found: ${featureKey}`);
}
const feature = await this.readFeature(featureKey);
Iif (!feature.required) {
return chain;
}
for (const r of feature.required) {
const requiredKey = typeof r === "string" ? r : r.key;
Iif (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);
Iif (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);
}
// 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);
}
}
|