In DynamoDB it is possible to set a ConditionExpression on a single attribute like this:
ConditionExpression: 'attribute_exists(user_id)'
If this ConditionExpression is defined on an update, and the user_id does not exist, the ConditionExpression evaluates to false and returns an exception:
message: 'The conditional request failed',
code: 'ConditionalCheckFailedException',
requestId: 'KPFMA7S5P110FCOMLPMKP15UDBVV4KQNSO6AEMVJF66Q9ASUAAJG',
statusCode: 400
Whilst there is only one condition to evaluate everything is clear, but when multiple conditions are specified, DynamoDB does not report which condition failed:
ConditionExpression: 'attribute_exists(user_id) and iq = 85'
then exception is the same as above, thus it's impossible to say what exactly caused the condition evaluated to false.
Is there a way (even hacky one) to get more details out of that exception information?
Unfortunately DynamoDB will not provide any additional details - it will not tell you which part of the ConditionExpression failed.
The only thing I can think of doing is to execute a query immediately before or after you run the update expression, compare the necessary attributes, and log the result.
If you ran the query before the update, you could execute or skip the update as required. In effect you would be implementing your own condition handling.
Alternatively you could execute the query as part of a catch block after your update try block, such that the query would only run if the update failed.
Related
Scenario: executing a stored procedure to insert row into a table
Output: normal, should insert record as set in SQL statement
Failure case: if unique key is violated, it should not update and throw error
All the above steps are working when manually executed in Azure Studio. The same when integrated with NodeJS by using ASYNC call, it works only for the +ve test case; which is a for fresh new record inserted and when the duplicate is inserted, the recordset.length is seen as undefined
This undefined is visible in 6.3.1 and not in the earlier version of 6.2.3
Now in 6.3.1, I could find only an option of using returnValue. Does anyone know other features available to get notified of the error. Below is the output
If it's successful, I get the result as
{
recordsets: [],
recordset: undefined,
output: {},
rowsAffected: [],
returnValue: 0
}
You can try to put your INSERT/UPDATE inside a TRY-CATCH block, then return a specific integer ir a RAISE ERROR. Also, put It on a transaction to rollback on the CATCH block.
Mono<WriteResult> result = reactiveCassandraTemplate.delete(...)
We are handling onSuccess() and onError(), but does something need to be handled specially where the WriteResult "wasApplied" is false but no error is returned? What does that actually mean if it didn't fail, BUT it was not applied.
Thanks!
The wasApplied need to be checked if your query contained the conditional update (for so-called light-weight transactions and for conditional creation of keyspaces/tables, etc.). So, if this field is equal to false then your query was executed but wasn't applied because condition in query didn't allow it.
By default this method always returns true for non-conditional queries.
When I db.collection('example').update({"a":1},{"$set":{"b":2}},{multi:true},function(e,r){
I get r:
{
n:3,
nModified:3,
ok:1
}
This works, I can see If I look at my db that I have successfully updated 3 documents but where are my results?
Quoted from https://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html
callback is the callback to be run after the records are updated. Has three parameters, the first is an error object (if error occured), the second is the count of records that were modified, the third is an object with the status of the operation.
I've tried with 3 outputs in the callback but, then I just get null as a result
db.collection('example').update({"a":1},{"$set":{"b":2}},{multi:true},function(e,n,r){
My documents have been successfully updated but r is null!
I am expecting for this to return my updated documents
It doesn't look like this operation ever does, so how can I manullay return the documents that got changed?
You can use findAndModify to get the updated document in the result. It's callback has 2 parameters:
1- error
2- Updated document
I am not sure this would work for you, but check [documentation]: https://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html#find-and-modify for more info.
To get the updated documents in the returned result, you'll need to use the db.collection.bulkWrite method instead.
I want to create a "prepared statement" in postgres using the node-postgres module. I want to create it without binding it to parameters because the binding will take place in a loop.
In the documentation i read :
query(object config, optional function callback) : Query
If _text_ and _name_ are provided within the config, the query will result in the creation of a prepared statement.
I tried
client.query({"name":"mystatement", "text":"select id from mytable where id=$1"});
but when I try passing only the text & name keys in the config object, I get an exception :
(translated) message is binding 0 parameters but the prepared statement expects 1
Is there something I am missing ? How do you create/prepare a statement without binding it to specific value in order to avoid re-preparing the statement in every step of a loop ?
I just found an answer on this issue by the author of node-postgres.
With node-postgres the first time you issue a named query it is
parsed, bound, and executed all at once. Every subsequent query issued
on the same connection with the same name will automatically skip the
"parse" step and only rebind and execute the already planned query.
Currently node-postgres does not support a way to create a named,
prepared query and not execute the query. This feature is supported
within libpq and the client/server protocol (used by the pure
javascript bindings), but I've not directly exposed it in the API. I
thought it would add complexity to the API without any real benefit.
Since named statements are bound to the client in which they are
created, if the client is disconnected and reconnected or a different
client is returned from the client pool, the named statement will no
longer work (it requires a re-parsing).
You can use pg-prepared for that:
var prep = require('pg-prepared')
// First prepare statement without binding parameters
var item = prep('select id from mytable where id=${id}')
// Then execute the query and bind parameters in loop
for (i in [1,2,3]) {
client.query(item({id: i}), function(err, result) {...})
}
Update: Reading your question again, here's what I believe you need to do. You need to pass a "value" array as well.
Just to clarify; where you would normally "prepare" your query, just prepare the object you pass to it, without the value array. Then where you would normally "execute" your query, set the value array in the object and pass it to the query. If it's the first time, the driver will do the actual prepare for you the first time around, and simple do binding and execution for the rest of the iteration.
I'm using Hector to interact with a cassandra database from a java application. (Hector 1.0-1)
In this example, it shows how to insert (or update) a field.
mutator.addInsertion("650222", "Npanxx", HFactory.createStringColumn("state", "CA"));
MutationResult mr = mutator.execute();
However, there is not much information on the outcome of the operation. How can we verify if the operation was successful or not? The return value is a ResultStatus implementation and the 3 methods that can be called are:
mr.getHostUsed()
mr.getExecutionTimeNano()
mr.getExecutionTimeMicro()
Can I assume that if there were no exceptions calling the execute() method, that the operation succeeded?
It looks like the execute method doesn't declare any exceptions thrown because it will throw instances of HectorException which is a RuntimeException.
So yes, if no exceptions are thrown, the insert succeeded. Otherwise you will get an instance of HectorException thrown (likely HTimedOutException/HUnavailableException for problems on the Cassandra side and something else for something on the Hector side).