Introducing New Features in Ampt's Data Interface
These powerful new features are designed to bring more control over how you add, update, and protect data in your Ampt apps.
- Jeremy Daly
Today we're introducing a series of powerful new features designed to bring you more control over how you add, update, and protect data in your Ampt apps. Backed by Amazon DynamoDB, Ampt's Data Interface provides a fast, flexible, and scalable NoSQL datastore integrated into every Ampt Environment. There's no configuration or provisioning necessary. With Ampt, access to your data is just there, freeing you up to focus on solving business problems instead of setting up and scaling a database.
Our ongoing mission to streamline the data interaction experience remains unchanged, so we're excited to extend its flexibility to address more complex use cases. These new features boost your control over data operations, improve data integrity, and cater to a wider array of specialized scenarios.
Existence Checks
The introduction of the exists
flag facilitates conditional checks before adding or updating an item. By default, the set
method performs UPSERTs – creating an item if it doesn't exist or updating it if it does. With this new feature, you can explicitly dictate the operation based on the existence of an item.
To add an item ONLY if it DOES NOT EXIST:
javascriptawait data.set('myKey', 'someValue', { exists: false });
And to update an item ONLY if it DOES EXIST:
javascriptawait data.set('myKey', { foo: 'bar' }, { exists: true });
Use cases
Existence checks add additional power that can be used in a variety of scenarios where conditional operations are required. Here are a few examples:
User Registration: When a new user tries to sign up, it's important to ensure that the username or email isn't already taken. Previously, you would have needed to look up the user account first to see if it exists. Now, you can use exists: false
when adding the new user, which will fail if the user already exists, preventing duplicate accounts and extraneous queries.
javascriptawait data.set('username', newUser, { exists: false });
Updating User Profiles: Let's say you have a profile page where users can update their information. When a user submits changes, you want to make sure the user account exists before attempting to update it. Here, exists: true
can be used to verify the user's existence and, if not, the operation fails, preventing the creation of unintended new data.
javascriptawait data.set('username', updatedUserInfo, { exists: true });
Inventory Management: In an ecommerce application, when adding a new product to the inventory, you'd use exists: false
to ensure the product isn't already present in the database to avoid overwriting an existing item.
Setting Default Values and Labels
The ability to set default values is now at your fingertips! Defaults are used to update an item's value conditionally if it doesn't exist, which is especially useful when working with complex objects.
Here's an example:
javascriptawait data.set( 'myKey', { key1: 'myValue' }, { default: { key2: 'defaultVal2' }, } );
In this scenario, key1
is overwritten with myValue
whether it exists or not, however, key2
is written ONLY if the attribute didn't pre-exist.
We're also extending this default functionality to item "labels". This allows you to overwrite a label's value only if it isn't already set on an item. Here's how:
javascriptawait data.set('myKey', 'myValue', { label1: 'someLabel', // overwrite label1 label2: ['defaultLabel'], // overwrite if label2 doesn't exist });
Use cases
The default values feature in Ampt's Data Interface can be extremely useful in scenarios where you want to ensure certain data fields have a value, even if none is explicitly provided during the creation or update of an item. Plus, labels let you ensure existing fields aren't overwritten if they already exist, giving you fine-grained control over data updates. Here are a few examples:
User Registration: When a new user signs up, there might be some fields like userStatus
or userRole
that should have a default value even if not specified by the user. For instance, you might want all new users to have a userStatus
of "Active" or a userRole
of "Standard User".
javascriptawait data.set('username', newUser, { default: { userStatus: 'Active', userRole: 'Standard User' }, });
Content Publishing: In a content management system (CMS), when a new article is created, you may want to set a default status
to "Draft" if no status
is specified, or default the viewCount
to 0.
javascriptawait data.set('articleId', newArticle, { default: { status: 'Draft', viewCount: 0 }, });
Product Inventory Management: In an ecommerce application, when adding a new product, certain fields might need to have default values. For example, stockLevel
might default to 0, or productStatus
to "Available".
Setting Default Labels: This is particularly useful for categorization or sorting. For example, you may want to default an urgency
label to "low" on a task management platform, or category
label to "general" on a blog post if none is provided by the user.
javascriptawait data.set('myKey', 'myValue', { label1: 'someLabel', label2: ['defaultLabel'], });
The ability to set default values ensures that your data remains consistent and predictable, which can significantly improve the stability and reliability of your application.
Advanced Options
To give you additional control over your data manipulation, we've added two new advanced features:
Created Timestamp Integrity Check: Maintain the original created
timestamp while overwriting an item by setting the created
value in the "options object" of the set method. This safeguards the integrity of your item's creation date when completely replacing an item.
Storing Null Values: While our default behavior eliminates null
and undefined
values to minimize unnecessary data storage, the new removeNulls: false
flag allows you to store null
values should your use case demand it. You can set this as a global option too!
javascriptimport { data } from '@ampt/data'; data.removeNulls = false;
What's next?
Our latest enhancements are designed to make your experience with Ampt's Data Interface even more versatile. You can learn more about these new features, and the rest of the Data interface's capabilities, by visiting our comprehensive documentation. We look forward to your feedback and are determined to keep refining our offerings to help you get things done faster in the cloud. Stay tuned for more updates!
Sign up to start building now and be sure to join our community on Discord to see what other amazing developers are building with Ampt! We can't wait to see the amazing things you'll create with these new features!