Skip to content

Commit 85219ab

Browse files
authored
Merge pull request #19 from OrcaPracticas/add/DataBase
[Pull Request] integracion de base de datos
2 parents d937481 + fd97c10 commit 85219ab

18 files changed

Lines changed: 435 additions & 39 deletions

.babelrc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
"presets" : [
33
[
44
"env",
5-
"minify"
6-
]
5+
{
6+
"targets": {
7+
"node": "current"
8+
}
9+
}
10+
],
11+
"minify",
12+
"stage-1"
713
],
814
"plugins" : [
915
["inline-json-import", {}]

package.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,34 @@
22
"author": "<jorge.mendez.ortega@gmail.com>",
33
"dependencies": {
44
"body-parser": "1.18.3",
5-
"casual": "^1.5.19",
5+
"casual": "1.5.19",
66
"express": "4.16.3",
77
"graphql": "0.13.2",
88
"graphql-server-express": "1.4.0",
9-
"graphql-tools": "3.1.1"
9+
"graphql-tools": "3.1.1",
10+
"knex": "0.15.2",
11+
"objection": "1.2.3",
12+
"sqlite3": "4.0.2"
1013
},
1114
"description": "Practica con GQL",
1215
"devDependencies": {
1316
"babel-cli": "6.26.0",
1417
"babel-minify": "0.4.3",
1518
"babel-plugin-inline-json-import": "0.2.1",
1619
"babel-preset-env": "1.7.0",
17-
"babel-register": "^6.26.0",
18-
"nodemon": "^1.18.3"
20+
"babel-preset-stage-1": "6.24.1",
21+
"babel-register": "6.26.0",
22+
"nodemon": "1.18.3"
1923
},
2024
"license": "MIT",
2125
"main": "index.js",
2226
"name": "GQL",
2327
"private": false,
2428
"repository": "git@konami12:OrcaPracticas/GQL.git",
2529
"scripts": {
26-
"start": "nodemon ./launcher"
30+
"start": "nodemon ./launcher",
31+
"db:migrate": "knex migrate:latest --knexfile=src/DB/knexfile.js",
32+
"db:seed": "knex seed:run --knexfile=src/DB/knexfile.js"
2733
},
2834
"version": "1.0.0"
2935
}

src/DB/db.sqlite

Whitespace-only changes.

