masumsoft / express-cassandra

Cassandra ORM/ODM/OGM for NodeJS with support for Apache Cassandra, ScyllaDB, Datastax Enterprise, Elassandra & JanusGraph.
http://express-cassandra.readthedocs.io
GNU Lesser General Public License v3.0
228 stars 67 forks source link

if_not_exist not working for model.save() #270

Closed aorlowskiddm closed 10 months ago

aorlowskiddm commented 10 months ago

"cassandra-driver": "^4.7.2", "express-cassandra": "^2.9.0", node v18.16.0

I am trying to conditionally save a record with the IF NOT EXISTS flag:

mapping.save({if_not_exist: true}, (err: any) => {
      if (err) {
        log.error(`Error saving mapping ${err}`)
        throw err
      }

      log.info(`Successfully saved mapping with id ${mapping.mappingId}`)
})

The if_not_exist flag seems to be doing nothing. It overwrites the prior data if it exists. I was able to reproduce this issue with a very simple model:

module.exports = {
  fields: {
    jsonPath: 'text'
  },
  key: ['jsonPath'],
  table_name: 'jsonpaths',
}
aorlowskiddm commented 10 months ago

It was just my misunderstanding of the library. My thought was that if the record previously existed, an error would be given. However, the actual result is that the insert silently fails.

I confirmed that the IF NOT EXISTS was being appended to the query by checking the generated queries by running the node server with the specified command:

DEBUG=express-cassandra npm run dev