@cyberismo/data-handler 0.0.17 → 0.0.19

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 (112) hide show
  1. package/dist/command-handler.d.ts +2 -0
  2. package/dist/command-handler.js +26 -2
  3. package/dist/command-handler.js.map +1 -1
  4. package/dist/command-manager.d.ts +2 -0
  5. package/dist/command-manager.js +3 -0
  6. package/dist/command-manager.js.map +1 -1
  7. package/dist/commands/create.d.ts +3 -1
  8. package/dist/commands/create.js +10 -1
  9. package/dist/commands/create.js.map +1 -1
  10. package/dist/commands/migrate.d.ts +33 -0
  11. package/dist/commands/migrate.js +66 -0
  12. package/dist/commands/migrate.js.map +1 -0
  13. package/dist/containers/project/card-cache.js +13 -1
  14. package/dist/containers/project/card-cache.js.map +1 -1
  15. package/dist/containers/project/project-paths.d.ts +1 -0
  16. package/dist/containers/project/project-paths.js +5 -2
  17. package/dist/containers/project/project-paths.js.map +1 -1
  18. package/dist/containers/project.d.ts +10 -0
  19. package/dist/containers/project.js +39 -2
  20. package/dist/containers/project.js.map +1 -1
  21. package/dist/containers/template.js +2 -0
  22. package/dist/containers/template.js.map +1 -1
  23. package/dist/interfaces/command-options.d.ts +6 -1
  24. package/dist/interfaces/project-interfaces.d.ts +4 -0
  25. package/dist/interfaces/project-interfaces.js.map +1 -1
  26. package/dist/migrations/index.d.ts +14 -0
  27. package/dist/migrations/index.js +14 -0
  28. package/dist/migrations/index.js.map +1 -0
  29. package/dist/migrations/migration-executor.d.ts +79 -0
  30. package/dist/migrations/migration-executor.js +312 -0
  31. package/dist/migrations/migration-executor.js.map +1 -0
  32. package/dist/migrations/migration-worker.d.ts +13 -0
  33. package/dist/migrations/migration-worker.js +156 -0
  34. package/dist/migrations/migration-worker.js.map +1 -0
  35. package/dist/migrations/worker-executor.d.ts +24 -0
  36. package/dist/migrations/worker-executor.js +157 -0
  37. package/dist/migrations/worker-executor.js.map +1 -0
  38. package/dist/project-settings.d.ts +2 -0
  39. package/dist/project-settings.js +7 -0
  40. package/dist/project-settings.js.map +1 -1
  41. package/dist/resources/calculation-resource.d.ts +9 -0
  42. package/dist/resources/calculation-resource.js +13 -2
  43. package/dist/resources/calculation-resource.js.map +1 -1
  44. package/dist/resources/card-type-resource.d.ts +12 -3
  45. package/dist/resources/card-type-resource.js +73 -91
  46. package/dist/resources/card-type-resource.js.map +1 -1
  47. package/dist/resources/field-type-resource.d.ts +10 -1
  48. package/dist/resources/field-type-resource.js +62 -61
  49. package/dist/resources/field-type-resource.js.map +1 -1
  50. package/dist/resources/file-resource.d.ts +27 -2
  51. package/dist/resources/file-resource.js +46 -8
  52. package/dist/resources/file-resource.js.map +1 -1
  53. package/dist/resources/graph-model-resource.d.ts +5 -0
  54. package/dist/resources/graph-model-resource.js +6 -0
  55. package/dist/resources/graph-model-resource.js.map +1 -1
  56. package/dist/resources/graph-view-resource.d.ts +5 -0
  57. package/dist/resources/graph-view-resource.js +6 -0
  58. package/dist/resources/graph-view-resource.js.map +1 -1
  59. package/dist/resources/link-type-resource.d.ts +11 -1
  60. package/dist/resources/link-type-resource.js +54 -30
  61. package/dist/resources/link-type-resource.js.map +1 -1
  62. package/dist/resources/report-resource.d.ts +6 -1
  63. package/dist/resources/report-resource.js +7 -1
  64. package/dist/resources/report-resource.js.map +1 -1
  65. package/dist/resources/resource-object.d.ts +22 -7
  66. package/dist/resources/resource-object.js +44 -15
  67. package/dist/resources/resource-object.js.map +1 -1
  68. package/dist/resources/template-resource.d.ts +5 -1
  69. package/dist/resources/template-resource.js +11 -27
  70. package/dist/resources/template-resource.js.map +1 -1
  71. package/dist/resources/workflow-resource.d.ts +7 -3
  72. package/dist/resources/workflow-resource.js +90 -82
  73. package/dist/resources/workflow-resource.js.map +1 -1
  74. package/dist/utils/card-utils.d.ts +1 -1
  75. package/dist/utils/common-utils.d.ts +8 -0
  76. package/dist/utils/common-utils.js +14 -0
  77. package/dist/utils/common-utils.js.map +1 -1
  78. package/dist/utils/file-utils.d.ts +15 -3
  79. package/dist/utils/file-utils.js +48 -9
  80. package/dist/utils/file-utils.js.map +1 -1
  81. package/dist/utils/json.js +2 -2
  82. package/dist/utils/json.js.map +1 -1
  83. package/package.json +5 -3
  84. package/src/command-handler.ts +38 -1
  85. package/src/command-manager.ts +3 -0
  86. package/src/commands/create.ts +11 -0
  87. package/src/commands/migrate.ts +88 -0
  88. package/src/containers/project/card-cache.ts +18 -1
  89. package/src/containers/project/project-paths.ts +6 -2
  90. package/src/containers/project.ts +66 -1
  91. package/src/containers/template.ts +5 -0
  92. package/src/interfaces/command-options.ts +8 -0
  93. package/src/interfaces/project-interfaces.ts +4 -0
  94. package/src/migrations/index.ts +20 -0
  95. package/src/migrations/migration-executor.ts +478 -0
  96. package/src/migrations/migration-worker.ts +190 -0
  97. package/src/migrations/worker-executor.ts +185 -0
  98. package/src/project-settings.ts +7 -0
  99. package/src/resources/calculation-resource.ts +13 -2
  100. package/src/resources/card-type-resource.ts +101 -114
  101. package/src/resources/field-type-resource.ts +78 -71
  102. package/src/resources/file-resource.ts +68 -9
  103. package/src/resources/graph-model-resource.ts +6 -0
  104. package/src/resources/graph-view-resource.ts +6 -0
  105. package/src/resources/link-type-resource.ts +66 -36
  106. package/src/resources/report-resource.ts +7 -1
  107. package/src/resources/resource-object.ts +57 -18
  108. package/src/resources/template-resource.ts +12 -27
  109. package/src/resources/workflow-resource.ts +119 -100
  110. package/src/utils/common-utils.ts +15 -0
  111. package/src/utils/file-utils.ts +56 -12
  112. package/src/utils/json.ts +2 -6
