@cyberismo/data-handler 0.0.12 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/dist/command-handler.d.ts +24 -42
  2. package/dist/command-handler.js +30 -25
  3. package/dist/command-handler.js.map +1 -1
  4. package/dist/commands/import.js +2 -2
  5. package/dist/commands/import.js.map +1 -1
  6. package/dist/commands/show.d.ts +4 -0
  7. package/dist/commands/show.js +4 -0
  8. package/dist/commands/show.js.map +1 -1
  9. package/dist/commands/update.d.ts +11 -1
  10. package/dist/commands/update.js +14 -2
  11. package/dist/commands/update.js.map +1 -1
  12. package/dist/commands/validate.d.ts +2 -1
  13. package/dist/commands/validate.js +3 -2
  14. package/dist/commands/validate.js.map +1 -1
  15. package/dist/containers/card-container.js +1 -1
  16. package/dist/containers/card-container.js.map +1 -1
  17. package/dist/containers/project/calculation-engine.js +1 -1
  18. package/dist/containers/project/calculation-engine.js.map +1 -1
  19. package/dist/index.d.ts +4 -2
  20. package/dist/index.js.map +1 -1
  21. package/dist/interfaces/command-options.d.ts +81 -0
  22. package/dist/interfaces/command-options.js +14 -0
  23. package/dist/interfaces/command-options.js.map +1 -0
  24. package/dist/interfaces/folder-content-interfaces.d.ts +50 -0
  25. package/dist/interfaces/folder-content-interfaces.js +45 -0
  26. package/dist/interfaces/folder-content-interfaces.js.map +1 -0
  27. package/dist/interfaces/resource-interfaces.d.ts +28 -10
  28. package/dist/interfaces/resource-interfaces.js.map +1 -1
  29. package/dist/macros/report/index.js +4 -4
  30. package/dist/macros/report/index.js.map +1 -1
  31. package/dist/resources/card-type-resource.js +11 -5
  32. package/dist/resources/card-type-resource.js.map +1 -1
  33. package/dist/resources/field-type-resource.js +1 -1
  34. package/dist/resources/field-type-resource.js.map +1 -1
  35. package/dist/resources/folder-resource.d.ts +37 -9
  36. package/dist/resources/folder-resource.js +108 -12
  37. package/dist/resources/folder-resource.js.map +1 -1
  38. package/dist/resources/graph-model-resource.d.ts +7 -4
  39. package/dist/resources/graph-model-resource.js +12 -25
  40. package/dist/resources/graph-model-resource.js.map +1 -1
  41. package/dist/resources/graph-view-resource.d.ts +7 -4
  42. package/dist/resources/graph-view-resource.js +12 -26
  43. package/dist/resources/graph-view-resource.js.map +1 -1
  44. package/dist/resources/link-type-resource.js +1 -1
  45. package/dist/resources/link-type-resource.js.map +1 -1
  46. package/dist/resources/report-resource.d.ts +14 -10
  47. package/dist/resources/report-resource.js +41 -45
  48. package/dist/resources/report-resource.js.map +1 -1
  49. package/dist/resources/resource-object.d.ts +7 -0
  50. package/dist/resources/resource-object.js.map +1 -1
  51. package/dist/resources/template-resource.d.ts +5 -1
  52. package/dist/resources/template-resource.js +12 -7
  53. package/dist/resources/template-resource.js.map +1 -1
  54. package/dist/resources/workflow-resource.js +6 -0
  55. package/dist/resources/workflow-resource.js.map +1 -1
  56. package/package.json +9 -7
  57. package/src/command-handler.ts +71 -58
  58. package/src/commands/import.ts +2 -0
  59. package/src/commands/show.ts +4 -0
  60. package/src/commands/update.ts +20 -2
  61. package/src/commands/validate.ts +6 -2
  62. package/src/containers/card-container.ts +1 -1
  63. package/src/containers/project/calculation-engine.ts +1 -3
  64. package/src/index.ts +36 -2
  65. package/src/interfaces/command-options.ts +144 -0
  66. package/src/interfaces/folder-content-interfaces.ts +69 -0
  67. package/src/interfaces/resource-interfaces.ts +41 -12
  68. package/src/macros/report/index.ts +4 -4
  69. package/src/resources/card-type-resource.ts +12 -6
  70. package/src/resources/field-type-resource.ts +1 -1
  71. package/src/resources/folder-resource.ts +149 -19
  72. package/src/resources/graph-model-resource.ts +16 -27
  73. package/src/resources/graph-view-resource.ts +16 -28
  74. package/src/resources/link-type-resource.ts +1 -1
  75. package/src/resources/report-resource.ts +60 -62
  76. package/src/resources/resource-object.ts +11 -0
  77. package/src/resources/template-resource.ts +12 -7
  78. package/src/resources/workflow-resource.ts +4 -0
