Skip to content

Create

For the complete documentation index see: llms.txt

All documentation pages available in markdown.

Jump to the Code block for a combined complete example.

Description

Demonstrates single record writes in an Aerospike database.

Setup

Boilerplate

The following examples leverage the following boilerplate to initialize the Aerospike module and connect to a server before creating a record.

const Aerospike = await import("aerospike");
// Set hosts to your server's address and port
const config = { hosts: "YOUR_HOST_ADDRESS:YOUR_PORT" };
// Establishes a connection to the server
const client = await Aerospike.connect(config);

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.

The following example creates a new write policy and 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
const 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
const 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
const geoLoc = new Aerospike.GeoJSON({
type: "Point",
coordinates: [42.2808, 83.743],
});
// Create the record bins
const bins = {
occurred: 20220531,
reported: 20220601,
posted: 20220601,
report: reportMap,
location: geoLoc,
};
// Metadata may be passed; use null to send no metadata
const metadata = null;

Write

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

// Write the record to the server
await client.put(key, bins, metadata, writePolicy);

Set a time to live

The metadata argument of put() sets the record’s time to live (TTL), in seconds. The following example writes the record with a TTL of one hour.

// Expire the record one hour after this write
const ttlMetadata = { ttl: 3600 };
await client.put(key, bins, ttlMetadata, writePolicy);

A read returns the record’s remaining TTL in its ttl property, or -1 if the record never expires. See Read. Instead of a number of seconds, ttl also accepts these constants:

ConstantEffect
Aerospike.ttl.NAMESPACE_DEFAULTUse the namespace’s default-ttl.
Aerospike.ttl.NEVER_EXPIREThe record never expires.
Aerospike.ttl.DONT_UPDATEKeep the record’s current TTL when updating it. If the write creates the record, the namespace’s default-ttl applies.

The server rejects a write with a positive TTL unless the namespace runs NSUP (nsup-period greater than 0) or sets allow-ttl-without-nsup to true. A TTL of 0, -1, or -2 is always accepted. See Data retention.

Create only if the record doesn’t exist

To fail rather than overwrite an existing record, set the write policy’s exists action to CREATE. If the record already exists, put() rejects with an AerospikeError whose code is Aerospike.status.ERR_RECORD_EXISTS.

const createPolicy = new Aerospike.WritePolicy({
exists: Aerospike.policy.exists.CREATE,
});
try {
await client.put(key, bins, null, createPolicy);
} catch (error) {
if (error.code !== Aerospike.status.ERR_RECORD_EXISTS) {
throw error;
}
console.info("Record already exists");
}

Code block

Expand this section for a single code block to create a record
const Aerospike = await import("aerospike");
// Set hosts to your server's address and port
const config = { hosts: "YOUR_HOST_ADDRESS:YOUR_PORT" };
// Establishes a connection to the server
const client = await Aerospike.connect(config);
// Create the record key
const key = new Aerospike.Key("sandbox", "ufodata", 5001);
const writePolicy = new Aerospike.WritePolicy({
key: Aerospike.policy.key.SEND,
});
// Create the report map
const 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
const geoLoc = new Aerospike.GeoJSON({
type: "Point",
coordinates: [42.2808, 83.743],
});
// Create the record bins
const bins = {
occurred: 20220531,
reported: 20220601,
posted: 20220601,
report: reportMap,
location: geoLoc,
};
// Metadata may be passed; use null to send no metadata
const metadata = null;
// Write the record to the server
await client.put(key, bins, metadata, writePolicy);
// Close the connection to the server
await client.close();