@@ -16,6 +16,7 @@ import { Edit } from './commands/edit.js';
16
16
  import { Export } from './commands/export.js';
17
17
  import { Fetch } from './commands/fetch.js';
18
18
  import { Import } from './commands/import.js';
19
+ import { Migrate } from './commands/migrate.js';
19
20
  import { Move } from './commands/move.js';
20
21
  import { Remove } from './commands/remove.js';
21
22
  import { Rename } from './commands/rename.js';
@@ -45,6 +46,7 @@ export class CommandManager {
45
46
  public exportCmd: Export;
46
47
  public fetchCmd: Fetch;
47
48
  public importCmd: Import;
49
+ public migrateCmd: Migrate;
48
50
  public moveCmd: Move;
49
51
  public removeCmd: Remove;
50
52
  public renameCmd: Rename;
@@ -69,6 +71,7 @@ export class CommandManager {
69
71
  this.editCmd = new Edit(this.project);
70
72
  this.exportCmd = new Export(this.project, this.showCmd);
71
73
  this.importCmd = new Import(this.project, this.createCmd, this.fetchCmd);
74
+ this.migrateCmd = new Migrate(this.project);
72
75
  this.moveCmd = new Move(this.project);
73
76
  this.removeCmd = new Remove(this.project, this.fetchCmd);
74
77
  this.renameCmd = new Rename(this.project);
@@ -48,6 +48,7 @@ export class Create {
48
48
  schemaVersion: SCHEMA_VERSION,
49
49
  cardKeyPrefix: '$PROJECT-PREFIX',
50
50
  name: '$PROJECT-NAME',
51
+ description: '',
51
52
  modules: [],
52
53
  hubs: [],
53
54
  },
@@ -372,11 +373,15 @@ export class Create {
372
373
  * @param projectPath where to create the project.
373
374
  * @param projectPrefix prefix for the project.
374
375
  * @param projectName name for the project.
376
+ * @param projectCategory category for the project (empty string if not provided).
377
+ * @param projectDescription description for the project (empty string if not provided).
375
378
  */
376
379
  public static async createProject(
377
380
  projectPath: string,
378
381
  projectPrefix: string,
379
382
  projectName: string,
383
+ projectCategory: string,
384
+ projectDescription: string,
380
385
  ) {
381
386
  projectPath = resolve(projectPath);
382
387
 
@@ -439,6 +444,12 @@ export class Create {
439
444
  if (entry.content.name.includes('$PROJECT-NAME')) {
440
445
  entry.content.name = projectName;
441
446
  }
447
+ if (projectCategory) {
448
+ entry.content.category = projectCategory;
449
+ }
450
+ if (projectDescription) {
451
+ entry.content.description = projectDescription;
452
+ }
442
453
  }
443
454
  await writeJsonFile(
444
455
  join(projectPath, entry.path, entry.name),
@@ -0,0 +1,88 @@
1
+ /**
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2025
4
+ This program is free software: you can redistribute it and/or modify it under
5
+ 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
11
+ License along with this program. If not, see <https://www.gnu.org/licenses/>.
12
+ */
13
+
14
+ import { SCHEMA_VERSION } from '@cyberismo/assets';
15
+ import type { MigrationResult } from '@cyberismo/migrations';
16
+ import type { Project } from '../containers/project.js';
17
+
18
+ /**
19
+ * Command that handles schema migration operations.
20
+ */
21
+ export class Migrate {
22
+ /**
23
+ * Constructs instance of Migrate command.
24
+ * @param project Project instance to use.
25
+ */
26
+ constructor(private project: Project) {}
27
+
28
+ /**
29
+ * Run migrations to bring project to target schema version.
30
+ * @param toVersion Target version (defaults to latest)
31
+ * @param backupDir Optional directory for backups
32
+ * @param timeoutMilliSeconds Optional timeout in milliseconds (defaults to 2 minutes)
33
+ * @returns Migration result
34
+ */
35
+ public async migrate(
36
+ toVersion?: number,
37
+ backupDir?: string,
38
+ timeoutMilliSeconds?: number,
39
+ ): Promise<MigrationResult> {
40
+ const currentVersion = this.project.configuration.schemaVersion;
41
+ if (currentVersion === undefined) {
42
+ throw new Error('Project has no schema version set');
43
+ }
44
+
45
+ const targetVersion = toVersion ?? SCHEMA_VERSION;
46
+
47
+ // Prevent downgrading
48
+ if (targetVersion < currentVersion) {
49
+ throw new Error(
50
+ `Cannot downgrade from version ${currentVersion} to ${targetVersion}. Downgrading is not supported.`,
51
+ );
52
+ }
53
+
54
+ // Cannot migrate beyond current application schema
55
+ if (targetVersion > SCHEMA_VERSION) {
56
+ throw new Error(
57
+ `Cannot migrate to version ${targetVersion}. Current application supports up to version ${SCHEMA_VERSION}.`,
58
+ );
59
+ }
60
+
61
+ // No migration needed
62
+ if (currentVersion === targetVersion) {
63
+ return {
64
+ success: true,
65
+ message: `Project is already at version ${currentVersion}. No migration needed.`,
66
+ stepsExecuted: [],
67
+ };
68
+ }
69
+
70
+ // Prevent skipping migrations when a specific target version is provided
71
+ // If no version specified, migrate to the latest
72
+ if (toVersion !== undefined && toVersion !== SCHEMA_VERSION) {
73
+ // Only allow next sequential version
74
+ if (targetVersion !== currentVersion + 1) {
75
+ throw new Error(
76
+ `Cannot skip to version ${targetVersion}. Project is at version ${currentVersion}, next version is ${currentVersion + 1}. Migrate one version at a time with 'cyberismo migrate ${currentVersion + 1}', or use 'cyberismo migrate' (without a version) to migrate to the latest version.`,
77
+ );
78
+ }
79
+ }
80
+
81
+ return await this.project.runMigrations(
82
+ currentVersion,
83
+ targetVersion,
84
+ backupDir,
85
+ timeoutMilliSeconds,
86
+ );
87
+ }
88
+ }
@@ -162,13 +162,30 @@ export class CardCache {
162
162
  this.fetchAttachments(currentPath),
163
163
  ]);
164
164
 
165
+ let metadata;
166
+ try {
167
+ metadata = JSON.parse(cardMetadata);
168
+ } catch (error) {
169
+ const metadataPath = join(currentPath, cardMetadataFile);
170
+ CardCache.logger.error(
171
+ { error, metadataPath },
172
+ `Incorrect card metadata file`,
173
+ );
174
+ if (error instanceof Error) {
175
+ throw new Error(
176
+ `Invalid JSON in file '${metadataPath}': ${error.message}`,
177
+ );
178
+ }
179
+ throw error;
180
+ }
181
+
165
182
  return {
166
183
  key: entry.name,
167
184
  path: currentPath,
168
185
  children: [],
169
186
  attachments: Array.isArray(cardAttachments) ? cardAttachments : [],
170
187
  content: typeof cardContent === 'string' ? cardContent : '',
171
- metadata: JSON.parse(cardMetadata),
188
+ metadata: metadata,
172
189
  parent: parentCard(currentPath),
173
190
  location: location,
174
191
  };
@@ -80,6 +80,10 @@ export class ProjectPaths {
80
80
  return join(this.resourcesFolder, 'graphViews');
81
81
  }
82
82
 
83
+ public get internalRootFolder(): string {
84
+ return join(this.path, '.cards');
85
+ }
86
+
83
87
  public get linkTypesFolder(): string {
84
88
  return join(this.resourcesFolder, 'linkTypes');
85
89
  }
@@ -93,7 +97,7 @@ export class ProjectPaths {
93
97
  }
94
98
 
95
99
  public get modulesFolder(): string {
96
- return join(this.path, '.cards', 'modules');
100
+ return join(this.internalRootFolder, 'modules');
97
101
  }
98
102
 
99
103
  public moduleResourcePath(
@@ -105,7 +109,7 @@ export class ProjectPaths {
105
109
  }
106
110
 
107
111
  public get resourcesFolder(): string {
108
- return join(this.path, '.cards', 'local');
112
+ return join(this.internalRootFolder, 'local');
109
113
  }
110
114
 
111
115
  public get reportsFolder(): string {
@@ -55,7 +55,9 @@ import { ResourceHandler } from './project/resource-handler.js';
55
55
  import { Validate } from '../commands/validate.js';
56
56
  import { ContentWatcher } from './project/project-content-watcher.js';
57
57
  import { getChildLogger } from '../utils/log-utils.js';
58
+ import { MigrationExecutor } from '../migrations/migration-executor.js';
58
59
 
60
+ import type { MigrationResult } from '@cyberismo/migrations';
59
61
  import type { Template } from './template.js';
60
62
 
61
63
  import { ROOT } from '../utils/constants.js';
@@ -572,12 +574,27 @@ export class Project extends CardContainer {
572
574
  public async handleCardDeleted(deletedCard: Card) {
573
575
  // Delete children from the cache first
574
576
  if (deletedCard.children && deletedCard.children.length > 0) {
577
+ const parentCachedCard = this.cardCache.getCard(deletedCard.key);
578
+ const parentLocation = parentCachedCard?.location || 'project';
579
+
575
580
  for (const child of deletedCard.children) {
576
581
  try {
577
582
  const childCard = this.findCard(child);
583
+ const childCachedCard = this.cardCache.getCard(child);
584
+
585
+ // Safety check: only delete children from the same location (project or template)
586
+ if (childCachedCard && childCachedCard.location !== parentLocation) {
587
+ const errorMessage =
588
+ `Cannot delete child card '${child}' from different location '${childCachedCard.location}' ` +
589
+ `than parent card '${deletedCard.key}' from '${parentLocation}'`;
590
+ this.logger.error(errorMessage);
591
+ throw new Error(errorMessage);
592
+ }
593
+
578
594
  await this.handleCardDeleted(childCard);
579
- } catch {
595
+ } catch (error) {
580
596
  this.logger.warn(
597
+ { error },
581
598
  `Accessing child '${child}' of '${deletedCard.key}' when deleting cards caused an exception`,
582
599
  );
583
600
  continue;
@@ -753,6 +770,7 @@ export class Project extends CardContainer {
753
770
  );
754
771
  return {
755
772
  name: moduleConfig.name,
773
+ description: moduleConfig.description || '',
756
774
  modules: moduleConfig.modules,
757
775
  hubs: moduleConfig.hubs,
758
776
  path: modulePath,
@@ -970,6 +988,51 @@ export class Project extends CardContainer {
970
988
  return this.resourceHandler;
971
989
  }
972
990
 
991
+ /**
992
+ * Run migrations to bring project schema to target version.
993
+ * @param fromVersion Current schema version
994
+ * @param toVersion Target schema version
995
+ * @param backupDir Optional directory for backups. If undefined, no backup is created.
996
+ * @param timeoutMilliSeconds Optional timeout in milliseconds. If undefined, uses default (2 minutes).
997
+ * @returns Migration result
998
+ */
999
+ public async runMigrations(
1000
+ fromVersion: number,
1001
+ toVersion: number,
1002
+ backupDir?: string,
1003
+ timeoutMilliSeconds?: number,
1004
+ ): Promise<MigrationResult> {
1005
+ this.logger.info({ fromVersion, toVersion }, 'Starting schema migration');
1006
+
1007
+ const executor = new MigrationExecutor(
1008
+ this,
1009
+ backupDir,
1010
+ timeoutMilliSeconds,
1011
+ );
1012
+ const result = await executor.migrate(
1013
+ fromVersion,
1014
+ toVersion,
1015
+ async (version: number) => {
1016
+ this.settings.schemaVersion = version;
1017
+ await this.settings.save();
1018
+ },
1019
+ );
1020
+
1021
+ if (result.success) {
1022
+ this.logger.info(
1023
+ { fromVersion, toVersion },
1024
+ 'Migration completed successfully',
1025
+ );
1026
+ } else {
1027
+ this.logger.error(
1028
+ { error: result.error, message: result.message },
1029
+ 'Migration failed',
1030
+ );
1031
+ }
1032
+
1033
+ return result;
1034
+ }
1035
+
973
1036
  /**
974
1037
  * Shows details of a project.
975
1038
  * @returns details of a project.
@@ -979,6 +1042,8 @@ export class Project extends CardContainer {
979
1042
  name: this.settings.name,
980
1043
  path: this.basePath,
981
1044
  prefix: this.projectPrefix,
1045
+ category: this.configuration.category,
1046
+ description: this.configuration.description,
982
1047
  hubs: this.configuration.hubs,
983
1048
  modules: this.resources.moduleNames(),
984
1049
  numberOfCards: (await this.listCards(CardLocation.projectOnly))[0].cards
@@ -152,6 +152,11 @@ export class Template extends CardContainer {
152
152
 
153
153
  card.key = templateIDMap.get(card.key) || card.key;
154
154
 
155
+ // Remap children keys from template keys to new project card keys
156
+ card.children = card.children.map(
157
+ (childKey) => templateIDMap.get(childKey) || childKey,
158
+ );
159
+
155
160
  // Set parent field based on template hierarchy and creation location
156
161
  // Store the original template parent before key remapping
157
162
  const originalParentKey = card.parent;
@@ -60,6 +60,12 @@ export interface ImportCommandOptions extends BaseCommandOptions {
60
60
  skipMigrationLog?: boolean;
61
61
  }
62
62
 
63
+ // Options for 'migrate' command
64
+ export interface MigrateCommandOptions extends BaseCommandOptions {
65
+ backup?: string;
66
+ timeout?: number;
67
+ }
68
+
63
69
  // Options for 'move' command
64
70
  export type MoveCommandOptions = BaseCommandOptions;
65
71
 
@@ -111,6 +117,7 @@ export type AllCommandOptions =
111
117
  | ExportCommandOptions
112
118
  | FetchCommandOptions
113
119
  | ImportCommandOptions
120
+ | MigrateCommandOptions
114
121
  | MoveCommandOptions
115
122
  | RankCommandOptions
116
123
  | RemoveCommandOptions
@@ -132,6 +139,7 @@ export type CommandOptions<T extends CmdKey> = {
132
139
  export: ExportCommandOptions;
133
140
  fetch: FetchCommandOptions;
134
141
  import: ImportCommandOptions;
142
+ migrate: MigrateCommandOptions;
135
143
  move: MoveCommandOptions;
136
144
  rank: RankCommandOptions;
137
145
  remove: RemoveCommandOptions;
@@ -171,6 +171,8 @@ export interface ProjectMetadata {
171
171
  name: string;
172
172
  path: string;
173
173
  prefix: string;
174
+ category?: string;
175
+ description?: string;
174
176
  modules: string[];
175
177
  hubs: HubSetting[];
176
178
  numberOfCards: number;
@@ -181,6 +183,8 @@ export interface ProjectSettings {
181
183
  schemaVersion?: number;
182
184
  cardKeyPrefix: string;
183
185
  name: string;
186
+ category?: string;
187
+ description: string;
184
188
  modules: ModuleSetting[];
185
189
  hubs: HubSetting[];
186
190
  }
@@ -0,0 +1,20 @@
1
+ /**
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2025
4
+ This program is free software: you can redistribute it and/or modify it under
5
+ 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
11
+ License along with this program. If not, see <https://www.gnu.org/licenses/>.
12
+ */
13
+
14
+ export { MigrationExecutor } from './migration-executor.js';
15
+ export type {
16
+ Migration,
17
+ MigrationContext,
18
+ MigrationResult,
19
+ MigrationStepResult,
20
+ } from '@cyberismo/migrations';