Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
corps-directory
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
fhurtado14
corps-directory
Commits
eb1ced18
Commit
eb1ced18
authored
6 months ago
by
Federico Hurtado
Browse files
Options
Downloads
Patches
Plain Diff
Create db config file with startup function.
parent
358cd2e6
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
backend/config/database/database.config.js
+45
-0
45 additions, 0 deletions
backend/config/database/database.config.js
backend/config/database/schema.sql
+9
-0
9 additions, 0 deletions
backend/config/database/schema.sql
backend/index.js
+1
-29
1 addition, 29 deletions
backend/index.js
with
55 additions
and
29 deletions
backend/config/database/database.config.js
0 → 100644
+
45
−
0
View file @
eb1ced18
// /config/db.js
const
mysql
=
require
(
"
mysql2
"
);
const
fs
=
require
(
"
fs
"
);
const
path
=
require
(
"
path
"
);
require
(
"
dotenv
"
).
config
();
const
pool
=
mysql
.
createPool
({
host
:
process
.
env
.
DB_HOST
,
user
:
process
.
env
.
DB_USER
||
"
corps_directory_dev
"
,
database
:
process
.
env
.
DB_NAME
||
"
corps_directory_db
"
,
password
:
process
.
env
.
DB_PASSWORD
||
"
corps_db_password
"
,
waitForConnections
:
true
,
connectionLimit
:
10
,
queueLimit
:
0
,
});
console
.
log
(
"
Host:
"
,
process
.
env
.
DB_HOST
);
// Function to execute the SQL file
const
executeSQLFile
=
(
filePath
)
=>
{
const
sql
=
fs
.
readFileSync
(
filePath
,
"
utf8
"
);
pool
.
getConnection
((
err
,
connection
)
=>
{
if
(
err
)
{
console
.
error
(
"
Error connecting to MySQL:
"
,
err
.
message
);
return
;
}
// Execute the SQL from the file
connection
.
query
(
sql
,
(
err
,
result
)
=>
{
if
(
err
)
{
console
.
error
(
"
Error executing SQL file:
"
,
err
.
message
);
connection
.
release
();
return
;
}
console
.
log
(
"
SQL file executed successfully.
"
);
connection
.
release
();
});
});
};
// Execute the SQL file during startup
const
schemaFilePath
=
path
.
join
(
__dirname
,
"
./schema.sql
"
);
executeSQLFile
(
schemaFilePath
);
module
.
exports
=
pool
;
This diff is collapsed.
Click to expand it.
backend/config/database/schema.sql
0 → 100644
+
9
−
0
View file @
eb1ced18
-- schema.sql
-- Create the 'people' table if it doesn't exist
CREATE
TABLE
IF
NOT
EXISTS
people
(
id
INT
AUTO_INCREMENT
PRIMARY
KEY
,
first_name
VARCHAR
(
255
)
NOT
NULL
,
last_name
VARCHAR
(
255
)
NOT
NULL
);
This diff is collapsed.
Click to expand it.
backend/index.js
+
1
−
29
View file @
eb1ced18
...
...
@@ -5,16 +5,7 @@ require("dotenv").config();
const
app
=
express
();
app
.
use
(
express
.
json
());
// mysql connection
const
pool
=
mysql
.
createPool
({
host
:
process
.
env
.
DB_HOST
,
user
:
process
.
env
.
DB_USER
||
"
corps_directory_dev
"
,
database
:
process
.
env
.
DB_NAME
||
"
corps_directory_db
"
,
password
:
process
.
env
.
DB_PASSWORD
||
"
corps_db_password
"
,
waitForConnections
:
true
,
connectionLimit
:
10
,
// Max number of connections in the pool
queueLimit
:
0
,
// Unlimited queue
});
const
pool
=
require
(
"
./config/database/database.config
"
);
// This will now run the schema.sql file on startup
// Check connection and create the 'people' table on initialization
pool
.
getConnection
((
err
,
connection
)
=>
{
...
...
@@ -23,25 +14,6 @@ pool.getConnection((err, connection) => {
return
;
}
console
.
log
(
"
Connected to MySQL
"
);
const
createTable
=
`
CREATE TABLE IF NOT EXISTS people (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL
)
`
;
connection
.
query
(
createTable
,
(
err
,
result
)
=>
{
if
(
err
)
{
console
.
error
(
"
Error creating table:
"
,
err
.
message
);
connection
.
release
();
// Release the connection back to the pool
return
;
}
console
.
log
(
"
Table 'people' exists or was created.
"
);
connection
.
release
();
// Release the connection back to the pool
});
});
// POST endpoint to add a new person
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment