Skip to content

Window problem #7

Description

@ducnguyen9988

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;

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions