folder 01
From this:
async getAll() {
// Open the file called this.filename
const contents = await fs.promises.readFile(this.filename, {
encoding: 'utf8',
})
// Read its contents
// console.log(contents)
// Parse the contents
const data = JSON.parse(contents)
// Return the parsed data
return data
}To this:
async getAll() {
// Open the file called this.filename
return JSON.parse(
await fs.promises.readFile(this.filename, {
encoding: 'utf8',
})
)
}async create(attrs) {
// Object { email: 'elfgodd@elfgodd.com', password: '1234 }
const records = await this.getAll()
records.push(attrs)
// write the updated 'records' array back to this.filename
await fs.promises.writeFile(this.filename, JSON.stringify(records))
}JSON.stringify(records, null, 2) 2nd arg is a custom formatter, 3rd arg designates the level of indentation to use inside of our string that we create
node
Welcome to Node.js v16.13.0.
Type ".help" for more information.
> const crypto = require('crypto')
undefined
> crypto.randomBytes(4)
<Buffer 3d d2 80 9c>
> crypto.randomBytes(4).toString('hex')
'7f979e27'randomId() {
// return Math.random() * 9999999
return crypto.randomBytes(4).toString('hex')
}async getOne(id) {
const records = await this.getAll()
return records.find(record => record.id === id)
}async delete(id) {
const records = await this.getAll()
const filteredRecords = records.filter((record) => record.id !== id)
await this.writeAll(filteredRecords)
}async update(id, attrs) {
const records = await this.getAll()
const record = records.find((record) => record.id === id)
if (!record) {
throw new Error(`Record with id: ${id} not found`)
}
// Takes all the different properties and key value pairs inside
// the attrs object and copy them on by on onto the record object
// Example:
// record === { email: elfgodd@elfgodd.com }
// attrs === { password: '12345' }
Object.assign(record, attrs)
// new record === { email: elfgodd@elfgodd.com, password: '12345' }
await this.writeAll(records)
}const test = async () => {
const repo = new UsersRepository('users.json')
// await repo.create({ email: 'elfgodd@elfgodd.com', password: '12345' })
// const users = await repo.getAll()
// const user = await repo.getOne('918b2447')
// const user = await repo.getOne('xxx') // undefined, user doesn't exists
// await repo.delete('5985b553')
// await repo.delete('918b2447')
// await repo.update('a89fa519', { password: '54321' })
// await repo.update('1235gg633', { password: '54321' })
// const user = await repo.getOneBy({
// email: 'elfgodd@elfgodd.com',
// // password: '54321',
// "password": "11111"
// })
const user = await repo.getOneBy({
// id: 'a89fa519',
id: '111111111',
})
// const user = await repo.getOneBy({
// userDontExist: 'userDontExist',
// })
// console.log(users)
console.log(user)
}
test()// Rather than exporting the entire class of users repository,
const UsersRepository = require('./users')
const repo = new UsersRepository('users.json')
// we're going to instead export an instance of the class
module.exports = UsersRepository('users.json')