src/DB/knexfile.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Archivo de configuracion en el cual
3+
* se le indica el cliente de motor
4+
* de base de datos
5+
*/
6+
module.exports = {
7+
development: {
8+
client: "sqlite3",
9+
connection: {
10+
filename: `${__dirname}/db.sqlite`,
11+
},
12+
},
13+
production: {},
14+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const NAME_TABLE = "comentarios";
2+
3+
/**
4+
* Inidica que se ejecuta se realizara en la migrcion
5+
* para este casos se crea la tabla.
6+
*/
7+
exports.up = (knex, Promise) => (
8+
Promise.all([
9+
knex.schema.createTable(
10+
NAME_TABLE,
11+
(table) => {
12+
table.increments("id").primary().unsigned();
13+
table.string("nombre");
14+
table.string("cuerpo");
15+
table.integer("curso_id").unsigned();
16+
},
17+
),
18+
])
19+
);
20+
21+
/**
22+
* Al terminar el proceso de migracion se libera la tabla.
23+
*/
24+
exports.down = (knex, Promise) => (
25+
Promise.all([
26+
knex.schema.dropTable(NAME_TABLE),
27+
])
28+
);

src/DB/migrations/cursos_table.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const NAME_TABLE = "cursos";
2+
3+
/**
4+
* Inidica que se ejecuta se realizara en la migrcion
5+
* para este casos se crea la tabla.
6+
*/
7+
exports.up = (knex, Promise) => (
8+
Promise.all([
9+
knex.schema.createTable(
10+
NAME_TABLE,
11+
(table) => {
12+
table.increments("id").primary().unsigned();
13+
table.string("titulo");
14+
table.string("descripcion");
15+
table.double("raiting");
16+
table.integer("profesor_id").unsigned();
17+
},
18+
),
19+
])
20+
);
21+
22+
/**
23+
* Al terminar el proceso de migracion se libera la tabla.
24+
*/
25+
exports.down = (knex, Promise) => (
26+
Promise.all([
27+
knex.schema.dropTable(NAME_TABLE),
28+
])
29+
);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
const NAME_TABLE = "profesores";
3+
4+
/**
5+
* Inidica que se ejecuta se realizara en la migrcion
6+
* para este casos se crea la tabla.
7+
*/
8+
exports.up = (knex, Promise) => (
9+
Promise.all([
10+
knex.schema.createTable(
11+
NAME_TABLE,
12+
(table) => {
13+
table.increments("id").primary().unsigned();
14+
table.string("nombre");
15+
table.string("nacionalidad");
16+
table.string("genero");
17+
},
18+
),
19+
])
20+
);
21+
22+
/**
23+
* Al terminar el proceso de migracion se libera la tabla.
24+
*/
25+
exports.down = (knex, Promise) => (
26+
Promise.all([
27+
knex.schema.dropTable(NAME_TABLE),
28+
])
29+
);

src/DB/models/Comentario.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Model } from "objection";
2+
import Path from "path";
3+
4+
class Comentario extends Model {
5+
/**
6+
* Definicion de la tabla
7+
*/
8+
static get tableName() {
9+
return "comentarios";
10+
}
11+
12+
/**
13+
* Definicion de las relaciones.
14+
*/
15+
static get relationMappings() {
16+
const RELATION_MAP = {
17+
curso: {
18+
relation: Model.BelongsToOneRelation,
19+
modelClass: Path.join(__dirname, "/Curso"),
20+
join: {
21+
from: "comentarios.curso_id",
22+
to: "cursos.id",
23+
},
24+
},
25+
};
26+
return RELATION_MAP;
27+
}
28+
}
29+
30+
export default Comentario;

src/DB/models/Curso.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Model } from "objection";
2+
import Path from "path";
3+
4+
class Curso extends Model {
5+
/**
6+
* Definicion de la tabla
7+
*/
8+
static get tableName() {
9+
return "cursos";
10+
}
11+
12+
/**
13+
* Definicion de las relaciones.
14+
*/
15+
static get relationMappings() {
16+
const RELATION_MAP = {
17+
profesor: {
18+
relation: Model.BelongsToOneRelation,
19+
modelClass: Path.join(__dirname, "/Profesor"),
20+
join: {
21+
from: "cursos.profesor_id",
22+
to: "profesores.id",
23+
},
24+
},
25+
comentarios: {
26+
relation: Model.HasManyRelation,
27+
modelClass: Path.join(__dirname, "/Comentario"),
28+
join: {
29+
from: "cursos.id",
30+
to: "comentarios.curso_id",
31+
},
32+
},
33+
};
34+
return RELATION_MAP;
35+
}
36+
}
37+
38+
export default Curso;

src/DB/models/Profesor.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Model } from "objection";
2+
import Path from "path";
3+
4+
class Curso extends Model {
5+
/**
6+
* Definicion de la tabla
7+
*/
8+
static get tableName() {
9+
return "profesores";
10+
}
11+
12+
/**
13+
* Definicion de las relaciones.
14+
*/
15+
static get relationMappings() {
16+
const RELATION_MAP = {
17+
cursos: {
18+
relation: Model.HasManyRelation,
19+
modelClass: Path.join(__dirname, "/Curso"),
20+
join: {
21+
from: "profesores.id",
22+
to: "cursos.profesor_id",
23+
},
24+
},
25+
};
26+
return RELATION_MAP;
27+
}
28+
}
29+
30+
export default Curso;

0 commit comments

Comments
 (0)