Home > Article > Web Front-end > nodejs does not use a database
Node.js is a back-end JavaScript runtime environment that can be easily integrated with other technology stacks through Node.js's built-in modules and the community's module manager. One of the most commonly used technology stacks is to store and manage data through databases. But in some cases, we may not want to use a database for data processing and storage, and data storage and management can be achieved in other ways. This article will introduce some ways to use Node.js without using a database.
JSON files are short for JavaScript Object Notation. It is a lightweight data exchange format that is highly readable and easy to automatically parse. So we can store data by using JSON files.
In Node.js, we can use the fs
module to operate the file system. Through the fs
module, we can read and write JSON files very conveniently. The following is a sample code that uses a JSON file to store data:
const fs = require('fs') const dataFilePath = './data.json' function loadData() { const fileContent = fs.readFileSync(dataFilePath, 'utf-8') const data = JSON.parse(fileContent) return data } function saveData(data) { const dataJSON = JSON.stringify(data) fs.writeFileSync(dataFilePath, dataJSON) } const data = loadData() console.log(data) data.push('new data') saveData(data)
In the above code, we define two functions that operate on data: loadData()
and saveData ()
. loadData()
Read data from a JSON file and convert it into a JavaScript object. saveData(data)
Convert a JavaScript object to a JSON string and write it to a JSON file.
At this point we can operate the data
array, such as adding an item to it, and finally write the updated data to the JSON file through saveData(data)
. This way we can use JSON files to store and manage data.
It is worth noting that when the amount of data is small, using JSON files is indeed a good choice, but when the amount of data becomes large, its performance and efficiency may not be very high, and there is no SQL database has strict binding rules, so it needs to be carefully considered in practical applications.
In addition to using JSON files to store data, we can also use file directories to store data. Save each document as a separate file with some data identifying information in the file name, and then store the files in a directory. This approach is similar to using Git to manage a code base, where each Git object is a file stored in the .git
directory.
The following is a sample code that uses a file directory to store data:
const fs = require('fs') const path = require('path') const dataDirPath = './data/' function saveData(data) { const { id, content } = data const filename = `${id}.json` fs.writeFileSync(path.join(dataDirPath, filename), content) } function loadData(id) { const filename = `${id}.json` const fileContent = fs.readFileSync(path.join(dataDirPath, filename), 'utf-8') return fileContent } const data = { id: '001', content: 'data content' } saveData(data) const retrievedData = loadData(data.id) console.log(retrievedData)
In the above code, we define two functions that operate on data: saveData(data)
and loadData(id)
. saveData(data)
Convert the data object to a JSON string, save it as a file, and set its file name to the unique identifier in the data object (for example, here we use a file named id
attribute). loadData(id)
Find the corresponding file based on the unique identifier of the data object and read the data in it.
By using file directories to store data, we avoid the additional costs and maintenance costs of using a database, and also facilitate data management and backup. But for data management and query operations, we need to write the logic of file operations ourselves. The complexity of these operations may be higher than SQL queries, so we need to choose an appropriate solution in actual applications.
In addition to the above two methods, we can also use cache to store data. There are many popular caching modules in Node.js, such as Node-Cache and Node-RAMCache. We can use these modules to store data, as well as read and update data.
The following is a sample code that uses the Node-Cache module to store data:
const cache = require('node-cache') const myCache = new cache() const data = { id: '001', content: 'data content' } myCache.set(data.id, data.content) const retrievedData = myCache.get(data.id) console.log(retrievedData)
In the above code, we store data by using the Node-Cache module. myCache.set(data.id, data.content)
Store data from the cache, myCache.get(data.id)
Read data from the cache. Their use is basically the same as Map or Object. However, it should be noted that the cached data is only valid while the application is running. Once the application stops running, the cached data will be lost.
The benefit of using cache is that cache usually has fast read and write speeds, allowing us to process data faster. However, it should be noted that when the cache becomes the bottleneck of the application (the cache expires, the memory occupied by the cache is too large, etc.), we need to optimize the cache usage, otherwise the performance of the application may be affected.
Conclusion
This article introduced some methods of using Node.js without using a database, including using JSON files, file directories, and caches to store data. These methods are not universally applicable in applications, and the most appropriate method needs to be selected based on application scenarios and requirements. At the same time, for data management and query operations, we also need to write our own code to implement it. If you have better ideas or suggestions, please share them in the comments.
The above is the detailed content of nodejs does not use a database. For more information, please follow other related articles on the PHP Chinese website!