nodejs MODEL 레이 어 패키지 (2)

1616 단어 Parse
저자: 단 공 방 기술 부 - 진문 철
'nodejs MODEL 레이 어 링 (1)' 의 패 키 지 를 통 해 MODEL 레이 어 는 간결 해 졌 지만 모든 MODEL 레이 어 링 방법 이 같 기 때문에 다음 패 키 지 는 같은 코드 를 모두 BaseMODEL 에 패키지 한 다음 에 사용자 정의 MODEL '계승' 을 BaseMODEL 에서 하면 된다.
MODEL 디 렉 터 리 에 BaseMODEL. js 를 새로 만 들 었 습 니 다. 코드 는 다음 과 같 습 니 다.
exports.statics = {
    //KeyValue   JSON  ,          
    findByKeyValue : function( KeyValue, data ) {
        return this.find(KeyValue).exec( data );
    },
    Find : function( query, data ) {
        if(!query.limit){
            query.limit = 1000;
        }
        if(!query.order){
            query.order = "-createdAt";
        }
        if(!query.include){
            query.include = "";
        }
        return this.find(query.where).populate(query.include).limit(query.limit).sort(query.order).exec( data );
    },
    Update : function( query,data){
        query.data.updatedAt = Date.now();  //      ,     updatedAt  
        return this.update({_id:query._id},query.data).exec(data);
    }
};

그리고 MODEL 코드 를 바 꿀 수 있 습 니 다.
var Schema = require('mongoose').Schema;

var MODEL = Schema({
    Advertiser: {type:Schema.Types.ObjectId,ref:'UserMODEL'},
    Name: String,  //    
    Intro: String,
    createdAt:{
        type:Date,
        default:Date.now()
    },
    updatedAt:{
        type:Date,
        default:Date.now()
    }
}, {collection: "Plan"});

var BaseMODEL = require("./BaseMODEL");
MODEL.statics = BaseMODEL.statics;

/* global db */
module.exports = db.model('PlanMODEL', MODEL);

좋은 웹페이지 즐겨찾기