我们有不止一个网站指向同一个 MongoDB。例如面向前端的公共网站、内部管理网站等。
我们希望为不同的网站收集不同的用户。在使用 Meteor.users 变量访问用户集合时,有什么方法可以指示 Meteor 在实际数据库中使用不同的集合名称。
我们有不止一个网站指向同一个 MongoDB。例如面向前端的公共网站、内部管理网站等。
我们希望为不同的网站收集不同的用户。在使用 Meteor.users 变量访问用户集合时,有什么方法可以指示 Meteor 在实际数据库中使用不同的集合名称。
从源代码来看,集合名称似乎是硬编码在accounts-base
包中的。我没有看到任何通过代码设置名称的选项。
Meteor.users = new Mongo.Collection("users", {
_preventAutopublish: true,
connection: Meteor.isClient ? Accounts.connection : Meteor.connection
});
不,遗憾的是,这是硬编码到包中的,正如布赖恩所说,包没有提供定制的空间。
但是,您可以非常轻松地为集合accountType
中的每个文档添加一个新键。可以指定该用户是属于面向前端的公共网站,还是属于内部管理网站。Meteor.users
accountType
例如,用户文档:
{
username: "Pavan"
accountType: "administrator"
// other fields below
}
当然,您可以从那里发布特定数据,或根据accountType
价值启用网站的不同部分。
例如,如果我希望管理员能够订阅并查看所有用户信息:
Meteor.publish("userData", function() {
if (this.userId) {
if (Meteor.users.find(this.userId).accountType === "admin") {
return Meteor.users.find();
} else {
return Meteor.users.find(this.userId);
}
} else {
this.ready();
}
});
这没有经过测试,但乍一看,这可能是更改用户集合名称的可行方法。将此代码放在 /lib 文件夹中的某个位置:
Accounts.users = new Mongo.Collection("another_users_collection", {
_preventAutopublish: true,
});
Meteor.users = Accounts.users;