Ask coding questions

← Back to all posts
How do I "push" something to a variabe in a file using nodeJs?
Coder100 (172)

I would like to "push" an object into my msg.js file:
File content: var m = [{name:"",desc:""}];
I would like to "push": {name:"test",desc:"test"}
New file content: var m = [{name:"",desc:""},{name:"test",desc:"test"}];

Answered by IbraheemRodrigues (76) [earned 5 cycles]
View Answer
IbraheemRodrigues (76)

To edit files you likely want to use the fs module.
However what is your use case? If you are storing objects such as the contents of the variable m you likely want to use JSON files.More info about what you are doing would be useful.

Coder100 (172)

@IbraheemRodrigues It is to store messages for a chat

IbraheemRodrigues (76)

@Coder100 As in message templates? or messages that have been sent and are to be recorded.

Coder100 (172)

@IbraheemRodrigues messages that have been sent and are to be recorded.

IbraheemRodrigues (76)

@Coder100 Here's my crash course in persistent data storage:

For your purpose, only the data in the message is important, so we don't need anything else from JS (control flow, variables, functions, errors, etc.).

For that purpose JSON (JavaScript Object Notation) was created, which only features Objects, Arrays, Booleans, Numbers and Strings. A JSON file contains ONE object or array, and so will start with {or [ and end with } or ]. It is good for 'one off ' data, such of config files that will be loaded once on startup or data to be transmitted to another process or across the internet, since it can be read in and evaluated as JS without much extra processing required.

However, the issue if you want to use it to create a database or something similar where there will be an unknown number of data points, and ones that must be added, changed and removed is that you have to worry about opening and closing {}, [] and "", or have to read in the entire data structure, evaluate it, change it, sterilise it (convert it from data in memory to JSON text) and save it to file (which can work well, but is tedious and messy for you to implement). For these cases things like CSV (Comma Separated Values) or SQL databases are used, which function like an excel table - you have a header that defines the data to be stored in each column and below that each row specifies this data for a single datapoint. CSV stores data as text, where as SQL stores stuff as binary data. If you are planning for you application to be big, a SQL database such as SQLite or MySQL would be most efficient, and are worth learning how to use since most applications use them. However for small projects they may not be ideal. The issue with these formats is that you loose the JS-ness of the data, and each data point must follow a rigid, linear (i.e no nested objects/arrays) pattern

To get the best of both worlds, use a database technology that stores data as JSON-like files or provides a translation layer, such as MongoDB (which is used widely in industry alongside NodeJS) or EnMaps (Enhanced Maps, basically JS Maps with a bunch of added features) which can be given a persistence provider to store them on disk. I've used EnMaps and they're pretty cool.

So in conclusion, I'd recommend that you either:

  • Learn SQL and use SQLite, thereby gaining great knowledge of this technology
  • Use node-persist to easily read and write your array (I presume an array) to and from disk.