# Counters Module

General purpose atomically incrementing counters.&#x20;

```
import counters from 'counters';
```

### Methods

[clear(search='')](https://docs.medable.com/reference#section-counters-clear-search-)\
[count(search='')](https://docs.medable.com/reference#section-counters-count-search-)\
[del(key)](https://docs.medable.com/reference#section-counters-del-key-)\
[get(key)](https://docs.medable.com/reference#section-counters-get-key-)\
[has(key)](https://docs.medable.com/reference#section-counters-has-key-)\
[list(search='', skip=0, limit=100)](https://docs.medable.com/reference#section-counters-list-search-skip-0-limit-100-)\
[next(key)](https://docs.medable.com/reference#section-counters-next-key-)

### counters.clear(search='')

Clears counters starting with `search` (or all counters with `''`).

**Arguments**

* `search` (String) Any keys starting with `search` are removed. To avoid inadvertently clearing keys, adopt a key naming strategy that guarantees key domains remain segregated. Leaving this argument blank will *delete all counters*.

**Returns**

* `removed` (Number) the number of counters removed.

### counters.count(search='')

Returns a count of counters entries starting with `search` (or all counters with `''`).

**Arguments**

* `search` (String) Any counters starting with `search` are counted.

**Returns**

* `count` (Number) the number of matching keys.

### counters.del(key)

Removes the counters with a key matching `key`.

**Arguments**

* `key` (String) The key to remove.

**Returns**

* `removed` (Boolean) true if the counters was removed.

### counters.get(key)

Returns the value of a counter matching `key` without incrementing.

**Arguments**

* `key` (String) The key to match.

**Returns**

* `value` (Object) Returns the value of the entry, or `null` if not found.

### counters.has(key)

Returns true if a counter matching `key` exists.

**Arguments**

* `key` (String) The counter to match.

**Returns**

* `exists` (Boolean) Returns `true` if the counter exists.

### counters.list(search='', skip=0, limit=100)

Returns an array of matching entry metadata, sorted by key.

**Arguments**

* `search` (String) The key filter. All entries starting with `search` are returned.
* `skip` (Number) The number of entries to skip. Useful for paging.
* `limit` (Number) The maximum number of entries to return, from 1 to 1000.

**Returns**

* `entries` (Object\[]) A list of matching entry metadata.
  * `key` (String) The entry key.
  * `created` (Date) The time the entry was initially created.
  * `val` (Number) The current counter value.
  * `updated` (Date) The date of the last update to the counter.

### counters.next(key)

Atomically increment and return the next counter value.

**Arguments**

* `key` (String) The key to set.

**Returns**

* `value` (Number) The next counter value.

### Examples

Example

```
import counters from 'counters';

return counters.next('my_counter');
```
