这是正确使用Mongoose静力学吗?

时间:2015-02-23 17:40:28

标签: node.js mongodb mongoose

我正在创建模板以从现有数据库生成Mongoose / MongoDB Web服务。我已经阅读了一些关于静力学和方法的文档,我想知道下面的例子是否可以长期运作。

在MongoDB / Mongoose生产环境中应该更改哪些内容有用?

实际模板位于:



/**
* Generated by fredl_000 on 2/23/2015 12:08:25 PM
* Copyright 2015 Fred Lackey
* Direct questions to the author: Fred Lackey <fred.lackey@gmail.com>
*/
var moment = require('moment'),
    mongoose = require('mongoose'),
    strings = require('dzstrings');
    uids = require('dzuuids');

var signupRequestSchema = new mongoose.Schema({
    _id: { type: String, trim: true, uppercase: true, default: uids.newUid, required: false },
    emailAddress: { type: String, trim: true, required: false },
    confirmationCode: { type: String, trim: true, required: false },
    _k: { type: Date, default: null, required: true }
});

signupRequestSchema.index({
    emailAddress: 1,
    _k: 1
}, { unique: true });

signupRequestSchema.statics.validateItem = function(item, callback) {
    if (typeof item == 'undefined'){ return callback(new Error('No item to validate')); }
    if (typeof item._id != 'undefined'){ if (!strings.isValidString(item._id)){ return callback(new Error('_id (id) is not a valid String')); } else { item._id = strings.trimToNull(item._id); } }
    if (typeof item.emailAddress != 'undefined'){ if (!strings.isValidString(item.emailAddress)){ return callback(new Error('emailAddress is not a valid String')); } else { item.emailAddress = strings.trimToNull(item.emailAddress); } }
    if (typeof item.confirmationCode != 'undefined'){ if (!strings.isValidString(item.confirmationCode)){ return callback(new Error('confirmationCode is not a valid String')); } else { item.confirmationCode = strings.trimToNull(item.confirmationCode); } }
    if (typeof item._k != 'undefined'){ if (!strings.DateOrNull(item._k)){ return callback(new Error('_k (auditDeletedDate) is not a valid Date or null value')); } else {  } }
    return callback(null, item);
};

signupRequestSchema.statics.toDto = function(item, callback) {
    if (!item){ return callback(new Error('No item to convert')); }
    var dto = {};
    if (typeof item._id != 'undefined'){ dto.id = item._id; }
    if (typeof item.emailAddress != 'undefined'){ dto.emailAddress = item.emailAddress; }
    if (typeof item.confirmationCode != 'undefined'){ dto.confirmationCode = item.confirmationCode; }
    if (typeof item._k != 'undefined'){ dto.auditDeletedDate = item._k; }
    return dto;
};

signupRequestSchema.statics.toDtoFull = function(item, callback) {
    if (!item){ return callback(new Error('No item to convert')); }
    var dto = {
        id: ((typeof item._id != 'undefined') ? item._id : null),
        emailAddress: ((typeof item.emailAddress != 'undefined') ? item.emailAddress : null),
        confirmationCode: ((typeof item.confirmationCode != 'undefined') ? item.confirmationCode : null),
        auditDeletedDate: ((typeof item._k != 'undefined') ? item._k : null)
    };
    return dto;
};

signupRequestSchema.statics.toItem = function(dto, callback) {
    if (!dto){ return callback(new Error('No DTO to convert')); }
    var item = {};
    if (typeof dto.id != 'undefined'){ item._id = dto.id; }
    if (typeof dto.emailAddress != 'undefined'){ item.emailAddress = dto.emailAddress; }
    if (typeof dto.confirmationCode != 'undefined'){ item.confirmationCode = dto.confirmationCode; }
    if (typeof dto.auditDeletedDate != 'undefined'){ item._k = dto.auditDeletedDate; }
    return item;
};

