@nmakarov/cli-toolkit 0.11.3 → 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/db.cjs +97 -2
- package/dist/db.cjs.map +1 -1
- package/dist/db.js +93 -1
- package/dist/db.js.map +1 -1
- package/dist/index.cjs +95 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +92 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2854,6 +2854,10 @@ var Db = class {
|
|
|
2854
2854
|
logger;
|
|
2855
2855
|
queriesLog = [];
|
|
2856
2856
|
isConnected = false;
|
|
2857
|
+
/**
|
|
2858
|
+
* Constructor - accepts config object
|
|
2859
|
+
* Use dbInit() function to initialize with Context
|
|
2860
|
+
*/
|
|
2857
2861
|
constructor(config2) {
|
|
2858
2862
|
if (!config2.connectionString) {
|
|
2859
2863
|
throw new ParamError("Db: connectionString is required");
|
|
@@ -3140,6 +3144,91 @@ var Db = class {
|
|
|
3140
3144
|
return this.isConnected && this.knexInstance !== null;
|
|
3141
3145
|
}
|
|
3142
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
|
+
}
|
|
3143
3232
|
|
|
3144
3233
|
// src/logger/index.ts
|
|
3145
3234
|
import chalk from "chalk";
|
|
@@ -3534,6 +3623,9 @@ export {
|
|
|
3534
3623
|
buildBreadcrumb,
|
|
3535
3624
|
buildDetailBreadcrumb,
|
|
3536
3625
|
buildFooter,
|
|
3626
|
+
dbConnect,
|
|
3627
|
+
dbFindAndConnect,
|
|
3628
|
+
dbInit,
|
|
3537
3629
|
defaultFileSynopsisFunction,
|
|
3538
3630
|
defaultVersionSynopsisFunction,
|
|
3539
3631
|
fileDatabaseInit,
|