Skip to content

Commit

Permalink
normalizacion
Browse files Browse the repository at this point in the history
  • Loading branch information
santi-homps committed Oct 5, 2021
1 parent f4ed4ec commit be6b29f
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 0 deletions.
23 changes: 23 additions & 0 deletions backend-2021/D23-JSON_Normalizacion/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
198 changes: 198 additions & 0 deletions backend-2021/D23-JSON_Normalizacion/books.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
const books =
{
id: 999,
books: [
{
"id":1,
"title": "Dracula",
"category" : "Terror",
"author": { "id": 1, "name": "Bram Stoker"},
"comments": [
{
"id": 1,
"author": "Bram Stoker",
"content": "Muy bueno, soy un capo"
},
{
"id": 7,
"author": "PEPE",
"content": "buen libro"
},
{
"id": 3,
"author": "Shakespeare",
"content": "Gran libro te felicito"
},
{
"id": 4,
"author": "JRR Tolkien",
"content": "a mi me gusto"
},
]
},
{
"id":2,
"title": "Fantasma de canterville",
"category" : "Terror",
"author": { "id": 2, "name": "Oscar Wilde"},
"comments": [
{
"id": 5,
"author": "Julio Verne",
"content": "Hay una version online?"
},
{
"id": 1,
"author": "Bram Stoker",
"content": "No me gusto tanto"
},
{
"id": 4,
"author": "JRR Tolkien",
"content": "me quede dormido"
},
]
},
{
"id":3,
"title": "El Señor de los anillos",
"category" : "Fantasia",
"author": { "id": 4, "name": "JRR Tolkien"},
"comments": [
{
"id": 5,
"author": "Julio Verne",
"content": "me parecion muy largo"
},
{
"id": 2,
"author": "Oscar Wilde",
"content": "No entendi el final"
},
{
"id": 1,
"author": "Bram Stoker",
"content": "me lo spoilearon"
},
]
},
{
"id":4,
"title": "20 mil leguas",
"category" : "Aventura",
"author": { "id": 5, "name": "Julio Verne"},
"comments": [
{
"id": 5,
"author": "Julio Verne",
"content": "Lo perdi"
},
{
"id": 3,
"author": "Shakespeare",
"content": "tremendo"
},
{
"id": 4,
"author": "JRR Tolkien",
"content": "hola"
},
]
},
{
"id":5,
"title": "El Hobbit",
"category" : "Aventura",
"author": { "id": 4, "name": "JRR Tolkien"},
"comments": [
{
"id": 1,
"author": "Bram Stoker",
"content": "bienvenidos al tren"
},
{
"id": 3,
"author": "Shakespeare",
"content": "de que hablas"
},
{
"id": 2,
"author": "Oscar Wilde",
"content": "que se yo"
},
]
},
{
"id":6,
"title": "El retrato de Dorian Grey",
"category" : "Terror",
"author": { "id": 2, "name": "Oscar Wilde"},
"comments": [
{
"id": 2,
"author": "Oscar Wilde",
"content": "me parecio largo"
},
{
"id": 1,
"author": "Bram Stoker",
"content": "un clasico"
},
{
"id": 4,
"author": "JRR Tolkien",
"content": "tremendo libro"
},
]
},
{
"id":7,
"title": "El silmarillion",
"category" : "Fantasia",
"author": { "id": 4, "name": "JRR Tolkien"},
"comments": [
{
"id": 5,
"author": "Julio Verne",
"content": "Hay que ver si sale la dos"
},
{
"id": 3,
"author": "Shakespeare",
"content": "espero q no lo vendan"
},
{
"id": 4,
"author": "JRR Tolkien",
"content": "ya nose q poner"
},
]
},
{
"id":8,
"title": "Romeo y Julieta",
"category" : "Fantasia",
"author": { "id": 3, "name": "Shakespeare"},
"comments": [
{
"id": 2,
"author": "Oscar Wilde",
"content": "para cuando la secuela"
},
{
"id": 1,
"author": "Bram Stoker",
"content": "estoy esperando que se casen"
},
{
"id": 4,
"author": "JRR Tolkien",
"content": "increible"
},
]
},
]
}