signupRequestSchema.statics.toItemFull = function(dto, callback) {
    if (!dto){ return callback(new Error('No DTO to convert')); }
    var item = {
        _id: ((typeof dto.id != 'undefined') ? dto.id : null),
        emailAddress: ((typeof dto.emailAddress != 'undefined') ? dto.emailAddress : null),
        confirmationCode: ((typeof dto.confirmationCode != 'undefined') ? dto.confirmationCode : null),
        _k: ((typeof dto.auditDeletedDate != 'undefined') ? dto.auditDeletedDate : null)
    };
    return item;
};

signupRequestSchema.statics.getById = function(id, callback) {
    var query = { _id: id, _k: null };
    signupRequestSchema.statics.validateItem(query, function(err){
        if (err){ return callback(err); }
        return this.findOne(query, function(err, item){
            if (err){ return callback(err); }
            return signupRequestSchema.statics.toDto(item);
        });
    });
};

signupRequestSchema.statics.getOneByEmailAddress = function(emailAddress, callback) {
    var query = {
        emailAddress: emailAddress,
        _k: null
    };
    signupRequestSchema.statics.validateItem(query, function(err){
        if (err){ return callback(err); }
        return this.findOne(query, function(err, item){
            if (err){ return callback(err); }
            return signupRequestSchema.statics.toDto(item);
        });
    });
};

signupRequestSchema.statics.create = function(emailAddress, confirmationCode, callback) {
    var data = {
        emailAddress: emailAddress,
        confirmationCode: confirmationCode,
        _k: null
    };
    signupRequestSchema.statics.validateItem(query, function(err){
        if (err){ return callback(err); }
        var newItem = new this(data);
        newItem.save(function(err, item){
            if (err){ return callback(err); }
            return signupRequestSchema.statics.toDto(item);
        });
    });
};

signupRequestSchema.statics.init = function(emailAddress, confirmationCode, suppressError, callback) {
    var dupErr = suppressError ? null : (new Error('Duplicate Signup Request detected'));
    signupRequestSchema.statics.getOneByEmailAddress(emailAddress, function(err, existing){
        if (err){ return callback(err); }
        if (existing){ return callback(dupErr, existing); }
        signupRequestSchema.statics.create(emailAddress, confirmationCode, callback);
    });
};

signupRequestSchema.statics.modify = function(queryDto, updateDto, callback) {
    if (!updateDto){ return callback(new Error('Nothing to update')); }
    signupRequestSchema.statics.toItem(queryDto, function(err, query){
        if (err){ return callback(new Error('Problem with query: ' + err.message)); }
        signupRequestSchema.statics.toItem(updateDto, function(err, query){
            if (err){ return callback(new Error('Problem with update: ' + err.message)); }
            return this.findAndUpdate(query, update, function(err, items){
                if (err){ return callback(err); }
                return signupRequestSchema.statics.toDtos(items);
            });
        });
    });
};

signupRequestSchema.statics.modifyOne = function(queryDto, updateDto, callback) {
    if (!updateDto){ return callback(new Error('Nothing to update')); }
    signupRequestSchema.statics.toItem(queryDto, function(err, query){
        if (err){ return callback(new Error('Problem with query: ' + err.message)); }
        signupRequestSchema.statics.toItem(updateDto, function(err, query){
            if (err){ return callback(new Error('Problem with update: ' + err.message)); }
            return this.findOneAndUpdate(query, update, function(err, item){
                if (err){ return callback(err); }
                return signupRequestSchema.statics.toDto(item);
            });
        });
    });
};

signupRequestSchema.statics.delete = function(queryDto, callback) {
    var updateDto = { auditDeletedDate: moment().utc().toDate() };
    signupRequestSchema.statics.modify(queryDto, updateDto, callback);
};

signupRequestSchema.statics.deleteOne = function(queryDto, callback) {
    var updateDto = { auditDeletedDate: moment().utc().toDate() };
    signupRequestSchema.statics.modifyOne(queryDto, updateDto, callback);
};

var model = mongoose.model('SignupRequest', signupRequestSchema);

module.exports = model;
&#13;
&#13;
&#13;

0 个答案:

没有答案