Skip to content
Visit booth 3171 at Google Cloud Next to see how to unlock real-time decisions at scaleMore info

Create

Jump to the Code block for a combined complete example.

Setup

The following examples will use the setup and record structure below to illustrate single record creation in an Aerospike database.

const Aerospike = require('aerospike');
const GeoJSON = Aerospike.GeoJSON;
// Define host configuration
let config = {hosts: '127.0.0.1:3000'};

The record structure:

Occurred: Integer
Reported: Integer
Posted: Integer
Report: Map
{
shape: List,
summary: String,
city: String,
state: String,
duration: String
}
Location: GeoJSON

Policies

Write policies define additional semantics for the write operation. Instead of using the default write policies, we can set them on a per command basis.

The following example creates a new write policy container object that sets the send key policy to true. This stores the user defined key with the record, and returns it with read commands.

// Create a new write policy
let writePolicy = new Aerospike.WritePolicy({
key: Aerospike.policy.key.SEND
});

Key

The key is a tuple made up of: (namespace, set, user defined key).

The following example creates a key using the sandbox namespace, ufodata set, and user defined key 5001.

// Create the record key
let key = new Aerospike.Key('sandbox', 'ufodata', 5001);

Create a record

A record is the basic unit of storage in the database. A record is composed of: (key, metadata, bins).

Setup

Expand the block below to see the creation of the variables used in the write example below.

View the data creation
// Create the report map
let reportMap = {
city: 'Ann Arbor',
state: 'Michigan',
shape: ['circle', 'flash', 'disc'],
duration: '5 minutes',
summary: "Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!"
};
// Format coordinates as a GeoJSON string
let geoLoc = new GeoJSON({type: 'Point', coordinates: [42.2808,83.7430]});

Write

The following example shows the creation of the bins and the writing of the record to the database.

let bins = {
occurred: 20220531,
reported: 20220601,
posted: 20220601,
// reportMap defined in the section above
report: reportMap,
// geoLoc defined in the section above
location: geoLoc,
};
// Write the record to Aerospike
;(async () => {
// Establishes a connection to the server
let client = await Aerospike.connect(config);
await client.put(key, bins, [], writePolicy);
// Close the connection to the server
client.close();
})();

Code block

Expand this section for a single code block to create a record
const Aerospike = require('aerospike');
const GeoJSON = Aerospike.GeoJSON;
// Define host configuration
let config = {hosts: '127.0.0.1:3000'};
// Create a new write policy
let writePolicy = new Aerospike.WritePolicy({
key: Aerospike.policy.key.SEND
});
// Create the record key
let key = new Aerospike.Key('sandbox', 'ufodata', 5001);
// Create the report map
let reportMap = {
city: 'Ann Arbor',
state: 'Michigan',
shape: ['circle', 'flash', 'disc'],
duration: '5 minutes',
summary: "Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!"
};
// Format coordinates as a GeoJSON string
let geoLoc = new GeoJSON({type: 'Point', coordinates: [42.2808,83.7430]});
let bins = {
occurred: 20220531,
reported: 20220601,
posted: 20220601,
// reportMap defined in the section above
report: reportMap,
// geoLoc defined in the section above
location: geoLoc,
};
// Write the record to Aerospike
;(async () => {
// Establishes a connection to the server
let client = await Aerospike.connect(config);
await client.put(key, bins, [], writePolicy);
// Close the connection to the server
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?