Policies
Using Policies
The Aerospike Node.js Client uses policies to define behavior for database operations. Policies are values that dictate the behavior of a database operation. Each operation, such as read, write, or query, relies on a set of policy values. Each operation accepts a policy and argument. For example, client.get
accepts an Aerospike.ReadPolicy
as the second argument.
const record = await client.get(key, policy);
Policies can also be provided through a config object upon initialization of the client.
const client = await Aerospike.connect(config);
Default Policy Values
Policies have a default value. For example, the socketTimeout read policy default is 30000 (ms). Also, the key read policy default is Aerospike.policy.key.DIGEST
. Default policy values are defined in the API Documentation.
Client-level Policies
Client-level policies are provided upon initialization of the client. Client-level policy values, when specified, override default policy values. For example, examine the following client-level policy:
const Aerospike = await import("aerospike");
// Set hosts to your server's address and portconst config = { hosts: "YOUR_HOST_ADDRESS:YOUR_PORT", policies: { read: new Aerospike.ReadPolicy({ key: Aerospike.policy.key.SEND }), },};
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001const key = new Aerospike.Key("sandbox", "ufodata", 5001);
// Establishes a connection to the serverconst client = await Aerospike.connect(config);
// Read the record using the specified client-policyconst record = await client.get(key);
The default _key_
policy for reads is Aerospike.policy.key.DIGEST
, but the client policy value provided overrides the _key_
read policy value to be Aerospike.policy.key.SEND
instead.
Command-level Policies
Command-level policies are provided on a per-command basis. Command-level policy values, when specified, override client-level policies and as well as default policy values. For example, examine the following:
const Aerospike = await import("aerospike");
// Create a client-level policyconst config = { policies: { read: new Aerospike.ReadPolicy({ key: Aerospike.policy.key.SEND }), },};
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001const key = new Aerospike.Key("sandbox", "ufodata", 5001);
// Create a policy to be used at the command-levelconst policy = new Aerospike.ReadPolicy({ key: Aerospike.policy.key.DIGEST });
// Establishes a connection to the serverconst client = await Aerospike.connect(config);
// Read a record using the specified command-policyconst record = await client.get(key, policy);
The first read operation includes a command-level policy which overrides the policy value provided at the client level. The key read policy value for the first read operation is Aerospike.policy.key.DIGEST.
However, the second read operation does not specify a command-level policy, so the client-level policy value (Aerospike.policy.key.SEND
) is used instead.
Since no other policy values were specified in client-level or command-level policies, all other read policy values (such as _totalTimeout_
and _maxRetries_
) use default policy values.
Replace Mode
In cases where all record bins are created or updated in a command, enable Replace mode on the command to increase performance. The server then does not have to read the old record before updating. Do not use Replace mode when updating a subset of bins.
const policy = new Aerospike.WritePolicy({ exists: Aerospike.policy.exists.REPLACE,});