Adding our first schema
Firstly let's add the Collection2 package to our app:
meteor add aldeed:collection2
Now we want our schema to be accessible to both the server and client (for form valiation), so we'll add the schema definitions in both/schemas/
. Let's start with our simplest collection; the "Teams" collection.
both/schemas/teams.js
Teams.attachSchema(new SimpleSchema({
name: {
type: String
},
ownerId: {
type: String
},
gameIds: {
type: [String],
optional: true
}
}));
Here we specify that each team must have a name
and an ownerId
attribute; both of the type String
. We also allow for an optional games
attribute, which takes an Array
, with each object in the array being of the type String
(eg. ['x', 'y', 'z']
).
We can test this schema using the Chrome console. Try typing the following into your console:
Teams.insert({name: 'Manchester', ownerId: Meteor.userId()})
It should work correctly. Okay, let's challenge our schema by not adding an ownerId
:
Teams.insert({name: 'Athletic'})
Here we get an error insert failed: Error: Owner is required
. Thankfully, our schema seems to be working. We can test the gameIds
by adding a new game and checking that gameIds
has updated properly. You can check the gameIds
on the client in Mongol with ctrl-m
.