Answer the question
In order to leave comments, you need to log in
How to understand the principle of working with the file system?
I’ll make a reservation right away, I work with nodejs, but I set more marks, because it seems to me that there is no difference ..
Here's the thing ... Until now, I had to work superficially with the file system, write something down, read something. And the logic of wedging in the file system (I will shorten it to fs in the future) was such that a class was created that had a link to fs and implemented the write and read methods. And everything was fine.
But now I'm doing something that recursively reads a directory and passes the data through many parsers that modify the read line. And I chose the same construction logic, but apparently in vain.
And the futility lies in this. After I repeated the architecture with pushing a reference to fs into one class and implementing the same write and read methods in it, everything was just as good until it was necessary to check the existence of the file in one of the parser-plugin classes.
According to all the canons, the plugins that I have parsers cannot have a reference to a class that deals with writing and reading ..
And the whole evening I went in circles and could not find an answer. But then a crazy thought came to my mind that I don’t understand how logic is built in non-web architecture.
And after all that has been said, the question can be formulated as follows - is it possible in the server architecture ... Or even - how in the server architecture is the most correct way to connect fs and classes that need some of its methods,
but should not have references?
I can't even formulate my question. To further explain, I'll add that in my understanding, the class should implement one-way behavior, in this case, it's working with files.
But it also seems to me that if the parser-plugin class has access to fs,
then it cannot have it partially. That is, you can't say - "here's fs, but take only the exists method, and turn away from the rest." And this theory is also supported by the fact that only one class needs to write and read in order to have full control over the application.
While writing, I thought that if I were collecting a class to work with fs, then it would implement more than one interface and in plugins I would only get access to those methods that are allowed by the interface with the exists method.
But there are no interfaces in nodejs...
Tell me something.
Answer the question
In order to leave comments, you need to log in
I think that no classes are needed here, you just need to do everything in the API style, break it into 2 layers:
var storage = {};
module.exports = storage;
storage.read = function(name, callback) {
...
callback(data); // отдаст data или null
};
storage.write = function(name, data, callback) {
...
callback(); // без папаметров
};
storage.exists = function(name, callback) {
...
callback(exists); // отдаст true/false
};
var storage = require('storage');
var processing = {};
module.exports = processing;
processing.parse = function(data) {
...
// тут можем парсить и вызывать storage API
return parsedStructures;
};
var storage = require('storage');
var processing = require('processing');
// далее можем использовать оба API storage и processing
storage.exists(name, function(exists) {
if (exists) {
// если файл есть, то читаем
storage.read(name, function(data) {
// и теперь его парсим
var matrix = processing.parse(data);
// делаем что нужно
});
} else {
// если нету, то создаем дэфолтную структуру
var matrix = { default: "structure" };
storage.write(name, matrix, function() {
// и пишем ее на диск
});
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question