2
\$\begingroup\$

Thiswill get quite clumsy if I continue down the road with this design, so I'm asking you, enlightened ones, is there a way to make this prettier and easier to understand? Primarily therun-function (Especially in theelse if whereself.query andself.find are set), also, I want it to be chain-able, with.run being the last function.

db = function(dir){    if(dir[dir.length - 1] == "/") this.dir = dir.slice(0, -1);    else this.dir = dir;    return this;}db.prototype.table = function(table){    this.table = table;    return this;}db.prototype.put = function(obj){    this.put = obj;    return this;}db.prototype.find = function(query){    this.query = query;    this.find = "all";    return this;}db.prototype.pluck = function(){    this.pluck = arguments;    return this;}db.prototype.run = function(callback){    var self = JSON.parse(JSON.stringify(this));    for(var key in this){        if(typeof this[key] != "function" && key != "dir" && key != "table") delete this[key];    }    helper.mkdb(self, function(err, created){        helper.mktbl(self, function(err, created){                      var docs = [];            if(self.put){                helper.put(self, function(err, docs){                    callback(err, docs);                })            }else if(self.query && self.find){                helper.find(self, function(err, docs){                    if(err) callback(err);                    else{                        self.docs = docs;                        if(self.pluck){                            helper.pluck(self, function(err, docs){                                callback(err, docs);                            })                        }else{                            callback(err, docs);                        }                    }                })            }else if(self.pluck){                helper.pluck(self, function(err, docs){                    callback(err, docs);                })            }else callback(new Error("Cannot execute \".run()\" without any functions before it"));        })    });}
200_success's user avatar
200_success
146k22 gold badges191 silver badges481 bronze badges
askedApr 6, 2015 at 18:18
Mobilpadde's user avatar
\$\endgroup\$
0

1 Answer1

1
\$\begingroup\$

I've found a way, it's not the best though. You can usenimble (Orasync, it's basically the same but both larger in size and contain more functions) to do a series of functions, and then check from within thehelper-class to see if a certain value has been set.

This is my new and improved version of therun-function:

db.prototype.run = function(callback){    var self = JSON.parse(JSON.stringify(this));    for(var key in this){        if(typeof this[key] != "functions" && key != "dir" && key != "table") delete this[key];    }    helper.mkdb(self, function(err, created){        helper.mktbl(self, function(err, created){                      self.docs = [];            nimble.series([                function(done){                    if(!self.put) done(err, []);                    else{                        helper.put(self, function(err, docs){                            self.docs = docs;                            done(err, docs);                        })                    }                },                function(done){                    if(!self.query && !self.find) done(err, self.docs || []);                    else{                        helper.find(self, function(err, docs){                            self.docs = docs;                            done(err, docs);                        })                    }                },                function(done){                    if(!self.pluck) done(err, self.docs || []);                    else{                        helper.pluck(self, function(err, docs){                            self.docs = docs;                            done(err, docs);                        })                    }                }            ], function(err, res){                callback(err, helper.lastData(res));            })        })    });}
answeredApr 6, 2015 at 23:21
Mobilpadde's user avatar
\$\endgroup\$

You mustlog in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.