🚀 White Paper: Fauna Architectural Overview - A distributed document-relational database delivered as a cloud API.
Download free
Fauna logo
Product
Solutions
Pricing
Resources
Company
Log InContact usStart for free
Fauna logo
Pricing
Customers
Log InContact usStart for free
© 0 Fauna, Inc. All Rights Reserved.

Related posts

Fauna Schema: Flexible, Enforceable, and Driven by Code with Zero downtime MigrationFauna Joins Google Cloud MarketplaceDatabase Joins for NoSQL

Start for free

Sign up and claim your forever-free Fauna account
Sign up and get started

Table of Contents

Computed Fields + Check Constraints

Elevating Schema as Code: Fauna Introduces Computed Fields and Check Constraints

Wyatt Wenzel & Bryan Reinero |Feb 1st, 2024|

Categories:

DevOpsFeaturesNews
We are excited to announce the general availability of two highly anticipated features that further improve the way developers create and manage their database schemas: Computed Fields and Check Constraints. These additions to Fauna’s Schema as Code features are designed to streamline the development of enterprise-grade applications, making it easier than ever to implement sophisticated business logic directly within your database. Computed Fields and Check Constraints further build on the first-class relational features offered in Fauna’s document-relational database – providing developers the power of a traditional relational database with the flexibility of NoSQL.

Computed Fields: Dynamic Data at Your Fingertips

Computed Fields are a powerful addition that allow the values of fields to be dynamically generated based on expressions defined by the user. This means you can now create fields whose values are calculated at the time of query, enabling dynamic composition of objects and field values. Whether you're calculating a customer's loyalty status or converting temperatures from Celsius to Fahrenheit, Computed Fields offer the flexibility to dynamically generate data in real-time. These fields can be indexed, providing even greater flexibility in your query patterns.
Computed Fields enable you to accurately define your true data model in the database, as they express the ‘has-a’, ‘has-many’ associations which historically had to be defined in mapping layers running outside of databases. Computed Fields allow you to define cross-collection joins and subqueries as an element of your schema design, so you can dynamically generate data, and object models in real-time. Users avoid the performance disadvantages inherent in a denormalization strategy required in other databases.
Example

Leveraging Computed Fields in an E-Commerce Application

Consider the case of modeling an e-commerce application. The application manages customer documents, each of which has a perpetually increasing history of orders. The requirement is to present customers with account information and a list of their most recent purchases within the same home page. Computed Fields satisfies this requirement, while persisting order objects to a separate collection. In this separate collection, new orders can be efficiently inserted and a customer's set of documents can grow without bound. Had the list of orders been instead statically nested within individual customer documents, it would result in data duplication, and having to update a larger document each time a new order was created. Additionally, Computed Fields enables users to easily adjust the size, composition, and order of the list with a simple update to a user’s FSL schema, rather than having to physically update every customer document with such change.

Check Constraints: Ensuring Data Integrity

Check Constraints are another crucial feature that enhance the robustness of your data model. By defining rules within your schema, Check Constraints ensure that data written to the database adheres to specific business logic, such as ensuring a bank account balance never falls below zero or a user's age falls within a reasonable range. This feature is especially valuable for applications that require strict data validation to maintain data integrity and enforce business rules directly at the data layer.
Example

Leveraging Check Constraints in an E-Commerce Application

Let's expand the earlier example e-commerce application by adding ‘marketplace’ functionality, where customers may order products from other customers. Each customer document has a "balance" field which defines the amount of money a customer has in their account. A customer must not be permitted to order any product which exceeds the amount in their "balance" field. This requirement is easily met by adding a Check Constraint to the Customer collection that prohibits any transaction from causing the buying customer's balance to fall to zero.

Check out the video walk-through and demo led by Head of Database Product Bryan Reneiro and Principal Database Advocate Kirk Kirkconnell for an exhaustive overview of these powerful new features.

Real-World Applications and Benefits

Consider a banking application where it's critical to prevent transactions that would result in a negative balance. With Check Constraints, you can define a rule that checks the account balance before completing a transaction, preventing operations that violate business logic. This not only reduces operational overhead by simplifying your application code, but also ensures consistency and integrity across your data model. Computed Fields and Check Constraints bring numerous advantages, including:
  • Reduced Application Overhead: By handling data validation and calculations within the database, application code becomes lighter and more efficient - eliminating the need for applications to reason about edge cases and reducing operational overhead. This ultimately makes Fauna a better fit for serverless architectures that are often delivered via HTTPS.
  • Enhanced Data Integrity: These features ensure that data within your database always meets the defined business rules and logic, preventing invalid data states.
  • Dynamic Data Generation: Computed Fields enables real-time data calculation, enabling more dynamic and responsive applications in categories like gaming or financial services.

Looking Forward

Computed Fields and Check Constraints not only make it easier to develop sophisticated applications, but also open new possibilities for data modeling and application design. We're excited to see how developers will leverage these new capabilities to build innovative and robust applications. Sign up for a Free Pro Trial account to try these new features for yourself. For more information on how to get started with Computed Fields and Check Constraints, check out the documentation here and here, and join the discussion on our community forums or discord.
We look forward to bringing even more features and improvements to our Schema as Code platform. Stay tuned for further updates and enhancements that will continue to make Fauna the go-to choice for developers seeking powerful, flexible, and easy-to-use database solutions.

If you enjoyed our blog, and want to work on systems and challenges related to globally distributed systems, and serverless databases, Fauna is hiring!

Share this post

TWITTERLINKEDIN
‹︁ PreviousNext ›︁

Subscribe to Fauna's newsletter

Get latest blog posts, development tips & tricks, and latest learning material delivered right to your inbox.