0
_id
을 암호화하고 복원 기반 API를 위해 id
으로 반환하기 위해 몽구스 기본 스키마를 만들었습니다. 다음 스키마에서 다음몽구스 기본 스키마에서 findAll() _id 또는 id param을 조정하는 방법
BaseSchema =() ->
Schema.apply @, arguments
# Ensure virtual fields are serialised.
@set 'toJSON', {
virtuals: true
}
@set 'toObject', {
virtuals: true
}
# transform our JSON so its friendly for our API
@options.toJSON.transform = (doc, ret, options) ->
# remove _id and __v so we dont expose it in the API
delete ret._id
delete ret.__v
return ret
# encrypted _id in id using a virtual
@virtual('id')
.get ->
id = hashids.encryptHex this._id.toHexString()
return id
.set (id) ->
console.log 'Setting', id
@_id = mongoose.Types.ObjectId hashids.decryptHex(id)
return
# some default and consistent schema fields
@add {
# when the document was created
createdAt: {
type: Date,
default: Date.now
},
# when the document was modified
modifiedAt: {
type: Date,
default: Date.now
}
}
그리고 :
server.use (req, res, next) ->
if req.params.id
req.params.id = hashids.decryptHex req.params.id
next()
:
PersonSchema = new BaseSchema {}, { id: false }
가 나는 다음과 같은 restify 미들웨어를 만든 API의 쿼리 호출에받은 ID를 변환하는 문제를 완화하려면
이것은 효과가 있지만 나는 이것에 만족하지 않습니다.
어딘가에 기본 스키마의 _id ObjectID에 대한 ID와 해독의 암호 해독을 처리하는 방법이 있는지 알고 싶습니다. 나는 할 수 있도록하고 싶습니다 :
server.get '/interfaces/:id', (req, res, next) ->
# get the interface based on id
iface = Interface.findOne { _id: req.params.id } , (err, iface) ->
res.send iface
next()
_id가 findOne {}
쿼리의 일부이며 실제 쿼리를 실행하기 전에 그래서 번역되어야한다.
이렇게하면 논리가 정중하게 집중됩니다.
감사합니다. 당신은 쿼리를 수행하기 전에 OBJECTID에 16 진수 문자열로 변환하여 MongoDB의 ObjectID을 사용할 수 있습니다
내 경로 모두 그 일을하지 않도록하고 싶습니다. 많은 사람들이있을 것입니다. 미들웨어를 사용하여 동일한 결과를 얻을 수 있지만 기본 스키마에 모두 포함됩니다. 여기 가능한 일이 있기를 바라는 것입니다 :) –