sails.js在模型中使用会话参数 [英] sails.js Use session param in model

查看:74
本文介绍了sails.js在模型中使用会话参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此问题的扩展.

在我的模型中,每个人都需要在创建时设置companyId,并且每个人都需要通过同一会话中持有的companyid过滤模型.

In my models, every one requires a companyId to be set on creation and every one requires models to be filtered by the same session held companyid.

使用 sails.js ,我已经阅读并理解该模型中的会话不可用,除非我使用控制器,但是这将需要我用非常非常重复的方式对所有控制器/动作进行编码.不幸的.

With sails.js, I have read and understand that session is not available in the model unless I inject it using the controller, however this would require me to code all my controller/actions with something very, very repetitive. Unfortunate.

我喜欢sails.js,并希望进行切换,但是有人可以向我描述一种更好的方法吗?我希望我只是错过了一些东西.

I like sails.js and want to make the switch, but can anyone describe to me a better way? I'm hoping I have just missed something.

推荐答案

因此,如果我对您的理解正确,则希望避免在控制器中使用大量这样的代码:

So, if I understand you correctly, you want to avoid lots of code like this in your controllers:

SomeModel.create({companyId: req.session.companyId, ...})
SomeModel.find({companyId: req.session.companyId, ...})

足够公平.也许您担心companyId将来会被重命名,或者需要进一步处理.如果使用自定义控制器操作,最简单的解决方案是为模型创建类方法,以将请求作为参数接受:

Fair enough. Maybe you're concerned that companyId will be renamed in the future, or need to be further processed. The simplest solution if you're using custom controller actions would be to make class methods for your models that accept the request as an argument:

SomeModel.doCreate(req, ...);
SomeModel.doFind(req, ...);

另一方面,如果您使用的是v0.10.x,并且可以将蓝图用于某些CRUD操作,则可以受益于

On the other hand, if you're on v0.10.x and you can use blueprints for some CRUD actions, you will benefit from the ability to override the blueprints with your own code, so that all of your creates and finds automatically use the companyId from the session.

如果您来自非Node背景,这可能会引起一些头疼. 为什么不让会议在任何地方都可以使用?"你可能会问. 就像他们在PHP中一样!"

If you're coming from a non-Node background, this might all induce some head-scratching. "Why can't you just make the session available everywhere?" you might ask. "LIKE THEY DO IN PHP!"

原因是PHP是无状态的-传入的每个请求本质上都是该应用的全新副本,而请求之间的内存共享却没有.这意味着任何全局变量将仅在单个请求的生命期内有效.奇妙的$_SESSION哈希值仅由您自己承担,一旦处理了请求,该哈希便消失了.

The reason is that PHP is stateless--every request that comes in gets essentially a fresh copy of the app, with nothing in memory being shared between requests. This means that any global variables will be valid for the life of a single request only. That wonderful $_SESSION hash is yours and yours alone, and once the request is processed, it disappears.

将其与Node应用程序进行对比,后者实际上是在单个进程中运行的.您设置的所有全局变量都将在传入的每个请求之间共享,并且由于请求是异步处理的,因此无法保证一个请求会在另一个请求开始之前完成.这样的情况很容易发生:

Contrast this with Node apps, which essentially run in a single process. Any global variables you set would be shared between every request that comes in, and since requests are handled asynchronously, there's no guarantee that one request will finish before another starts. So a scenario like this could easily occur:

  1. 请求A进入.
  2. Sails获取请求A的会话并将其存储在全局$_SESSION对象中.
  3. 请求A调用SomeModel.find(),异步调用数据库
  4. 尽管数据库发挥了魔力,但请求A放弃了对Node线程的控制
  5. 请求B进入.
  6. Sails获取请求B的会话并将其存储在全局$_SESSION对象中.
  7. 请求B放弃对线程的控制,以执行其他一些异步调用.
  8. 请求A返回其数据库调用的结果,并从$_SESSION对象读取某些内容.
  1. Request A comes in.
  2. Sails acquires the session for Request A and stores it in the global $_SESSION object.
  3. Request A calls SomeModel.find(), which calls out to a database asynchronously
  4. While the database does its magic, Request A surrenders its control of the Node thread
  5. Request B comes in.
  6. Sails acquires the session for Request B and stores it in the global $_SESSION object.
  7. Request B surrenders its control of the thread to do some other asynchronous call.
  8. Request A comes back with the result of its database call, and reads something from the $_SESSION object.

您可以在此处看到问题-请求A现在具有错误的会话数据.这就是为什么会话对象驻留在请求对象内以及为什么需要将其传递给任何想要使用它的代码的原因.太努力地避免这一点将不可避免地导致麻烦.

You can see the issue here--Request A now has the wrong session data. This is the reason why the session object lives inside the request object, and why it needs to be passed around to any code that wants to use it. Trying too hard to circumvent this will inevitably lead to trouble.

这篇关于sails.js在模型中使用会话参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