@@ -11,7 +11,11 @@
11
11
  License along with this program. If not, see <https://www.gnu.org/licenses/>.
12
12
  */
13
13
 
14
- import type { Schema } from 'jsonschema';
14
+ import type {
15
+ GraphModelContent,
16
+ GraphViewContent,
17
+ ReportContent,
18
+ } from './folder-content-interfaces.js';
15
19
 
16
20
  /**
17
21
  * Each resource represents a file (or a folder in some cases) with metadata stored
@@ -32,6 +36,12 @@ export interface CardType extends ResourceBaseMetadata {
32
36
  optionallyVisibleFields: string[];
33
37
  }
34
38
 
39
+ // Base content update key interface
40
+ export interface ContentUpdateKey {
41
+ key: 'content';
42
+ subKey: string; // Resource-specific types should narrow this
43
+ }
44
+
35
45
  // Custom field
36
46
  // todo: merge with FieldType.
37
47
  export interface CustomField {
@@ -71,19 +81,29 @@ export interface FieldType extends ResourceBaseMetadata {
71
81
  export interface GraphModelMetadata extends ResourceBaseMetadata {
72
82
  category?: string;
73
83
  }
74
-
75
84
  export interface GraphModel extends GraphModelMetadata {
76
- calculationFile: string;
85
+ content: GraphModelContent;
77
86
  }
87
+ export type GraphModelContentPropertyName = 'model';
88
+ export interface GraphModelContentUpdateKey {
89
+ key: 'content';
90
+ subKey: GraphModelContentPropertyName;
91
+ }
92
+ export type GraphModelUpdateKey = string | GraphModelContentUpdateKey;
78
93
 
79
94
  // Graph view content.
80
95
  export interface GraphViewMetadata extends ResourceBaseMetadata {
81
96
  category?: string;
82
97
  }
83
-
98
+ export type GraphViewContentPropertyName = 'viewTemplate';
84
99
  export interface GraphView extends GraphViewMetadata {
85
- handleBarFile: string;
100
+ content: GraphViewContent;
86
101
  }
102
+ export interface GraphViewContentUpdateKey {
103
+ key: 'content';
104
+ subKey: GraphViewContentPropertyName;
105
+ }
106
+ export type GraphViewUpdateKey = string | GraphViewContentUpdateKey;
87
107
 
88
108
  // Link content.
89
109
  export interface Link {
@@ -103,13 +123,20 @@ export interface LinkType extends ResourceBaseMetadata {
103
123
 
104
124
  // Report resource.
105
125
  export interface Report extends ResourceBaseMetadata {
106
- name: string;
107
- metadata: ReportMetadata;
108
- contentTemplate: string;
109
- queryTemplate: string;
110
- schema?: Schema;
126
+ content: ReportContent;
111
127
  }
112
128
 
129
+ // Resource-specific content names
130
+ export type ReportContentPropertyName =
131
+ | 'contentTemplate'
132
+ | 'queryTemplate'
133
+ | 'schema';
134
+ export interface ReportContentUpdateKey {
135
+ key: 'content';
136
+ subKey: ReportContentPropertyName;
137
+ }
138
+ export type ReportUpdateKey = string | ReportContentUpdateKey;
139
+
113
140
  // Metadata for report
114
141
  export interface ReportMetadata extends ResourceBaseMetadata {
115
142
  category: string;
@@ -136,10 +163,9 @@ export type ResourceContent =
136
163
  | Workflow;
137
164
 
138
165
  // Template configuration details.
139
- export interface TemplateConfiguration extends ResourceBaseMetadata {
166
+ export interface TemplateConfiguration extends TemplateMetadata {
140
167
  path: string;
141
168
  numberOfCards: number;
142
- metadata: TemplateMetadata;
143
169
  }
144
170
 
145
171
  // Template configuration content details.
@@ -147,6 +173,9 @@ export interface TemplateMetadata extends ResourceBaseMetadata {
147
173
  category?: string;
148
174
  }
149
175
 
176
+ // Generic update key
177
+ export type UpdateKey = string | ContentUpdateKey;
178
+
150
179
  // Workflow's json file content.
151
180
  export interface Workflow extends ResourceBaseMetadata {
152
181
  states: WorkflowState[];
@@ -45,16 +45,16 @@ class ReportMacro extends BaseMacro {
45
45
 
46
46
  if (!report) throw new Error(`Report ${options.name} does not exist`);
47
47
 
48
- if (report.schema) {
48
+ if (report.content.schema) {
49
49
  validateJson(options, {
50
- schema: report.schema,
50
+ schema: report.content.schema,
51
51
  });
52
52
  }
53
53
  try {
54
54
  return await generateReportContent({
55
55
  calculate: context.project.calculationEngine,
56
- contentTemplate: report.contentTemplate,
57
- queryTemplate: report.queryTemplate,
56
+ contentTemplate: report.content.contentTemplate,
57
+ queryTemplate: report.content.queryTemplate,
58
58
  options: {
59
59
  cardKey: context.cardKey,
60
60
  ...options,
@@ -233,7 +233,10 @@ export class CardTypeResource extends FileResource {
233
233
  }
234
234
 
235
235
  // If value from 'customFields' is removed, remove it also from 'optionallyVisible' and 'alwaysVisible' arrays.
236
- private removeValueFromOtherArrays<Type>(op: Operation<Type>) {
236
+ private removeValueFromOtherArrays<Type>(
237
+ op: Operation<Type>,
238
+ content: CardType,
239
+ ) {
237
240
  // Update target can be a string, or an object. Of object, fetch only 'name'
238
241
  // todo: fetching 'name' or using string as name could be function in resource base class.
239
242
  const target = (op as RemoveOperation<Type>).target as Type;
@@ -242,8 +245,8 @@ export class CardTypeResource extends FileResource {
242
245
  field = { name: target['name' as keyof Type] };
243
246
  }
244
247
  const fieldName = (field ? field.name : target) as string;
245
- this.removeValue(this.data.alwaysVisibleFields, fieldName);
246
- this.removeValue(this.data.optionallyVisibleFields, fieldName);
248
+ this.removeValue(content.alwaysVisibleFields, fieldName);
249
+ this.removeValue(content.optionallyVisibleFields, fieldName);
247
250
  }
248
251
 
249
252
  // Sets content container values to be either '[]' or with proper values.
@@ -481,7 +484,7 @@ export class CardTypeResource extends FileResource {
481
484
  const existingName = this.content.name;
482
485
  await super.update(key, op);
483
486
 
484
- const content = this.content as CardType;
487
+ const content = structuredClone(this.content) as CardType;
485
488
  if (key === 'name') {
486
489
  content.name = super.handleScalar(op) as string;
487
490
  } else if (key === 'alwaysVisibleFields') {
@@ -513,12 +516,15 @@ export class CardTypeResource extends FileResource {
513
516
  content.customFields as Type[],
514
517
  ) as CustomField[];
515
518
  if (op.name === 'remove') {
516
- this.removeValueFromOtherArrays(op);
519
+ this.removeValueFromOtherArrays(op, content);
517
520
  }
521
+ } else if (key === 'description') {
522
+ content.description = super.handleScalar(op) as string;
523
+ } else if (key === 'displayName') {
524
+ content.displayName = super.handleScalar(op) as string;
518
525
  } else {
519
526
  throw new Error(`Unknown property '${key}' for CardType`);
520
527
  }
521
-
522
528
  await super.postUpdate(content, key, op);
523
529
 
524
530
  // Renaming this card type causes that references to its name must be updated.
@@ -433,7 +433,7 @@ export class FieldTypeResource extends FileResource {
433
433
 
434
434
  await super.update(key, op);
435
435
 
436
- const content = this.content as FieldType;
436
+ const content = structuredClone(this.content) as FieldType;
437
437
  if (key === 'name') {
438
438
  content.name = super.handleScalar(op) as string;
439
439
  } else if (key === 'dataType') {
@@ -1,35 +1,44 @@
1
1
  /**
2
2
  Cyberismo
3
3
  Copyright © Cyberismo Ltd and contributors 2024
4
+
4
5
  This program is free software: you can redistribute it and/or modify it under
5
6
  the terms of the GNU Affero General Public License version 3 as published by
6
- the Free Software Foundation.
7
- This program is distributed in the hope that it will be useful, but WITHOUT
8
- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9
- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
10
- details. You should have received a copy of the GNU Affero General Public
7
+ the Free Software Foundation. This program is distributed in the hope that it
8
+ will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
9
+ of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
+ See the GNU Affero General Public License for more details.
11
+ You should have received a copy of the GNU Affero General Public
11
12
  License along with this program. If not, see <https://www.gnu.org/licenses/>.
12
13
  */
