We’re going to use the fs module and a json file as our DB to store our notes. Create a db.json file on the root:

{
	"notes": []
}

Next, create a db.js file in the src directory. Here we will create helper functions to interact with the JSON file.

import fs from 'node:fs/promises'

const DB_PATH = new URL('../db.json', import.meta.url).pathname

export const getDB = async () => {
  const db = await fs.readFile(DB_PATH, 'utf-8')
  return JSON.parse(db)
}

export const saveDB = async (db) => {
  await fs.writeFile(DB_PATH, JSON.stringify(db, null, 2))
  return db
}

export const insert = async (data) => {
  const db = await getDB()
  db.notes.push(data)
  await saveDB(db)
  return data 
}

First we import the fs module. However, we want to use async/await which works with promises. So instead of using the callback versions of fs methods, node has a promise version of fs methods that we can import instead.

Next we define the path to where the json file is. Then we create 3 functions:

We will use these methods in another file to help us work with our notes