Insert Items in NoSQL TableAdmin Scope
Using the insertItems() SDK method, you can insert items in a specific NoSQL table after you construct them.
- Catalyst enables you to insert a maximum of 25 items in bulk in a NoSQL table with a single SDK operation.
- Learn more about the Catalyst custom JSON format.
- Learn more about the supported data types.
Insert Items Without Conditions
Insert new items into a NoSQL table without any conditions by constructing the items in the Catalyst custom JSON format. This will require you to pass the values for the partition key and sort key attributes configured for the table.
For example, consider a NoSQL table with the following values:
| Attribute Name | Attribute Data Type | Attribute Value |
|---|---|---|
| fruitName(Partition Key) | String | Banana |
| fruitColor(map:fruitProperties) | String | Yellow |
| fruitType(map:fruitProperties) | String | Berries |
// Insert a NoSQL item without conditions
const plainInsert = await table.insertItems({
// Define the item to be inserted with the partition key fruitName
item: NoSQLItem.from({
fruitName: 'Banana',
//Provide values for the other attributes of the item
fruitProperties: {
fruitColor: 'Yellow',
fruitType: 'Berries'
}
}),
// Set the return value in the response. Other supported values are "OLD" and "NULL"
return: NoSQLReturnValue.NEW
});
Insert Items With Conditional Functions
You can insert attributes in existing items in a NoSQL table using specific conditions that you define in the Catalyst custom JSON format. In this type, the existing data of the table is retrieved and evaluated against the specified condition. The items are inserted only if the evaluation is true. If there is no existing data, the conditions are ignored and the items are inserted.
Catalyst supports multiple operators to evaluate conditions. The supported operators are represented as shown below.
| Operators | Notation |
|---|---|
| CONTAINS | contains |
| NOT_CONTAINS | not_contains |
| BEGINS_WITH | begins_with |
| ENDS_WITH | ends_with |
| IN | in |
| NOT_IN | not_in |
| BETWEEN | between |
| NOT_BETWEEN | not_between |
| EQUALS | equals |
| NOT_EQUALS | not_equals |
| GREATER_THAN | greater_than |
| LESS_THAN | less_than |
| GREATER_THAN_OR_EQUALS | greater_equal |
| LESSER_THAN_OR_EQUALS | less_equal |
| AND | AND |
| OR | OR |
The example below illustrates this by defining a condition for the data type of the attribute fruitName to be String in the existing data. If the condition is satisfied, the attribute taste with the value “Sweet” is added to these items.
// Insert a NoSQL item with the "attribute_type" function
const attrTypeInsert = await table.insertItems({
// Define the item to be inserted
item: NoSQLItem.from({
taste: 'Sweet'
}),
// Define the condition for insert
condition: {
// The condition specifies that the item should be added if the attribute type is String ("S")
function: {
// Set the function type
function_name: 'attribute_type',
// Supply the arguments to the function
args: [{
// Set the attribute path
attribute_path: ['fruitName']
},
// Set the attribute type
NoSQLMarshall.makeString('S') // => { "S": "S" }
]
}
}
});
The following code snippet details further examples of inserting items with conditional functions.
//Insert a NoSQL Item with the "equals" operator, attribute "name" value equals "apple"
const operatorEqInsert = await table.insertItems({
// Define the item to be inserted
item: NoSQLItem.from({
taste: 'Sweet'
}),
// Define the condition for insert
condition: {
// Set the attribute path
attribute: ['name'],
// Set the operator based on the operation
operator: NoSQLOperator.EQUALS,
// Set the value for comparison
value: NoSQLMarshall.makeString('apple') // => { "S": "apple" }
}
});
//Insert a NoSQL Item with "group_operator", attribute "name" is "apple" AND attribute "variety" is "gala"
const groupOpInsert = await table.insertItems({
// Define the item to be inserted
item: NoSQLItem.from({
taste: 'Sweet'
}),
// Define the condition for insert
condition: {
// Set the group operator
group_operator: NoSQLConditionGroupOperator.AND,
// Supply the group conditions
group: [{
// Set the attribute path
attribute: 'name',
// Set operator based on the operation
operator: NoSQLOperator.EQUALS,
// Set the value for comparison
value: NoSQLMarshall.makeString('apple') // => { "S": "apple" }
},
{
// Set the attribute path
attribute: 'variety',
// Set the operator based on the operation
operator: NoSQLOperator.EQUALS,
// Set the value for comparison
value: NoSQLMarshall.makeString('gala') // => { "S": "gala" }
}
]
}
});
//Insert a NoSQL Item with the "begins_with" operator, attribute "name" value begins with "app"
const beginsWithInsert = await table.insertItems({
// Define the item to be inserted
item: NoSQLItem.from({
taste: 'Sweet'
}),
// Define the condition for insert
condition: {
// Set the attribute path
attribute: ['name'],
// Set the operator based on the operation
operator: NoSQLOperator.BEGINS_WITH,
// set the value for comparison
value: NoSQLMarshall.makeString('app') // => { "S": "app" }
}
});
Last Updated 2026-07-02 14:51:41 +0530 IST
Yes
No
Send your feedback to us