13
14
 
14
15
  import { basename, dirname, join, normalize } from 'node:path';
15
16
  import { mkdir, readdir, readFile, rename, rm } from 'node:fs/promises';
16
17
 
17
- import { writeFileSafe } from '../utils/file-utils.js';
18
-
18
+ import type { Card, Operation, ResourceName } from './file-resource.js';
19
+ import type {
20
+ ContentUpdateKey,
21
+ ResourceContent,
22
+ UpdateKey,
23
+ } from '../interfaces/resource-interfaces.js';
19
24
  import type { ResourceFolderType } from '../interfaces/project-interfaces.js';
25
+ import type { Schema } from 'jsonschema';
26
+
20
27
  import {
21
- type Card,
22
28
  DefaultContent,
23
29
  FileResource,
24
- type Operation,
25
30
  Project,
26
31
  resourceName,
27
- type ResourceName,
28
32
  resourceNameToString,
29
33
  sortCards,
30
34
  } from './file-resource.js';
31
- import type { ResourceContent } from '../interfaces/resource-interfaces.js';
35
+ import {
36
+ filename,
37
+ propertyName,
38
+ } from '../interfaces/folder-content-interfaces.js';
39
+ import { readJsonFile } from '../utils/json.js';
32
40
  import { VALID_FOLDER_RESOURCE_FILES } from '../utils/constants.js';
