@nmakarov/cli-toolkit 0.11.2 → 0.11.4

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.
package/dist/index.js CHANGED
@@ -2506,14 +2506,26 @@ var FileDatabase = class {
2506
2506
  figureOutDataAndFileToWrite(data) {
2507
2507
  let dataToWrite;
2508
2508
  let dataLeftOver;
2509
- if (!this.metadata.dataType) {
2510
- this.metadata.dataType = detectDataType(data);
2509
+ const incomingDataType = detectDataType(data);
2510
+ if (this.metadata.dataType !== incomingDataType) {
2511
+ this.metadata.dataType = incomingDataType;
2511
2512
  }
2512
2513
  if (this.metadata.files.length === 0) {
2513
2514
  this.makeNewFile();
2514
2515
  }
2515
2516
  const lastFile = this.metadata.files[this.metadata.files.length - 1];
2516
2517
  const lastFileRecordsCount = lastFile.recordsCount;
2518
+ if (!Array.isArray(data)) {
2519
+ const lastFileExtension = path3.extname(lastFile.fileName);
2520
+ const expectedExtension = `.${getFileExtension(incomingDataType)}`;
2521
+ if (lastFileExtension !== expectedExtension) {
2522
+ if (lastFileRecordsCount > 0) {
2523
+ this.makeNewFile();
2524
+ } else {
2525
+ lastFile.fileName = `${this.currentFileNumber.toString().padStart(6, "0")}.${getFileExtension(incomingDataType)}`;
2526
+ }
2527
+ }
2528
+ }
2517
2529
  if (Array.isArray(data)) {
2518
2530
  if (lastFileRecordsCount < this.pageSize) {
2519
2531
  dataToWrite = [...this.lastFileData || [], ...data.slice(0, this.pageSize - lastFileRecordsCount)];
@@ -2696,10 +2708,13 @@ var FileDatabase = class {
2696
2708
  throw new FileDatabaseError("Cannot use forceNewVersion in non-versioned mode");
2697
2709
  }
2698
2710
  await this.prepare({ write: true });
2711
+ const incomingDataType = detectDataType(data);
2712
+ this.metadata.dataType = incomingDataType;
2699
2713
  if (options.forceNewVersion) {
2700
2714
  await this.makeNewVersion();
2701
2715
  this.metadata = this.getDefaultMetadata();
2702
2716
  this.metadata.version = this.currentVersion;
2717
+ this.metadata.dataType = incomingDataType;
2703
2718
  this.makeNewFile();
2704
2719
  }
2705
2720
  let { dataToWrite, dataLeftOver, fileName } = this.figureOutDataAndFileToWrite(data);
@@ -2839,6 +2854,10 @@ var Db = class {
2839
2854
  logger;
2840
2855
  queriesLog = [];
2841
2856
  isConnected = false;
2857
+ /**
2858
+ * Constructor - accepts config object
2859
+ * Use dbInit() function to initialize with Context
2860
+ */
2842
2861
  constructor(config2) {
2843
2862
  if (!config2.connectionString) {
2844
2863
  throw new ParamError("Db: connectionString is required");
@@ -3125,6 +3144,91 @@ var Db = class {
3125
3144
  return this.isConnected && this.knexInstance !== null;
3126
3145
  }
3127
3146
  };
3147
+ function capitalizeFirstLetter(str) {
3148
+ return str.charAt(0).toUpperCase() + str.slice(1);
3149
+ }
3150
+ async function dbConnect(context, connectionString, name, dbProfile) {
3151
+ const defs = {
3152
+ testDbConnection: "boolean default true",
3153
+ name: "string",
3154
+ poolMin: "number default 2",
3155
+ poolMax: "number default 10",
3156
+ acquireConnectionTimeout: "number default 10000",
3157
+ sslRejectUnauthorized: "boolean default false"
3158
+ };
3159
+ const paramsConfig = context.params.getAll(defs);
3160
+ const config2 = {
3161
+ connectionString,
3162
+ name: paramsConfig.name || name || "default",
3163
+ testConnection: paramsConfig.testDbConnection,
3164
+ profile: dbProfile ?? false,
3165
+ pool: {
3166
+ min: paramsConfig.poolMin,
3167
+ max: paramsConfig.poolMax
3168
+ },
3169
+ acquireConnectionTimeout: paramsConfig.acquireConnectionTimeout,
3170
+ ssl: {
3171
+ rejectUnauthorized: paramsConfig.sslRejectUnauthorized
3172
+ },
3173
+ logger: context.logger
3174
+ };
3175
+ try {
3176
+ const db = new Db(config2);
3177
+ context.registerCleanup(async () => {
3178
+ await db.disconnect();
3179
+ context.logger.debug(`[Db] instance "${name || connectionString}" destroyed`);
3180
+ });
3181
+ await db.connect();
3182
+ context.logger.debug(`[Db] instance "${name || connectionString}" initialized`);
3183
+ return db;
3184
+ } catch (error) {
3185
+ if (error instanceof ParamError) {
3186
+ throw error;
3187
+ }
3188
+ const errorMsg = error instanceof Error ? error.message : String(error);
3189
+ throw new ParamError(`[Db] connect error: ${errorMsg}`);
3190
+ }
3191
+ }
3192
+ async function dbFindAndConnect(context, dbNameOrConnectionString) {
3193
+ let dbName;
3194
+ let dbConnectionString;
3195
+ let dbProfile;
3196
+ if (dbNameOrConnectionString) {
3197
+ if (dbNameOrConnectionString.match(/^(postgresql|mysql):\/\/[^\s]+:[^\s]+@[^\s]+:\d+\/[^\s]+$/)) {
3198
+ dbName = void 0;
3199
+ dbConnectionString = dbNameOrConnectionString;
3200
+ } else {
3201
+ dbName = dbNameOrConnectionString;
3202
+ }
3203
+ } else {
3204
+ const defs = {
3205
+ dbName: "string",
3206
+ dbConnectionString: "string",
3207
+ dbProfile: "boolean default false"
3208
+ };
3209
+ const paramsConfig = context.params.getAll(defs);
3210
+ dbName = paramsConfig.dbName;
3211
+ dbConnectionString = paramsConfig.dbConnectionString;
3212
+ dbProfile = paramsConfig.dbProfile;
3213
+ }
3214
+ if (!dbName && !dbConnectionString) {
3215
+ throw new ParamError("Db: either dbName or dbConnectionString must be specified");
3216
+ }
3217
+ if (dbName) {
3218
+ const paramName = `dbConnectionString${capitalizeFirstLetter(dbName)}`;
3219
+ dbConnectionString = await context.params.get(paramName, "string");
3220
+ if (!dbConnectionString) {
3221
+ throw new ParamError(
3222
+ `Db: cannot find dbConnectionString for dbName="${dbName}" (looked for param "${paramName}")`
3223
+ );
3224
+ }
3225
+ }
3226
+ const db = await dbConnect(context, dbConnectionString, dbName, dbProfile);
3227
+ return db;
3228
+ }
3229
+ async function dbInit(context, dbNameOrConnectionString) {
3230
+ return await dbFindAndConnect(context, dbNameOrConnectionString);
3231
+ }
3128
3232
 
3129
3233
  // src/logger/index.ts
3130
3234
  import chalk from "chalk";
@@ -3519,6 +3623,9 @@ export {
3519
3623
  buildBreadcrumb,
3520
3624
  buildDetailBreadcrumb,
3521
3625
  buildFooter,
3626
+ dbConnect,
3627
+ dbFindAndConnect,
3628
+ dbInit,
3522
3629
  defaultFileSynopsisFunction,
3523
3630
  defaultVersionSynopsisFunction,
3524
3631
  fileDatabaseInit,