1

I want to create a system where multiple contracts can be created belonging to different users.

In Django, Ruby, etc., I would create a model Contract with field user = models.ForeignKey(settings.AUTH_USER_MODEL) but I don't know how to do this in Meteor.

Is it better to make a schema with

Schema.User = new SimpleSchema({
  contracts: {
    type: [Object],
  },
  "contracts.$.start_date": {
    type: Date,
  },
  "contracts.$.end_date": {
    type: Date,
  },
  "contracts.$.salary": {
    type: Number,
  }
});

or something like that? And then use meteor-autoform to create these? It seems very difficult to make objects relational in Meteor.

Jamgreen
  • 10,329
  • 29
  • 113
  • 224

1 Answers1

0

MongoDB isn't a relational database and so you'll need to manually handle relationships yourself (using multiple queries). Traditionally a Mongo document would use embedding whenever possible and use separate collections when necessary. See here for more information:

MongoDB relationships: embed or reference?

However, Meteor throws a spanner in the works since the publish model can only publish top-level documents, and there's little support for document hierarchies when writing templates etc.

Therefore the normal approach under Meteor is to create table for each collection and to have a records refer to other records using an ID:

Schema.User = new SimpleSchema({
  contracts: {
    type: [String],
  },
  ...
});

Schema.Contract = new SimpleSchema({
  user: {
    type: String,
    index: true
  },
  ...
});

Although this will result in you having to do multiple queries, the structure will work very will with Meteor's design philosophies.

Community
  • 1
  • 1
tarmes
  • 15,366
  • 10
  • 53
  • 87