Eve: Building RESTful APIs with MongoDB and Flask Transcripts
Chapter: Your first Eve service
Lecture: Defining document schemas

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We want to define a schema against which every single document coming into our people endpoint will be validated. So let's go back to our code editor.
0:12 In Eve, schemas are Python dictionaries. Every key in the dictionary is actually a field name and the values are dictionaries themselves.
0:27 Let's define the type of this field. I did a string and let's also add last name field, again of type string.
0:41 Now that we have a definition for our endpoint let's add it to our people endpoint configuration. The schema is this one.
0:54 So we're telling Eve that our domain is composed of just one people endpoint and the schema for the endpoint is this one, we have two fields,
1:05 first name, last name, both of type string. So every document coming into our API at the people endpoint, will be validated against this schema.
1:16 We need to save our file and relaunch the app. Now let's go back to our client and try to store our John here. And here we go, the status is created,
1:34 and we get a number of metafield back, so the data it was created and updated, which are, off course, the same, the etag for the document,
1:43 the unique id, the link to the document itself, and the status, which is ok. So now that the document has been created,
1:51 we can try and go back and perform our send request to the people endpoint and what we get back is an array with one document inside it, the meta node,
2:03 as you can see, we have page one and one document total. So let's open our document and we see that it is indeed our John, as expected. Success!

Talk Python's Mastodon Michael Kennedy's Mastodon