Skip to content

Commit

Permalink
Manejo de archivos
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscotimez committed Jul 30, 2022
1 parent f2503b7 commit c8b95b3
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
94 changes: 94 additions & 0 deletions archivos/contenedor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@

const {promises:fs} = require('fs')

class Contenedor {
constructor(filePath){
this.filePath = filePath
this.lastId = 0
}

async save(obj){
let data = await this.getAll()
const newObj = {id: ++this.lastId, ...obj};
console.log(`Nuevo objeto: `, newObj);
data = [...data, newObj];

await fs.writeFile(this.filePath, JSON.stringify(data, null, 2));
console.log(`Se creo archivo ${this.filePath}`);
return newObj
}

async getById(id){
let data = await this.getAll()
return data.find(data => data.id === id)
}

async getAll(){
try {
let raw = await fs.readFile(this.filePath, 'utf8');
let data = JSON.parse(raw);
let lastId = [0, ...data.map(item => item.id)]
this.lastId = Math.max(...lastId)
console.log(`Se leyo archivo ${this.filePath}, lastId: `, this.lastId);
return data;
} catch (e) {
if( e.code === 'ENOENT') {
try{
await fs.writeFile(this.filePath, JSON.stringify([]));
console.log(`Se creo archivo ${this.filePath}`);
return []
} catch (error) {
return `No se puede modificar los datos`
}
}
}
}

async deleteById(id){
let data = await this.getAll()
let datafiltered = data.filter(item => item.id != id)
console.log("delete by id", datafiltered)
try {
await fs.writeFile(this.filePath, JSON.stringify(datafiltered, null, 2))
return `Dato borrado`
} catch (error) {
return `No se puede modificar los datos`
}
}

async deleteAll(){
try {
await fs.writeFile(this.filePath, JSON.stringify([]));
} catch (error) {
return `No se puede modificar los datos`
}
}
}

module.exports = {Contenedor}
// ( async () => {

// let data = new Contenedor("./data.json")
// let result

// result = await data.save({name: "Contenedor"})
// console.log(result)

// result = await data.deleteById(3)
// console.log(result)
// })()

// data.getAll()
// .then((data) => {
// console.log(data)
// })

// data.getById(3)
// .then((data) => {
// console.log(data)
// })

// data.deleteAll()
// .then((data) => {
// console.log(data)
// })
41 changes: 41 additions & 0 deletions archivos/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { Contenedor } = require('./contenedor');


( async () => {
const productos = new Contenedor("./products.json")

let result = ``

result = await productos.save({
title: "Producto 1",
price: 25.4,
thumbnail: "https://images.com/img1.jpg"
})
console.log(result)

result = await productos.save({
title: "Producto 2",
price: 52.21,
thumbnail: "https://images.com/img2.jpg"
})
console.log(result)

result = await productos.save({
title: "Producto 3",
price: 1552.26,
thumbnail: "https://images.com/img3.jpg"
})
console.log(result)

result = await productos.getById(2)
console.log("Producto 2: ",result)

result = await productos.getAll()
console.log(result)

result = await productos.deleteById(2)
console.log(result)

// result = await productos.deleteAll()
})()

0 comments on commit c8b95b3

Please sign in to comment.