Skip to content

Transactions

CRUD operations

The following example demonstrates create, update, read, and delete (CRUD) operations within a transaction. For the purposes of this example, the following records are held in a bin named sightings:

[
{
"sighting": {
"occurred": 20200912,
"reported": 20200916,
"posted": 20201105,
"report": {
"city": "Kirkland",
"duration": "~30 minutes",
"shape": ["circle"],
"state": "WA",
"summary": "4 rotating orange lights in the Kingsgate area above the Safeway. Around 9pm the power went out in the Kingsgate area. Four lights were spotted rotating above the local Safeway and surrounding streets. They were rotating fast but staying relatively in the same spots. Also described as orange lights. About thirty minutes later they disappeared. The second they disappeared the power was restored. Later a station of police from Woodinville and Kirkland came to guard the street where it happened. They wouldn't let anyone go past the street, putting out search lights and flare signals so people couldn't drive past Safeway. The police also would not let people walk past to go home."
},
"location": {
"type": "Point",
"coordinates": [-122.1966441, 47.69328259]
}
}
},
{
"sighting": {
"occurred": 20200322,
"reported": 20200322,
"posted": 20200515,
"report": {
"city": "Pismo Beach",
"duration": "5 minutes",
"shape": ["light"],
"state": "CA",
"summary": "About 20 solid, bright lights moving at the same altitude, heading and speed. Spaced perfectly apart flying over the ocean headed south."
},
"location": {
"type": "Point",
"coordinates": [-120.6595, 35.1546]
}
}
},
{
"sighting": {
"occurred": 20200530,
"reported": 20200531,
"posted": 20200625,
"report": {
"city": "New York Staten Island",
"duration": "2 minutes",
"shape": ["disk"],
"state": "NY",
"summary": "Round shaped object observed over Staten Island NYC, while sitting in my back yard. My daughter also observed this object. Bright White shaped object moving fast from East to West. Observed over Graniteville, Staten Island towards the Elizabeth NJ area and appears to be fast. We then lost view of it due to the clouds."
}
}
}
]

Complete example:

const Aerospike = require('aerospike')
let config = {
hosts: [
{ addr: "127.0.0.1", port: 3000}
]
}
;(async function () {
let client
try {
client = await Aerospike.connect(config)
const namespace = 'test'
const set= 'demo'
const binName = 'sighting'
const startIndexOffset = 5001
const sightingsMap = require('./create_a_document_record.json');
let writePolicy = new Aerospike.WritePolicy({})
let readPolicy = new Aerospike.ReadPolicy({})
let removePolicy = new Aerospike.RemovePolicy({})
const exampleTransaction = new Aerospike.Transaction()
console.log("Begin transaction: %d", exampleTransaction.getId());
for (var i = 0; i < 3; i++) {
const key = new Aerospike.Key(namespace, set, i)
writePolicy.txn = exampleTransaction
readPolicy.txn = exampleTransaction
const entry = sightingsMap[i]
const value = entry.sighting
//debug
const sightingsBin = {[binName]: entry}
try{
// Add initial record for operations
await client.put(key, {'sighting': "No data"}, {})
// Delete
await client.remove(key, removePolicy)
// Create
await client.put(key, {'sighting': "No data"} , {}, writePolicy)
// Update
await client.put(key, sightingsBin, {}, writePolicy)
// Read
await client.get(key, readPolicy)
console.log("Create succeeded")
console.log("Key:")
console.log(key)
console.log("Record: ")
console.log(value)
}
catch(error) {
await client.abort(exampleTransaction)
console.error("Write failed\\nError: " + error.message)
await client.close()
return
}
}
// Commit successful transaction
console.log("Commit transaction: %d", exampleTransaction.getId());
await client.commit(exampleTransaction);
} catch (error) {
console.error('Error:', error)
process.exit(1)
} finally {
if (client) await client.close()
}
})()
Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?