41
+ import { writeFileSafe } from '../utils/file-utils.js';
33
42
 
34
43
  export {
35
44
  type Card,
@@ -48,10 +57,23 @@ export {
48
57
  export class FolderResource extends FileResource {
49
58
  protected internalFolder: string = '';
50
59
 
60
+ // Cache for content files to avoid repeated filesystem operations. Content is stored as string.
61
+ private contentFilesCache = new Map<string, string>();
62
+
51
63
  constructor(project: Project, name: ResourceName, type: ResourceFolderType) {
52
64
  super(project, name, type);
53
65
  }
54
66
 
67
+ // Clears the content files cache.
68
+ private clearContentCache() {
69
+ this.contentFilesCache.clear();
70
+ }
71
+
72
+ // Type guard to check if a key is a ContentUpdateKey
73
+ private isContentUpdateKey(key: UpdateKey): key is ContentUpdateKey {
74
+ return typeof key === 'object' && key.key === 'content' && 'subKey' in key;
75
+ }
76
+
55
77
  /**
56
78
  * Creates a new folder type object. Base class writes the object to disk automatically.
57
79
  * @param newContent Content for the type.
@@ -61,6 +83,25 @@ export class FolderResource extends FileResource {
61
83
  await mkdir(this.internalFolder, { recursive: true });
62
84
  }
63
85
 
86
+ /**
87
+ * Gets content of all files to properties.
88
+ * @returns object with property names as keys and file contents as values.
89
+ */
90
+ public async contentData(): Promise<Record<string, string | Schema>> {
91
+ const fileNames = await this.showFileNames();
92
+ const content: Record<string, string | Schema> = {};
93
+
94
+ for (const fileName of fileNames) {
95
+ const name = propertyName(fileName);
96
+ if (name) {
97
+ const JSONFile = name === 'schema';
98
+ content[name] = await this.showFile(fileName, JSONFile);
99
+ }
100
+ }
101
+
102
+ return content;
103
+ }
104
+
64
105
  /**
65
106
  * Returns content data.
66
107
  */
@@ -74,6 +115,7 @@ export class FolderResource extends FileResource {
74
115
  protected async delete() {
75
116
  await super.delete();
76
117
  await rm(this.internalFolder, { recursive: true, force: true });
118
+ this.clearContentCache();
77
119
  }
78
120
 
79
121
  // Get (resource folder) type name
@@ -81,11 +123,15 @@ export class FolderResource extends FileResource {
81
123
  return super.getType;
82
124
  }
83
125
 
126
+ // Get logger instance
84
127
  protected get logger() {
85
128
  return super.getLogger(this.getType);
86
129
  }
87
130
 
88
- protected initialize(): void {
131
+ /**
132
+ * Initialize the resource item.
133
+ */
134
+ protected initialize() {
89
135
  super.initialize();
90
136
 
91
137
  this.internalFolder = join(
@@ -111,22 +157,69 @@ export class FolderResource extends FileResource {
111
157
  }
112
158
 
113
159
  /**
160
+ * TODO: to be made protected - no direct access to files
114
161
  * Shows the content of a file in the resource.
115
162
  * @param fileName Name of the file to show.
163
+ * @param json Content is JSON file.
116
164
  * @returns the content of the file.
117
165
  */
118
- public async showFile(fileName: string): Promise<string> {
166
+ public async showFile(
167
+ fileName: string,
168
+ json: boolean = false,
169
+ ): Promise<string> {
170
+ // Always first check cache...
171
+ if (this.contentFilesCache.has(fileName)) {
172
+ const cached = this.contentFilesCache.get(fileName)!;
173
+ return json ? JSON.parse(cached) : cached;
174
+ }
175
+
176
+ // ...cache miss, read from filesystem
119
177
  const filePath = join(this.internalFolder, fileName);
120
- return readFile(filePath, 'utf8');
178
+ const content = json
179
+ ? await readJsonFile(filePath)
180
+ : await readFile(filePath, 'utf8');
181
+
182
+ // Update cache
183
+ const contentStr =
184
+ typeof content === 'string' ? content : JSON.stringify(content, null, 2);
185
+ this.contentFilesCache.set(fileName, contentStr);
186
+
187
+ return json ? content : contentStr;
121
188
  }
122
189
 
123
190
  /**
191
+ * TODO: to be made protected - no direct access to files
124
192
  * Shows all file names in the resource.
125
193
  * @returns all file names in the resource.
126
194
  */
127
195
  public async showFileNames(): Promise<string[]> {
196
+ // Always first check cache...
197
+ if (this.contentFilesCache.size > 0) {
198
+ return Array.from(this.contentFilesCache.keys());
199
+ }
200
+
201
+ // ...cache miss, read from filesystem and populate cache
128
202
  const files = await readdir(this.internalFolder);
129
- return files.filter((file) => VALID_FOLDER_RESOURCE_FILES.includes(file));
203
+ const validFiles = files.filter((file) =>
204
+ VALID_FOLDER_RESOURCE_FILES.includes(file),
205
+ );
206
+
207
+ // Update cache by reading all files. Each method call updates specific cache item.
208
+ for (const fileName of validFiles) {
209
+ await this.showFile(fileName);
210
+ }
211
+
212
+ return validFiles;
213
+ }
214
+
215
+ /**
216
+ * Updates content files from a content object.
217
+ * @param contentFiles Object with file names as keys and file contents as values.
218
+ */
219
+ public async updateContentFiles(contentFiles: Record<string, string>) {
220
+ for (const [fileName, fileContent] of Object.entries(contentFiles)) {
221
+ await this.updateFile(fileName, fileContent);
222
+ }
130
223
  }
131
224
 
132
225
  /**
@@ -148,22 +241,59 @@ export class FolderResource extends FileResource {
148
241
  if (basename(normalizedFilePath) !== fileName) {
149
242
  throw new Error(`File '${fileName}' is not in the resource`);
150
243
  }
151
- // check if the file is whitelisted
244
+ // check if the file is allow-listed
152
245
  if (!VALID_FOLDER_RESOURCE_FILES.includes(fileName)) {
153
- throw new Error(`File '${fileName}' is not whitelisted`);
246
+ throw new Error(`File '${fileName}' is not allowed`);
154
247
  }
155
248
 
156
249
  await writeFileSafe(filePath, changedContent, { flag: 'w' });
250
+
251
+ // Update cache with new content
252
+ this.contentFilesCache.set(fileName, changedContent);
157
253
  }
254
+
158
255
  /**
159
256
  * Updates resource.
160
257
  * @param key Key to modify
161
258
  * @param op Operation to perform on 'key'
259
+ * @throws if key is unknown.
162
260
  */
163
- protected async update<Type>(key: string, op: Operation<Type>) {
164
- return super.update(key, op);
261
+ protected async update<Type>(key: UpdateKey, op: Operation<Type>) {
262
+ if (this.isContentUpdateKey(key)) {
263
+ const fileName = filename(key.subKey)!;
264
+ const fileContent = super.handleScalar(op) as string;
265
+ await this.updateFile(fileName, fileContent);
266
+ return;
267
+ }
268
+
269
+ const nameChange = key === 'name';
270
+ const existingName = this.content.name;
271
+ await super.update(key, op);
272
+ const content = structuredClone(this.content);
273
+
274
+ if (key === 'name') {
275
+ content.name = super.handleScalar(op) as string;
276
+ } else if (key === 'displayName') {
277
+ content.displayName = super.handleScalar(op) as string;
278
+ } else if (key === 'description') {
279
+ content.description = super.handleScalar(op) as string;
280
+ } else {
281
+ throw new Error(`Unknown property '${key}' for folder resource`);
282
+ }
283
+
284
+ await super.postUpdate(content, key, op);
285
+
286
+ if (nameChange) {
287
+ await this.onNameChange?.(existingName);
288
+ }
165
289
  }
166
290
 
291
+ /**
292
+ * For handling name changes.
293
+ * @param previousName The previous name before the change
294
+ */
295
+ protected async onNameChange?(previousName: string): Promise<void>;
296
+
167
297
  /**
168
298
  * Returns an array of card keys, and/or resource names where this resource is used.
169
299
  * @param cards Optional. If defined, only these cards are checked.
@@ -30,7 +30,9 @@ import {
30
30
  import type {
31
31
  GraphModel,
32
32
  GraphModelMetadata,
33
+ GraphModelUpdateKey,
33
34
  } from '../interfaces/resource-interfaces.js';
35
+ import type { GraphModelContent } from '../interfaces/folder-content-interfaces.js';
34
36
  import { writeFileSafe } from '../utils/file-utils.js';
35
37
 
36
38
  /**
@@ -46,8 +48,11 @@ export class GraphModelResource extends FolderResource {
46
48
  this.initialize();
47
49
  }
48
50
 
49
- // When resource name changes.
50
- private async handleNameChange(existingName: string) {
51
+ /**
52
+ * Handle name changes for graph models
53
+ * @param existingName The previous name before the change
54
+ */
55
+ protected async onNameChange(existingName: string): Promise<void> {
51
56
  await Promise.all([
52
57
  super.updateHandleBars(existingName, this.content.name, [
53
58
  await this.calculationFile(),
@@ -122,7 +127,7 @@ export class GraphModelResource extends FolderResource {
122
127
  public async rename(newName: ResourceName) {
123
128
  const existingName = this.content.name;
124
129
  await super.rename(newName);
125
- return this.handleNameChange(existingName);
130
+ return this.onNameChange(existingName);
126
131
  }
127
132
 
128
133
  /**
@@ -130,11 +135,10 @@ export class GraphModelResource extends FolderResource {
130
135
  * @returns graph model metadata.
131
136
  */
132
137
  public async show(): Promise<GraphModel> {
133
- const showOnlyFileName = true;
134
138
  const baseData = (await super.show()) as GraphModelMetadata;
135
139
  return {
136
140
  ...baseData,
137
- calculationFile: await this.calculationFile(showOnlyFileName),
141
+ content: (await super.contentData()) as GraphModelContent,
138
142
  };
139
143
  }
140
144
 
@@ -142,32 +146,17 @@ export class GraphModelResource extends FolderResource {
142
146
  * Updates graph model resource.
143
147
  * @param key Key to modify
144
148
  * @param op Operation to perform on 'key'
145
- * @throws if key is unknown.
146
149
  */
147
- public async update<Type>(key: string, op: Operation<Type>) {
148
- const nameChange = key === 'name';
149
- const existingName = this.content.name;
150
-
151
- await super.update(key, op);
152
-
153
- const content = structuredClone(this.content) as GraphModel;
154
-
155
- if (key === 'name') {
156
- content.name = super.handleScalar(op) as string;
157
- } else if (key === 'displayName') {
158
- content.displayName = super.handleScalar(op) as string;
159
- } else if (key === 'description') {
160
- content.description = super.handleScalar(op) as string;
161
- } else if (key === 'category') {
150
+ public async update<Type>(key: GraphModelUpdateKey, op: Operation<Type>) {
151
+ if (key === 'category') {
152
+ const content = structuredClone(this.content) as GraphModelMetadata;
162
153
  content.category = super.handleScalar(op) as string;
163
- }
164
-
165
- await super.postUpdate(content, key, op);
166
154
 
167
- // Renaming this graph model causes that references to its name must be updated.
168
- if (nameChange) {
169
- await this.handleNameChange(existingName);
155
+ await super.postUpdate(content, key, op);
156
+ return;
170
157
  }
158
+
159
+ await super.update(key, op);
171
160
  }
172
161
 
173
162
  /**
@@ -30,7 +30,9 @@ import {
30
30
  import type {
31
31
  GraphView,
32
32
  GraphViewMetadata,
33
+ GraphViewUpdateKey,
33
34
  } from '../interfaces/resource-interfaces.js';
35
+ import type { GraphViewContent } from '../interfaces/folder-content-interfaces.js';
34
36
 
35
37
  import { getStaticDirectoryPath } from '@cyberismo/assets';
36
38
  import { copyDir } from '../utils/file-utils.js';
@@ -48,15 +50,17 @@ export class GraphViewResource extends FolderResource {
48
50
  this.initialize();
49
51
  }
50
52
 
51
- // When resource name changes.
52
- private async handleNameChange(existingName: string) {
53
+ /**
54
+ * Handle name changes for graph views
55
+ * @param existingName The previous name before the change
56
+ */
57
+ protected async onNameChange(existingName: string): Promise<void> {
53
58
  await Promise.all([
54
59
  super.updateHandleBars(existingName, this.content.name, [
55
60
  await this.handleBarFile(),
56
61
  ]),
57
62
  super.updateCalculations(existingName, this.content.name),
58
63
  ]);
59
- // Finally, write updated content.
60
64
  await this.write();
61
65
  }
62
66
 
@@ -118,7 +122,7 @@ export class GraphViewResource extends FolderResource {
118
122
  public async rename(newName: ResourceName) {
119
123
  const existingName = this.content.name;
120
124
  await super.rename(newName);
121
- return this.handleNameChange(existingName);
125
+ return this.onNameChange(existingName);
122
126
  }
123
127
 
124
128
  /**
@@ -126,11 +130,10 @@ export class GraphViewResource extends FolderResource {
126
130
  * @returns graph view metadata.
127
131
  */
128
132
  public async show(): Promise<GraphView> {
129
- const showOnlyFileName = true;
130
133
  const baseData = (await super.show()) as GraphViewMetadata;
131
134
  return {
132
135
  ...baseData,
133
- handleBarFile: await this.handleBarFile(showOnlyFileName),
136
+ content: (await super.contentData()) as GraphViewContent,
134
137
  };
135
138
  }
136
139
 
@@ -138,32 +141,17 @@ export class GraphViewResource extends FolderResource {
138
141
  * Updates graph view resource.
139
142
  * @param key Key to modify
140
143
  * @param op Operation to perform on 'key'
141
- * @throws if key is unknown.
142
144
  */
143
- public async update<Type>(key: string, op: Operation<Type>) {
144
- const nameChange = key === 'name';
145
- const existingName = this.content.name;
146
-
147
- await super.update(key, op);
148
-
149
- const content = structuredClone(this.content) as GraphView;
150
-
151
- if (key === 'name') {
152
- content.name = super.handleScalar(op) as string;
153
- } else if (key === 'displayName') {
154
- content.displayName = super.handleScalar(op) as string;
155
- } else if (key === 'description') {
156
- content.description = super.handleScalar(op) as string;
157
- } else if (key === 'category') {
145
+ public async update<Type>(key: GraphViewUpdateKey, op: Operation<Type>) {
146
+ if (key === 'category') {
147
+ const content = structuredClone(this.content) as GraphViewMetadata;
158
148
  content.category = super.handleScalar(op) as string;
159
- }
160
-
161
- await super.postUpdate(content, key, op);
162
149
 
163
- // Renaming this graph view causes that references to its name must be updated.
164
- if (nameChange) {
165
- await this.handleNameChange(existingName);
150
+ await super.postUpdate(content, key, op);
151
+ return;
166
152
  }
153
+
154
+ await super.update(key, op);
167
155
  }
168
156
 
169
157
  /**
@@ -117,7 +117,7 @@ export class LinkTypeResource extends FileResource {
117
117
 
118
118
  await super.update(key, op);
119
119
 
120
- const content = this.content as LinkType;
120
+ const content = structuredClone(this.content) as LinkType;
121
121
  if (key === 'name') {
122
122
  content.name = super.handleScalar(op) as string;
123
123
  } else if (key === 'destinationCardTypes') {