Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express_boilerplate_cli",
"version": "1.0.5",
"version": "1.0.6",
"description": "A powerful tool designed to simplify the process of creating API endpoints in Node.js applications.",
"main": "script.js",
"type": "module",
Expand Down
58 changes: 39 additions & 19 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@ import inquirer from "inquirer";
import knex from "knex";
import path from "path";
const moduleDir = process.cwd();
let dbConfig;

async function importIfFileExists(filePath) {
try {
await fs.access(filePath); // Check if the file exists
const importedModule = await import(filePath); // Dynamically import the module
return importedModule;
} catch (error) {
console.error(`The file at ${filePath} does not exist.`);
return null; // Return null or handle the case when the file doesn't exist
}
}
const connectionPath = "./src/db/connection.js";

async function promptUser() {
const tableData = await inquirer.prompt([
Expand Down Expand Up @@ -146,23 +157,29 @@ async function dbConnectionPrompt() {
}

const executeMigration = async (query) => {
const connection = knex({
client: "mysql2",
connection: {
host: dbConfig.host,
user: dbConfig.username,
password: dbConfig.password,
database: dbConfig.dbName,
},
pool: {
min: 2,
max: 10,
},
});

await connection.raw(query);

connection.destroy();
try {
const importedModule = await importIfFileExists(connectionPath);
if (importedModule) {
const { cred } = importedModule;
const connection = knex({
client: "mysql2",
connection: {
host: cred.host,
user: cred.user,
password: cred.password,
database: cred.database,
},
pool: {
min: 2,
max: 10,
},
});

await connection.raw(query);
}
} catch (err) {
throw err;
}
};
async function generateCreateTableStatement(tableConfig) {
const columns = tableConfig.columns
Expand Down Expand Up @@ -305,7 +322,10 @@ export default ${tableConfig.tableName}Controller
function generateConnectionJs(payload) {
return `
import knex from "knex";

export const cred = { host: '${payload.host}',
user: '${payload.username}',
password: '${payload.password}',
database: '${payload.dbName}',}
const connection = knex({
client: 'mysql2',
connection: {
Expand Down