Skip to main content

Condition

Condition class represents a conditional that you can pass to Entity Manager methods and also to build more complex conditions in Query and Scan classes.

Condition is also a base class for Query and Scan, meaning that all methods listed here are also available in Query and Scan classes.

new Condition(Entity) or EntityManager.condition()

Every condition has to be initialized with Entity to infer its underlying properties. You can achieve this in two ways:

// Below definitions are equivalent
new Condition(User);
UserManager.condition();

User is an example class that extends Entity.

Condition.attribute(key)

This method prepares a new conditional expression. The key parameter is a string, narrowed down to flattened entity properties.

info

These properties are allowed for the User entity

UserManager.condition().attribute('partitionKey');
UserManager.condition().attribute('sortKey');
UserManager.condition().attribute('username');
UserManager.condition().attribute('age');
UserManager.condition().attribute('friends');
UserManager.condition().attribute('config');
UserManager.condition().attribute('config.isAdmin');
UserManager.condition().attribute('friends[1]');
UserManager.condition().attribute('friends[10]');
caution

Typescript error: Argument of type '"unknownProperty"' is not assignable to parameter of type: "username" | "partitionKey" | "sortKey" | "age" | "config" | "config.isAdmin" | friends | friends.${number}

UserManager.condition().attribute('unknownProperty');

To complete the conditional you need to use one of undermentioned functions.

UserManager.condition().attribute('username'); // This condition has no impact on the final conditional
UserManager.condition().attribute('username').eq('blazej'); // Adding comparison function (eq) after `attribute` method will complete the conditional. Resulting in: `username = blazej`

.eq(value)

This comparison function will check if the given key is equal to the value that is passed in as a parameter.

UserManager.condition().attribute('username').eq('blazej'); // Resulting in: `username = blazej`

.ne(value)

This comparison function will check if the given key is not equal to the value that is passed in as a parameter.

UserManager.condition().attribute('username').ne('blazej'); // Resulting in: `username <> blazej`

.lt(value)

This comparison function will check if the given key is less than the value that is passed in as a parameter.

UserManager.condition().attribute('username').lt('blazej'); // Resulting in: `username < blazej`

.le(value)

This comparison function will check if the given key is less than or equal to the value that is passed in as a parameter.

UserManager.condition().attribute('username').le('blazej'); // Resulting in: `username <= blazej`

.gt(value)

This comparison function will check if the given key is greater than the value that is passed in as a parameter.

UserManager.condition().attribute('username').gt('blazej'); // Resulting in: `username > blazej`

.ge(value)

This comparison function will check if the given key is greater than or equal to the value that is passed in as a parameter.

UserManager.condition().attribute('username').ge('blazej'); // Resulting in: `username >= blazej`

.beginsWith(value)

This comparison function will check if the given key is begins with the value that is passed in as a parameter.

UserManager.condition().attribute('username').beginsWith('bla'); // Resulting in: `begins_with(username, bla)`

.between(value1, value2)

This comparison function will check if the given key is between the values that were passed in as parameters.

UserManager.condition().attribute('age').between(21, 37); // Resulting in: `age BETWEEN 21 AND 37`

.contains(values)

This comparison function will check if the given key contains the value that is passed in as a parameter.

UserManager.condition().attribute('username').contains('blazej'); // Resulting in: `contains(username, blazej)`

.in(values)

This comparison function will check if the given key equals to any of the items that are passed in as parameters.

UserManager.condition().attribute('username').in(['blazej', 'david']); // Resulting in: `username IN blazej, david`

.type(value)

This comparison function will check if the given key is of a given type that is passed in as a parameter.

import { AttributeType } from 'dynamode/condition';

UserManager.condition().attribute('username').type(AttributeType.String); // Resulting in: `attribute_type(username, 'S')`

.exists()

This comparison function will check if the given key exists in the item.

UserManager.condition().attribute('username').exists(); // Resulting in: `attribute_exists(username)`

.size()

This function will compare the size of property under the given key. List of possible functions:

FunctionResult
.eq(value)'size(key) = value'
.ne(value)'size(key) <> value'
.lt(value)'size(key) < value'
.le(value)'size(key) <= value'
.gt(value)'size(key) > value'
.ge(value)'size(key) >= value'

.not()

This function use the opposite comparison for a given condition. List of possible functions:

FunctionWhat is usedResult
.eq(value).ne(value)'key <> value'
.ne(value).eq(value)'key = value'
.lt(value).ge(value)'key >= value'
.le(value).gt(value)'key > value'
.gt(value).le(value)'key <= value'
.ge(value).lt(value)'key < value'
.contains(value)N/A'NOT contains(key, value)'
.in(values)N/A'NOT (key IN value1, value2)'
.exists()N/A'attribute_not_exists(key)'
UserManager.condition().attribute('username').not().contains('blazej'); // Resulting in: `NOT contains(username, blazej)`
UserManager.condition().attribute('username').not().eq('blazej'); // Resulting in: `username <> blazej`

Condition.parenthesis(condition)

This method takes initialized Condition as a parameter and groups the inner condition with a parenthesis.

UserManager.condition().attribute('username').eq('blazej').and.parenthesis(
UserManager.condition()
.attribute('age').eq(21)
.attribute('age').eq(37)
); // username = blazej AND ( age = 21 OR age = 37 )

Condition.group(condition)

This method is identical to Condition.parenthesis(condition) and is used as an alias.

Condition.condition(condition)

This method takes initialized Condition as a parameter and merges both conditions together without parenthesis.

UserManager.condition().attribute('username').eq('blazej').and.condition(
UserManager.condition()
.attribute('age').eq(21).or
.attribute('age').eq(37)
); // username = blazej AND age = 21 OR age = 37

UserManager.condition().attribute('username').eq('blazej').or.condition(
UserManager.condition()
.attribute('age').eq(21).or
.attribute('age').eq(37)
); // username = blazej OR age = 21 OR age = 37

Condition.and

This method specifies an AND operator between two conditions, condition will return true only when both conditions are met. This function can be omitted as AND operator is set as default

// conditions below are identical
UserManager.condition().attribute('username').eq('blazej').and.attribute('age').eq(21); // username = blazej AND age = 21
UserManager.condition().attribute('username').eq('blazej').attribute('age').eq(21); // username = blazej AND age = 21

Condition.or

This method specifies an OR operator between two conditions, condition will return true if either of two conditions are met.

UserManager.condition().attribute('username').eq('blazej').or.attribute('age').eq(21); // username = blazej OR age = 21