简体   繁体   English

在Mongoose模型中访问Express.js请求上下文

[英]Accessing Express.js request context in Mongoose models

I am developing a REST API with my colleagues using Express.js and Mongoose . 我正在与同事一起使用Express.jsMongoose开发REST API。

For some of the Mongoose Model methods and statics we need the Express.js Request object as an added context information source, like the authenticated user currently doing the request. 对于某些Mongoose Model methodsstatics我们需要Express.js Request对象作为添加的上下文信息源,就像当前正在执行请求的经过身份验证的用户一样。

What we currently do is attach a custom property called context to a Model 's this context by prototyping Mongoose 's Model function. 我们现在要做的就是连接称为自定义属性contextModelthis通过原型背景MongooseModel作用。

The issue is that we ran into a nasty bug that made us realise that while with Model.methods , who are called on Model instances, it works fine. 问题是我们遇到了一个令人讨厌的错误,让我们意识到,在使用Model.methods ,在Model实例上调用它时,它运行正常。

For Model.statics , who are called on the Model "class" which is implemented as a singleton though, the scope is shared by the whole app meaning that if you have concurrent requests, they will override each-other's this.context value if you have any async behaviour in the request processing. 对于在Model “类”上调用的Model.statics ,它实现为singleton但整个应用程序共享范围,这意味着如果你有并发请求,它们将覆盖彼此的this.context值,如果你在请求处理中有任何异步行为。

Now my question is the following: 现在我的问题如下:

  • What are the best practices with Express.js and Mongoose for keeping and accessing the request context in Model.statics and Model.methods functions ? Express.jsMongooseModel.staticsModel.methods函数中保留和访问request上下文的最佳实践是什么?

Of course there are some possible hacks to simulate containment like: 当然有一些可能的黑客来模拟遏制像:

  1. Function.bind() every Model function to add the request context. Function.bind()每个Model函数添加请求上下文。

    -> This would mean though that if Model.statics.a() internally makes a call to Model.statics.b() , function b() wouldn't have a this.context . - >这意味着如果Model.statics.a()内部调用Model.statics.b() ,则函数b()将没有this.context

  2. Use the with() {} statement on the request object and call every Model function within that scope. request对象上使用with() {}语句并调用该范围内的每个Model函数。

    -> Which would be pretty dirty and slow. - >哪个会很脏又慢。

  3. Create containers with separate scopes with the vm standard Node.js module. 使用vm标准Node.js模块创建具有单独范围的容器。

    -> Same as (2), performance would take a big hit. - >与(2)相同,表现会受到重创。

Any ideas ? 有任何想法吗 ? Thank you in advance ! 先感谢您 !

Hope you are still interested in the answer 希望你仍然对答案感兴趣

What you really want is to mimic java's thread local concept which basically allows you to define something in the context of that thread. 你真正想要的是模仿java的线程本地概念 ,它基本上允许你在该线程的上下文中定义一些东西。

Node is a single threaded environment and it does not provide anything like that but domains . Node是一个单线程环境,除了域之外 ,它不提供任何类似的东西。 Domains are very useful in providing context for particular set of operations and mimicking thread local storage. 域在为特定操作集提供上下文和模仿线程本地存储方面非常有用。

Node js domains will soon be deprecated but similar solutions like continuation-local-storage are available in npm which will solve your purpose Node js域很快就会被弃用,但是类似于continuation-local-storage的解决方案可以在npm中使用,这将解决您的目的

Eg: Create domain in the context of request 例如:在请求的上下文中创建域

app.use(function(req, res, next) {
    var reqDomain = domain.create();
    reqDomain.run(next);            //here your request context is created
});

Now with the use of process.domain, I will be able to access 'data' bound to above created domain anywhere 现在通过使用process.domain,我将能够访问绑定到上面创建的域的“数据”

var d = process.domain              //process.domain is always be available to you while serving the request
d.obj = {};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM