如何防止服务发生变化
How to prevent changes from services
我将 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>
上创建 post 并更改字段 status
。
此字段应仅在内部更改。当一些批准服务请求时。
我该如何实现这种行为?
这是 Feathers hooks. When accessed externally, in a service method call params.provider 的完美用例,您可以检查它并从 data
中删除该字段,如果它是:
module.exports = function() {
return async context => {
if(context.params.provider) {
delete context.data.status;
}
}
}
此挂钩将是 create
、update
和 patch
方法的 before
挂钩。
我将 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>
上创建 post 并更改字段 status
。
此字段应仅在内部更改。当一些批准服务请求时。
我该如何实现这种行为?
这是 Feathers hooks. When accessed externally, in a service method call params.provider 的完美用例,您可以检查它并从 data
中删除该字段,如果它是:
module.exports = function() {
return async context => {
if(context.params.provider) {
delete context.data.status;
}
}
}
此挂钩将是 create
、update
和 patch
方法的 before
挂钩。