Book Image

Learning Azure DocumentDB

By : Riccardo Becker
Book Image

Learning Azure DocumentDB

By: Riccardo Becker

Overview of this book

Learning DocumentDB adopts a practical, step-by-step approach to help you learn the basics of DocumentDB and use your new-found abilities in real-life scenarios and enterprise solutions. We start with the absolute basics, such as setting up a DocumentDB environment, and guide you through managing your databases, and executing simple and complex queries. Next, we explain how to work with DocumentDB using the open REST protocol, and demonstrate how JavaScript works with DocumentDB. We’ll also show you how to authenticate and execute queries. Moving on, you’ll find out how to use DocumentDB from within Node.js to kick-start your Node.js projects. Next, you’ll discover how to increase the performance of your DocumentDB database and fine-tune it. Finally, you’ll get to grips with using DocumentDB in conjunction with other services offered from the Microsoft Azure platform.
Table of Contents (15 chapters)
Learning Azure DocumentDB
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Building triggers


A trigger is application logic or a business rule that is written in JavaScript. It looks similar to a function or method, but a trigger is only executed by the database engine when a document gets inserted, replaced, or deleted. Triggers can be useful, for example, to create an audit trail to keep track of all the changes in the system or to set default values for a document to be created.

The example trigger is a trigger that is fired before the actual insert (although triggers and their actual insert, replace, or delete actions on a document are inside one transaction) and checks for the first name. If it is not there or empty, it will set a default value. To test the trigger, we will also create a new property on the document affected.

The triggers get the property FirstName from the document involved (that is retrieved by request.GetBody()). If the property is not there or empty, it will set this FirstName property with the value TheDefaultFirstName. Additionally, a...