Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs')
- const path = require('path')
- const dbPath = path.normalize(path.join(__dirname, '/database.json'))
- module.exports.products = {}
- module.exports.products.add = (product) => {
- let products = getProducts()
- product.id = products.length + 1
- products.push(product)
- saveProducts(products)
- }
- let getProducts = () => {
- if (!fs.existsSync(dbPath)) {
- fs.writeFileSync(dbPath, '[]')
- return []
- }
- let json = fs.readFileSync(dbPath).toString() || '[]'
- return JSON.parse(json)
- }
- let saveProducts = (products) => {
- let json = JSON.stringify(products, null, 2)
- fs.writeFileSync(dbPath, json)
- }
- module.exports.products.getAll = getProducts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement