【问题标题】:Simplest way to build a relationship between two collections in Meteor?在 Meteor 中的两个集合之间建立关系的最简单方法?
【发布时间】:2015-02-18 12:05:26
【问题描述】:

我是 Meteor 的新手,正在构建我的第一个应用程序。这是一款鸡尾酒配方应用,包含一系列饮品和一系列食材。

每种饮料都可以有多种成分。对于饮料中的每种成分,都会有一个数量(数字)和一个计量单位(字符串)。

我隐约怀疑,对于这个用例,像 publish-with-relations 包这样的东西可能有点过头了,但我不清楚创建和填充第三个集合的最佳方法,该集合将容纳饮料之间的这种关系和成分。

我的饮料系列如下所示:

Drinks = new Mongo.Collection('drinks');

Drinks.allow({
  update: function(userId, drink) { return ownsDocument(userId, drink); },
  remove: function(userId, drink) { return ownsDocument(userId, drink); }
});

Drinks.deny({
  update: function(userId, drink, fieldNames) {
    //only allow editing these fields:
    return (_.without(fieldNames, 'drinkName', 'drinkIngredients').length > 0);
  }
});

Meteor.methods({
  drinkInsert: function(drinkAttributes) {
    check(Meteor.userId(), String);
    check(drinkAttributes, {
      drinkName: String,
      drinkIngredients: Array,
      drinkDescription: String,
      drinkInstructions: String
    });

    var errors = validateDrink(drinkAttributes);

    if (errors.drinkName || errors.drinkIngredients)
      throw new Meteor.Error('invalid-drink', "You must enter a drink name and ingredients.");

    var drinkWithSameName = Drinks.findOne({drinkName: drinkAttributes.drinkName});
    if (drinkWithSameName) {
      return {
        drinkExists: true,
        _id: drinkWithSameName._id
      }
    }

    var user = Meteor.user();

    var drink = _.extend(drinkAttributes, {
      userId: user._id,
      author: user.username,
      submitted: new Date(),
      commentsCount: 0,
      upvoters: [],
      votes: 0
    });

    var drinkId = Drinks.insert(drink);

    return {
      _id: drinkId
    };
  },

  upvote: function(drinkId) {
    check(this.userId, String);
    check(drinkId, String);

    var affected = Drinks.update({
      _id: drinkId,
      upvoters: {$ne: this.userId}
    }, {
      $addToSet: {upvoters: this.userId},
      $inc: {votes: 1}
    });
    if (! affected)
      throw new Meteor.Error('invalid', "Vote not counted.");
  }
});

validateDrink = function (drink) {
  var errors = {};

  if (!drink.drinkName)
    errors.drinkName = "Please name your drink.";

  if (!drink.drinkIngredients)
    errors.drinkIngredients = "Please add some ingredients.";

  if (!drink.drinkDescription)
    errors.drinkDescription = "Please write a brief description of your drink.";

  if (!drink.drinkInstructions)
    errors.drinkInstructions = "Please include step-by-step instructions for mixing this drink.";

  return errors;
}

这是我的配料集合:

Ingredients = new Mongo.Collection('ingredients');

Meteor.methods({
  ingredientInsert: function(ingredientAttributes) {
    check(this.userId, String);
    check(ingredientAttributes, {
      ingredientName: String
    });

    var user = Meteor.user();

    ingredient = _.extend(ingredientAttributes, {
      userId: user._id,
      author: user.username,
      submitted: new Date()
    });

    //create the ingredient, save the id
    ingredient._id = Ingredients.insert(ingredient);

    return ingredient._id;
  }
});

我的想法是这种关系应该存在于一个名为drinkIngredients 的集合中,但我不清楚在Meteor 中设置该集合的最佳方式。我很感激这方面的一些指导。或者,如果我只是通过使用发布关系来让自己变得更难,请告诉我。

非常感谢。

【问题讨论】:

    标签: meteor


    【解决方案1】:

    不幸的是,添加更多集合将无济于事,除非您做一些真正多余的事情,例如将两个集合非规范化为一组文档。我的建议是选择here 描述的路径之一。

    请注意,在您的情况下,您实际上不能使用 PWR,因为它不会通过数组进行响应式连接。我建议使用“加入客户端”技术。这与我们目前在 Edthena 遵循的模式相同 - 尽可能使用 PWR 在服务器上加入,但在存在数组关系时加入客户端(使用路由器)。

    有关更多详细信息,另请参阅以下相关问题:

    【讨论】:

      猜你喜欢
      • 2021-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多