Refactor to use string IDs for executables #262
This commit unifies the concepts of executables having same ID structure. It paves the way for more complex ID structure and using IDs in collection files as part of new ID solution (#262). Using string IDs also leads to more expressive test code. This commit also refactors the rest of the code to adopt to the changes. This commit: - Separate concerns from entities for data access (in repositories) and executables. Executables use `Identifiable` meanwhile repositories use `RepositoryEntity`. - Refactor unnecessary generic parameters for enttities and ids, enforcing string gtype everwyhere. - Changes numeric IDs to string IDs for categories to unify the retrieval and construction for executables, using pseudo-ids (their names) just like scripts. - Remove `BaseEntity` for simplicity. - Simplify usage and construction of executable objects. Move factories responsible for creation of category/scripts to domain layer. Do not longer export `CollectionCategorY` and `CollectionScript`. - Use named typed for string IDs for better differentation of different ID contexts in code.
This commit is contained in:
50
src/domain/Executables/Script/ScriptFactory.ts
Normal file
50
src/domain/Executables/Script/ScriptFactory.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { RecommendationLevel } from './RecommendationLevel';
|
||||
import type { ScriptCode } from './Code/ScriptCode';
|
||||
import type { Script } from './Script';
|
||||
|
||||
export interface ScriptInitParameters {
|
||||
readonly executableId: string;
|
||||
readonly name: string;
|
||||
readonly code: ScriptCode;
|
||||
readonly docs: ReadonlyArray<string>;
|
||||
readonly level?: RecommendationLevel;
|
||||
}
|
||||
|
||||
export type ScriptFactory = (
|
||||
parameters: ScriptInitParameters,
|
||||
) => Script;
|
||||
|
||||
export const createScript: ScriptFactory = (parameters) => {
|
||||
return new CollectionScript(parameters);
|
||||
};
|
||||
|
||||
class CollectionScript implements Script {
|
||||
public readonly executableId: string;
|
||||
|
||||
public readonly name: string;
|
||||
|
||||
public readonly code: ScriptCode;
|
||||
|
||||
public readonly docs: ReadonlyArray<string>;
|
||||
|
||||
public readonly level?: RecommendationLevel;
|
||||
|
||||
constructor(parameters: ScriptInitParameters) {
|
||||
this.executableId = parameters.executableId;
|
||||
this.name = parameters.name;
|
||||
this.code = parameters.code;
|
||||
this.docs = parameters.docs;
|
||||
this.level = parameters.level;
|
||||
validateLevel(parameters.level);
|
||||
}
|
||||
|
||||
public canRevert(): boolean {
|
||||
return Boolean(this.code.revert);
|
||||
}
|
||||
}
|
||||
|
||||
function validateLevel(level?: RecommendationLevel) {
|
||||
if (level !== undefined && !(level in RecommendationLevel)) {
|
||||
throw new Error(`invalid level: ${level}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user