@memberjunction/metadata-sync 2.48.0 → 2.50.0

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.
@@ -1,4 +1,27 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
27
  };
@@ -13,6 +36,7 @@ const provider_utils_1 = require("../../lib/provider-utils");
13
36
  const provider_utils_2 = require("../../lib/provider-utils");
14
37
  const config_manager_1 = require("../../lib/config-manager");
15
38
  const singleton_manager_1 = require("../../lib/singleton-manager");
39
+ const global_1 = require("@memberjunction/global");
16
40
  class Push extends core_1.Command {
17
41
  static description = 'Push local file changes to the database';
18
42
  static examples = [
@@ -30,6 +54,7 @@ class Push extends core_1.Command {
30
54
  async run() {
31
55
  const { flags } = await this.parse(Push);
32
56
  const spinner = (0, ora_classic_1.default)();
57
+ let sqlLogger = null;
33
58
  try {
34
59
  // Load configurations
35
60
  spinner.start('Loading configuration');
@@ -37,7 +62,9 @@ class Push extends core_1.Command {
37
62
  if (!mjConfig) {
38
63
  this.error('No mj.config.cjs found in current directory or parent directories');
39
64
  }
40
- const syncConfig = await (0, config_1.loadSyncConfig)(config_manager_1.configManager.getOriginalCwd());
65
+ // Load sync config from target directory if --dir is specified, otherwise from current directory
66
+ const syncConfigDir = flags.dir ? path_1.default.resolve(config_manager_1.configManager.getOriginalCwd(), flags.dir) : config_manager_1.configManager.getOriginalCwd();
67
+ const syncConfig = await (0, config_1.loadSyncConfig)(syncConfigDir);
41
68
  // Stop spinner before provider initialization (which logs to console)
42
69
  spinner.stop();
43
70
  // Initialize data provider
@@ -46,8 +73,45 @@ class Push extends core_1.Command {
46
73
  const syncEngine = await (0, singleton_manager_1.getSyncEngine)((0, provider_utils_1.getSystemUser)());
47
74
  // Show success after all initialization is complete
48
75
  spinner.succeed('Configuration and metadata loaded');
76
+ // Initialize SQL logging AFTER provider setup is complete
77
+ if (syncConfig?.sqlLogging?.enabled) {
78
+ const outputDir = syncConfig.sqlLogging.outputDirectory || './sql_logging';
79
+ const formatAsMigration = syncConfig.sqlLogging.formatAsMigration || false;
80
+ // Ensure output directory exists
81
+ const fullOutputDir = path_1.default.resolve(outputDir);
82
+ await fs_extra_1.default.ensureDir(fullOutputDir);
83
+ // Generate filename with timestamp and directory name
84
+ const now = new Date();
85
+ const humanReadableTimestamp = now.toISOString()
86
+ .replace('T', '_')
87
+ .replace(/:/g, '-')
88
+ .slice(0, -5); // Remove milliseconds and Z
89
+ // Get directory name for filename
90
+ const targetDir = flags.dir ? path_1.default.resolve(config_manager_1.configManager.getOriginalCwd(), flags.dir) : config_manager_1.configManager.getOriginalCwd();
91
+ const dirName = path_1.default.basename(targetDir);
92
+ const filename = formatAsMigration
93
+ ? `V${now.toISOString().replace(/[:.T-]/g, '').slice(0, -5)}__MetadataSync_Push.sql`
94
+ : `metadata-sync-push_${dirName}_${humanReadableTimestamp}.sql`;
95
+ const logFilePath = path_1.default.join(fullOutputDir, filename);
96
+ // Import and access the data provider from the provider utils
97
+ const { getDataProvider } = await Promise.resolve().then(() => __importStar(require('../../lib/provider-utils')));
98
+ const dataProvider = getDataProvider();
99
+ if (dataProvider && typeof dataProvider.createSqlLogger === 'function') {
100
+ sqlLogger = await dataProvider.createSqlLogger(logFilePath, {
101
+ formatAsMigration,
102
+ description: 'MetadataSync Push Operation',
103
+ statementTypes: 'mutations', // Only log mutations (data changes)
104
+ batchSeparator: 'GO', // Add GO statements for SQL Server batch processing
105
+ prettyPrint: true // Enable pretty printing for readable output
106
+ });
107
+ this.log(`📝 SQL logging enabled: ${path_1.default.relative(process.cwd(), logFilePath)}`);
108
+ }
109
+ else {
110
+ this.warn('SQL logging requested but data provider does not support createSqlLogger');
111
+ }
112
+ }
49
113
  // Find entity directories to process
50
- const entityDirs = (0, provider_utils_1.findEntityDirectories)(config_manager_1.configManager.getOriginalCwd(), flags.dir);
114
+ const entityDirs = (0, provider_utils_1.findEntityDirectories)(config_manager_1.configManager.getOriginalCwd(), flags.dir, syncConfig?.directoryOrder);
51
115
  if (entityDirs.length === 0) {
52
116
  this.error('No entity directories found');
53
117
  }
@@ -79,9 +143,45 @@ class Push extends core_1.Command {
79
143
  }
80
144
  catch (error) {
81
145
  spinner.fail('Push failed');
146
+ // Enhanced error logging for debugging
147
+ this.log('\n=== Push Error Details ===');
148
+ this.log(`Error type: ${error?.constructor?.name || 'Unknown'}`);
149
+ this.log(`Error message: ${error instanceof Error ? error.message : String(error)}`);
150
+ if (error instanceof Error && error.stack) {
151
+ this.log(`\nStack trace:`);
152
+ this.log(error.stack);
153
+ }
154
+ // Log context information
155
+ this.log(`\nContext:`);
156
+ this.log(`- Working directory: ${config_manager_1.configManager.getOriginalCwd()}`);
157
+ this.log(`- Flags: ${JSON.stringify(flags, null, 2)}`);
158
+ // Check if error is related to common issues
159
+ const errorMessage = error instanceof Error ? error.message : String(error);
160
+ if (errorMessage.includes('entity directories')) {
161
+ this.log(`\nHint: This appears to be an entity directory configuration issue.`);
162
+ this.log(`Make sure each entity directory has a .mj-sync.json file.`);
163
+ }
164
+ else if (errorMessage.includes('database') || errorMessage.includes('connection')) {
165
+ this.log(`\nHint: This appears to be a database connectivity issue.`);
166
+ this.log(`Check your mj.config.cjs configuration and database connectivity.`);
167
+ }
168
+ else if (errorMessage.includes('config') || errorMessage.includes('mj.config.cjs')) {
169
+ this.log(`\nHint: This appears to be a configuration file issue.`);
170
+ this.log(`Make sure mj.config.cjs exists and is properly configured.`);
171
+ }
82
172
  this.error(error);
83
173
  }
84
174
  finally {
175
+ // Dispose SQL logging session if active
176
+ if (sqlLogger) {
177
+ try {
178
+ await sqlLogger.dispose();
179
+ this.log('✅ SQL logging session closed');
180
+ }
181
+ catch (error) {
182
+ this.warn(`Failed to close SQL logging session: ${error}`);
183
+ }
184
+ }
85
185
  // Reset sync engine singleton
86
186
  (0, singleton_manager_1.resetSyncEngine)();
87
187
  // Clean up database connection
@@ -195,6 +295,33 @@ class Push extends core_1.Command {
195
295
  entity = await syncEngine.createEntityObject(entityName);
196
296
  entity.NewRecord();
197
297
  isNew = true;
298
+ // Handle primary keys for new records
299
+ const entityInfo = syncEngine.getEntityInfo(entityName);
300
+ if (entityInfo) {
301
+ for (const pk of entityInfo.PrimaryKeys) {
302
+ if (!pk.AutoIncrement) {
303
+ // Check if we have a value in primaryKey object
304
+ if (recordData.primaryKey?.[pk.Name]) {
305
+ // User specified a primary key for new record, set it on entity directly
306
+ // Don't add to fields as it will be in primaryKey section
307
+ entity[pk.Name] = recordData.primaryKey[pk.Name];
308
+ if (verbose) {
309
+ this.log(` Using specified primary key ${pk.Name}: ${recordData.primaryKey[pk.Name]}`);
310
+ }
311
+ }
312
+ else if (pk.Type.toLowerCase() === 'uniqueidentifier' && !recordData.fields[pk.Name]) {
313
+ // Generate UUID for this primary key and set it on entity directly
314
+ const uuid = (0, global_1.uuidv4)();
315
+ // Don't add to fields as it will be in primaryKey section after save
316
+ if (verbose) {
317
+ this.log(` Generated UUID for primary key ${pk.Name}: ${uuid}`);
318
+ }
319
+ // Set the generated UUID on the entity
320
+ entity[pk.Name] = uuid;
321
+ }
322
+ }
323
+ }
324
+ }
198
325
  }
199
326
  // Apply defaults first
200
327
  for (const [field, value] of Object.entries(defaults)) {
@@ -227,7 +354,11 @@ class Push extends core_1.Command {
227
354
  // Save the record
228
355
  const saved = await entity.Save();
229
356
  if (!saved) {
230
- const errors = entity.LatestResult?.Errors?.join(', ') || 'Unknown error';
357
+ const message = entity.LatestResult?.Message;
358
+ if (message) {
359
+ throw new Error(`Failed to save record: ${message}`);
360
+ }
361
+ const errors = entity.LatestResult?.Errors?.map(err => typeof err === 'string' ? err : (err?.message || JSON.stringify(err)))?.join(', ') || 'Unknown error';
231
362
  throw new Error(`Failed to save record: ${errors}`);
232
363
  }
233
364
  // Process related entities after saving parent
@@ -276,6 +407,32 @@ class Push extends core_1.Command {
276
407
  entity = await syncEngine.createEntityObject(entityName);
277
408
  entity.NewRecord();
278
409
  isNew = true;
410
+ // Handle primary keys for new related entity records
411
+ const entityInfo = syncEngine.getEntityInfo(entityName);
412
+ if (entityInfo) {
413
+ for (const pk of entityInfo.PrimaryKeys) {
414
+ if (!pk.AutoIncrement) {
415
+ // Check if we have a value in primaryKey object
416
+ if (relatedRecord.primaryKey?.[pk.Name]) {
417
+ // User specified a primary key for new record, set it on entity directly
418
+ // Don't add to fields as it will be in primaryKey section
419
+ entity[pk.Name] = relatedRecord.primaryKey[pk.Name];
420
+ if (verbose) {
421
+ this.log(`${indent} Using specified primary key ${pk.Name}: ${relatedRecord.primaryKey[pk.Name]}`);
422
+ }
423
+ }
424
+ else if (pk.Type.toLowerCase() === 'uniqueidentifier' && !relatedRecord.fields[pk.Name]) {
425
+ // Generate UUID for this primary key and set it on entity directly
426
+ const uuid = (0, global_1.uuidv4)();
427
+ // Don't add to fields as it will be in primaryKey section after save
428
+ entity[pk.Name] = uuid;
429
+ if (verbose) {
430
+ this.log(`${indent} Generated UUID for primary key ${pk.Name}: ${uuid}`);
431
+ }
432
+ }
433
+ }
434
+ }
435
+ }
279
436
  }
280
437
  // Apply fields with parent/root context
281
438
  for (const [field, value] of Object.entries(relatedRecord.fields)) {
@@ -298,7 +455,11 @@ class Push extends core_1.Command {
298
455
  // Save the related entity
299
456
  const saved = await entity.Save();
300
457
  if (!saved) {
301
- const errors = entity.LatestResult?.Errors?.join(', ') || 'Unknown error';
458
+ const message = entity.LatestResult?.Message;
459
+ if (message) {
460
+ throw new Error(`Failed to save related ${entityName}: ${message}`);
461
+ }
462
+ const errors = entity.LatestResult?.Errors?.map(err => typeof err === 'string' ? err : (err?.message || JSON.stringify(err)))?.join(', ') || 'Unknown error';
302
463
  throw new Error(`Failed to save related ${entityName}: ${errors}`);
303
464
  }
304
465
  if (verbose) {
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/commands/push/index.ts"],"names":[],"mappings":";;;;;AAAA,sCAA6C;AAC7C,wDAA0B;AAC1B,gDAAwB;AAExB,8DAA8B;AAC9B,0DAAiC;AACjC,yCAA8E;AAE9E,6DAAoG;AAEpG,6DAA2D;AAC3D,6DAAyD;AACzD,mEAA6E;AAE7E,MAAqB,IAAK,SAAQ,cAAO;IACvC,MAAM,CAAC,WAAW,GAAG,yCAAyC,CAAC;IAE/D,MAAM,CAAC,QAAQ,GAAG;QAChB,qCAAqC;QACrC,+CAA+C;QAC/C,wDAAwD;QACxD,0CAA0C;KAC3C,CAAC;IAEF,MAAM,CAAC,KAAK,GAAG;QACb,GAAG,EAAE,YAAK,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,mCAAmC,EAAE,CAAC;QACvE,SAAS,EAAE,YAAK,CAAC,OAAO,CAAC,EAAE,WAAW,EAAE,oDAAoD,EAAE,CAAC;QAC/F,EAAE,EAAE,YAAK,CAAC,OAAO,CAAC,EAAE,WAAW,EAAE,sCAAsC,EAAE,CAAC;QAC1E,OAAO,EAAE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CAAC,6BAA6B,UAAU,KAAK,KAAK,EAAE,CAAC,CAAC;gBACvE,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;;AAjcH,uBAkcC","sourcesContent":["import { Command, Flags } from '@oclif/core';\nimport fs from 'fs-extra';\nimport path from 'path';\nimport { confirm } from '@inquirer/prompts';\nimport ora from 'ora-classic';\nimport fastGlob from 'fast-glob';\nimport { loadMJConfig, loadSyncConfig, loadEntityConfig } from '../../config';\nimport { SyncEngine, RecordData } from '../../lib/sync-engine';\nimport { initializeProvider, findEntityDirectories, getSystemUser } from '../../lib/provider-utils';\nimport { BaseEntity } from '@memberjunction/core';\nimport { cleanupProvider } from '../../lib/provider-utils';\nimport { configManager } from '../../lib/config-manager';\nimport { getSyncEngine, resetSyncEngine } from '../../lib/singleton-manager';\n\nexport default class Push extends Command {\n static description = 'Push local file changes to the database';\n \n static examples = [\n `<%= config.bin %> <%= command.id %>`,\n `<%= config.bin %> <%= command.id %> --dry-run`,\n `<%= config.bin %> <%= command.id %> --dir=\"ai-prompts\"`,\n `<%= config.bin %> <%= command.id %> --ci`,\n ];\n \n static flags = {\n dir: Flags.string({ description: 'Specific entity directory to push' }),\n 'dry-run': Flags.boolean({ description: 'Show what would be pushed without actually pushing' }),\n ci: Flags.boolean({ description: 'CI mode - no prompts, fail on issues' }),\n verbose: Flags.boolean({ char: 'v', description: 'Show detailed field-level output' }),\n };\n \n async run(): Promise<void> {\n const { flags } = await this.parse(Push);\n const spinner = ora();\n \n try {\n // Load configurations\n spinner.start('Loading configuration');\n const mjConfig = loadMJConfig();\n if (!mjConfig) {\n this.error('No mj.config.cjs found in current directory or parent directories');\n }\n \n const syncConfig = await loadSyncConfig(configManager.getOriginalCwd());\n \n // Stop spinner before provider initialization (which logs to console)\n spinner.stop();\n \n // Initialize data provider\n await initializeProvider(mjConfig);\n \n // Initialize sync engine using singleton pattern\n const syncEngine = await getSyncEngine(getSystemUser());\n \n // Show success after all initialization is complete\n spinner.succeed('Configuration and metadata loaded');\n \n // Find entity directories to process\n const entityDirs = findEntityDirectories(configManager.getOriginalCwd(), flags.dir);\n \n if (entityDirs.length === 0) {\n this.error('No entity directories found');\n }\n \n this.log(`Found ${entityDirs.length} entity ${entityDirs.length === 1 ? 'directory' : 'directories'} to process`);\n \n // Process each entity directory\n let totalCreated = 0;\n let totalUpdated = 0;\n let totalErrors = 0;\n \n for (const entityDir of entityDirs) {\n const entityConfig = await loadEntityConfig(entityDir);\n if (!entityConfig) {\n this.warn(`Skipping ${entityDir} - no valid entity configuration`);\n continue;\n }\n \n this.log(`\\nProcessing ${entityConfig.entity} in ${entityDir}`);\n \n const result = await this.processEntityDirectory(\n entityDir,\n entityConfig,\n syncEngine,\n flags,\n syncConfig\n );\n \n totalCreated += result.created;\n totalUpdated += result.updated;\n totalErrors += result.errors;\n }\n \n // Summary\n this.log('\\n=== Push Summary ===');\n this.log(`Created: ${totalCreated}`);\n this.log(`Updated: ${totalUpdated}`);\n this.log(`Errors: ${totalErrors}`);\n \n if (totalErrors > 0 && flags.ci) {\n this.error('Push failed with errors in CI mode');\n }\n \n } catch (error) {\n spinner.fail('Push failed');\n this.error(error as Error);\n } finally {\n // Reset sync engine singleton\n resetSyncEngine();\n // Clean up database connection\n await cleanupProvider();\n \n // Exit process to prevent background MJ tasks from throwing errors\n process.exit(0);\n }\n }\n \n private async processEntityDirectory(\n entityDir: string,\n entityConfig: any,\n syncEngine: SyncEngine,\n flags: any,\n syncConfig: any\n ): Promise<{ created: number; updated: number; errors: number }> {\n const result = { created: 0, updated: 0, errors: 0 };\n \n // Find files matching the configured pattern\n const pattern = entityConfig.filePattern || '*.json';\n const jsonFiles = await fastGlob(pattern, {\n cwd: entityDir,\n ignore: ['.mj-sync.json', '.mj-folder.json', '**/*.backup'],\n dot: true, // Include dotfiles (files starting with .)\n onlyFiles: true\n });\n \n this.log(`Processing ${jsonFiles.length} records in ${path.relative(process.cwd(), entityDir) || '.'}`);\n \n // First, process all JSON files in this directory\n await this.processJsonFiles(jsonFiles, entityDir, entityConfig, syncEngine, flags, result);\n \n // Then, recursively process subdirectories\n const entries = await fs.readdir(entityDir, { withFileTypes: true });\n for (const entry of entries) {\n if (entry.isDirectory() && !entry.name.startsWith('.')) {\n const subDir = path.join(entityDir, entry.name);\n \n // Load subdirectory config and merge with parent config\n let subEntityConfig = { ...entityConfig };\n const subDirConfig = await loadEntityConfig(subDir);\n \n if (subDirConfig) {\n // Check if this is a new entity type (has different entity name)\n if (subDirConfig.entity && subDirConfig.entity !== entityConfig.entity) {\n // This is a different entity type, skip it (will be processed separately)\n continue;\n }\n \n // Merge defaults: parent defaults + subdirectory overrides\n subEntityConfig = {\n ...entityConfig,\n ...subDirConfig,\n defaults: {\n ...entityConfig.defaults,\n ...(subDirConfig.defaults || {})\n }\n };\n }\n \n // Process subdirectory with merged config\n const subResult = await this.processEntityDirectory(\n subDir,\n subEntityConfig,\n syncEngine,\n flags,\n syncConfig\n );\n \n result.created += subResult.created;\n result.updated += subResult.updated;\n result.errors += subResult.errors;\n }\n }\n \n return result;\n }\n \n private async processJsonFiles(\n jsonFiles: string[],\n entityDir: string,\n entityConfig: any,\n syncEngine: SyncEngine,\n flags: any,\n result: { created: number; updated: number; errors: number }\n ): Promise<void> {\n if (jsonFiles.length === 0) {\n return;\n }\n \n const spinner = ora();\n spinner.start('Processing records');\n \n let totalRecords = 0;\n \n for (const file of jsonFiles) {\n try {\n const filePath = path.join(entityDir, file);\n const fileContent = await fs.readJson(filePath);\n \n // Process templates in the loaded content\n const processedContent = await syncEngine.processTemplates(fileContent, entityDir);\n \n // Check if the file contains a single record or an array of records\n const isArray = Array.isArray(processedContent);\n const records: RecordData[] = isArray ? processedContent : [processedContent];\n totalRecords += records.length;\n \n // Build and process defaults (including lookups)\n const defaults = await syncEngine.buildDefaults(filePath, entityConfig);\n \n // Process each record in the file\n for (let i = 0; i < records.length; i++) {\n const recordData = records[i];\n \n // Process the record\n const isNew = await this.pushRecord(\n recordData,\n entityConfig.entity,\n path.dirname(filePath),\n file,\n defaults,\n syncEngine,\n flags['dry-run'],\n flags.verbose,\n isArray ? i : undefined\n );\n \n if (!flags['dry-run']) {\n if (isNew) {\n result.created++;\n } else {\n result.updated++;\n }\n }\n \n spinner.text = `Processing records (${result.created + result.updated + result.errors}/${totalRecords})`;\n }\n \n // Write back the entire file if it's an array\n if (isArray && !flags['dry-run']) {\n await fs.writeJson(filePath, records, { spaces: 2 });\n }\n \n } catch (error) {\n result.errors++;\n const errorMessage = error instanceof Error ? error.message : String(error);\n this.error(`Failed to process ${file}: ${errorMessage}`, { exit: false });\n }\n }\n \n spinner.succeed(`Processed ${totalRecords} records from ${jsonFiles.length} files`);\n }\n \n private async pushRecord(\n recordData: RecordData,\n entityName: string,\n baseDir: string,\n fileName: string,\n defaults: Record<string, any>,\n syncEngine: SyncEngine,\n dryRun: boolean,\n verbose: boolean = false,\n arrayIndex?: number\n ): Promise<boolean> {\n // Load or create entity\n let entity: BaseEntity | null = null;\n let isNew = false;\n \n if (recordData.primaryKey) {\n entity = await syncEngine.loadEntity(entityName, recordData.primaryKey);\n }\n \n if (!entity) {\n // New record\n entity = await syncEngine.createEntityObject(entityName);\n entity.NewRecord();\n isNew = true;\n }\n \n // Apply defaults first\n for (const [field, value] of Object.entries(defaults)) {\n if (field in entity) {\n (entity as any)[field] = value;\n }\n }\n \n // Apply record fields\n for (const [field, value] of Object.entries(recordData.fields)) {\n if (field in entity) {\n try {\n const processedValue = await syncEngine.processFieldValue(value, baseDir, null, null);\n if (verbose) {\n this.log(` Setting ${field}: ${JSON.stringify(value)} -> ${JSON.stringify(processedValue)}`);\n }\n (entity as any)[field] = processedValue;\n } catch (error) {\n throw new Error(`Failed to process field '${field}': ${error}`);\n }\n } else {\n this.warn(`Field '${field}' does not exist on entity '${entityName}'`);\n }\n }\n \n if (dryRun) {\n this.log(`Would ${isNew ? 'create' : 'update'} ${entityName} record`);\n return isNew;\n }\n \n // Save the record\n const saved = await entity.Save();\n if (!saved) {\n const errors = entity.LatestResult?.Errors?.join(', ') || 'Unknown error';\n throw new Error(`Failed to save record: ${errors}`);\n }\n \n // Process related entities after saving parent\n if (recordData.relatedEntities && !dryRun) {\n await this.processRelatedEntities(\n recordData.relatedEntities,\n entity,\n entity, // root is same as parent for top level\n baseDir,\n syncEngine,\n verbose\n );\n }\n \n // Update the local file with new primary key if created\n if (isNew) {\n const entityInfo = syncEngine.getEntityInfo(entityName);\n if (entityInfo) {\n const newPrimaryKey: Record<string, any> = {};\n for (const pk of entityInfo.PrimaryKeys) {\n newPrimaryKey[pk.Name] = entity.Get(pk.Name);\n }\n recordData.primaryKey = newPrimaryKey;\n }\n }\n \n // Always update sync metadata\n // This ensures related entities are persisted with their metadata\n recordData.sync = {\n lastModified: new Date().toISOString(),\n checksum: syncEngine.calculateChecksum(recordData.fields)\n };\n \n // Write back to file only if it's a single record (not part of an array)\n // Array records are written back in bulk after all records are processed\n if (arrayIndex === undefined) {\n const filePath = path.join(baseDir, fileName);\n await fs.writeJson(filePath, recordData, { spaces: 2 });\n }\n \n return isNew;\n }\n \n private async processRelatedEntities(\n relatedEntities: Record<string, RecordData[]>,\n parentEntity: BaseEntity,\n rootEntity: BaseEntity,\n baseDir: string,\n syncEngine: SyncEngine,\n verbose: boolean = false,\n indentLevel: number = 1\n ): Promise<void> {\n const indent = ' '.repeat(indentLevel);\n \n for (const [entityName, records] of Object.entries(relatedEntities)) {\n this.log(`${indent}↳ Processing ${records.length} related ${entityName} records`);\n \n for (const relatedRecord of records) {\n try {\n // Load or create entity\n let entity = null;\n let isNew = false;\n \n if (relatedRecord.primaryKey) {\n entity = await syncEngine.loadEntity(entityName, relatedRecord.primaryKey);\n }\n \n if (!entity) {\n entity = await syncEngine.createEntityObject(entityName);\n entity.NewRecord();\n isNew = true;\n }\n \n // Apply fields with parent/root context\n for (const [field, value] of Object.entries(relatedRecord.fields)) {\n if (field in entity) {\n try {\n const processedValue = await syncEngine.processFieldValue(\n value, \n baseDir, \n parentEntity, \n rootEntity\n );\n if (verbose) {\n this.log(`${indent} Setting ${field}: ${JSON.stringify(value)} -> ${JSON.stringify(processedValue)}`);\n }\n (entity as any)[field] = processedValue;\n } catch (error) {\n throw new Error(`Failed to process field '${field}' in ${entityName}: ${error}`);\n }\n } else {\n this.warn(`${indent} Field '${field}' does not exist on entity '${entityName}'`);\n }\n }\n \n // Save the related entity\n const saved = await entity.Save();\n if (!saved) {\n const errors = entity.LatestResult?.Errors?.join(', ') || 'Unknown error';\n throw new Error(`Failed to save related ${entityName}: ${errors}`);\n }\n \n if (verbose) {\n this.log(`${indent} ✓ ${isNew ? 'Created' : 'Updated'} ${entityName} record`);\n }\n \n // Update the related record with primary key and sync metadata\n const entityInfo = syncEngine.getEntityInfo(entityName);\n if (entityInfo) {\n // Update primary key if new\n if (isNew) {\n relatedRecord.primaryKey = {};\n for (const pk of entityInfo.PrimaryKeys) {\n relatedRecord.primaryKey[pk.Name] = entity.Get(pk.Name);\n }\n }\n \n // Always update sync metadata\n relatedRecord.sync = {\n lastModified: new Date().toISOString(),\n checksum: syncEngine.calculateChecksum(relatedRecord.fields)\n };\n }\n \n // Process nested related entities if any\n if (relatedRecord.relatedEntities) {\n await this.processRelatedEntities(\n relatedRecord.relatedEntities,\n entity,\n rootEntity,\n baseDir,\n syncEngine,\n verbose,\n indentLevel + 1\n );\n }\n } catch (error) {\n throw new Error(`Failed to process related ${entityName}: ${error}`);\n }\n }\n }\n }\n}"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/commands/push/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sCAA6C;AAC7C,wDAA0B;AAC1B,gDAAwB;AAExB,8DAA8B;AAC9B,0DAAiC;AACjC,yCAA8E;AAE9E,6DAAoG;AAEpG,6DAA2D;AAC3D,6DAAyD;AACzD,mEAA6E;AAE7E,mDAAgD;AAEhD,MAAqB,IAAK,SAAQ,cAAO;IACvC,MAAM,CAAC,WAAW,GAAG,yCAAyC,CAAC;IAE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AC;YACH,CAAC;QACH,CAAC;IACH,CAAC;;AA1lBH,uBA2lBC","sourcesContent":["import { Command, Flags } from '@oclif/core';\nimport fs from 'fs-extra';\nimport path from 'path';\nimport { confirm } from '@inquirer/prompts';\nimport ora from 'ora-classic';\nimport fastGlob from 'fast-glob';\nimport { loadMJConfig, loadSyncConfig, loadEntityConfig } from '../../config';\nimport { SyncEngine, RecordData } from '../../lib/sync-engine';\nimport { initializeProvider, findEntityDirectories, getSystemUser } from '../../lib/provider-utils';\nimport { BaseEntity } from '@memberjunction/core';\nimport { cleanupProvider } from '../../lib/provider-utils';\nimport { configManager } from '../../lib/config-manager';\nimport { getSyncEngine, resetSyncEngine } from '../../lib/singleton-manager';\nimport type { SqlLoggingSession } from '@memberjunction/sqlserver-dataprovider';\nimport { uuidv4 } from '@memberjunction/global';\n\nexport default class Push extends Command {\n static description = 'Push local file changes to the database';\n \n static examples = [\n `<%= config.bin %> <%= command.id %>`,\n `<%= config.bin %> <%= command.id %> --dry-run`,\n `<%= config.bin %> <%= command.id %> --dir=\"ai-prompts\"`,\n `<%= config.bin %> <%= command.id %> --ci`,\n ];\n \n static flags = {\n dir: Flags.string({ description: 'Specific entity directory to push' }),\n 'dry-run': Flags.boolean({ description: 'Show what would be pushed without actually pushing' }),\n ci: Flags.boolean({ description: 'CI mode - no prompts, fail on issues' }),\n verbose: Flags.boolean({ char: 'v', description: 'Show detailed field-level output' }),\n };\n \n async run(): Promise<void> {\n const { flags } = await this.parse(Push);\n const spinner = ora();\n let sqlLogger: SqlLoggingSession | null = null;\n \n try {\n // Load configurations\n spinner.start('Loading configuration');\n const mjConfig = loadMJConfig();\n if (!mjConfig) {\n this.error('No mj.config.cjs found in current directory or parent directories');\n }\n \n // Load sync config from target directory if --dir is specified, otherwise from current directory\n const syncConfigDir = flags.dir ? path.resolve(configManager.getOriginalCwd(), flags.dir) : configManager.getOriginalCwd();\n const syncConfig = await loadSyncConfig(syncConfigDir);\n \n // Stop spinner before provider initialization (which logs to console)\n spinner.stop();\n \n // Initialize data provider\n await initializeProvider(mjConfig);\n \n // Initialize sync engine using singleton pattern\n const syncEngine = await getSyncEngine(getSystemUser());\n \n // Show success after all initialization is complete\n spinner.succeed('Configuration and metadata loaded');\n \n // Initialize SQL logging AFTER provider setup is complete\n if (syncConfig?.sqlLogging?.enabled) {\n const outputDir = syncConfig.sqlLogging.outputDirectory || './sql_logging';\n const formatAsMigration = syncConfig.sqlLogging.formatAsMigration || false;\n \n // Ensure output directory exists\n const fullOutputDir = path.resolve(outputDir);\n await fs.ensureDir(fullOutputDir);\n \n // Generate filename with timestamp and directory name\n const now = new Date();\n const humanReadableTimestamp = now.toISOString()\n .replace('T', '_')\n .replace(/:/g, '-')\n .slice(0, -5); // Remove milliseconds and Z\n \n // Get directory name for filename\n const targetDir = flags.dir ? path.resolve(configManager.getOriginalCwd(), flags.dir) : configManager.getOriginalCwd();\n const dirName = path.basename(targetDir);\n \n const filename = formatAsMigration \n ? `V${now.toISOString().replace(/[:.T-]/g, '').slice(0, -5)}__MetadataSync_Push.sql`\n : `metadata-sync-push_${dirName}_${humanReadableTimestamp}.sql`;\n const logFilePath = path.join(fullOutputDir, filename);\n \n // Import and access the data provider from the provider utils\n const { getDataProvider } = await import('../../lib/provider-utils');\n const dataProvider = getDataProvider();\n \n if (dataProvider && typeof dataProvider.createSqlLogger === 'function') {\n sqlLogger = await dataProvider.createSqlLogger(logFilePath, {\n formatAsMigration,\n description: 'MetadataSync Push Operation',\n statementTypes: 'mutations', // Only log mutations (data changes)\n batchSeparator: 'GO', // Add GO statements for SQL Server batch processing\n prettyPrint: true // Enable pretty printing for readable output\n });\n \n this.log(`📝 SQL logging enabled: ${path.relative(process.cwd(), logFilePath)}`);\n } else {\n this.warn('SQL logging requested but data provider does not support createSqlLogger');\n }\n }\n \n // Find entity directories to process\n const entityDirs = findEntityDirectories(configManager.getOriginalCwd(), flags.dir, syncConfig?.directoryOrder);\n \n if (entityDirs.length === 0) {\n this.error('No entity directories found');\n }\n \n this.log(`Found ${entityDirs.length} entity ${entityDirs.length === 1 ? 'directory' : 'directories'} to process`);\n \n // Process each entity directory\n let totalCreated = 0;\n let totalUpdated = 0;\n let totalErrors = 0;\n \n for (const entityDir of entityDirs) {\n const entityConfig = await loadEntityConfig(entityDir);\n if (!entityConfig) {\n this.warn(`Skipping ${entityDir} - no valid entity configuration`);\n continue;\n }\n \n this.log(`\\nProcessing ${entityConfig.entity} in ${entityDir}`);\n \n const result = await this.processEntityDirectory(\n entityDir,\n entityConfig,\n syncEngine,\n flags,\n syncConfig\n );\n \n totalCreated += result.created;\n totalUpdated += result.updated;\n totalErrors += result.errors;\n }\n \n // Summary\n this.log('\\n=== Push Summary ===');\n this.log(`Created: ${totalCreated}`);\n this.log(`Updated: ${totalUpdated}`);\n this.log(`Errors: ${totalErrors}`);\n \n if (totalErrors > 0 && flags.ci) {\n this.error('Push failed with errors in CI mode');\n }\n \n } catch (error) {\n spinner.fail('Push failed');\n \n // Enhanced error logging for debugging\n this.log('\\n=== Push Error Details ===');\n this.log(`Error type: ${error?.constructor?.name || 'Unknown'}`);\n this.log(`Error message: ${error instanceof Error ? error.message : String(error)}`);\n \n if (error instanceof Error && error.stack) {\n this.log(`\\nStack trace:`);\n this.log(error.stack);\n }\n \n // Log context information\n this.log(`\\nContext:`);\n this.log(`- Working directory: ${configManager.getOriginalCwd()}`);\n this.log(`- Flags: ${JSON.stringify(flags, null, 2)}`);\n \n // Check if error is related to common issues\n const errorMessage = error instanceof Error ? error.message : String(error);\n if (errorMessage.includes('entity directories')) {\n this.log(`\\nHint: This appears to be an entity directory configuration issue.`);\n this.log(`Make sure each entity directory has a .mj-sync.json file.`);\n } else if (errorMessage.includes('database') || errorMessage.includes('connection')) {\n this.log(`\\nHint: This appears to be a database connectivity issue.`);\n this.log(`Check your mj.config.cjs configuration and database connectivity.`);\n } else if (errorMessage.includes('config') || errorMessage.includes('mj.config.cjs')) {\n this.log(`\\nHint: This appears to be a configuration file issue.`);\n this.log(`Make sure mj.config.cjs exists and is properly configured.`);\n }\n \n this.error(error as Error);\n } finally {\n // Dispose SQL logging session if active\n if (sqlLogger) {\n try {\n await sqlLogger.dispose();\n this.log('✅ SQL logging session closed');\n } catch (error) {\n this.warn(`Failed to close SQL logging session: ${error}`);\n }\n }\n \n // Reset sync engine singleton\n resetSyncEngine();\n // Clean up database connection\n await cleanupProvider();\n \n // Exit process to prevent background MJ tasks from throwing errors\n process.exit(0);\n }\n }\n \n private async processEntityDirectory(\n entityDir: string,\n entityConfig: any,\n syncEngine: SyncEngine,\n flags: any,\n syncConfig: any\n ): Promise<{ created: number; updated: number; errors: number }> {\n const result = { created: 0, updated: 0, errors: 0 };\n \n // Find files matching the configured pattern\n const pattern = entityConfig.filePattern || '*.json';\n const jsonFiles = await fastGlob(pattern, {\n cwd: entityDir,\n ignore: ['.mj-sync.json', '.mj-folder.json', '**/*.backup'],\n dot: true, // Include dotfiles (files starting with .)\n onlyFiles: true\n });\n \n this.log(`Processing ${jsonFiles.length} records in ${path.relative(process.cwd(), entityDir) || '.'}`);\n \n // First, process all JSON files in this directory\n await this.processJsonFiles(jsonFiles, entityDir, entityConfig, syncEngine, flags, result);\n \n // Then, recursively process subdirectories\n const entries = await fs.readdir(entityDir, { withFileTypes: true });\n for (const entry of entries) {\n if (entry.isDirectory() && !entry.name.startsWith('.')) {\n const subDir = path.join(entityDir, entry.name);\n \n // Load subdirectory config and merge with parent config\n let subEntityConfig = { ...entityConfig };\n const subDirConfig = await loadEntityConfig(subDir);\n \n if (subDirConfig) {\n // Check if this is a new entity type (has different entity name)\n if (subDirConfig.entity && subDirConfig.entity !== entityConfig.entity) {\n // This is a different entity type, skip it (will be processed separately)\n continue;\n }\n \n // Merge defaults: parent defaults + subdirectory overrides\n subEntityConfig = {\n ...entityConfig,\n ...subDirConfig,\n defaults: {\n ...entityConfig.defaults,\n ...(subDirConfig.defaults || {})\n }\n };\n }\n \n // Process subdirectory with merged config\n const subResult = await this.processEntityDirectory(\n subDir,\n subEntityConfig,\n syncEngine,\n flags,\n syncConfig\n );\n \n result.created += subResult.created;\n result.updated += subResult.updated;\n result.errors += subResult.errors;\n }\n }\n \n return result;\n }\n \n private async processJsonFiles(\n jsonFiles: string[],\n entityDir: string,\n entityConfig: any,\n syncEngine: SyncEngine,\n flags: any,\n result: { created: number; updated: number; errors: number }\n ): Promise<void> {\n if (jsonFiles.length === 0) {\n return;\n }\n \n const spinner = ora();\n spinner.start('Processing records');\n \n let totalRecords = 0;\n \n for (const file of jsonFiles) {\n try {\n const filePath = path.join(entityDir, file);\n const fileContent = await fs.readJson(filePath);\n \n // Process templates in the loaded content\n const processedContent = await syncEngine.processTemplates(fileContent, entityDir);\n \n // Check if the file contains a single record or an array of records\n const isArray = Array.isArray(processedContent);\n const records: RecordData[] = isArray ? processedContent : [processedContent];\n totalRecords += records.length;\n \n // Build and process defaults (including lookups)\n const defaults = await syncEngine.buildDefaults(filePath, entityConfig);\n \n // Process each record in the file\n for (let i = 0; i < records.length; i++) {\n const recordData = records[i];\n \n // Process the record\n const isNew = await this.pushRecord(\n recordData,\n entityConfig.entity,\n path.dirname(filePath),\n file,\n defaults,\n syncEngine,\n flags['dry-run'],\n flags.verbose,\n isArray ? i : undefined\n );\n \n if (!flags['dry-run']) {\n if (isNew) {\n result.created++;\n } else {\n result.updated++;\n }\n }\n \n spinner.text = `Processing records (${result.created + result.updated + result.errors}/${totalRecords})`;\n }\n \n // Write back the entire file if it's an array\n if (isArray && !flags['dry-run']) {\n await fs.writeJson(filePath, records, { spaces: 2 });\n }\n \n } catch (error) {\n result.errors++;\n const errorMessage = error instanceof Error ? error.message : String(error);\n this.error(`Failed to process ${file}: ${errorMessage}`, { exit: false });\n }\n }\n \n spinner.succeed(`Processed ${totalRecords} records from ${jsonFiles.length} files`);\n }\n \n private async pushRecord(\n recordData: RecordData,\n entityName: string,\n baseDir: string,\n fileName: string,\n defaults: Record<string, any>,\n syncEngine: SyncEngine,\n dryRun: boolean,\n verbose: boolean = false,\n arrayIndex?: number\n ): Promise<boolean> {\n // Load or create entity\n let entity: BaseEntity | null = null;\n let isNew = false;\n \n if (recordData.primaryKey) {\n entity = await syncEngine.loadEntity(entityName, recordData.primaryKey);\n }\n \n if (!entity) {\n // New record\n entity = await syncEngine.createEntityObject(entityName);\n entity.NewRecord();\n isNew = true;\n \n // Handle primary keys for new records\n const entityInfo = syncEngine.getEntityInfo(entityName);\n if (entityInfo) {\n for (const pk of entityInfo.PrimaryKeys) {\n if (!pk.AutoIncrement) {\n // Check if we have a value in primaryKey object\n if (recordData.primaryKey?.[pk.Name]) {\n // User specified a primary key for new record, set it on entity directly\n // Don't add to fields as it will be in primaryKey section\n (entity as any)[pk.Name] = recordData.primaryKey[pk.Name];\n if (verbose) {\n this.log(` Using specified primary key ${pk.Name}: ${recordData.primaryKey[pk.Name]}`);\n }\n } else if (pk.Type.toLowerCase() === 'uniqueidentifier' && !recordData.fields[pk.Name]) {\n // Generate UUID for this primary key and set it on entity directly\n const uuid = uuidv4();\n // Don't add to fields as it will be in primaryKey section after save\n if (verbose) {\n this.log(` Generated UUID for primary key ${pk.Name}: ${uuid}`);\n }\n // Set the generated UUID on the entity\n (entity as any)[pk.Name] = uuid;\n }\n }\n }\n }\n }\n \n // Apply defaults first\n for (const [field, value] of Object.entries(defaults)) {\n if (field in entity) {\n (entity as any)[field] = value;\n }\n }\n \n // Apply record fields\n for (const [field, value] of Object.entries(recordData.fields)) {\n if (field in entity) {\n try {\n const processedValue = await syncEngine.processFieldValue(value, baseDir, null, null);\n if (verbose) {\n this.log(` Setting ${field}: ${JSON.stringify(value)} -> ${JSON.stringify(processedValue)}`);\n }\n (entity as any)[field] = processedValue;\n } catch (error) {\n throw new Error(`Failed to process field '${field}': ${error}`);\n }\n } else {\n this.warn(`Field '${field}' does not exist on entity '${entityName}'`);\n }\n }\n \n if (dryRun) {\n this.log(`Would ${isNew ? 'create' : 'update'} ${entityName} record`);\n return isNew;\n }\n \n // Save the record\n const saved = await entity.Save();\n if (!saved) {\n const message = entity.LatestResult?.Message;\n if (message) {\n throw new Error(`Failed to save record: ${message}`);\n }\n \n const errors = entity.LatestResult?.Errors?.map(err => \n typeof err === 'string' ? err : (err?.message || JSON.stringify(err))\n )?.join(', ') || 'Unknown error';\n throw new Error(`Failed to save record: ${errors}`);\n }\n \n // Process related entities after saving parent\n if (recordData.relatedEntities && !dryRun) {\n await this.processRelatedEntities(\n recordData.relatedEntities,\n entity,\n entity, // root is same as parent for top level\n baseDir,\n syncEngine,\n verbose\n );\n }\n \n // Update the local file with new primary key if created\n if (isNew) {\n const entityInfo = syncEngine.getEntityInfo(entityName);\n if (entityInfo) {\n const newPrimaryKey: Record<string, any> = {};\n for (const pk of entityInfo.PrimaryKeys) {\n newPrimaryKey[pk.Name] = entity.Get(pk.Name);\n }\n recordData.primaryKey = newPrimaryKey;\n }\n }\n \n // Always update sync metadata\n // This ensures related entities are persisted with their metadata\n recordData.sync = {\n lastModified: new Date().toISOString(),\n checksum: syncEngine.calculateChecksum(recordData.fields)\n };\n \n // Write back to file only if it's a single record (not part of an array)\n // Array records are written back in bulk after all records are processed\n if (arrayIndex === undefined) {\n const filePath = path.join(baseDir, fileName);\n await fs.writeJson(filePath, recordData, { spaces: 2 });\n }\n \n return isNew;\n }\n \n private async processRelatedEntities(\n relatedEntities: Record<string, RecordData[]>,\n parentEntity: BaseEntity,\n rootEntity: BaseEntity,\n baseDir: string,\n syncEngine: SyncEngine,\n verbose: boolean = false,\n indentLevel: number = 1\n ): Promise<void> {\n const indent = ' '.repeat(indentLevel);\n \n for (const [entityName, records] of Object.entries(relatedEntities)) {\n this.log(`${indent}↳ Processing ${records.length} related ${entityName} records`);\n \n for (const relatedRecord of records) {\n try {\n // Load or create entity\n let entity = null;\n let isNew = false;\n \n if (relatedRecord.primaryKey) {\n entity = await syncEngine.loadEntity(entityName, relatedRecord.primaryKey);\n }\n \n if (!entity) {\n entity = await syncEngine.createEntityObject(entityName);\n entity.NewRecord();\n isNew = true;\n \n // Handle primary keys for new related entity records\n const entityInfo = syncEngine.getEntityInfo(entityName);\n if (entityInfo) {\n for (const pk of entityInfo.PrimaryKeys) {\n if (!pk.AutoIncrement) {\n // Check if we have a value in primaryKey object\n if (relatedRecord.primaryKey?.[pk.Name]) {\n // User specified a primary key for new record, set it on entity directly\n // Don't add to fields as it will be in primaryKey section\n (entity as any)[pk.Name] = relatedRecord.primaryKey[pk.Name];\n if (verbose) {\n this.log(`${indent} Using specified primary key ${pk.Name}: ${relatedRecord.primaryKey[pk.Name]}`);\n }\n } else if (pk.Type.toLowerCase() === 'uniqueidentifier' && !relatedRecord.fields[pk.Name]) {\n // Generate UUID for this primary key and set it on entity directly\n const uuid = uuidv4();\n // Don't add to fields as it will be in primaryKey section after save\n (entity as any)[pk.Name] = uuid;\n if (verbose) {\n this.log(`${indent} Generated UUID for primary key ${pk.Name}: ${uuid}`);\n }\n }\n }\n }\n }\n }\n \n // Apply fields with parent/root context\n for (const [field, value] of Object.entries(relatedRecord.fields)) {\n if (field in entity) {\n try {\n const processedValue = await syncEngine.processFieldValue(\n value, \n baseDir, \n parentEntity, \n rootEntity\n );\n if (verbose) {\n this.log(`${indent} Setting ${field}: ${JSON.stringify(value)} -> ${JSON.stringify(processedValue)}`);\n }\n (entity as any)[field] = processedValue;\n } catch (error) {\n throw new Error(`Failed to process field '${field}' in ${entityName}: ${error}`);\n }\n } else {\n this.warn(`${indent} Field '${field}' does not exist on entity '${entityName}'`);\n }\n }\n \n // Save the related entity\n const saved = await entity.Save();\n if (!saved) {\n const message = entity.LatestResult?.Message;\n if (message) {\n throw new Error(`Failed to save related ${entityName}: ${message}`);\n }\n \n const errors = entity.LatestResult?.Errors?.map(err => \n typeof err === 'string' ? err : (err?.message || JSON.stringify(err))\n )?.join(', ') || 'Unknown error';\n throw new Error(`Failed to save related ${entityName}: ${errors}`);\n }\n \n if (verbose) {\n this.log(`${indent} ✓ ${isNew ? 'Created' : 'Updated'} ${entityName} record`);\n }\n \n // Update the related record with primary key and sync metadata\n const entityInfo = syncEngine.getEntityInfo(entityName);\n if (entityInfo) {\n // Update primary key if new\n if (isNew) {\n relatedRecord.primaryKey = {};\n for (const pk of entityInfo.PrimaryKeys) {\n relatedRecord.primaryKey[pk.Name] = entity.Get(pk.Name);\n }\n }\n \n // Always update sync metadata\n relatedRecord.sync = {\n lastModified: new Date().toISOString(),\n checksum: syncEngine.calculateChecksum(relatedRecord.fields)\n };\n }\n \n // Process nested related entities if any\n if (relatedRecord.relatedEntities) {\n await this.processRelatedEntities(\n relatedRecord.relatedEntities,\n entity,\n rootEntity,\n baseDir,\n syncEngine,\n verbose,\n indentLevel + 1\n );\n }\n } catch (error) {\n throw new Error(`Failed to process related ${entityName}: ${error}`);\n }\n }\n }\n }\n}"]}
@@ -79,6 +79,37 @@ class Status extends core_1.Command {
79
79
  }
80
80
  catch (error) {
81
81
  spinner.fail('Status check failed');
82
+ // Enhanced error logging for debugging
83
+ this.log('\n=== Status Check Error Details ===');
84
+ this.log(`Error type: ${error?.constructor?.name || 'Unknown'}`);
85
+ this.log(`Error message: ${error instanceof Error ? error.message : String(error)}`);
86
+ if (error instanceof Error && error.stack) {
87
+ this.log(`\nStack trace:`);
88
+ this.log(error.stack);
89
+ }
90
+ // Log context information
91
+ this.log(`\nContext:`);
92
+ this.log(`- Working directory: ${process.cwd()}`);
93
+ this.log(`- Specific directory: ${flags.dir || 'none (checking all directories)'}`);
94
+ this.log(`- Flags: ${JSON.stringify(flags, null, 2)}`);
95
+ // Check if error is related to common issues
96
+ const errorMessage = error instanceof Error ? error.message : String(error);
97
+ if (errorMessage.includes('No entity directories found')) {
98
+ this.log(`\nHint: This appears to be an entity directory configuration issue.`);
99
+ this.log(`Make sure directories have .mj-sync.json files or run "mj-sync init".`);
100
+ }
101
+ else if (errorMessage.includes('database') || errorMessage.includes('connection')) {
102
+ this.log(`\nHint: This appears to be a database connectivity issue.`);
103
+ this.log(`Check your mj.config.cjs configuration and database connectivity.`);
104
+ }
105
+ else if (errorMessage.includes('config') || errorMessage.includes('mj.config.cjs')) {
106
+ this.log(`\nHint: This appears to be a configuration file issue.`);
107
+ this.log(`Make sure mj.config.cjs exists and is properly configured.`);
108
+ }
109
+ else if (errorMessage.includes('metadata')) {
110
+ this.log(`\nHint: This appears to be a metadata loading issue.`);
111
+ this.log(`Check database connectivity and entity configurations.`);
112
+ }
82
113
  this.error(error);
83
114
  }
84
115
  finally {
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/commands/status/index.ts"],"names":[],"mappings":";;;;;AAAA,sCAA6C;AAC7C,wDAA0B;AAC1B,gDAAwB;AACxB,0DAAiC;AACjC,8DAA8B;AAC9B,yCAA8D;AAE9D,6DAAoG;AACpG,mEAA6E;AAC7E,6DAA2D;AAE3D,MAAqB,MAAO,SAAQ,cAAO;IACzC,MAAM,CAAC,WAAW,GAAG,wCAAwC,CAAC;IAE9D,MAAM,CAAC,QAAQ,GAAG;QAChB,qCAAqC;QACrC,wDAAwD;KACzD,CAAC;IAEF,MAAM,CAAC,KAAK,GAAG;QACb,GAAG,EAAE,YAAK,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,2CAA2C,EAAE,CAAC;KAChF,CAAC;IAEF,KAAK,CAAC,GAAG;QACP,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QAC3C,MAAM,OAAO,GAAG,IAAA,qBAAG,GAAE,CAAC;QAEtB,IAAI,CAAC;YACH,sBAAsB;YACtB,OAAO,CAAC,KAAK,CAAC,uBAAuB,CAAC,CAAC;YACvC,MAAM,QAAQ,GAAG,IAAA,qBAAY,GAAE,CAAC;YAChC,IAAI,CAAC,QAAQ,EAAE,CAAC;gBACd,IAAI,CAAC,KAAK,CAAC,mEAAmE,CAAC,CAAC;YAClF,CAAC;YAED,sEAAsE;YACtE,OAAO,CAAC,IAAI,EAAE,CAAC;YAEf,2BAA2B;YAC3B,MAAM,QAAQ,GAAG,MAAM,IAA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C/C,MAAM,EACN,eAAe,EACf,UAAU,CACX,CAAC;gBAEF,MAAM,CAAC,GAAG,IAAI,SAAS,CAAC,GAAG,CAAC;gBAC5B,MAAM,CAAC,QAAQ,IAAI,SAAS,CAAC,QAAQ,CAAC;gBACtC,MAAM,CAAC,OAAO,IAAI,SAAS,CAAC,OAAO,CAAC;gBACpC,MAAM,CAAC,SAAS,IAAI,SAAS,CAAC,SAAS,CAAC;YAC1C,CAAC;QACH,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;;AA5LH,yBA6LC","sourcesContent":["import { Command, Flags } from '@oclif/core';\nimport fs from 'fs-extra';\nimport path from 'path';\nimport fastGlob from 'fast-glob';\nimport ora from 'ora-classic';\nimport { loadMJConfig, loadEntityConfig } from '../../config';\nimport { SyncEngine, RecordData } from '../../lib/sync-engine';\nimport { initializeProvider, findEntityDirectories, getSystemUser } from '../../lib/provider-utils';\nimport { getSyncEngine, resetSyncEngine } from '../../lib/singleton-manager';\nimport { cleanupProvider } from '../../lib/provider-utils';\n\nexport default class Status extends Command {\n static description = 'Show status of local files vs database';\n \n static examples = [\n `<%= config.bin %> <%= command.id %>`,\n `<%= config.bin %> <%= command.id %> --dir=\"ai-prompts\"`,\n ];\n \n static flags = {\n dir: Flags.string({ description: 'Specific entity directory to check status' }),\n };\n \n async run(): Promise<void> {\n const { flags } = await this.parse(Status);\n const spinner = ora();\n \n try {\n // Load configurations\n spinner.start('Loading configuration');\n const mjConfig = loadMJConfig();\n if (!mjConfig) {\n this.error('No mj.config.cjs found in current directory or parent directories');\n }\n \n // Stop spinner before provider initialization (which logs to console)\n spinner.stop();\n \n // Initialize data provider\n const provider = await initializeProvider(mjConfig);\n \n // Initialize sync engine using singleton pattern\n const syncEngine = await getSyncEngine(getSystemUser());\n \n // Show success after all initialization is complete\n spinner.succeed('Configuration and metadata loaded');\n \n // Find entity directories to process\n const entityDirs = findEntityDirectories(process.cwd(), flags.dir);\n \n if (entityDirs.length === 0) {\n this.error('No entity directories found');\n }\n \n this.log(`Found ${entityDirs.length} entity ${entityDirs.length === 1 ? 'directory' : 'directories'} to check`);\n \n // Process each entity directory\n let totalNew = 0;\n let totalModified = 0;\n let totalDeleted = 0;\n let totalUnchanged = 0;\n \n for (const entityDir of entityDirs) {\n const entityConfig = await loadEntityConfig(entityDir);\n if (!entityConfig) {\n this.warn(`Skipping ${entityDir} - no valid entity configuration`);\n continue;\n }\n \n this.log(`\\nChecking ${entityConfig.entity} in ${entityDir}`);\n \n const result = await this.checkEntityDirectory(\n entityDir,\n entityConfig,\n syncEngine\n );\n \n totalNew += result.new;\n totalModified += result.modified;\n totalDeleted += result.deleted;\n totalUnchanged += result.unchanged;\n \n // Show directory summary\n if (result.new > 0 || result.modified > 0 || result.deleted > 0) {\n this.log(` New: ${result.new}, Modified: ${result.modified}, Deleted: ${result.deleted}, Unchanged: ${result.unchanged}`);\n } else {\n this.log(` All ${result.unchanged} records are up to date`);\n }\n }\n \n // Overall summary\n this.log('\\n=== Status Summary ===');\n this.log(`New (local only): ${totalNew}`);\n this.log(`Modified locally: ${totalModified}`);\n this.log(`Deleted locally: ${totalDeleted}`);\n this.log(`Unchanged: ${totalUnchanged}`);\n \n } catch (error) {\n spinner.fail('Status check failed');\n this.error(error as Error);\n } finally {\n // Reset sync engine singleton\n resetSyncEngine();\n // Clean up database connection\n await cleanupProvider();\n \n // Exit process to prevent background MJ tasks from throwing errors\n process.exit(0);\n }\n }\n \n private async checkEntityDirectory(\n entityDir: string,\n entityConfig: any,\n syncEngine: SyncEngine\n ): Promise<{ new: number; modified: number; deleted: number; unchanged: number }> {\n const result = { new: 0, modified: 0, deleted: 0, unchanged: 0 };\n \n // Find files matching the configured pattern\n const pattern = entityConfig.filePattern || '*.json';\n const jsonFiles = await fastGlob(pattern, {\n cwd: entityDir,\n ignore: ['.mj-sync.json', '.mj-folder.json', '**/*.backup'],\n dot: true // Include dotfiles (files starting with .)\n });\n \n for (const file of jsonFiles) {\n try {\n const filePath = path.join(entityDir, file);\n const recordData: RecordData = await fs.readJson(filePath);\n \n if (recordData.primaryKey) {\n // Check if record exists in database\n const entity = await syncEngine.loadEntity(entityConfig.entity, recordData.primaryKey);\n \n if (!entity) {\n result.deleted++;\n } else {\n // Check if modified\n const currentChecksum = syncEngine.calculateChecksum(recordData.fields);\n if (recordData.sync?.checksum !== currentChecksum) {\n result.modified++;\n } else {\n result.unchanged++;\n }\n }\n } else {\n // New record\n result.new++;\n }\n \n } catch (error) {\n this.warn(`Failed to check ${file}: ${error}`);\n }\n }\n \n // Recursively process subdirectories\n const entries = await fs.readdir(entityDir, { withFileTypes: true });\n for (const entry of entries) {\n if (entry.isDirectory() && !entry.name.startsWith('.')) {\n const subDir = path.join(entityDir, entry.name);\n \n // Load subdirectory config and merge with parent config\n let subEntityConfig = { ...entityConfig };\n const subDirConfig = await loadEntityConfig(subDir);\n \n if (subDirConfig) {\n // Check if this is a new entity type (has different entity name)\n if (subDirConfig.entity && subDirConfig.entity !== entityConfig.entity) {\n // This is a different entity type, skip it (will be processed separately)\n continue;\n }\n \n // Merge defaults: parent defaults + subdirectory overrides\n subEntityConfig = {\n ...entityConfig,\n ...subDirConfig,\n defaults: {\n ...entityConfig.defaults,\n ...(subDirConfig.defaults || {})\n }\n };\n }\n \n // Process subdirectory with merged config\n const subResult = await this.checkEntityDirectory(\n subDir,\n subEntityConfig,\n syncEngine\n );\n \n result.new += subResult.new;\n result.modified += subResult.modified;\n result.deleted += subResult.deleted;\n result.unchanged += subResult.unchanged;\n }\n }\n \n return result;\n }\n}"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/commands/status/index.ts"],"names":[],"mappings":";;;;;AAAA,sCAA6C;AAC7C,wDAA0B;AAC1B,gDAAwB;AACxB,0DAAiC;AACjC,8DAA8B;AAC9B,yCAA8D;AAE9D,6DAAoG;AACpG,mEAA6E;AAC7E,6DAA2D;AAE3D,MAAqB,MAAO,SAAQ,cAAO;IACzC,MAAM,CAAC,WAAW,GAAG,wCA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AAG,MAAM,IAAI,CAAC,oBAAoB,CAC/C,MAAM,EACN,eAAe,EACf,UAAU,CACX,CAAC;gBAEF,MAAM,CAAC,GAAG,IAAI,SAAS,CAAC,GAAG,CAAC;gBAC5B,MAAM,CAAC,QAAQ,IAAI,SAAS,CAAC,QAAQ,CAAC;gBACtC,MAAM,CAAC,OAAO,IAAI,SAAS,CAAC,OAAO,CAAC;gBACpC,MAAM,CAAC,SAAS,IAAI,SAAS,CAAC,SAAS,CAAC;YAC1C,CAAC;QACH,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;;AA7NH,yBA8NC","sourcesContent":["import { Command, Flags } from '@oclif/core';\nimport fs from 'fs-extra';\nimport path from 'path';\nimport fastGlob from 'fast-glob';\nimport ora from 'ora-classic';\nimport { loadMJConfig, loadEntityConfig } from '../../config';\nimport { SyncEngine, RecordData } from '../../lib/sync-engine';\nimport { initializeProvider, findEntityDirectories, getSystemUser } from '../../lib/provider-utils';\nimport { getSyncEngine, resetSyncEngine } from '../../lib/singleton-manager';\nimport { cleanupProvider } from '../../lib/provider-utils';\n\nexport default class Status extends Command {\n static description = 'Show status of local files vs database';\n \n static examples = [\n `<%= config.bin %> <%= command.id %>`,\n `<%= config.bin %> <%= command.id %> --dir=\"ai-prompts\"`,\n ];\n \n static flags = {\n dir: Flags.string({ description: 'Specific entity directory to check status' }),\n };\n \n async run(): Promise<void> {\n const { flags } = await this.parse(Status);\n const spinner = ora();\n \n try {\n // Load configurations\n spinner.start('Loading configuration');\n const mjConfig = loadMJConfig();\n if (!mjConfig) {\n this.error('No mj.config.cjs found in current directory or parent directories');\n }\n \n // Stop spinner before provider initialization (which logs to console)\n spinner.stop();\n \n // Initialize data provider\n const provider = await initializeProvider(mjConfig);\n \n // Initialize sync engine using singleton pattern\n const syncEngine = await getSyncEngine(getSystemUser());\n \n // Show success after all initialization is complete\n spinner.succeed('Configuration and metadata loaded');\n \n // Find entity directories to process\n const entityDirs = findEntityDirectories(process.cwd(), flags.dir);\n \n if (entityDirs.length === 0) {\n this.error('No entity directories found');\n }\n \n this.log(`Found ${entityDirs.length} entity ${entityDirs.length === 1 ? 'directory' : 'directories'} to check`);\n \n // Process each entity directory\n let totalNew = 0;\n let totalModified = 0;\n let totalDeleted = 0;\n let totalUnchanged = 0;\n \n for (const entityDir of entityDirs) {\n const entityConfig = await loadEntityConfig(entityDir);\n if (!entityConfig) {\n this.warn(`Skipping ${entityDir} - no valid entity configuration`);\n continue;\n }\n \n this.log(`\\nChecking ${entityConfig.entity} in ${entityDir}`);\n \n const result = await this.checkEntityDirectory(\n entityDir,\n entityConfig,\n syncEngine\n );\n \n totalNew += result.new;\n totalModified += result.modified;\n totalDeleted += result.deleted;\n totalUnchanged += result.unchanged;\n \n // Show directory summary\n if (result.new > 0 || result.modified > 0 || result.deleted > 0) {\n this.log(` New: ${result.new}, Modified: ${result.modified}, Deleted: ${result.deleted}, Unchanged: ${result.unchanged}`);\n } else {\n this.log(` All ${result.unchanged} records are up to date`);\n }\n }\n \n // Overall summary\n this.log('\\n=== Status Summary ===');\n this.log(`New (local only): ${totalNew}`);\n this.log(`Modified locally: ${totalModified}`);\n this.log(`Deleted locally: ${totalDeleted}`);\n this.log(`Unchanged: ${totalUnchanged}`);\n \n } catch (error) {\n spinner.fail('Status check failed');\n \n // Enhanced error logging for debugging\n this.log('\\n=== Status Check Error Details ===');\n this.log(`Error type: ${error?.constructor?.name || 'Unknown'}`);\n this.log(`Error message: ${error instanceof Error ? error.message : String(error)}`);\n \n if (error instanceof Error && error.stack) {\n this.log(`\\nStack trace:`);\n this.log(error.stack);\n }\n \n // Log context information\n this.log(`\\nContext:`);\n this.log(`- Working directory: ${process.cwd()}`);\n this.log(`- Specific directory: ${flags.dir || 'none (checking all directories)'}`);\n this.log(`- Flags: ${JSON.stringify(flags, null, 2)}`);\n \n // Check if error is related to common issues\n const errorMessage = error instanceof Error ? error.message : String(error);\n if (errorMessage.includes('No entity directories found')) {\n this.log(`\\nHint: This appears to be an entity directory configuration issue.`);\n this.log(`Make sure directories have .mj-sync.json files or run \"mj-sync init\".`);\n } else if (errorMessage.includes('database') || errorMessage.includes('connection')) {\n this.log(`\\nHint: This appears to be a database connectivity issue.`);\n this.log(`Check your mj.config.cjs configuration and database connectivity.`);\n } else if (errorMessage.includes('config') || errorMessage.includes('mj.config.cjs')) {\n this.log(`\\nHint: This appears to be a configuration file issue.`);\n this.log(`Make sure mj.config.cjs exists and is properly configured.`);\n } else if (errorMessage.includes('metadata')) {\n this.log(`\\nHint: This appears to be a metadata loading issue.`);\n this.log(`Check database connectivity and entity configurations.`);\n }\n \n this.error(error as Error);\n } finally {\n // Reset sync engine singleton\n resetSyncEngine();\n // Clean up database connection\n await cleanupProvider();\n \n // Exit process to prevent background MJ tasks from throwing errors\n process.exit(0);\n }\n }\n \n private async checkEntityDirectory(\n entityDir: string,\n entityConfig: any,\n syncEngine: SyncEngine\n ): Promise<{ new: number; modified: number; deleted: number; unchanged: number }> {\n const result = { new: 0, modified: 0, deleted: 0, unchanged: 0 };\n \n // Find files matching the configured pattern\n const pattern = entityConfig.filePattern || '*.json';\n const jsonFiles = await fastGlob(pattern, {\n cwd: entityDir,\n ignore: ['.mj-sync.json', '.mj-folder.json', '**/*.backup'],\n dot: true // Include dotfiles (files starting with .)\n });\n \n for (const file of jsonFiles) {\n try {\n const filePath = path.join(entityDir, file);\n const recordData: RecordData = await fs.readJson(filePath);\n \n if (recordData.primaryKey) {\n // Check if record exists in database\n const entity = await syncEngine.loadEntity(entityConfig.entity, recordData.primaryKey);\n \n if (!entity) {\n result.deleted++;\n } else {\n // Check if modified\n const currentChecksum = syncEngine.calculateChecksum(recordData.fields);\n if (recordData.sync?.checksum !== currentChecksum) {\n result.modified++;\n } else {\n result.unchanged++;\n }\n }\n } else {\n // New record\n result.new++;\n }\n \n } catch (error) {\n this.warn(`Failed to check ${file}: ${error}`);\n }\n }\n \n // Recursively process subdirectories\n const entries = await fs.readdir(entityDir, { withFileTypes: true });\n for (const entry of entries) {\n if (entry.isDirectory() && !entry.name.startsWith('.')) {\n const subDir = path.join(entityDir, entry.name);\n \n // Load subdirectory config and merge with parent config\n let subEntityConfig = { ...entityConfig };\n const subDirConfig = await loadEntityConfig(subDir);\n \n if (subDirConfig) {\n // Check if this is a new entity type (has different entity name)\n if (subDirConfig.entity && subDirConfig.entity !== entityConfig.entity) {\n // This is a different entity type, skip it (will be processed separately)\n continue;\n }\n \n // Merge defaults: parent defaults + subdirectory overrides\n subEntityConfig = {\n ...entityConfig,\n ...subDirConfig,\n defaults: {\n ...entityConfig.defaults,\n ...(subDirConfig.defaults || {})\n }\n };\n }\n \n // Process subdirectory with merged config\n const subResult = await this.checkEntityDirectory(\n subDir,\n subEntityConfig,\n syncEngine\n );\n \n result.new += subResult.new;\n result.modified += subResult.modified;\n result.deleted += subResult.deleted;\n result.unchanged += subResult.unchanged;\n }\n }\n \n return result;\n }\n}"]}
@@ -103,6 +103,41 @@ class Watch extends core_1.Command {
103
103
  }
104
104
  catch (error) {
105
105
  spinner.fail('Watch failed');
106
+ // Enhanced error logging for debugging
107
+ this.log('\n=== Watch Error Details ===');
108
+ this.log(`Error type: ${error?.constructor?.name || 'Unknown'}`);
109
+ this.log(`Error message: ${error instanceof Error ? error.message : String(error)}`);
110
+ if (error instanceof Error && error.stack) {
111
+ this.log(`\nStack trace:`);
112
+ this.log(error.stack);
113
+ }
114
+ // Log context information
115
+ this.log(`\nContext:`);
116
+ this.log(`- Working directory: ${process.cwd()}`);
117
+ this.log(`- Specific directory: ${flags.dir || 'none (watching all directories)'}`);
118
+ this.log(`- Flags: ${JSON.stringify(flags, null, 2)}`);
119
+ // Check if error is related to common issues
120
+ const errorMessage = error instanceof Error ? error.message : String(error);
121
+ if (errorMessage.includes('No entity directories found')) {
122
+ this.log(`\nHint: This appears to be an entity directory configuration issue.`);
123
+ this.log(`Make sure directories have .mj-sync.json files or run "mj-sync init".`);
124
+ }
125
+ else if (errorMessage.includes('database') || errorMessage.includes('connection')) {
126
+ this.log(`\nHint: This appears to be a database connectivity issue.`);
127
+ this.log(`Check your mj.config.cjs configuration and database connectivity.`);
128
+ }
129
+ else if (errorMessage.includes('config') || errorMessage.includes('mj.config.cjs')) {
130
+ this.log(`\nHint: This appears to be a configuration file issue.`);
131
+ this.log(`Make sure mj.config.cjs exists and is properly configured.`);
132
+ }
133
+ else if (errorMessage.includes('ENOENT') || errorMessage.includes('no such file')) {
134
+ this.log(`\nHint: File or directory access issue.`);
135
+ this.log(`Check that the directories exist and are accessible.`);
136
+ }
137
+ else if (errorMessage.includes('chokidar') || errorMessage.includes('watch')) {
138
+ this.log(`\nHint: File watching system issue.`);
139
+ this.log(`Check file system permissions and available file descriptors.`);
140
+ }
106
141
  // Reset sync engine singleton
107
142
  (0, singleton_manager_1.resetSyncEngine)();
108
143
  // Clean up database connection
@@ -174,7 +209,11 @@ class Watch extends core_1.Command {
174
209
  // Save the record
175
210
  const saved = await entity.Save();
176
211
  if (!saved) {
177
- const errors = entity.LatestResult?.Errors?.join(', ') || 'Unknown error';
212
+ const message = entity.LatestResult?.Message;
213
+ if (message) {
214
+ throw new Error(`Failed to save record: ${message}`);
215
+ }
216
+ const errors = entity.LatestResult?.Errors?.map(err => typeof err === 'string' ? err : (err?.message || JSON.stringify(err)))?.join(', ') || 'Unknown error';
178
217
  throw new Error(`Failed to save record: ${errors}`);
179
218
  }
180
219
  this.log(`Successfully ${isNew ? 'created' : 'updated'} ${entityConfig.entity} record`);