Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window problem #7

Open
ducnguyen9988 opened this issue Dec 9, 2014 · 0 comments
Open

Window problem #7

ducnguyen9988 opened this issue Dec 9, 2014 · 0 comments

Comments

@ducnguyen9988
Copy link

I can't get this running in window any ideas
const _ = require('underscore');
const async = require('async');
const prototype = require('prototype');

//To get include deleted data, add the option {deleted: true}
//Other options, you can use from streamsql

var DEFAULT_CONDITION = {
deleted: null
}

var removeCustomOptions = function (object) {
delete object.deleted;
}

var Base = Class.create();

Base.prototype = {
initialize: function (modelObject) {
this.modelObject = modelObject;
},
put: function(object, callback) {
this.modelObject.put(object, function (err, result) {
if (err) {
return callback(err);
};

        var returnObject = {};
        returnObject.id = result.insertId;
        _.extend(returnObject, result.row);

        return callback(err, returnObject);
    });
},
get: function(conditions, options, callback){
    if (typeof options == 'function') {
        callback = options;
        options = {};
    };

    //if options.deleted != true, meaning want to get not deleted objects
    if (!options.deleted) {
        conditions = _.extend(conditions, DEFAULT_CONDITION);
    };

    // options.deleted is a custom options, so we need to remove it before use.
    removeCustomOptions(options);

    this.modelObject.get(conditions, options, function (err, results) {
        if (results.length == 0) {
            return callback(err, null);
        } else {
            return callback(err, results);
        }
    });
},
getOne: function(conditions, options, callback) {
    if (typeof options == 'function') {
        callback = options;
        options = {};
    };

    if (!options.deleted) {
        conditions = _.extend(conditions, DEFAULT_CONDITION);
    };
    removeCustomOptions(options);

    this.modelObject.getOne(conditions, options, function (err, result) {
        return callback(err, result);
    });
},
getById: function(id, options, callback) {
    if (typeof options == 'function') {
        callback = options;
        options = {};
    };

    this.getOne({id: id}, options, function (err, result) {
        return callback(err, result);
    })
},
getMany: function(options, callback) {
    this.get({}, options, function (err, results) {
        return callback(err, results);
    })
},
update: function(updateObject, callback) {
    this.put(updateObject, function (err, result) {
        return callback(err, result);
    })
},
create: function(object, callback) {
    this.put(object, function (err, result) {
        return callback(err, result);
    })
},
remove: function(id, callback) {
    var base = this;
    async.waterfall([
        function (innerCallback) {
            base.getOne({id: id}, function (err, result) {
                if (err) {
                    return callback(err);
                };

                result.deleted = new Date();
                innerCallback(err, result);
            })
        },
        function (deletedObject, innerCallback) {
            base.put(deletedObject, function (err, result) {
                innerCallback(err, result);
            })
        }
    ], function (err, result) {
        callback(err, result);
    });
},
restore: function(id, callback) {
    var base = this;
    async.waterfall([
        function (innerCallback) {
            var options = {deleted: true};

            base.getOne({id: id}, options, function (err, result) {
                if (err) {
                    return callback(err);
                };

                result.deleted = null;
                innerCallback(err, result);
            })
        },
        function (restoredObject, innerCallback) {
            base.put(restoredObject, function (err, result) {
                innerCallback(err, result);
            })
        }
    ], function (err, result) {
        callback(err, result);
    });
},
permanentDelete: function(id, callback) {
    this.modelObject.del({id: id}, function (err) {
        return callback(err);
    });
}

}
exports = module.exports = Base;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant