Encapsulate data in module

I was wondering, why one would not put data in a module and expose the data with functions? The following code works quite fine. Any hints?

module.exports.myread = myread;

const sample = [
{id: 1, name: ‘Marcus’},
{id: 2, name: ‘Maria’},
];

function myread(id) {
if (id === undefined)
return sample;
else
return sample.find(c => c.id === id);
}

It is abstracting the data structure so it seems good. However, if you aren’t calling the “myread” function in multiple files, it might be overkill.

By the way, could you please mention why you are particularly using this approach?

Hi Sufian, thx for the answer.

I am doing that out of educational purpose. Teaching some programming class, and using Node.js as sample.

I pack the CRUD methods in a module and using that in a REST sample. Not only it is good abstraction imho, but one can see how a DB would work eventually. Plus learning by fixing problems, e.g. one cannot delete an element without confusing the create for getting a new id.