Insert Items in NoSQL Table
Catalyst enables you to insert items in a specific NoSQL table after you construct them. The items can be inserted in different ways as described in this section.
You can refer to the help sections on adding and working with data, the Catalyst custom JSON format, and the supported data types to learn these topics in detail.
Insert Items without Conditions
You can 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 mandatorily pass the values for the partition key and sort key attributes configured for the table.
In the example given below, an item containing the value of the partition key attribute fruitName is provided as “Banana”. Other attributes of the string data type such as fruitColor and fruitType are also added as a map called fruitProperties. The item is inserted using the insertItems() method.
// 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" }
]
}
}
});
Here are some more sample snippets for 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 2025-06-20 16:21:48 +0530 IST
Yes
No
Send your feedback to us