text: const fs = require('fs');
  const crypto = require('crypto');
  
  module.exports = class Repository {
  	constructor(filename) {
  		if (!filename) {
  			throw new Error('Creating a repository requires a filename');
  		}
  
  		this.filename = filename;
  		try {
  			fs.accessSync(this.filename);
  		} catch (err) {
  			fs.writeFileSync(this.filename, '[]');
  		}
  	}
  
  	/** Generic create */
  	async create(attrs) {
  		attrs.id = this.randomId();
  
  		const records = await this.getAll();
  		records.push(attrs);
  		await this.writeAll(records);
  
  		return attrs;
  	}
  
  	/** read users array data from fs */
  	async getAll() {
  		return JSON.parse(
  			await fs.promises.readFile(this.filename, {
  				encoding: 'utf8'
  			})
  		);
  	}
  
  	/** WRITE ALL */
  	async writeAll(records) {
  		await fs.promises.writeFile(
  			this.filename,
  			JSON.stringify(records, null, 2)
  		);
  	}
  
  	/** RANDOM ID */
  	randomId() {
  		return crypto.randomBytes(4).toString('hex');
  	}
  
  	/** GET ONE */
  
  	async getOne(id) {
  		const records = await this.getAll();
  
  		return records.find(record => record.id === id);
  	}
  
  	/** DELETE */
  
  	async delete(id) {
  		const records = await this.getAll();
  
  		const filteredRecords = records.filter(record => record.id !== id);
  
  		await this.writeAll(filteredRecords);
  	}
  
  	/** UPDATE */
  
  	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!`);
  		}
  
  		//update
  		Object.assign(record, attrs);
  
  		await this.writeAll(records);
  	}
  
  	/** GET ONE BY - filter */
  
  	async getOneBy(filters) {
  		const records = await this.getAll();
  
  		for (let record of records) {
  			let found = true;
  
  			for (let key in filters) {
  				if (record[key] !== filters[key]) {
  					found = false;
  				}
  			}
  
  			if (found) {
  				return record;
  			}
  		}
  	}
  };
  