2017-12-21 13 views
0

私はFeathers.jsのMongooseを使用しており、サービスによって変更できないフィールドを作成したいと考えています。サービスからの変更を防止する方法

// account-model.js - A mongoose model 
// 
// See http://mongoosejs.com/docs/models.html 
// for more of what you can do here. 
const mongoose = require('mongoose'); 
require('mongoose-type-email'); 

module.exports = function(app) { 
    const mongooseClient = app.get('mongooseClient'); 

    const recovery = new mongooseClient.Schema({ 
    token: { type: String, required: true } 
    }, { 
    timestamps: true 
    }); 

    const account = new mongooseClient.Schema({ 
    firstName: { type: String, required: true }, 
    surname: { type: String, require: true }, 
    phone: { type: String, require: true }, 
    email: { type: mongoose.SchemaTypes.Email, required: true, unique: true }, 
    birth: { type: Date, required: true }, 
    gender: { type: String, required: true }, 
    country: { type: String, required: true }, 
    address: { type: String, required: true }, 
    address2: { type: String, required: false }, 
    city: { type: String, required: true }, 
    postcode: { type: String, required: true }, 
    password: { type: String, required: true }, 
    status: { type: String, required true } 
    }, { 
    timestamps: true 
    }); 

    return mongooseClient.model('account', account); 
}; 

/account/<id>でポストをしないとフィールドstatusを変更することができます。 このフィールドは、内部でのみ変更する必要があります。いくつかの承認サービス要求。

この動作はどのように実装できますか?

答えて

1

これはFeathers hooksの完璧な使用例です。外部からアクセスすると、service methodコールにparams.providerので、あなたがそれをチェックし、それがある場合dataからフィールドを削除することができます設定されます。

module.exports = function() { 
    return async context => { 
    if(context.params.provider) { 
     delete context.data.status; 
    } 
    } 
} 

このフックはcreateupdatepatch方法についてbeforeフックになります。

関連する問題