@memberjunction/metadata-sync 2.49.0 → 2.51.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.
@@ -0,0 +1,149 @@
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
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const core_1 = require("@oclif/core");
30
+ const path = __importStar(require("path"));
31
+ const fs = __importStar(require("fs"));
32
+ const chalk_1 = __importDefault(require("chalk"));
33
+ const ValidationService_1 = require("../../services/ValidationService");
34
+ const FormattingService_1 = require("../../services/FormattingService");
35
+ const config_1 = require("../../config");
36
+ const provider_utils_1 = require("../../lib/provider-utils");
37
+ const ora_classic_1 = __importDefault(require("ora-classic"));
38
+ class Validate extends core_1.Command {
39
+ static description = 'Validate metadata files for correctness and dependencies';
40
+ static examples = [
41
+ `<%= config.bin %> <%= command.id %>`,
42
+ `<%= config.bin %> <%= command.id %> --verbose`,
43
+ `<%= config.bin %> <%= command.id %> --format=json`,
44
+ `<%= config.bin %> <%= command.id %> --dir=./metadata`,
45
+ `<%= config.bin %> <%= command.id %> --no-report`,
46
+ `<%= config.bin %> <%= command.id %> -o validation-results.md`,
47
+ ];
48
+ static flags = {
49
+ dir: core_1.Flags.string({
50
+ description: 'Directory to validate (defaults to current directory)',
51
+ default: process.cwd()
52
+ }),
53
+ verbose: core_1.Flags.boolean({
54
+ char: 'v',
55
+ description: 'Show detailed validation output'
56
+ }),
57
+ format: core_1.Flags.string({
58
+ description: 'Output format',
59
+ options: ['human', 'json'],
60
+ default: 'human'
61
+ }),
62
+ 'max-depth': core_1.Flags.integer({
63
+ description: 'Maximum nesting depth before warning',
64
+ default: 10
65
+ }),
66
+ 'no-best-practices': core_1.Flags.boolean({
67
+ description: 'Skip best practice checks'
68
+ }),
69
+ 'no-report': core_1.Flags.boolean({
70
+ description: 'Skip saving validation report to markdown file'
71
+ }),
72
+ 'output-file': core_1.Flags.string({
73
+ char: 'o',
74
+ description: 'Custom output filename for report'
75
+ })
76
+ };
77
+ async run() {
78
+ const { flags } = await this.parse(Validate);
79
+ const spinner = (0, ora_classic_1.default)();
80
+ const options = {
81
+ verbose: flags.verbose,
82
+ outputFormat: flags.format,
83
+ maxNestingDepth: flags['max-depth'],
84
+ checkBestPractices: !flags['no-best-practices']
85
+ };
86
+ try {
87
+ // Load MJ config and initialize provider
88
+ spinner.start('Loading configuration');
89
+ const mjConfig = (0, config_1.loadMJConfig)();
90
+ if (!mjConfig) {
91
+ spinner.fail('No mj.config.cjs found');
92
+ this.error('No mj.config.cjs found in current directory or parent directories');
93
+ }
94
+ spinner.stop();
95
+ await (0, provider_utils_1.initializeProvider)(mjConfig);
96
+ const validator = new ValidationService_1.ValidationService(options);
97
+ const formatter = new FormattingService_1.FormattingService();
98
+ const resolvedPath = path.resolve(flags.dir);
99
+ if (options.outputFormat === 'human') {
100
+ this.log(chalk_1.default.blue(`Validating metadata in: ${resolvedPath}`));
101
+ this.log('');
102
+ }
103
+ const result = await validator.validateDirectory(resolvedPath);
104
+ // Save report by default (unless --no-report is specified)
105
+ if (!flags['no-report']) {
106
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, -5);
107
+ const defaultFilename = `validation-report-${timestamp}.md`;
108
+ const outputFile = flags['output-file'] || defaultFilename;
109
+ // Save to the validation directory by default
110
+ const outputPath = path.resolve(resolvedPath, outputFile);
111
+ const markdownReport = formatter.formatValidationResultAsMarkdown(result);
112
+ fs.writeFileSync(outputPath, markdownReport, 'utf8');
113
+ if (options.outputFormat === 'human') {
114
+ this.log(chalk_1.default.gray(`\nReport saved to: ${outputPath}`));
115
+ }
116
+ }
117
+ if (options.outputFormat === 'json') {
118
+ this.log(formatter.formatValidationResultAsJson(result));
119
+ }
120
+ else {
121
+ const output = formatter.formatValidationResult(result, options.verbose);
122
+ this.log(output);
123
+ }
124
+ // Exit with error code if validation failed
125
+ if (!result.isValid) {
126
+ this.exit(1);
127
+ }
128
+ }
129
+ catch (error) {
130
+ if (options.outputFormat === 'json') {
131
+ this.log(JSON.stringify({
132
+ error: error instanceof Error ? error.message : String(error),
133
+ isValid: false
134
+ }, null, 2));
135
+ }
136
+ else {
137
+ this.error(chalk_1.default.red(`Validation failed: ${error instanceof Error ? error.message : String(error)}`));
138
+ }
139
+ this.exit(1);
140
+ }
141
+ finally {
142
+ // Exit process to prevent background MJ tasks from throwing errors
143
+ // We don't explicitly close the connection - let the process termination handle it
144
+ process.exit(0);
145
+ }
146
+ }
147
+ }
148
+ exports.default = Validate;
149
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/commands/validate/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sCAA6C;AAC7C,2CAA6B;AAC7B,uCAAyB;AACzB,kDAA0B;AAC1B,w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AAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;oBAC7D,OAAO,EAAE,KAAK;iBACjB,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;YACjB,CAAC;iBAAM,CAAC;gBACJ,IAAI,CAAC,KAAK,CAAC,eAAK,CAAC,GAAG,CAAC,sBAAsB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC;YAC1G,CAAC;YACD,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACjB,CAAC;gBAAS,CAAC;YACP,mEAAmE;YACnE,mFAAmF;YACnF,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACpB,CAAC;IACL,CAAC;;AAvHL,2BAwHC","sourcesContent":["import { Command, Flags } from '@oclif/core';\nimport * as path from 'path';\nimport * as fs from 'fs';\nimport chalk from 'chalk';\nimport { ValidationService } from '../../services/ValidationService';\nimport { FormattingService } from '../../services/FormattingService';\nimport { ValidationOptions } from '../../types/validation';\nimport { loadMJConfig } from '../../config';\nimport { initializeProvider } from '../../lib/provider-utils';\nimport ora from 'ora-classic';\n\nexport default class Validate extends Command {\n static description = 'Validate metadata files for correctness and dependencies';\n\n static examples = [\n `<%= config.bin %> <%= command.id %>`,\n `<%= config.bin %> <%= command.id %> --verbose`,\n `<%= config.bin %> <%= command.id %> --format=json`,\n `<%= config.bin %> <%= command.id %> --dir=./metadata`,\n `<%= config.bin %> <%= command.id %> --no-report`,\n `<%= config.bin %> <%= command.id %> -o validation-results.md`,\n ];\n\n static flags = {\n dir: Flags.string({ \n description: 'Directory to validate (defaults to current directory)',\n default: process.cwd()\n }),\n verbose: Flags.boolean({ \n char: 'v', \n description: 'Show detailed validation output' \n }),\n format: Flags.string({\n description: 'Output format',\n options: ['human', 'json'],\n default: 'human'\n }),\n 'max-depth': Flags.integer({\n description: 'Maximum nesting depth before warning',\n default: 10\n }),\n 'no-best-practices': Flags.boolean({\n description: 'Skip best practice checks'\n }),\n 'no-report': Flags.boolean({\n description: 'Skip saving validation report to markdown file'\n }),\n 'output-file': Flags.string({\n char: 'o',\n description: 'Custom output filename for report'\n })\n };\n\n async run(): Promise<void> {\n const { flags } = await this.parse(Validate);\n const spinner = ora();\n \n const options: ValidationOptions = {\n verbose: flags.verbose,\n outputFormat: flags.format as 'human' | 'json',\n maxNestingDepth: flags['max-depth'],\n checkBestPractices: !flags['no-best-practices']\n };\n \n try {\n // Load MJ config and initialize provider\n spinner.start('Loading configuration');\n const mjConfig = loadMJConfig();\n if (!mjConfig) {\n spinner.fail('No mj.config.cjs found');\n this.error('No mj.config.cjs found in current directory or parent directories');\n }\n \n spinner.stop();\n await initializeProvider(mjConfig);\n \n const validator = new ValidationService(options);\n const formatter = new FormattingService();\n \n const resolvedPath = path.resolve(flags.dir);\n \n if (options.outputFormat === 'human') {\n this.log(chalk.blue(`Validating metadata in: ${resolvedPath}`));\n this.log('');\n }\n \n const result = await validator.validateDirectory(resolvedPath);\n \n // Save report by default (unless --no-report is specified)\n if (!flags['no-report']) {\n const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, -5);\n const defaultFilename = `validation-report-${timestamp}.md`;\n const outputFile = flags['output-file'] || defaultFilename;\n // Save to the validation directory by default\n const outputPath = path.resolve(resolvedPath, outputFile);\n \n const markdownReport = formatter.formatValidationResultAsMarkdown(result);\n fs.writeFileSync(outputPath, markdownReport, 'utf8');\n \n if (options.outputFormat === 'human') {\n this.log(chalk.gray(`\\nReport saved to: ${outputPath}`));\n }\n }\n \n if (options.outputFormat === 'json') {\n this.log(formatter.formatValidationResultAsJson(result));\n } else {\n const output = formatter.formatValidationResult(result, options.verbose);\n this.log(output);\n }\n \n // Exit with error code if validation failed\n if (!result.isValid) {\n this.exit(1);\n }\n } catch (error) {\n if (options.outputFormat === 'json') {\n this.log(JSON.stringify({ \n error: error instanceof Error ? error.message : String(error),\n isValid: false\n }, null, 2));\n } else {\n this.error(chalk.red(`Validation failed: ${error instanceof Error ? error.message : String(error)}`));\n }\n this.exit(1);\n } finally {\n // Exit process to prevent background MJ tasks from throwing errors\n // We don't explicitly close the connection - let the process termination handle it\n process.exit(0);\n }\n }\n}"]}
@@ -73,8 +73,8 @@ export declare function getSystemUser(): UserInfo;
73
73
  * @example
74
74
  * ```typescript
75
75
  * const provider = getDataProvider();
76
- * if (provider?.createSqlLogger) {
77
- * const logger = await provider.createSqlLogger('/path/to/log.sql');
76
+ * if (provider?.CreateSqlLogger) {
77
+ * const logger = await provider.CreateSqlLogger('/path/to/log.sql');
78
78
  * }
79
79
  * ```
80
80
  */
@@ -92,8 +92,7 @@ async function initializeProvider(config) {
92
92
  // Store for cleanup
93
93
  globalPool = pool;
94
94
  // Create provider config
95
- const providerConfig = new sqlserver_dataprovider_1.SQLServerProviderConfigData(pool, 'system@sync.cli', // Default user for CLI
96
- config.mjCoreSchema || '__mj', 0);
95
+ const providerConfig = new sqlserver_dataprovider_1.SQLServerProviderConfigData(pool, config.mjCoreSchema || '__mj');
97
96
  // Use setupSQLServerClient to properly initialize
98
97
  globalProvider = await (0, sqlserver_dataprovider_1.setupSQLServerClient)(providerConfig);
99
98
  return globalProvider;
@@ -161,8 +160,8 @@ exports.getSystemUser = getSystemUser;
161
160
  * @example
162
161
  * ```typescript
163
162
  * const provider = getDataProvider();
164
- * if (provider?.createSqlLogger) {
165
- * const logger = await provider.createSqlLogger('/path/to/log.sql');
163
+ * if (provider?.CreateSqlLogger) {
164
+ * const logger = await provider.CreateSqlLogger('/path/to/log.sql');
166
165
  * }
167
166
  * ```
168
167
  */
@@ -1 +1 @@
1
- {"version":3,"file":"provider-utils.js","sourceRoot":"","sources":["../../src/lib/provider-utils.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,2CAA6B;AAC7B,mFAA6I;AAE7I,uCAAyB;AACzB,2CAA6B;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E,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAE/E,OAAO,CAAC,GAAG,WAAW,EAAE,GAAG,aAAa,CAAC,CAAC;IAC5C,CAAC;IAED,0EAA0E;IAC1E,OAAO,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC3F,CAAC;AAlFD,sDAkFC","sourcesContent":["/**\n * @fileoverview Database provider utilities for MetadataSync\n * @module provider-utils\n * \n * This module provides utilities for initializing and managing the database\n * connection, accessing system users, and finding entity directories. It handles\n * the mssql ConnectionPool lifecycle and MemberJunction provider initialization.\n */\n\nimport * as sql from 'mssql';\nimport { SQLServerDataProvider, SQLServerProviderConfigData, UserCache, setupSQLServerClient } from '@memberjunction/sqlserver-dataprovider';\nimport type { MJConfig } from '../config';\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport { UserInfo } from '@memberjunction/core';\nimport { configManager } from './config-manager';\n\n/** Global ConnectionPool instance for connection lifecycle management */\nlet globalPool: sql.ConnectionPool | null = null;\n\n/** Global provider instance to ensure single initialization */\nlet globalProvider: SQLServerDataProvider | null = null;\n\n/** Promise to track ongoing initialization */\nlet initializationPromise: Promise<SQLServerDataProvider> | null = null;\n\n/**\n * Initialize a SQLServerDataProvider with the given configuration\n * \n * Creates and initializes a mssql ConnectionPool for SQL Server, then sets up\n * the MemberJunction SQLServerDataProvider. The connection is stored globally\n * for proper cleanup. Auto-detects Azure SQL databases for encryption settings.\n * \n * @param config - MemberJunction configuration with database connection details\n * @returns Promise resolving to initialized SQLServerDataProvider instance\n * @throws Error if database connection fails\n * \n * @example\n * ```typescript\n * const config = loadMJConfig();\n * const provider = await initializeProvider(config);\n * // Provider is ready for use\n * ```\n */\nexport async function initializeProvider(config: MJConfig): Promise<SQLServerDataProvider> {\n // Return existing provider if already initialized\n if (globalProvider) {\n return globalProvider;\n }\n \n // Return ongoing initialization if in progress\n if (initializationPromise) {\n return initializationPromise;\n }\n \n // Start new initialization\n initializationPromise = (async () => {\n // Create mssql config\n const poolConfig: sql.config = {\n server: config.dbHost,\n port: config.dbPort ? Number(config.dbPort) : 1433,\n database: config.dbDatabase,\n user: config.dbUsername,\n password: config.dbPassword,\n options: {\n encrypt: config.dbEncrypt === 'Y' || config.dbEncrypt === 'true' || \n config.dbHost.includes('.database.windows.net'), // Auto-detect Azure SQL\n trustServerCertificate: config.dbTrustServerCertificate === 'Y',\n instanceName: config.dbInstanceName,\n enableArithAbort: true\n }\n };\n \n // Create and connect pool\n const pool = new sql.ConnectionPool(poolConfig);\n await pool.connect();\n \n // Store for cleanup\n globalPool = pool;\n \n // Create provider config\n const providerConfig = new SQLServerProviderConfigData(\n pool,\n 'system@sync.cli', // Default user for CLI\n config.mjCoreSchema || '__mj',\n 0\n );\n \n // Use setupSQLServerClient to properly initialize\n globalProvider = await setupSQLServerClient(providerConfig);\n return globalProvider;\n })();\n \n return initializationPromise;\n}\n\n/**\n * Clean up the global database connection\n * \n * Closes the mssql ConnectionPool if it exists and is connected.\n * Should be called when the CLI command completes to ensure proper cleanup.\n * \n * @returns Promise that resolves when cleanup is complete\n * \n * @example\n * ```typescript\n * try {\n * // Do work with database\n * } finally {\n * await cleanupProvider();\n * }\n * ```\n */\nexport async function cleanupProvider(): Promise<void> {\n if (globalPool && globalPool.connected) {\n await globalPool.close();\n globalPool = null;\n }\n globalProvider = null;\n initializationPromise = null;\n}\n\n/**\n * Get the system user from the UserCache\n * \n * Retrieves the \"System\" user from MemberJunction's UserCache. This user is\n * typically used for CLI operations where no specific user context exists.\n * \n * @returns The System UserInfo object\n * @throws Error if System user is not found in the cache\n * \n * @example\n * ```typescript\n * const systemUser = getSystemUser();\n * const syncEngine = new SyncEngine(systemUser);\n * ```\n */\nexport function getSystemUser(): UserInfo {\n const sysUser = UserCache.Instance.UserByName(\"System\", false);\n if (!sysUser) {\n throw new Error(\"System user not found in cache. Ensure the system user exists in the database.\"); \n }\n return sysUser;\n}\n\n/**\n * Get the current data provider instance\n * \n * Returns the global SQLServerDataProvider instance that was initialized by\n * initializeProvider. This allows access to data provider features like SQL logging.\n * \n * @returns The global SQLServerDataProvider instance or null if not initialized\n * \n * @example\n * ```typescript\n * const provider = getDataProvider();\n * if (provider?.createSqlLogger) {\n * const logger = await provider.createSqlLogger('/path/to/log.sql');\n * }\n * ```\n */\nexport function getDataProvider(): SQLServerDataProvider | null {\n return globalProvider;\n}\n\n/**\n * Find entity directories at the immediate level only\n * \n * Searches for directories containing .mj-sync.json files, which indicate\n * entity data directories. Only searches immediate subdirectories, not recursive.\n * If a specific directory is provided, only checks that directory.\n * \n * @param dir - Base directory to search from\n * @param specificDir - Optional specific subdirectory name to check\n * @param directoryOrder - Optional array specifying the order directories should be processed\n * @returns Array of absolute directory paths containing .mj-sync.json files, ordered according to directoryOrder\n * \n * @example\n * ```typescript\n * // Find all entity directories\n * const dirs = findEntityDirectories(process.cwd());\n * \n * // Check specific directory\n * const dirs = findEntityDirectories(process.cwd(), 'ai-prompts');\n * \n * // Find directories with custom ordering\n * const dirs = findEntityDirectories(process.cwd(), undefined, ['prompts', 'agent-types']);\n * ```\n */\nexport function findEntityDirectories(dir: string, specificDir?: string, directoryOrder?: string[]): string[] {\n const results: string[] = [];\n \n // If specific directory is provided, check if it's an entity directory or root config directory\n if (specificDir) {\n const targetDir = path.isAbsolute(specificDir) ? specificDir : path.join(dir, specificDir);\n if (fs.existsSync(targetDir)) {\n const syncConfigPath = path.join(targetDir, '.mj-sync.json');\n const hasSyncConfig = fs.existsSync(syncConfigPath);\n \n if (hasSyncConfig) {\n try {\n const config = JSON.parse(fs.readFileSync(syncConfigPath, 'utf8'));\n \n // If this config has an entity field, it's an entity directory\n if (config.entity) {\n results.push(targetDir);\n return results;\n }\n \n // If this config has directoryOrder but no entity, treat it as a root config\n // and look for entity directories in its subdirectories\n if (config.directoryOrder) {\n return findEntityDirectories(targetDir, undefined, config.directoryOrder);\n }\n } catch (error) {\n // If we can't parse the config, treat it as a regular directory\n }\n }\n \n // Fallback: look for entity subdirectories in the target directory\n return findEntityDirectories(targetDir, undefined, directoryOrder);\n }\n return results;\n }\n \n // Otherwise, find all immediate subdirectories with .mj-sync.json\n const entries = fs.readdirSync(dir, { withFileTypes: true });\n const foundDirectories: string[] = [];\n \n for (const entry of entries) {\n if (entry.isDirectory() && !entry.name.startsWith('.')) {\n const subDir = path.join(dir, entry.name);\n const hasSyncConfig = fs.existsSync(path.join(subDir, '.mj-sync.json'));\n \n if (hasSyncConfig) {\n foundDirectories.push(subDir);\n }\n }\n }\n \n // If directoryOrder is specified, sort directories according to it\n if (directoryOrder && directoryOrder.length > 0) {\n const orderedDirs: string[] = [];\n const unorderedDirs: string[] = [];\n \n // First, add directories in the specified order\n for (const dirName of directoryOrder) {\n const matchingDir = foundDirectories.find(fullPath => \n path.basename(fullPath) === dirName\n );\n if (matchingDir) {\n orderedDirs.push(matchingDir);\n }\n }\n \n // Then, add any remaining directories in alphabetical order\n for (const foundDir of foundDirectories) {\n const dirName = path.basename(foundDir);\n if (!directoryOrder.includes(dirName)) {\n unorderedDirs.push(foundDir);\n }\n }\n \n // Sort unordered directories alphabetically\n unorderedDirs.sort((a, b) => path.basename(a).localeCompare(path.basename(b)));\n \n return [...orderedDirs, ...unorderedDirs];\n }\n \n // No ordering specified, return in alphabetical order (existing behavior)\n return foundDirectories.sort((a, b) => path.basename(a).localeCompare(path.basename(b)));\n}"]}
1
+ {"version":3,"file":"provider-utils.js","sourceRoot":"","sources":["../../src/lib/provider-utils.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,2CAA6B;AAC7B,mFAA6I;AAE7I,uCAAyB;AACzB,2CAA6B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CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAE/E,OAAO,CAAC,GAAG,WAAW,EAAE,GAAG,aAAa,CAAC,CAAC;IAC5C,CAAC;IAED,0EAA0E;IAC1E,OAAO,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC3F,CAAC;AAlFD,sDAkFC","sourcesContent":["/**\n * @fileoverview Database provider utilities for MetadataSync\n * @module provider-utils\n * \n * This module provides utilities for initializing and managing the database\n * connection, accessing system users, and finding entity directories. It handles\n * the mssql ConnectionPool lifecycle and MemberJunction provider initialization.\n */\n\nimport * as sql from 'mssql';\nimport { SQLServerDataProvider, SQLServerProviderConfigData, UserCache, setupSQLServerClient } from '@memberjunction/sqlserver-dataprovider';\nimport type { MJConfig } from '../config';\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport { UserInfo } from '@memberjunction/core';\nimport { configManager } from './config-manager';\n\n/** Global ConnectionPool instance for connection lifecycle management */\nlet globalPool: sql.ConnectionPool | null = null;\n\n/** Global provider instance to ensure single initialization */\nlet globalProvider: SQLServerDataProvider | null = null;\n\n/** Promise to track ongoing initialization */\nlet initializationPromise: Promise<SQLServerDataProvider> | null = null;\n\n/**\n * Initialize a SQLServerDataProvider with the given configuration\n * \n * Creates and initializes a mssql ConnectionPool for SQL Server, then sets up\n * the MemberJunction SQLServerDataProvider. The connection is stored globally\n * for proper cleanup. Auto-detects Azure SQL databases for encryption settings.\n * \n * @param config - MemberJunction configuration with database connection details\n * @returns Promise resolving to initialized SQLServerDataProvider instance\n * @throws Error if database connection fails\n * \n * @example\n * ```typescript\n * const config = loadMJConfig();\n * const provider = await initializeProvider(config);\n * // Provider is ready for use\n * ```\n */\nexport async function initializeProvider(config: MJConfig): Promise<SQLServerDataProvider> {\n // Return existing provider if already initialized\n if (globalProvider) {\n return globalProvider;\n }\n \n // Return ongoing initialization if in progress\n if (initializationPromise) {\n return initializationPromise;\n }\n \n // Start new initialization\n initializationPromise = (async () => {\n // Create mssql config\n const poolConfig: sql.config = {\n server: config.dbHost,\n port: config.dbPort ? Number(config.dbPort) : 1433,\n database: config.dbDatabase,\n user: config.dbUsername,\n password: config.dbPassword,\n options: {\n encrypt: config.dbEncrypt === 'Y' || config.dbEncrypt === 'true' || \n config.dbHost.includes('.database.windows.net'), // Auto-detect Azure SQL\n trustServerCertificate: config.dbTrustServerCertificate === 'Y',\n instanceName: config.dbInstanceName,\n enableArithAbort: true\n }\n };\n \n // Create and connect pool\n const pool = new sql.ConnectionPool(poolConfig);\n await pool.connect();\n \n // Store for cleanup\n globalPool = pool;\n \n // Create provider config\n const providerConfig = new SQLServerProviderConfigData(\n pool,\n config.mjCoreSchema || '__mj' \n );\n \n // Use setupSQLServerClient to properly initialize\n globalProvider = await setupSQLServerClient(providerConfig);\n return globalProvider;\n })();\n \n return initializationPromise;\n}\n\n/**\n * Clean up the global database connection\n * \n * Closes the mssql ConnectionPool if it exists and is connected.\n * Should be called when the CLI command completes to ensure proper cleanup.\n * \n * @returns Promise that resolves when cleanup is complete\n * \n * @example\n * ```typescript\n * try {\n * // Do work with database\n * } finally {\n * await cleanupProvider();\n * }\n * ```\n */\nexport async function cleanupProvider(): Promise<void> {\n if (globalPool && globalPool.connected) {\n await globalPool.close();\n globalPool = null;\n }\n globalProvider = null;\n initializationPromise = null;\n}\n\n/**\n * Get the system user from the UserCache\n * \n * Retrieves the \"System\" user from MemberJunction's UserCache. This user is\n * typically used for CLI operations where no specific user context exists.\n * \n * @returns The System UserInfo object\n * @throws Error if System user is not found in the cache\n * \n * @example\n * ```typescript\n * const systemUser = getSystemUser();\n * const syncEngine = new SyncEngine(systemUser);\n * ```\n */\nexport function getSystemUser(): UserInfo {\n const sysUser = UserCache.Instance.UserByName(\"System\", false);\n if (!sysUser) {\n throw new Error(\"System user not found in cache. Ensure the system user exists in the database.\"); \n }\n return sysUser;\n}\n\n/**\n * Get the current data provider instance\n * \n * Returns the global SQLServerDataProvider instance that was initialized by\n * initializeProvider. This allows access to data provider features like SQL logging.\n * \n * @returns The global SQLServerDataProvider instance or null if not initialized\n * \n * @example\n * ```typescript\n * const provider = getDataProvider();\n * if (provider?.CreateSqlLogger) {\n * const logger = await provider.CreateSqlLogger('/path/to/log.sql');\n * }\n * ```\n */\nexport function getDataProvider(): SQLServerDataProvider | null {\n return globalProvider;\n}\n\n/**\n * Find entity directories at the immediate level only\n * \n * Searches for directories containing .mj-sync.json files, which indicate\n * entity data directories. Only searches immediate subdirectories, not recursive.\n * If a specific directory is provided, only checks that directory.\n * \n * @param dir - Base directory to search from\n * @param specificDir - Optional specific subdirectory name to check\n * @param directoryOrder - Optional array specifying the order directories should be processed\n * @returns Array of absolute directory paths containing .mj-sync.json files, ordered according to directoryOrder\n * \n * @example\n * ```typescript\n * // Find all entity directories\n * const dirs = findEntityDirectories(process.cwd());\n * \n * // Check specific directory\n * const dirs = findEntityDirectories(process.cwd(), 'ai-prompts');\n * \n * // Find directories with custom ordering\n * const dirs = findEntityDirectories(process.cwd(), undefined, ['prompts', 'agent-types']);\n * ```\n */\nexport function findEntityDirectories(dir: string, specificDir?: string, directoryOrder?: string[]): string[] {\n const results: string[] = [];\n \n // If specific directory is provided, check if it's an entity directory or root config directory\n if (specificDir) {\n const targetDir = path.isAbsolute(specificDir) ? specificDir : path.join(dir, specificDir);\n if (fs.existsSync(targetDir)) {\n const syncConfigPath = path.join(targetDir, '.mj-sync.json');\n const hasSyncConfig = fs.existsSync(syncConfigPath);\n \n if (hasSyncConfig) {\n try {\n const config = JSON.parse(fs.readFileSync(syncConfigPath, 'utf8'));\n \n // If this config has an entity field, it's an entity directory\n if (config.entity) {\n results.push(targetDir);\n return results;\n }\n \n // If this config has directoryOrder but no entity, treat it as a root config\n // and look for entity directories in its subdirectories\n if (config.directoryOrder) {\n return findEntityDirectories(targetDir, undefined, config.directoryOrder);\n }\n } catch (error) {\n // If we can't parse the config, treat it as a regular directory\n }\n }\n \n // Fallback: look for entity subdirectories in the target directory\n return findEntityDirectories(targetDir, undefined, directoryOrder);\n }\n return results;\n }\n \n // Otherwise, find all immediate subdirectories with .mj-sync.json\n const entries = fs.readdirSync(dir, { withFileTypes: true });\n const foundDirectories: string[] = [];\n \n for (const entry of entries) {\n if (entry.isDirectory() && !entry.name.startsWith('.')) {\n const subDir = path.join(dir, entry.name);\n const hasSyncConfig = fs.existsSync(path.join(subDir, '.mj-sync.json'));\n \n if (hasSyncConfig) {\n foundDirectories.push(subDir);\n }\n }\n }\n \n // If directoryOrder is specified, sort directories according to it\n if (directoryOrder && directoryOrder.length > 0) {\n const orderedDirs: string[] = [];\n const unorderedDirs: string[] = [];\n \n // First, add directories in the specified order\n for (const dirName of directoryOrder) {\n const matchingDir = foundDirectories.find(fullPath => \n path.basename(fullPath) === dirName\n );\n if (matchingDir) {\n orderedDirs.push(matchingDir);\n }\n }\n \n // Then, add any remaining directories in alphabetical order\n for (const foundDir of foundDirectories) {\n const dirName = path.basename(foundDir);\n if (!directoryOrder.includes(dirName)) {\n unorderedDirs.push(foundDir);\n }\n }\n \n // Sort unordered directories alphabetically\n unorderedDirs.sort((a, b) => path.basename(a).localeCompare(path.basename(b)));\n \n return [...orderedDirs, ...unorderedDirs];\n }\n \n // No ordering specified, return in alphabetical order (existing behavior)\n return foundDirectories.sort((a, b) => path.basename(a).localeCompare(path.basename(b)));\n}"]}
@@ -406,12 +406,37 @@ class SyncEngine {
406
406
  * ```
407
407
  */
408
408
  async loadEntity(entityName, primaryKey) {
409
- const entity = await this.createEntityObject(entityName);
410
409
  const entityInfo = this.getEntityInfo(entityName);
411
410
  if (!entityInfo) {
412
411
  throw new Error(`Entity not found: ${entityName}`);
413
412
  }
414
- // Handle composite keys
413
+ // First, check if the record exists using RunView to avoid "Error in BaseEntity.Load" messages
414
+ // when records don't exist (which is a normal scenario during sync operations)
415
+ const rv = new core_1.RunView();
416
+ // Build filter for primary key(s)
417
+ const filters = [];
418
+ for (const pk of entityInfo.PrimaryKeys) {
419
+ const value = primaryKey[pk.Name];
420
+ if (value === undefined || value === null) {
421
+ throw new Error(`Missing primary key value for ${pk.Name} in entity ${entityName}`);
422
+ }
423
+ // Check if field needs quotes
424
+ const field = entityInfo.Fields.find(f => f.Name === pk.Name);
425
+ const quotes = field?.NeedsQuotes ? "'" : '';
426
+ const escapedValue = field?.NeedsQuotes && typeof value === 'string' ? value.replace(/'/g, "''") : value;
427
+ filters.push(`${pk.Name} = ${quotes}${escapedValue}${quotes}`);
428
+ }
429
+ const result = await rv.RunView({
430
+ EntityName: entityName,
431
+ ExtraFilter: filters.join(' AND '),
432
+ MaxRows: 1
433
+ }, this.contextUser);
434
+ // If no record found, return null without attempting to load
435
+ if (!result.Success || result.Results.length === 0) {
436
+ return null;
437
+ }
438
+ // Record exists, now load it properly through the entity
439
+ const entity = await this.createEntityObject(entityName);
415
440
  const compositeKey = new core_1.CompositeKey();
416
441
  compositeKey.LoadFromSimpleObject(primaryKey);
417
442
  const loaded = await entity.InnerLoad(compositeKey);
@@ -1 +1 @@
1
- {"version":3,"file":"sync-engine.js","sourceRoot":"","sources":["../../src/lib/sync-engine.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;;;;AAEH,gDAAwB;AACxB,wDAA0B;AAC1B,oDAA4B;AAC5B,kDAA0B;AAC1B,+CAAyG;AACzG,mDAAgD;AAsBhD;;;;;;;;;;;;GAYG;AACH,MAAa,UAAU;IACb,QAAQ,CAAW;IACnB,WAAW,CAAW;IAE9B;;;OAGG;IACH,YAAY,WAAqB;QAC/B,IAAI,CAAC,QAAQ,GAAG,IAAI,eAAQ,EAAE,CAAC;QAC/B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;IACjC,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,UAAU;QACd,sBA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B,SAAS,CAAC,wBAAwB;YACpC,CAAC;YAED,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;gBACpE,OAAO,MAAM,CAAC,GAAG,CAAC,KAAK,QAAQ,IAAI,MAAM,CAAC,GAAG,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;gBAC3F,sCAAsC;gBACtC,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC;YACnD,CAAC;iBAAM,CAAC;gBACN,+BAA+B;gBAC/B,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;YACtB,CAAC;QACH,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CAEF;AApmBD,gCAomBC","sourcesContent":["/**\n * @fileoverview Core synchronization engine for MemberJunction metadata\n * @module sync-engine\n * \n * This module provides the core functionality for synchronizing metadata between\n * the MemberJunction database and local file system representations. It handles\n * special reference types (@file, @url, @lookup, @env, @parent, @root, @template),\n * manages entity operations, and provides utilities for data transformation.\n */\n\nimport path from 'path';\nimport fs from 'fs-extra';\nimport crypto from 'crypto';\nimport axios from 'axios';\nimport { EntityInfo, Metadata, RunView, BaseEntity, CompositeKey, UserInfo } from '@memberjunction/core';\nimport { uuidv4 } from '@memberjunction/global';\nimport { EntityConfig, FolderConfig } from '../config';\n\n/**\n * Represents the structure of a metadata record with optional sync tracking\n */\nexport interface RecordData {\n /** Primary key field(s) and their values */\n primaryKey?: Record<string, any>;\n /** Entity field names and their values */\n fields: Record<string, any>;\n /** Related entities organized by entity name */\n relatedEntities?: Record<string, RecordData[]>;\n /** Synchronization metadata for change tracking */\n sync?: {\n /** ISO timestamp of last modification */\n lastModified: string;\n /** SHA256 checksum of the fields object */\n checksum: string;\n };\n}\n\n/**\n * Core engine for synchronizing MemberJunction metadata between database and files\n * \n * @class SyncEngine\n * @example\n * ```typescript\n * const syncEngine = new SyncEngine(systemUser);\n * await syncEngine.initialize();\n * \n * // Process a field value with special references\n * const value = await syncEngine.processFieldValue('@lookup:Users.Email=admin@example.com', '/path/to/base');\n * ```\n */\nexport class SyncEngine {\n private metadata: Metadata;\n private contextUser: UserInfo;\n\n /**\n * Creates a new SyncEngine instance\n * @param contextUser - The user context for database operations\n */\n constructor(contextUser: UserInfo) {\n this.metadata = new Metadata();\n this.contextUser = contextUser;\n }\n \n /**\n * Initializes the sync engine by refreshing metadata cache\n * @returns Promise that resolves when initialization is complete\n */\n async initialize(): Promise<void> {\n // Initialize metadata\n await this.metadata.Refresh();\n }\n \n /**\n * Process special references in field values\n * \n * Handles the following reference types:\n * - `@parent:fieldName` - References a field from the parent record\n * - `@root:fieldName` - References a field from the root record\n * - `@file:path` - Reads content from an external file\n * - `@url:address` - Fetches content from a URL\n * - `@lookup:Entity.Field=Value` - Looks up an entity ID by field value\n * - `@env:VARIABLE` - Reads an environment variable\n * \n * @param value - The field value to process\n * @param baseDir - Base directory for resolving relative file paths\n * @param parentRecord - Optional parent entity for @parent references\n * @param rootRecord - Optional root entity for @root references\n * @returns The processed value with all references resolved\n * @throws Error if a reference cannot be resolved\n * \n * @example\n * ```typescript\n * // File reference\n * const content = await processFieldValue('@file:template.md', '/path/to/dir');\n * \n * // Lookup with auto-create\n * const userId = await processFieldValue('@lookup:Users.Email=john@example.com?create', '/path');\n * ```\n */\n async processFieldValue(value: any, baseDir: string, parentRecord?: BaseEntity | null, rootRecord?: BaseEntity | null): Promise<any> {\n if (typeof value !== 'string') {\n return value;\n }\n \n // Check for @parent: reference\n if (value.startsWith('@parent:')) {\n if (!parentRecord) {\n throw new Error(`@parent reference used but no parent record available: ${value}`);\n }\n const fieldName = value.substring(8);\n return parentRecord.Get(fieldName);\n }\n \n // Check for @root: reference\n if (value.startsWith('@root:')) {\n if (!rootRecord) {\n throw new Error(`@root reference used but no root record available: ${value}`);\n }\n const fieldName = value.substring(6);\n return rootRecord.Get(fieldName);\n }\n \n // Check for @file: reference\n if (value.startsWith('@file:')) {\n const filePath = value.substring(6);\n const fullPath = path.resolve(baseDir, filePath);\n \n if (await fs.pathExists(fullPath)) {\n return await fs.readFile(fullPath, 'utf-8');\n } else {\n throw new Error(`File not found: ${fullPath}`);\n }\n }\n \n // Check for @url: reference\n if (value.startsWith('@url:')) {\n const url = value.substring(5);\n \n try {\n const response = await axios.get(url);\n return response.data;\n } catch (error) {\n throw new Error(`Failed to fetch URL: ${url} - ${error}`);\n }\n }\n \n // Check for @lookup: reference\n if (value.startsWith('@lookup:')) {\n const lookupStr = value.substring(8);\n \n // Parse lookup with optional create syntax\n // Format: EntityName.FieldName=Value?create&OtherField=Value\n const entityMatch = lookupStr.match(/^([^.]+)\\./);\n if (!entityMatch) {\n throw new Error(`Invalid lookup format: ${value}`);\n }\n \n const entityName = entityMatch[1];\n const remaining = lookupStr.substring(entityName.length + 1);\n \n // Check if this has ?create syntax\n const hasCreate = remaining.includes('?create');\n const lookupPart = hasCreate ? remaining.split('?')[0] : remaining;\n \n // Parse the main lookup field\n const fieldMatch = lookupPart.match(/^(.+?)=(.+)$/);\n if (!fieldMatch) {\n throw new Error(`Invalid lookup format: ${value}`);\n }\n \n const [, fieldName, fieldValue] = fieldMatch;\n \n // Parse additional fields for creation if ?create is present\n let createFields: Record<string, any> = {};\n if (hasCreate && remaining.includes('?create&')) {\n const createPart = remaining.split('?create&')[1];\n const pairs = createPart.split('&');\n for (const pair of pairs) {\n const [key, val] = pair.split('=');\n if (key && val) {\n createFields[key] = decodeURIComponent(val);\n }\n }\n }\n \n return await this.resolveLookup(entityName, fieldName, fieldValue, hasCreate, createFields);\n }\n \n // Check for @env: reference\n if (value.startsWith('@env:')) {\n const envVar = value.substring(5);\n const envValue = process.env[envVar];\n \n if (envValue === undefined) {\n throw new Error(`Environment variable not found: ${envVar}`);\n }\n \n return envValue;\n }\n \n return value;\n }\n \n /**\n * Resolve a lookup reference to an ID, optionally creating the record if it doesn't exist\n * \n * @param entityName - Name of the entity to search in\n * @param fieldName - Field to match against\n * @param fieldValue - Value to search for\n * @param autoCreate - Whether to create the record if not found\n * @param createFields - Additional fields to set when creating\n * @returns The ID of the found or created record\n * @throws Error if lookup fails and autoCreate is false\n * \n * @example\n * ```typescript\n * // Simple lookup\n * const categoryId = await resolveLookup('Categories', 'Name', 'Technology');\n * \n * // Lookup with auto-create\n * const tagId = await resolveLookup('Tags', 'Name', 'New Tag', true, {\n * Description: 'Auto-created tag',\n * Status: 'Active'\n * });\n * ```\n */\n async resolveLookup(\n entityName: string, \n fieldName: string, \n fieldValue: string,\n autoCreate: boolean = false,\n createFields: Record<string, any> = {}\n ): Promise<string> {\n // Debug logging handled by caller if needed\n \n const rv = new RunView();\n const entityInfo = this.metadata.EntityByName(entityName);\n if (!entityInfo) {\n throw new Error(`Entity not found: ${entityName}`);\n }\n \n const field = entityInfo.Fields.find(f => f.Name.trim().toLowerCase() === fieldName.trim().toLowerCase());\n const quotes = field?.NeedsQuotes ? \"'\" : '';\n const result = await rv.RunView({\n EntityName: entityName,\n ExtraFilter: `${fieldName} = ${quotes}${fieldValue.replace(/'/g, \"''\")}${quotes}`,\n MaxRows: 1\n }, this.contextUser);\n \n if (result.Success && result.Results.length > 0) {\n if (entityInfo.PrimaryKeys.length > 0) {\n const pkeyField = entityInfo.PrimaryKeys[0].Name;\n const id = result.Results[0][pkeyField];\n return id;\n }\n }\n \n // If not found and auto-create is enabled, create the record\n if (autoCreate) {\n \n const newEntity = await this.metadata.GetEntityObject(entityName, this.contextUser);\n if (!newEntity) {\n throw new Error(`Failed to create entity object for: ${entityName}`);\n }\n \n newEntity.NewRecord();\n \n // Handle explicit ID setting for new records\n if (entityInfo.PrimaryKeys.length > 0) {\n for (const pk of entityInfo.PrimaryKeys) {\n if (!pk.AutoIncrement && pk.Type.toLowerCase() === 'uniqueidentifier') {\n // Generate UUID for this primary key and set it explicitly\n const uuid = uuidv4();\n (newEntity as any)[pk.Name] = uuid;\n }\n }\n }\n \n // Set the lookup field\n if (fieldName in newEntity) {\n (newEntity as any)[fieldName] = fieldValue;\n }\n \n // Set any additional fields provided\n for (const [key, value] of Object.entries(createFields)) {\n if (key in newEntity) {\n (newEntity as any)[key] = value;\n }\n }\n \n // Save the new record\n const saved = await newEntity.Save();\n if (!saved) {\n const message = newEntity.LatestResult?.Message;\n if (message) {\n throw new Error(`Failed to auto-create ${entityName}: ${message}`);\n }\n \n const errors = newEntity.LatestResult?.Errors?.map(err => \n typeof err === 'string' ? err : (err?.message || JSON.stringify(err))\n )?.join(', ') || 'Unknown error';\n throw new Error(`Failed to auto-create ${entityName}: ${errors}`);\n }\n \n // Return the new ID\n if (entityInfo.PrimaryKeys.length > 0) {\n const pkeyField = entityInfo.PrimaryKeys[0].Name;\n const newId = newEntity.Get(pkeyField);\n return newId;\n }\n }\n \n throw new Error(`Lookup failed: No record found in '${entityName}' where ${fieldName}='${fieldValue}'`);\n }\n \n /**\n * Build cascading defaults for a file path and process field values\n * \n * Walks up the directory tree from the file location, collecting defaults from\n * entity config and folder configs, with deeper folders overriding parent values.\n * All default values are processed for special references.\n * \n * @param filePath - Path to the file being processed\n * @param entityConfig - Entity configuration containing base defaults\n * @returns Processed defaults with all references resolved\n * @throws Error if any default value processing fails\n */\n async buildDefaults(filePath: string, entityConfig: EntityConfig): Promise<Record<string, any>> {\n const parts = path.dirname(filePath).split(path.sep);\n let defaults: Record<string, any> = { ...entityConfig.defaults };\n \n // Walk up the directory tree building defaults\n let currentPath = '';\n for (const part of parts) {\n currentPath = path.join(currentPath, part);\n const folderConfig = await this.loadFolderConfig(currentPath);\n \n if (folderConfig?.defaults) {\n defaults = { ...defaults, ...folderConfig.defaults };\n }\n }\n \n // Process all default values (lookups, file references, etc.)\n const processedDefaults: Record<string, any> = {};\n const baseDir = path.dirname(filePath);\n \n for (const [field, value] of Object.entries(defaults)) {\n try {\n processedDefaults[field] = await this.processFieldValue(value, baseDir, null, null);\n } catch (error) {\n throw new Error(`Failed to process default for field '${field}': ${error}`);\n }\n }\n \n return processedDefaults;\n }\n \n /**\n * Load folder configuration from .mj-folder.json file\n * \n * @param dir - Directory to check for configuration\n * @returns Folder configuration or null if not found/invalid\n * @private\n */\n private async loadFolderConfig(dir: string): Promise<FolderConfig | null> {\n const configPath = path.join(dir, '.mj-folder.json');\n \n if (await fs.pathExists(configPath)) {\n try {\n return await fs.readJson(configPath);\n } catch (error) {\n console.error(`Error loading folder config at ${configPath}:`, error);\n return null;\n }\n }\n \n return null;\n }\n \n /**\n * Calculate SHA256 checksum for data\n * \n * Generates a deterministic hash of the provided data by converting it to\n * formatted JSON and calculating a SHA256 digest. Used for change detection\n * in sync operations.\n * \n * @param data - Any data structure to calculate checksum for\n * @returns Hexadecimal string representation of the SHA256 hash\n * \n * @example\n * ```typescript\n * const checksum = syncEngine.calculateChecksum({\n * name: 'Test Record',\n * value: 42,\n * tags: ['a', 'b']\n * });\n * // Returns consistent hash for same data structure\n * ```\n */\n calculateChecksum(data: any): string {\n const hash = crypto.createHash('sha256');\n hash.update(JSON.stringify(data, null, 2));\n return hash.digest('hex');\n }\n \n /**\n * Get entity metadata information by name\n * \n * Retrieves the EntityInfo object containing schema metadata for the specified entity.\n * Returns null if the entity is not found in the metadata cache.\n * \n * @param entityName - Name of the entity to look up\n * @returns EntityInfo object with schema details or null if not found\n * \n * @example\n * ```typescript\n * const entityInfo = syncEngine.getEntityInfo('AI Prompts');\n * if (entityInfo) {\n * console.log(`Primary keys: ${entityInfo.PrimaryKeys.map(pk => pk.Name).join(', ')}`);\n * }\n * ```\n */\n getEntityInfo(entityName: string): EntityInfo | null {\n return this.metadata.EntityByName(entityName);\n }\n \n /**\n * Create a new entity object instance\n * \n * Uses the MemberJunction metadata system to properly instantiate an entity object.\n * This ensures correct class registration and respects any custom entity subclasses.\n * \n * @param entityName - Name of the entity to create\n * @returns Promise resolving to the new BaseEntity instance\n * @throws Error if entity creation fails\n * \n * @example\n * ```typescript\n * const entity = await syncEngine.createEntityObject('AI Prompts');\n * entity.NewRecord();\n * entity.Set('Name', 'My Prompt');\n * await entity.Save();\n * ```\n */\n async createEntityObject(entityName: string): Promise<BaseEntity> {\n const entity = await this.metadata.GetEntityObject(entityName, this.contextUser);\n if (!entity) {\n throw new Error(`Failed to create entity object for: ${entityName}`);\n }\n return entity;\n }\n \n /**\n * Load an entity record by primary key\n * \n * Retrieves an existing entity record from the database using its primary key values.\n * Supports both single and composite primary keys. Returns null if the record is not found.\n * \n * @param entityName - Name of the entity to load\n * @param primaryKey - Object containing primary key field names and values\n * @returns Promise resolving to the loaded entity or null if not found\n * @throws Error if entity metadata is not found\n * \n * @example\n * ```typescript\n * // Single primary key\n * const entity = await syncEngine.loadEntity('Users', { ID: '123-456' });\n * \n * // Composite primary key\n * const entity = await syncEngine.loadEntity('UserRoles', { \n * UserID: '123-456',\n * RoleID: '789-012'\n * });\n * ```\n */\n async loadEntity(entityName: string, primaryKey: Record<string, any>): Promise<BaseEntity | null> {\n const entity = await this.createEntityObject(entityName);\n const entityInfo = this.getEntityInfo(entityName);\n \n if (!entityInfo) {\n throw new Error(`Entity not found: ${entityName}`);\n }\n \n // Handle composite keys\n const compositeKey = new CompositeKey();\n compositeKey.LoadFromSimpleObject(primaryKey);\n const loaded = await entity.InnerLoad(compositeKey);\n \n return loaded ? entity : null;\n }\n \n /**\n * Process JSON object with template references\n * \n * Recursively processes JSON data structures to resolve `@template` references.\n * Templates can be defined at any level and support:\n * - Single template references: `\"@template:path/to/template.json\"`\n * - Object with @template field: `{ \"@template\": \"file.json\", \"override\": \"value\" }`\n * - Array of templates for merging: `{ \"@template\": [\"base.json\", \"overrides.json\"] }`\n * - Nested template references within templates\n * \n * @param data - JSON data structure to process\n * @param baseDir - Base directory for resolving relative template paths\n * @returns Promise resolving to the processed data with all templates resolved\n * @throws Error if template file is not found or contains invalid JSON\n * \n * @example\n * ```typescript\n * // Input data with template reference\n * const data = {\n * \"@template\": \"defaults/ai-prompt.json\",\n * \"Name\": \"Custom Prompt\",\n * \"Prompt\": \"Override the template prompt\"\n * };\n * \n * // Resolves template and merges with overrides\n * const result = await syncEngine.processTemplates(data, '/path/to/dir');\n * ```\n */\n async processTemplates(data: any, baseDir: string): Promise<any> {\n // Handle arrays\n if (Array.isArray(data)) {\n const processedArray = [];\n for (const item of data) {\n processedArray.push(await this.processTemplates(item, baseDir));\n }\n return processedArray;\n }\n \n // Handle objects\n if (data && typeof data === 'object') {\n // Check for @template reference\n if (typeof data === 'string' && data.startsWith('@template:')) {\n const templatePath = data.substring(10);\n return await this.loadAndProcessTemplate(templatePath, baseDir);\n }\n \n // Process object with possible @template field\n const processed: any = {};\n let templateData: any = {};\n \n // First, check if there's a @template field to process\n if (data['@template']) {\n const templates = Array.isArray(data['@template']) ? data['@template'] : [data['@template']];\n \n // Process templates in order, merging them\n for (const templateRef of templates) {\n const templateContent = await this.loadAndProcessTemplate(templateRef, baseDir);\n templateData = this.deepMerge(templateData, templateContent);\n }\n }\n \n // Process all other fields\n for (const [key, value] of Object.entries(data)) {\n if (key === '@template') continue; // Skip the template field itself\n \n // Process the value recursively\n processed[key] = await this.processTemplates(value, baseDir);\n }\n \n // Merge template data with processed data (processed data takes precedence)\n return this.deepMerge(templateData, processed);\n }\n \n // Return primitive values as-is\n return data;\n }\n \n /**\n * Load and process a template file\n * \n * Loads a JSON template file from the filesystem and recursively processes any\n * nested template references within it. Template paths are resolved relative to\n * the template file's directory, enabling template composition.\n * \n * @param templatePath - Path to the template file (relative or absolute)\n * @param baseDir - Base directory for resolving relative paths\n * @returns Promise resolving to the processed template content\n * @throws Error if template file not found or contains invalid JSON\n * @private\n */\n private async loadAndProcessTemplate(templatePath: string, baseDir: string): Promise<any> {\n const fullPath = path.resolve(baseDir, templatePath);\n \n if (!await fs.pathExists(fullPath)) {\n throw new Error(`Template file not found: ${fullPath}`);\n }\n \n try {\n const templateContent = await fs.readJson(fullPath);\n \n // Recursively process any nested templates\n const templateDir = path.dirname(fullPath);\n return await this.processTemplates(templateContent, templateDir);\n } catch (error) {\n throw new Error(`Failed to load template ${fullPath}: ${error}`);\n }\n }\n \n /**\n * Deep merge two objects with target taking precedence\n * \n * Recursively merges two objects, with values from the target object overriding\n * values from the source object. Arrays and primitive values are not merged but\n * replaced entirely by the target value. Undefined values in target are skipped.\n * \n * @param source - Base object to merge from\n * @param target - Object with values that override source\n * @returns New object with merged values\n * @private\n * \n * @example\n * ```typescript\n * const source = {\n * a: 1,\n * b: { x: 10, y: 20 },\n * c: [1, 2, 3]\n * };\n * const target = {\n * a: 2,\n * b: { y: 30, z: 40 },\n * d: 'new'\n * };\n * const result = deepMerge(source, target);\n * // Result: { a: 2, b: { x: 10, y: 30, z: 40 }, c: [1, 2, 3], d: 'new' }\n * ```\n */\n private deepMerge(source: any, target: any): any {\n if (!source) return target;\n if (!target) return source;\n \n // If target is not an object, it completely overrides source\n if (typeof target !== 'object' || target === null || Array.isArray(target)) {\n return target;\n }\n \n // If source is not an object, target wins\n if (typeof source !== 'object' || source === null || Array.isArray(source)) {\n return target;\n }\n \n // Both are objects, merge them\n const result: any = { ...source };\n \n for (const [key, value] of Object.entries(target)) {\n if (value === undefined) {\n continue; // Skip undefined values\n }\n \n if (typeof value === 'object' && value !== null && !Array.isArray(value) &&\n typeof result[key] === 'object' && result[key] !== null && !Array.isArray(result[key])) {\n // Both are objects, merge recursively\n result[key] = this.deepMerge(result[key], value);\n } else {\n // Otherwise, target value wins\n result[key] = value;\n }\n }\n \n return result;\n }\n \n}"]}
1
+ {"version":3,"file":"sync-engine.js","sourceRoot":"","sources":["../../src/lib/sync-engine.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;;;;AAEH,gDAAwB;AACxB,wDAA0B;AAC1B,oDAA4B;AAC5B,kDAA0B;AAC1B,+CAAyG;AACzG,mDAAgD;AAsBhD;;;;;;;;;;;;GAYG;AACH,MAAa,UAAU;IACb,QAAQ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C,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;YAC3E,OAAO,MAAM,CAAC;QAChB,CAAC;QAED,+BAA+B;QAC/B,MAAM,MAAM,GAAQ,EAAE,GAAG,MAAM,EAAE,CAAC;QAElC,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;YAClD,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACxB,SAAS,CAAC,wBAAwB;YACpC,CAAC;YAED,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;gBACpE,OAAO,MAAM,CAAC,GAAG,CAAC,KAAK,QAAQ,IAAI,MAAM,CAAC,GAAG,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;gBAC3F,sCAAsC;gBACtC,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC;YACnD,CAAC;iBAAM,CAAC;gBACN,+BAA+B;gBAC/B,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;YACtB,CAAC;QACH,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CAEF;AAloBD,gCAkoBC","sourcesContent":["/**\n * @fileoverview Core synchronization engine for MemberJunction metadata\n * @module sync-engine\n * \n * This module provides the core functionality for synchronizing metadata between\n * the MemberJunction database and local file system representations. It handles\n * special reference types (@file, @url, @lookup, @env, @parent, @root, @template),\n * manages entity operations, and provides utilities for data transformation.\n */\n\nimport path from 'path';\nimport fs from 'fs-extra';\nimport crypto from 'crypto';\nimport axios from 'axios';\nimport { EntityInfo, Metadata, RunView, BaseEntity, CompositeKey, UserInfo } from '@memberjunction/core';\nimport { uuidv4 } from '@memberjunction/global';\nimport { EntityConfig, FolderConfig } from '../config';\n\n/**\n * Represents the structure of a metadata record with optional sync tracking\n */\nexport interface RecordData {\n /** Primary key field(s) and their values */\n primaryKey?: Record<string, any>;\n /** Entity field names and their values */\n fields: Record<string, any>;\n /** Related entities organized by entity name */\n relatedEntities?: Record<string, RecordData[]>;\n /** Synchronization metadata for change tracking */\n sync?: {\n /** ISO timestamp of last modification */\n lastModified: string;\n /** SHA256 checksum of the fields object */\n checksum: string;\n };\n}\n\n/**\n * Core engine for synchronizing MemberJunction metadata between database and files\n * \n * @class SyncEngine\n * @example\n * ```typescript\n * const syncEngine = new SyncEngine(systemUser);\n * await syncEngine.initialize();\n * \n * // Process a field value with special references\n * const value = await syncEngine.processFieldValue('@lookup:Users.Email=admin@example.com', '/path/to/base');\n * ```\n */\nexport class SyncEngine {\n private metadata: Metadata;\n private contextUser: UserInfo;\n\n /**\n * Creates a new SyncEngine instance\n * @param contextUser - The user context for database operations\n */\n constructor(contextUser: UserInfo) {\n this.metadata = new Metadata();\n this.contextUser = contextUser;\n }\n \n /**\n * Initializes the sync engine by refreshing metadata cache\n * @returns Promise that resolves when initialization is complete\n */\n async initialize(): Promise<void> {\n // Initialize metadata\n await this.metadata.Refresh();\n }\n \n /**\n * Process special references in field values\n * \n * Handles the following reference types:\n * - `@parent:fieldName` - References a field from the parent record\n * - `@root:fieldName` - References a field from the root record\n * - `@file:path` - Reads content from an external file\n * - `@url:address` - Fetches content from a URL\n * - `@lookup:Entity.Field=Value` - Looks up an entity ID by field value\n * - `@env:VARIABLE` - Reads an environment variable\n * \n * @param value - The field value to process\n * @param baseDir - Base directory for resolving relative file paths\n * @param parentRecord - Optional parent entity for @parent references\n * @param rootRecord - Optional root entity for @root references\n * @returns The processed value with all references resolved\n * @throws Error if a reference cannot be resolved\n * \n * @example\n * ```typescript\n * // File reference\n * const content = await processFieldValue('@file:template.md', '/path/to/dir');\n * \n * // Lookup with auto-create\n * const userId = await processFieldValue('@lookup:Users.Email=john@example.com?create', '/path');\n * ```\n */\n async processFieldValue(value: any, baseDir: string, parentRecord?: BaseEntity | null, rootRecord?: BaseEntity | null): Promise<any> {\n if (typeof value !== 'string') {\n return value;\n }\n \n // Check for @parent: reference\n if (value.startsWith('@parent:')) {\n if (!parentRecord) {\n throw new Error(`@parent reference used but no parent record available: ${value}`);\n }\n const fieldName = value.substring(8);\n return parentRecord.Get(fieldName);\n }\n \n // Check for @root: reference\n if (value.startsWith('@root:')) {\n if (!rootRecord) {\n throw new Error(`@root reference used but no root record available: ${value}`);\n }\n const fieldName = value.substring(6);\n return rootRecord.Get(fieldName);\n }\n \n // Check for @file: reference\n if (value.startsWith('@file:')) {\n const filePath = value.substring(6);\n const fullPath = path.resolve(baseDir, filePath);\n \n if (await fs.pathExists(fullPath)) {\n return await fs.readFile(fullPath, 'utf-8');\n } else {\n throw new Error(`File not found: ${fullPath}`);\n }\n }\n \n // Check for @url: reference\n if (value.startsWith('@url:')) {\n const url = value.substring(5);\n \n try {\n const response = await axios.get(url);\n return response.data;\n } catch (error) {\n throw new Error(`Failed to fetch URL: ${url} - ${error}`);\n }\n }\n \n // Check for @lookup: reference\n if (value.startsWith('@lookup:')) {\n const lookupStr = value.substring(8);\n \n // Parse lookup with optional create syntax\n // Format: EntityName.FieldName=Value?create&OtherField=Value\n const entityMatch = lookupStr.match(/^([^.]+)\\./);\n if (!entityMatch) {\n throw new Error(`Invalid lookup format: ${value}`);\n }\n \n const entityName = entityMatch[1];\n const remaining = lookupStr.substring(entityName.length + 1);\n \n // Check if this has ?create syntax\n const hasCreate = remaining.includes('?create');\n const lookupPart = hasCreate ? remaining.split('?')[0] : remaining;\n \n // Parse the main lookup field\n const fieldMatch = lookupPart.match(/^(.+?)=(.+)$/);\n if (!fieldMatch) {\n throw new Error(`Invalid lookup format: ${value}`);\n }\n \n const [, fieldName, fieldValue] = fieldMatch;\n \n // Parse additional fields for creation if ?create is present\n let createFields: Record<string, any> = {};\n if (hasCreate && remaining.includes('?create&')) {\n const createPart = remaining.split('?create&')[1];\n const pairs = createPart.split('&');\n for (const pair of pairs) {\n const [key, val] = pair.split('=');\n if (key && val) {\n createFields[key] = decodeURIComponent(val);\n }\n }\n }\n \n return await this.resolveLookup(entityName, fieldName, fieldValue, hasCreate, createFields);\n }\n \n // Check for @env: reference\n if (value.startsWith('@env:')) {\n const envVar = value.substring(5);\n const envValue = process.env[envVar];\n \n if (envValue === undefined) {\n throw new Error(`Environment variable not found: ${envVar}`);\n }\n \n return envValue;\n }\n \n return value;\n }\n \n /**\n * Resolve a lookup reference to an ID, optionally creating the record if it doesn't exist\n * \n * @param entityName - Name of the entity to search in\n * @param fieldName - Field to match against\n * @param fieldValue - Value to search for\n * @param autoCreate - Whether to create the record if not found\n * @param createFields - Additional fields to set when creating\n * @returns The ID of the found or created record\n * @throws Error if lookup fails and autoCreate is false\n * \n * @example\n * ```typescript\n * // Simple lookup\n * const categoryId = await resolveLookup('Categories', 'Name', 'Technology');\n * \n * // Lookup with auto-create\n * const tagId = await resolveLookup('Tags', 'Name', 'New Tag', true, {\n * Description: 'Auto-created tag',\n * Status: 'Active'\n * });\n * ```\n */\n async resolveLookup(\n entityName: string, \n fieldName: string, \n fieldValue: string,\n autoCreate: boolean = false,\n createFields: Record<string, any> = {}\n ): Promise<string> {\n // Debug logging handled by caller if needed\n \n const rv = new RunView();\n const entityInfo = this.metadata.EntityByName(entityName);\n if (!entityInfo) {\n throw new Error(`Entity not found: ${entityName}`);\n }\n \n const field = entityInfo.Fields.find(f => f.Name.trim().toLowerCase() === fieldName.trim().toLowerCase());\n const quotes = field?.NeedsQuotes ? \"'\" : '';\n const result = await rv.RunView({\n EntityName: entityName,\n ExtraFilter: `${fieldName} = ${quotes}${fieldValue.replace(/'/g, \"''\")}${quotes}`,\n MaxRows: 1\n }, this.contextUser);\n \n if (result.Success && result.Results.length > 0) {\n if (entityInfo.PrimaryKeys.length > 0) {\n const pkeyField = entityInfo.PrimaryKeys[0].Name;\n const id = result.Results[0][pkeyField];\n return id;\n }\n }\n \n // If not found and auto-create is enabled, create the record\n if (autoCreate) {\n \n const newEntity = await this.metadata.GetEntityObject(entityName, this.contextUser);\n if (!newEntity) {\n throw new Error(`Failed to create entity object for: ${entityName}`);\n }\n \n newEntity.NewRecord();\n \n // Handle explicit ID setting for new records\n if (entityInfo.PrimaryKeys.length > 0) {\n for (const pk of entityInfo.PrimaryKeys) {\n if (!pk.AutoIncrement && pk.Type.toLowerCase() === 'uniqueidentifier') {\n // Generate UUID for this primary key and set it explicitly\n const uuid = uuidv4();\n (newEntity as any)[pk.Name] = uuid;\n }\n }\n }\n \n // Set the lookup field\n if (fieldName in newEntity) {\n (newEntity as any)[fieldName] = fieldValue;\n }\n \n // Set any additional fields provided\n for (const [key, value] of Object.entries(createFields)) {\n if (key in newEntity) {\n (newEntity as any)[key] = value;\n }\n }\n \n // Save the new record\n const saved = await newEntity.Save();\n if (!saved) {\n const message = newEntity.LatestResult?.Message;\n if (message) {\n throw new Error(`Failed to auto-create ${entityName}: ${message}`);\n }\n \n const errors = newEntity.LatestResult?.Errors?.map(err => \n typeof err === 'string' ? err : (err?.message || JSON.stringify(err))\n )?.join(', ') || 'Unknown error';\n throw new Error(`Failed to auto-create ${entityName}: ${errors}`);\n }\n \n // Return the new ID\n if (entityInfo.PrimaryKeys.length > 0) {\n const pkeyField = entityInfo.PrimaryKeys[0].Name;\n const newId = newEntity.Get(pkeyField);\n return newId;\n }\n }\n \n throw new Error(`Lookup failed: No record found in '${entityName}' where ${fieldName}='${fieldValue}'`);\n }\n \n /**\n * Build cascading defaults for a file path and process field values\n * \n * Walks up the directory tree from the file location, collecting defaults from\n * entity config and folder configs, with deeper folders overriding parent values.\n * All default values are processed for special references.\n * \n * @param filePath - Path to the file being processed\n * @param entityConfig - Entity configuration containing base defaults\n * @returns Processed defaults with all references resolved\n * @throws Error if any default value processing fails\n */\n async buildDefaults(filePath: string, entityConfig: EntityConfig): Promise<Record<string, any>> {\n const parts = path.dirname(filePath).split(path.sep);\n let defaults: Record<string, any> = { ...entityConfig.defaults };\n \n // Walk up the directory tree building defaults\n let currentPath = '';\n for (const part of parts) {\n currentPath = path.join(currentPath, part);\n const folderConfig = await this.loadFolderConfig(currentPath);\n \n if (folderConfig?.defaults) {\n defaults = { ...defaults, ...folderConfig.defaults };\n }\n }\n \n // Process all default values (lookups, file references, etc.)\n const processedDefaults: Record<string, any> = {};\n const baseDir = path.dirname(filePath);\n \n for (const [field, value] of Object.entries(defaults)) {\n try {\n processedDefaults[field] = await this.processFieldValue(value, baseDir, null, null);\n } catch (error) {\n throw new Error(`Failed to process default for field '${field}': ${error}`);\n }\n }\n \n return processedDefaults;\n }\n \n /**\n * Load folder configuration from .mj-folder.json file\n * \n * @param dir - Directory to check for configuration\n * @returns Folder configuration or null if not found/invalid\n * @private\n */\n private async loadFolderConfig(dir: string): Promise<FolderConfig | null> {\n const configPath = path.join(dir, '.mj-folder.json');\n \n if (await fs.pathExists(configPath)) {\n try {\n return await fs.readJson(configPath);\n } catch (error) {\n console.error(`Error loading folder config at ${configPath}:`, error);\n return null;\n }\n }\n \n return null;\n }\n \n /**\n * Calculate SHA256 checksum for data\n * \n * Generates a deterministic hash of the provided data by converting it to\n * formatted JSON and calculating a SHA256 digest. Used for change detection\n * in sync operations.\n * \n * @param data - Any data structure to calculate checksum for\n * @returns Hexadecimal string representation of the SHA256 hash\n * \n * @example\n * ```typescript\n * const checksum = syncEngine.calculateChecksum({\n * name: 'Test Record',\n * value: 42,\n * tags: ['a', 'b']\n * });\n * // Returns consistent hash for same data structure\n * ```\n */\n calculateChecksum(data: any): string {\n const hash = crypto.createHash('sha256');\n hash.update(JSON.stringify(data, null, 2));\n return hash.digest('hex');\n }\n \n /**\n * Get entity metadata information by name\n * \n * Retrieves the EntityInfo object containing schema metadata for the specified entity.\n * Returns null if the entity is not found in the metadata cache.\n * \n * @param entityName - Name of the entity to look up\n * @returns EntityInfo object with schema details or null if not found\n * \n * @example\n * ```typescript\n * const entityInfo = syncEngine.getEntityInfo('AI Prompts');\n * if (entityInfo) {\n * console.log(`Primary keys: ${entityInfo.PrimaryKeys.map(pk => pk.Name).join(', ')}`);\n * }\n * ```\n */\n getEntityInfo(entityName: string): EntityInfo | null {\n return this.metadata.EntityByName(entityName);\n }\n \n /**\n * Create a new entity object instance\n * \n * Uses the MemberJunction metadata system to properly instantiate an entity object.\n * This ensures correct class registration and respects any custom entity subclasses.\n * \n * @param entityName - Name of the entity to create\n * @returns Promise resolving to the new BaseEntity instance\n * @throws Error if entity creation fails\n * \n * @example\n * ```typescript\n * const entity = await syncEngine.createEntityObject('AI Prompts');\n * entity.NewRecord();\n * entity.Set('Name', 'My Prompt');\n * await entity.Save();\n * ```\n */\n async createEntityObject(entityName: string): Promise<BaseEntity> {\n const entity = await this.metadata.GetEntityObject(entityName, this.contextUser);\n if (!entity) {\n throw new Error(`Failed to create entity object for: ${entityName}`);\n }\n return entity;\n }\n \n /**\n * Load an entity record by primary key\n * \n * Retrieves an existing entity record from the database using its primary key values.\n * Supports both single and composite primary keys. Returns null if the record is not found.\n * \n * @param entityName - Name of the entity to load\n * @param primaryKey - Object containing primary key field names and values\n * @returns Promise resolving to the loaded entity or null if not found\n * @throws Error if entity metadata is not found\n * \n * @example\n * ```typescript\n * // Single primary key\n * const entity = await syncEngine.loadEntity('Users', { ID: '123-456' });\n * \n * // Composite primary key\n * const entity = await syncEngine.loadEntity('UserRoles', { \n * UserID: '123-456',\n * RoleID: '789-012'\n * });\n * ```\n */\n async loadEntity(entityName: string, primaryKey: Record<string, any>): Promise<BaseEntity | null> {\n const entityInfo = this.getEntityInfo(entityName);\n \n if (!entityInfo) {\n throw new Error(`Entity not found: ${entityName}`);\n }\n \n // First, check if the record exists using RunView to avoid \"Error in BaseEntity.Load\" messages\n // when records don't exist (which is a normal scenario during sync operations)\n const rv = new RunView();\n \n // Build filter for primary key(s)\n const filters: string[] = [];\n for (const pk of entityInfo.PrimaryKeys) {\n const value = primaryKey[pk.Name];\n if (value === undefined || value === null) {\n throw new Error(`Missing primary key value for ${pk.Name} in entity ${entityName}`);\n }\n \n // Check if field needs quotes\n const field = entityInfo.Fields.find(f => f.Name === pk.Name);\n const quotes = field?.NeedsQuotes ? \"'\" : '';\n const escapedValue = field?.NeedsQuotes && typeof value === 'string' ? value.replace(/'/g, \"''\") : value;\n filters.push(`${pk.Name} = ${quotes}${escapedValue}${quotes}`);\n }\n \n const result = await rv.RunView({\n EntityName: entityName,\n ExtraFilter: filters.join(' AND '),\n MaxRows: 1\n }, this.contextUser);\n \n // If no record found, return null without attempting to load\n if (!result.Success || result.Results.length === 0) {\n return null;\n }\n \n // Record exists, now load it properly through the entity\n const entity = await this.createEntityObject(entityName);\n const compositeKey = new CompositeKey();\n compositeKey.LoadFromSimpleObject(primaryKey);\n const loaded = await entity.InnerLoad(compositeKey);\n \n return loaded ? entity : null;\n }\n \n /**\n * Process JSON object with template references\n * \n * Recursively processes JSON data structures to resolve `@template` references.\n * Templates can be defined at any level and support:\n * - Single template references: `\"@template:path/to/template.json\"`\n * - Object with @template field: `{ \"@template\": \"file.json\", \"override\": \"value\" }`\n * - Array of templates for merging: `{ \"@template\": [\"base.json\", \"overrides.json\"] }`\n * - Nested template references within templates\n * \n * @param data - JSON data structure to process\n * @param baseDir - Base directory for resolving relative template paths\n * @returns Promise resolving to the processed data with all templates resolved\n * @throws Error if template file is not found or contains invalid JSON\n * \n * @example\n * ```typescript\n * // Input data with template reference\n * const data = {\n * \"@template\": \"defaults/ai-prompt.json\",\n * \"Name\": \"Custom Prompt\",\n * \"Prompt\": \"Override the template prompt\"\n * };\n * \n * // Resolves template and merges with overrides\n * const result = await syncEngine.processTemplates(data, '/path/to/dir');\n * ```\n */\n async processTemplates(data: any, baseDir: string): Promise<any> {\n // Handle arrays\n if (Array.isArray(data)) {\n const processedArray = [];\n for (const item of data) {\n processedArray.push(await this.processTemplates(item, baseDir));\n }\n return processedArray;\n }\n \n // Handle objects\n if (data && typeof data === 'object') {\n // Check for @template reference\n if (typeof data === 'string' && data.startsWith('@template:')) {\n const templatePath = data.substring(10);\n return await this.loadAndProcessTemplate(templatePath, baseDir);\n }\n \n // Process object with possible @template field\n const processed: any = {};\n let templateData: any = {};\n \n // First, check if there's a @template field to process\n if (data['@template']) {\n const templates = Array.isArray(data['@template']) ? data['@template'] : [data['@template']];\n \n // Process templates in order, merging them\n for (const templateRef of templates) {\n const templateContent = await this.loadAndProcessTemplate(templateRef, baseDir);\n templateData = this.deepMerge(templateData, templateContent);\n }\n }\n \n // Process all other fields\n for (const [key, value] of Object.entries(data)) {\n if (key === '@template') continue; // Skip the template field itself\n \n // Process the value recursively\n processed[key] = await this.processTemplates(value, baseDir);\n }\n \n // Merge template data with processed data (processed data takes precedence)\n return this.deepMerge(templateData, processed);\n }\n \n // Return primitive values as-is\n return data;\n }\n \n /**\n * Load and process a template file\n * \n * Loads a JSON template file from the filesystem and recursively processes any\n * nested template references within it. Template paths are resolved relative to\n * the template file's directory, enabling template composition.\n * \n * @param templatePath - Path to the template file (relative or absolute)\n * @param baseDir - Base directory for resolving relative paths\n * @returns Promise resolving to the processed template content\n * @throws Error if template file not found or contains invalid JSON\n * @private\n */\n private async loadAndProcessTemplate(templatePath: string, baseDir: string): Promise<any> {\n const fullPath = path.resolve(baseDir, templatePath);\n \n if (!await fs.pathExists(fullPath)) {\n throw new Error(`Template file not found: ${fullPath}`);\n }\n \n try {\n const templateContent = await fs.readJson(fullPath);\n \n // Recursively process any nested templates\n const templateDir = path.dirname(fullPath);\n return await this.processTemplates(templateContent, templateDir);\n } catch (error) {\n throw new Error(`Failed to load template ${fullPath}: ${error}`);\n }\n }\n \n /**\n * Deep merge two objects with target taking precedence\n * \n * Recursively merges two objects, with values from the target object overriding\n * values from the source object. Arrays and primitive values are not merged but\n * replaced entirely by the target value. Undefined values in target are skipped.\n * \n * @param source - Base object to merge from\n * @param target - Object with values that override source\n * @returns New object with merged values\n * @private\n * \n * @example\n * ```typescript\n * const source = {\n * a: 1,\n * b: { x: 10, y: 20 },\n * c: [1, 2, 3]\n * };\n * const target = {\n * a: 2,\n * b: { y: 30, z: 40 },\n * d: 'new'\n * };\n * const result = deepMerge(source, target);\n * // Result: { a: 2, b: { x: 10, y: 30, z: 40 }, c: [1, 2, 3], d: 'new' }\n * ```\n */\n private deepMerge(source: any, target: any): any {\n if (!source) return target;\n if (!target) return source;\n \n // If target is not an object, it completely overrides source\n if (typeof target !== 'object' || target === null || Array.isArray(target)) {\n return target;\n }\n \n // If source is not an object, target wins\n if (typeof source !== 'object' || source === null || Array.isArray(source)) {\n return target;\n }\n \n // Both are objects, merge them\n const result: any = { ...source };\n \n for (const [key, value] of Object.entries(target)) {\n if (value === undefined) {\n continue; // Skip undefined values\n }\n \n if (typeof value === 'object' && value !== null && !Array.isArray(value) &&\n typeof result[key] === 'object' && result[key] !== null && !Array.isArray(result[key])) {\n // Both are objects, merge recursively\n result[key] = this.deepMerge(result[key], value);\n } else {\n // Otherwise, target value wins\n result[key] = value;\n }\n }\n \n return result;\n }\n \n}"]}
@@ -0,0 +1,44 @@
1
+ import { ValidationResult } from '../types/validation';
2
+ export declare class FormattingService {
3
+ /**
4
+ * Format validation result as JSON
5
+ */
6
+ formatValidationResultAsJson(result: ValidationResult): string;
7
+ private readonly symbols;
8
+ /**
9
+ * Format validation result for terminal output
10
+ */
11
+ formatValidationResult(result: ValidationResult, verbose?: boolean): string;
12
+ /**
13
+ * Format push/pull summary report
14
+ */
15
+ formatSyncSummary(operation: 'push' | 'pull', stats: {
16
+ created: number;
17
+ updated: number;
18
+ deleted: number;
19
+ skipped: number;
20
+ errors: number;
21
+ duration: number;
22
+ }): string;
23
+ private formatHeader;
24
+ private formatSectionHeader;
25
+ private formatSummaryBox;
26
+ private formatFileResult;
27
+ private formatError;
28
+ private formatWarning;
29
+ private formatFooter;
30
+ private shortenPath;
31
+ private formatDuration;
32
+ /**
33
+ * Get count of errors by type
34
+ */
35
+ private getErrorsByType;
36
+ /**
37
+ * Get count of warnings by type
38
+ */
39
+ private getWarningsByType;
40
+ /**
41
+ * Format validation result as markdown
42
+ */
43
+ formatValidationResultAsMarkdown(result: ValidationResult): string;
44
+ }