Skip to content

Commit 40ea735

Browse files
committed
Configuración culminada de las conexiones a MySQL y MongoDB 12/09/1998
0 parents  commit 40ea735

23 files changed

+836
-0
lines changed

backend/.eslintrc.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"es2021": true,
5+
"node": true
6+
},
7+
"extends": [
8+
"eslint:recommended",
9+
"plugin:@typescript-eslint/recommended"
10+
],
11+
"overrides": [
12+
],
13+
"parser": "@typescript-eslint/parser",
14+
"parserOptions": {
15+
"ecmaVersion": "latest",
16+
"sourceType": "module"
17+
},
18+
"plugins": [
19+
"@typescript-eslint"
20+
],
21+
"rules": {
22+
"indent": [
23+
"error",
24+
3
25+
],
26+
"linebreak-style": [
27+
"error",
28+
"windows"
29+
],
30+
"quotes": [
31+
"error",
32+
"single"
33+
],
34+
"semi": [
35+
"error",
36+
"always"
37+
]
38+
}
39+
}

backend/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.env
3+
package-lock.json

backend/package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "backend",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"scripts": {
6+
"dev": "ts-node-dev src/index.ts"
7+
},
8+
"keywords": [],
9+
"author": "",
10+
"license": "ISC",
11+
"description": "",
12+
"devDependencies": {
13+
"@types/cors": "^2.8.12",
14+
"@types/express": "^4.17.13",
15+
"@types/morgan": "^1.9.3",
16+
"@types/mysql": "^2.15.21",
17+
"@typescript-eslint/eslint-plugin": "^5.36.2",
18+
"@typescript-eslint/parser": "^5.36.2",
19+
"eslint": "^8.23.0",
20+
"ts-node-dev": "^2.0.0",
21+
"typescript": "^4.8.3"
22+
},
23+
"dependencies": {
24+
"@typegoose/typegoose": "^9.11.2",
25+
"cors": "^2.8.5",
26+
"dotenv": "^16.0.2",
27+
"express": "^4.18.1",
28+
"express-promise-router": "^4.1.1",
29+
"mongoose": "^6.5.5",
30+
"morgan": "^1.10.0",
31+
"mysql": "^2.18.1",
32+
"typeorm": "^0.3.9"
33+
}
34+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Request, Response } from 'express';
2+
3+
export const getWelcome = async (req: Request, res: Response) => {
4+
res.status(200).json({
5+
msg: 'Welcome to API MySQL and MongoDB'
6+
});
7+
};
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { Request, Response } from 'express';
2+
import { PacienteInterface } from '../../types.dt';
3+
import PacientsModel from '../../models/mongodb/Pacients';
4+
5+
export const allPacients = async (req: Request, res: Response) => {
6+
const allPacients = await PacientsModel.find();
7+
8+
if (allPacients.length > 0) {
9+
res.status(200).json(allPacients);
10+
} else {
11+
res.status(404).json({
12+
msg: 'No existe ningún paciente registrado'
13+
});
14+
}
15+
};
16+
17+
export const savePacient = async (req: Request, res: Response) => {
18+
const {
19+
cedula, apellidos, nombres, fechNacimiento, genero, direccion, telefono, email
20+
} = req.body;
21+
22+
const onePaciente = await PacientsModel.findOne({
23+
cedula
24+
});
25+
26+
if (onePaciente) {
27+
res.status(404).json({
28+
msg: 'Paciente ya registrado...'
29+
});
30+
} else {
31+
const newPacient = new PacientsModel({
32+
cedula,
33+
apellidos,
34+
nombres,
35+
fechNacimiento,
36+
genero,
37+
direccion,
38+
telefono,
39+
email
40+
});
41+
42+
const Paciente: PacienteInterface = await newPacient.save();
43+
44+
res.status(200).json({
45+
msg: 'Paciente registrado con éxito',
46+
Paciente
47+
});
48+
}
49+
};
50+
51+
export const onePacient = async (req: Request, res: Response) => {
52+
const { id } = req.params;
53+
54+
const onePacient = await PacientsModel.findOne({
55+
_id: id
56+
});
57+
58+
if (onePacient) {
59+
res.status(200).json(onePacient);
60+
} else {
61+
res.status(404).json({
62+
msg: 'Paciente no encontrado'
63+
});
64+
}
65+
};
66+
67+
export const deletePacient = async (req: Request, res: Response) => {
68+
const { id } = req.params;
69+
70+
const onePacient = await PacientsModel
71+
.findByIdAndDelete({
72+
_id: id
73+
})
74+
.select({
75+
cedula: 1,
76+
apellidos: 1,
77+
nombres: 1
78+
});
79+
80+
if (onePacient) {
81+
res.status(200).json({
82+
msg: 'Paciente eliminado con éxito',
83+
onePacient
84+
});
85+
} else {
86+
res.status(404).json({
87+
msg: 'Paciente no encontrado'
88+
});
89+
}
90+
};
91+
92+
export const updatePacient = async (req: Request, res: Response) => {
93+
const { id } = req.params;
94+
95+
const {
96+
cedula, apellidos, nombres, fechNacimiento, genero, direccion, telefono, email
97+
} = req.body;
98+
99+
const onePaciente = await PacientsModel.findOne({
100+
_id: id
101+
});
102+
103+
if (onePaciente) {
104+
const pacientUpdate = await PacientsModel.findByIdAndUpdate({
105+
_id: id
106+
}, {
107+
$set: {
108+
cedula,
109+
apellidos,
110+
nombres,
111+
fechNacimiento,
112+
genero,
113+
direccion,
114+
telefono,
115+
email
116+
}
117+
});
118+
119+
if (pacientUpdate) {
120+
res.status(200).json({
121+
msg: 'Paciente actualizado con éxito',
122+
Paciente: pacientUpdate
123+
});
124+
} else {
125+
res.status(304).json({
126+
msg: 'Paciente no actualizado'
127+
});
128+
}
129+
} else {
130+
res.status(404).json({
131+
msg: 'Paciente no registrado...'
132+
});
133+
}
134+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { Request, Response } from 'express';
2+
import { PacienteInterface } from '../../types.dt';

backend/src/db/mongodb.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import mongoose from 'mongoose';
2+
3+
const mongoDB: string = process.env.MONGO_DB ?? 'mongodb://localhost/typemongo';
4+
5+
export async function connMongo() {
6+
try {
7+
const db = await mongoose.connect(mongoDB);
8+
9+
console.log(`MongoDB is connect to ${db.connection.host} - ${db.connection.name}...`);
10+
} catch (error) {
11+
console.error(error);
12+
}
13+
}

backend/src/db/mysql.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { DataSource } from 'typeorm';
2+
import { ERRORS_DB } from '../types.dt';
3+
import { Nurses } from '../models/mysql/Nurses';
4+
import { Pacients } from '../models/mysql/Pacients';
5+
import { Vaccinations } from '../models/mysql/Vaccinations';
6+
import { Vaccines } from '../models/mysql/Vaccines';
7+
8+
export const connMySQL = async () => {
9+
const conn = new DataSource({
10+
type: 'mysql',
11+
host: process.env.HOST_DB,
12+
port: Number(process.env.PORT_DB),
13+
username: process.env.USER_DB,
14+
password: process.env.PASS_DB,
15+
database: process.env.NAME_DB,
16+
logging: false, // Views logs in the console - TRUE
17+
synchronize: Boolean(process.env.SYNC_DB),
18+
ssl: false,
19+
entities: [Pacients, Nurses, Vaccines, Vaccinations],
20+
subscribers: [],
21+
migrations: []
22+
});
23+
24+
conn.initialize()
25+
.then(() => {
26+
console.log(`MySQLDB is connect to ${process.env.HOST_DB} - ${process.env.NAME_DB}...`);
27+
})
28+
.catch((err) => {
29+
const errorDB = ERRORS_DB[err.code];
30+
31+
if (errorDB) {
32+
console.error(`Error DB ==> ${errorDB}`);
33+
} else {
34+
console.error(err);
35+
}
36+
});
37+
};

backend/src/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'dotenv/config';
2+
3+
import { connMySQL } from './db/mysql';
4+
connMySQL();
5+
6+
import { connMongo } from './db/mongodb';
7+
connMongo();
8+
9+
import app from './server';
10+
11+
app.listen(app.get('port'), () => {
12+
console.log(
13+
`[${new Date().toLocaleDateString()} - ${new Date().toLocaleTimeString()}] - Servidor en el puerto ${app.get('port')}`
14+
);
15+
});

backend/src/models/mongodb/Nurses.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { prop, getModelForClass, modelOptions } from '@typegoose/typegoose';
2+
3+
@modelOptions({
4+
schemaOptions: {
5+
timestamps: true,
6+
versionKey: false
7+
},
8+
})
9+
10+
class Nurses {
11+
@prop({
12+
maxlength: 10,
13+
required: true,
14+
})
15+
cedula: string;
16+
17+
@prop({
18+
maxlength: 75,
19+
required: true,
20+
})
21+
apellidos: string;
22+
23+
@prop({
24+
maxlength: 75,
25+
required: true,
26+
})
27+
nombres: string;
28+
29+
@prop({
30+
maxlength: 10,
31+
required: true,
32+
})
33+
fechNacimiento: string;
34+
35+
@prop({
36+
maxlength: 15,
37+
required: true,
38+
})
39+
genero: string;
40+
41+
@prop({
42+
maxlength: 75,
43+
required: true,
44+
})
45+
direccion: string;
46+
47+
@prop({
48+
maxlength: 10,
49+
required: true,
50+
})
51+
telefono: string;
52+
53+
@prop({
54+
maxlength: 100,
55+
required: true,
56+
lowercase: true
57+
})
58+
email: string;
59+
}
60+
61+
const NurseSchema = getModelForClass(Nurses);
62+
export default NurseSchema;

0 commit comments

Comments
 (0)