module.exports = books
1 change: 1 addition & 0 deletions backend-2021/D23-JSON_Normalizacion/denormalizedBook.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":999,"books":[{"id":1,"title":"Dracula","category":"Terror","author":{"id":1,"name":"Bram Stoker"},"comments":[{"id":1,"author":"Bram Stoker","content":"estoy esperando que se casen"},{"id":7,"author":"PEPE","content":"buen libro"},{"id":3,"author":"Shakespeare","content":"espero q no lo vendan"},{"id":4,"author":"JRR Tolkien","content":"increible"}]},{"id":2,"title":"Fantasma de canterville","category":"Terror","author":{"id":2,"name":"Oscar Wilde"},"comments":[{"id":5,"author":"Julio Verne","content":"Hay que ver si sale la dos"},{"id":1,"author":"Bram Stoker","content":"estoy esperando que se casen"},{"id":4,"author":"JRR Tolkien","content":"increible"}]},{"id":3,"title":"El Señor de los anillos","category":"Fantasia","author":{"id":4,"name":"JRR Tolkien"},"comments":[{"id":5,"author":"Julio Verne","content":"Hay que ver si sale la dos"},{"id":2,"author":"Oscar Wilde","content":"para cuando la secuela"},{"id":1,"author":"Bram Stoker","content":"estoy esperando que se casen"}]},{"id":4,"title":"20 mil leguas","category":"Aventura","author":{"id":5,"name":"Julio Verne"},"comments":[{"id":5,"author":"Julio Verne","content":"Hay que ver si sale la dos"},{"id":3,"author":"Shakespeare","content":"espero q no lo vendan"},{"id":4,"author":"JRR Tolkien","content":"increible"}]},{"id":5,"title":"El Hobbit","category":"Aventura","author":{"id":4,"name":"JRR Tolkien"},"comments":[{"id":1,"author":"Bram Stoker","content":"estoy esperando que se casen"},{"id":3,"author":"Shakespeare","content":"espero q no lo vendan"},{"id":2,"author":"Oscar Wilde","content":"para cuando la secuela"}]},{"id":6,"title":"El retrato de Dorian Grey","category":"Terror","author":{"id":2,"name":"Oscar Wilde"},"comments":[{"id":2,"author":"Oscar Wilde","content":"para cuando la secuela"},{"id":1,"author":"Bram Stoker","content":"estoy esperando que se casen"},{"id":4,"author":"JRR Tolkien","content":"increible"}]},{"id":7,"title":"El silmarillion","category":"Fantasia","author":{"id":4,"name":"JRR Tolkien"},"comments":[{"id":5,"author":"Julio Verne","content":"Hay que ver si sale la dos"},{"id":3,"author":"Shakespeare","content":"espero q no lo vendan"},{"id":4,"author":"JRR Tolkien","content":"increible"}]},{"id":8,"title":"Romeo y Julieta","category":"Fantasia","author":{"id":3,"name":"Shakespeare"},"comments":[{"id":2,"author":"Oscar Wilde","content":"para cuando la secuela"},{"id":1,"author":"Bram Stoker","content":"estoy esperando que se casen"},{"id":4,"author":"JRR Tolkien","content":"increible"}]}]}
51 changes: 51 additions & 0 deletions backend-2021/D23-JSON_Normalizacion/normalize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const books = require('./books')
const fs = require('fs')

const { normalize, denormalize, schema } = require("normalizr");

//Schemas

const authors = new schema.Entity("authors")

const comments = new schema.Entity("comments")

const bookSchema = new schema.Entity('books', {
author: authors,
comments: [comments]
});

const library = new schema.Entity('library', {
books: [bookSchema]
});

//Normalizacion

const normalizedBooks = normalize(books, library)

//Peso Normalizado
console.log("----Normalizado----")
const pesoNormalizado = (JSON.stringify(normalizedBooks).length)
console.log(pesoNormalizado)

fs.writeFile("./normalizedBook.json", JSON.stringify(normalizedBooks), "utf-8", (err)=> {
if(err) throw new Error(err)
})

//Desnormalizacion

const denormalizedBooks = denormalize(normalizedBooks.result, library, normalizedBooks.entities)

//Peso desnormalizado
console.log("----Desnormalizado----")
const pesoDesnormalizado = (JSON.stringify(denormalizedBooks).length)
console.log(pesoDesnormalizado)

fs.writeFile("./denormalizedBook.json", JSON.stringify(denormalizedBooks), "utf-8", (err)=> {
if(err) throw new Error(err)
})

//Diferencia en peso

console.log("----Diferencia----")
const diferenciaPeso = pesoDesnormalizado - pesoNormalizado
console.log(diferenciaPeso)
1 change: 1 addition & 0 deletions backend-2021/D23-JSON_Normalizacion/normalizedBook.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"entities":{"authors":{"1":{"id":1,"name":"Bram Stoker"},"2":{"id":2,"name":"Oscar Wilde"},"3":{"id":3,"name":"Shakespeare"},"4":{"id":4,"name":"JRR Tolkien"},"5":{"id":5,"name":"Julio Verne"}},"comments":{"1":{"id":1,"author":"Bram Stoker","content":"estoy esperando que se casen"},"2":{"id":2,"author":"Oscar Wilde","content":"para cuando la secuela"},"3":{"id":3,"author":"Shakespeare","content":"espero q no lo vendan"},"4":{"id":4,"author":"JRR Tolkien","content":"increible"},"5":{"id":5,"author":"Julio Verne","content":"Hay que ver si sale la dos"},"7":{"id":7,"author":"PEPE","content":"buen libro"}},"books":{"1":{"id":1,"title":"Dracula","category":"Terror","author":1,"comments":[1,7,3,4]},"2":{"id":2,"title":"Fantasma de canterville","category":"Terror","author":2,"comments":[5,1,4]},"3":{"id":3,"title":"El Señor de los anillos","category":"Fantasia","author":4,"comments":[5,2,1]},"4":{"id":4,"title":"20 mil leguas","category":"Aventura","author":5,"comments":[5,3,4]},"5":{"id":5,"title":"El Hobbit","category":"Aventura","author":4,"comments":[1,3,2]},"6":{"id":6,"title":"El retrato de Dorian Grey","category":"Terror","author":2,"comments":[2,1,4]},"7":{"id":7,"title":"El silmarillion","category":"Fantasia","author":4,"comments":[5,3,4]},"8":{"id":8,"title":"Romeo y Julieta","category":"Fantasia","author":3,"comments":[2,1,4]}},"library":{"999":{"id":999,"books":[1,2,3,4,5,6,7,8]}}},"result":999}
27 changes: 27 additions & 0 deletions backend-2021/D23-JSON_Normalizacion/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions backend-2021/D23-JSON_Normalizacion/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "ejemplo2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"normalizr": "^3.6.1"
}
}

0 comments on commit be6b29f

Please sign in to comment.