2015-01-20 2 views
0

나는 다음과 같은 몽구스 모델이 있습니다방법 및 정적 : 액세스 "필드"

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 
var addressDefinition = require('./addressPersistenceModel'); 

var UserEntityModel = new Schema({ 
    firstname: String, 
    lastname: String, 
    address: addressDefinition 
}); 
mongoose.model('User', UserEntityModel); 

주소의 정의가 그런 식으로 이루어집니다 :

module.exports = { 
    street: String, 
    ... 
}; 

내가 재사용의 이유로이 작업을 수행합니다.

그리고 내 비즈니스 로직에

나는이 작업을 수행 :

UserBusinessLogic.prototype.create = function(inputModel, callback) { 
    var user = new UserPersistenceModel(); 
    user.firstname = inputModel.firstname; 
    user.lastname = inputModel.lastname; 
    ... 

    user.save(function(error) { 
     ... 
     } 
    }); 
}; 

대신 비즈니스 로직에서 모델로 내 입력에서 모든 값을 할당하는, 내 모델에 입력 모델을 통과하고 싶습니다 (

var user = new UserPersistenceModel(inputModel); 

입력의 모든 값을 읽고 내 모델의 "필드"에 할당해야합니다.

이렇게하려면 방법 및/또는 통계에 대해 생각했습니다. 내가 아는 한, "인스턴스 레벨"(하나의 문서를 저장하고 싶음)에서 작업 할 때 메소드를 사용해야한다. 맞습니까? 내 방법은 어떻게 보이나요? 거기에있는 들판에 접근하는 방법을 모르겠습니다.

업데이트

이 내 UserCreateInputModel는 모습입니다 같은 :

var Address = require('../address'); 

var UserCreateInputModel = function(req) { 
    this.alias = req.param('alias'); 
    this.email = req.param('email'); 
    this.firstName = req.param('firstName'); 
    this.lastName = req.param('lastName'); 
    this.password = req.param('password'); 
    this.address = new Address(req); 
}; 
module.exports = UserCreateInputModel; 

그리고이 주소가 모습입니다 같은 :

var Address = function(req, persistenceModel) {  
    if(req !== null && req !== undefined) { 
     this.city = req.param('city'); 
     this.country = req.param('country'); 
     this.state = req.param('state'); 
     this.street = req.param('street'); 
     this.zipCode = req.param('zipCode'); 
    } 

    if(persistenceModel !== null && persistenceModel !== undefined) { 
     this.city = persistenceModel.city; 
     this.country = persistenceModel.country; 
     this.state = persistenceModel.state; 
     this.street = persistenceModel.street; 
     this.zipCode = persistenceModel.zipCode; 
    } 
}; 

module.exports = 주소;

+1

자바 스크립트 객체를 인스턴스화 할 때 몽구스 모델에 전달하면 속성이 자동으로 할당됩니다. –

답변

1

이미 몽구스 User 모델이 있습니다. 당신은 당신의 비즈니스 로직에 대한 필요가 무엇 :

// assuming you're doing a 'module.exports = mongoose.model('User', UserEntityModel);' in your schema file 
var UserPersistenceModel = require('./your_user_schema_file.js'); 

UserBusinessLogic.prototype.create = function(inputModel, callback) { 

    // if your inputModel passed to this function is a javascript object like: 
    // { 
    // firstName: "First", 
    // lastName: "Last", 
    // ... 
    // } 
    var user = new UserPersistenceModel(inputModel); 
    ... 

    user.save(function(error) { 
    ... 
    }); 
}; 

당신은 잘못 주소 모델을 참조하고

업데이트되었습니다. 당신은 심지어이 필요하지 않아도

var UserEntityModel = new Schema({ 
    firstname: String, 
    lastname: String, 
    address: { type: Schema.Types.ObjectId, ref: 'Address' } 
}); 

:

당신이 당신의 Adress 모델의 끝에 module.exports = mongoose.model('Address', AddressEntityModel); 라인을 가지고 있다고 가정하면, 이것은 당신이 당신의 User 모델을 참조 할 수있는 방법입니다 주소 모델 파일.

몽구스는 참조 된 개체의 id 만 저장합니다. 따라서 참조 할 수있는 주소에서 변경할 수있는 유일한 속성은 id입니다 (예 : 참조 정리 또는 다른 주소로 지정).

+0

감사합니다. 모델의 생성자에 객체를 전달할 수 있다는 것을 알지 못했습니다. 그러나 내 모델 (업데이트 된 코드 참조) 안에 하나의 하위 문서를 사용하고 있으며이 메커니즘을 하위 문서에 대해 작동하지 않는 것처럼 보입니다. 어떤 생각? – mosquito87

+0

이 주소를 포함 된 문서 (참조 없음)로 사용하고 싶지만, 재사용을 위해 외부 파일에 정의를 보관하십시오. – mosquito87

+0

좋습니다. 이 경우 외부 몽구스 모델로 주소를 참조하지 않으므로 하나의 몽구스 스키마가 필요합니다. 이렇게하려면 'module.exports = {street : String, city : String, zipCode : Number}'와 같은 주소 논리를 내보내는 등의 작업을 수행하십시오. 그런 다음 새 스키마를 인스턴스화하지 않고 일반 자바 스크립트로 사용자 스키마에서이를 요구합니다. –