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.
let record = client.get(key, policy, callback)
Policies can also be provided through a config object upon initialization of the client.
let 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 = require('aerospike')let config = { policies: { read : new Aerospike.ReadPolicy({key : Aerospike.policy.key.SEND}), }};(async function () { let client try { client = await Aerospike.connect(config) let 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 = require('aerospike')let config = { policies: { read : new Aerospike.ReadPolicy({key : Aerospike.policy.key.SEND}), }};(async function () { let client try { client = await Aerospike.connect(config) let policy = new Aerospike.ReadPolicy({key : Aerospike.policy.key.DIGEST}) let record = await client.get(key, policy) record = await client.get(key)...
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.
let policy = new Aerospike.WritePolicy({exists : Aerospike.policy.exists.REPLACE})await client.put(key, bins, {}, policy);...