Installation:
$ npm i @or-sdk/contacts
Contacts is a composition of separate Api entities.
Usage:
import { Contacts } from '@or-sdk/contacts'
const contacts = new Contacts({
token: 'my-account-token-string',
contactsApiUrl: 'http://example.account-settings/endpoint'
});
const contacts = new Contacts({
token: 'my-account-token-string',
discoveryUrl: 'http://example.account-settings/endpoint'
});
After when you initialize the Contacts Api,
various entities are available at your service with which you can work
(all entities presented in api
folder )
Examples
Basic usage
Let's say you want to perform some CRUD operation on the contactBook entity
All you need to do, it's just refer to contactBookApi(entity)
import { Contacts } from '@or-sdk/contacts'
const {contactBookApi, migrationsApi, ...rest} = new Contacts(...);
const migrationsStatus = migrationsApi.getMigrationState()
const book = contactBookApi.getContactsBook(id)
const newContactBook = contactBookApi.createContactBook(...)
Bulk create
import { Contacts } from '@or-sdk/contacts'
const { bulkCreateApi } = new Contacts({
contactsApiUrl: ...,
accountId: ...,
token: ...,
withKeepAliveAgents: true
});
const { created, failed } = await bulkCreateApi.bulkCreateContacts(
'some bulk name',
{
contact_book: 'some book id',
contacts: [...]
},
{ batchSize: 4000000 }
);
Track bulk progress
import { Contacts } from '@or-sdk/contacts'
const { bulkCreateApi } = new Contacts({
contactsApiUrl: ...,
accountId: ...,
token: ...,
withKeepAliveAgents: true
});
for await (const bulkResults of bulkCreateApi.trackBulkCreateContacts('bulkName', data)) {
if (bulkResults.type === 'progress') {
const progress = bulkResults.results;
} else {
const results = bulkResults.results;
}
}
Tuning bulk
Create bulk consumes lots of resources, in terms of the quantity of launched lambdas (it's huge!), and in terms of created workload on the DB (DB server memory, CPU, connections to the DB).
So basically it means that the more activities are taking place at particular point of time the higher is the probability of bulk failure. It is strongly recommended to execute the bulk create operation during non working hours!
Bulk provides some interface for tuning the workload by means of the options
argument. With the default options, the bulk successfully completes 200k contacts for approximately 5 mins. Tuning options may result into the bulk operation performance degrade, however it might dramatically reduce workload on the system and turn failed bulk into successful one!
Here is the short description of the bulk create contacts mechanism to understand how to affect it by tuning options:
- bulk examines the size of passed data and if it is more that 4000000 bytes the data is broken into array of data with the same type, however each element of this array is less than the allowed batchSize; each element of this array is called
batch
- bulk executes 4 (by default, might be tuned with the
parallelBatchesAmount
) batches in parallel - we call it batches series
; executing here means sending the batch data to the server where contacts, actually, are created and inserted into DB - batch execution duration can't be determined and to avoid the gateway timeout error the execution results are poled. Every poling attempt are repeated in the amount of time specified by
polingDuration
- if batches series fails (it might happen due to DB server overload, for example), one more attempt is made after the amount of time specified with
repeatFailedParallelBatchesIn
- when all batches are executed, bulk results is formed
Here is the options:
{
batchSize?: number;
parallelBatchesAmount?: number;
batchesToProcess?: number;
polingDuration?: number;
repeatFailedParallelBatchesIn?: number;
logger?: BulkLogger;
};