1.4.6 • Published 26 days ago

@lpgroup/feathers-counters-service v1.4.6

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
26 days ago

@lpgroup/feathers-counters-service

npm version Known VulnerabilitiesLicence MIT tested with jest codecov

Feathersjs hook that adds a counter key to data or result.

The package consists of the service counters and the hook counter. The hook will store the number of times a specific event has occured in a database through the service.

This funcionality can for example be used for statistics purpose. Counting the number of times GET has occured on a specific endpoint. Number of times a specific user has accessed a service. It can also be used to calculate a unique customer number on a customer endpoint on a specific organisation.

Install

Installation of the npm

npm install @lpgroup/feathers-counters-service
// or
yarn add @lpgroup/feathers-counters-service

The hook

beforeaftermethodsmultisource
yesyesallyessource

arguments

propertytypedefaultdescription
formatstringrequiredA format that makes the counter unique
periodFmtstringrequiredThe date period that the counter is counting. https://date-fns.org/v2.28.0/docs/format
keystringtrueThe name of the key in the returned json data/result
stepnumber1Number of steps to increase the counter each time, postive or negative.
descriptionstringnullDescription of the counter

The service

The service is required by the hook. To install it, modify feathers service file, app-ng/src/services/index.js, add the follwoing.

import { counters } from "@lpgroup/feathers-counters-service";

export default (app) => {
  /* ... */
  app.configure(counters);
};

Endpoints

POST: /counters
GET: /counters
GET: /counters/:alias
PATCH: /counters/:alias
PUT: /counters/:alias
DELETE: /counters/:alias

Object

{
  "alias": "status-visits-2021-11",
  "group": "status-visits",
  "period": "2021-11",
  "value": 50,
  "description": "status-visits"
  /* commonAttributes */
}

Example 1

Give each customer on an organisation a unique customer number, on the endpoint /organisations/:organisationAlias/customer.

The before hook will add the key customerNumber to data and store it in the database handled by the endpoint.

{organisationAlias} comes internally from the url in params.query.

import { counter } from "@lpgroup/feathers-counters-service";

export default {
  before: {
    all: [],
    find: [],
    get: [],
    create: [
      counter("{organisationAlias}-visitors", null, "customerNumber", 1, "Latest customerNumber"),
    ],
    update: [],
    patch: [],
    remove: [],
  },
};

Example 2

Calculate the number of times a user has accessed the GET method on /status?user=admin.

{user} comes internally from the query parameter in params.query.

import { counter } from "@lpgroup/feathers-counters-service";

export default {
  after: {
    all: [],
    find: [],
    get: [
      counter(
        "total_gets-{user}",
        null,
        "userAccesses",
        1,
        "Total number of calls done by a specific user",
      ),
    ],
    create: [],
    update: [],
    patch: [],
    remove: [],
  },
};

Example 3

Calculate number of calls during a month.

The counter will start from the beginning again every month. The statistics for earlier months will be stored in the /counters service.

import { counter } from "@lpgroup/feathers-counters-service";

export default {
  after: {
    all: [],
    find: [],
    get: [],
    create: [counter("visitors", "yyyy-MM", "visits", 1, "Number of visits during a month")],
    update: [],
    patch: [],
    remove: [],
  },
};

Example 4

Keep track of a stack of files, and for every create will increase 1 and for every delete will decrease 1.

import { counter } from "@lpgroup/feathers-counters-service";

export default {
  after: {
    all: [],
    find: [],
    get: [],
    create: [counter("files", null, "files", 1, "Total number of files")],
    update: [],
    patch: [],
    remove: [counter("files", null, "files", -1, "Total number of files")],
  },
};

Contribute

See contribute

License

MIT - See licence

1.4.6

26 days ago

1.4.1

26 days ago

1.4.0

2 months ago

1.3.182

2 months ago

1.3.183

2 months ago

1.3.181

2 months ago

1.3.180

2 months ago

1.3.179

2 months ago

1.3.178

3 months ago

1.3.174

3 months ago

1.3.177

4 months ago

1.3.176

4 months ago

1.3.175

4 months ago

1.3.171

6 months ago

1.3.170

6 months ago

1.3.173

6 months ago

1.3.149

8 months ago

1.3.148

9 months ago

1.3.147

9 months ago

1.3.157

7 months ago

1.3.156

7 months ago

1.3.155

7 months ago

1.3.154

8 months ago

1.3.158

7 months ago

1.3.153

8 months ago

1.3.152

8 months ago

1.3.151

8 months ago

1.3.169

6 months ago

1.3.160

7 months ago

1.3.163

7 months ago

1.3.162

7 months ago

1.3.135

12 months ago

1.3.133

1 year ago

1.3.139

11 months ago

1.3.138

12 months ago

1.3.136

12 months ago

1.3.145

11 months ago

1.3.144

11 months ago

1.3.143

11 months ago

1.3.142

11 months ago

1.3.141

11 months ago

1.3.140

11 months ago

1.3.129

1 year ago

1.3.132

1 year ago

1.3.131

1 year ago

1.3.130

1 year ago

1.3.119

1 year ago

1.3.124

1 year ago

1.3.123

1 year ago

1.3.122

1 year ago

1.3.121

1 year ago

1.3.127

1 year ago

1.3.126

1 year ago

1.3.125

1 year ago

1.3.120

1 year ago

1.3.118

1 year ago

1.3.106

1 year ago

1.3.109

1 year ago

1.3.107

1 year ago

1.3.113

1 year ago

1.3.112

1 year ago

1.3.111

1 year ago

1.3.110

1 year ago

1.3.117

1 year ago

1.3.116

1 year ago

1.3.115

1 year ago

1.3.114

1 year ago

1.3.105

2 years ago

1.3.104

2 years ago

1.3.90

2 years ago

1.3.93

2 years ago

1.3.94

2 years ago

1.3.91

2 years ago

1.3.92

2 years ago

1.3.97

2 years ago

1.3.98

2 years ago

1.3.95

2 years ago

1.3.96

2 years ago

1.3.99

2 years ago

1.3.102

2 years ago

1.3.101

2 years ago

1.3.100

2 years ago

1.3.103

2 years ago

1.3.75

2 years ago

1.3.76

2 years ago

1.3.79

2 years ago

1.3.77

2 years ago

1.3.78

2 years ago

1.3.82

2 years ago

1.3.83

2 years ago

1.3.80

2 years ago

1.3.81

2 years ago

1.3.86

2 years ago

1.3.87

2 years ago

1.3.84

2 years ago

1.3.85

2 years ago

1.3.89

2 years ago

1.3.71

2 years ago

1.3.72

2 years ago

1.3.70

2 years ago

1.3.73

2 years ago

1.3.74

2 years ago

1.3.65

2 years ago

1.3.68

2 years ago

1.3.69

2 years ago

1.3.66

2 years ago

1.3.67

2 years ago

1.3.50

2 years ago

1.3.54

2 years ago

1.3.52

2 years ago

1.3.57

2 years ago

1.3.58

2 years ago

1.3.55

2 years ago

1.3.56

2 years ago

1.3.59

2 years ago

1.3.60

2 years ago

1.3.61

2 years ago

1.3.64

2 years ago

1.3.62

2 years ago

1.3.63

2 years ago

1.3.46

2 years ago

1.3.45

2 years ago

1.3.42

2 years ago

1.3.43

2 years ago

1.3.40

2 years ago

1.3.41

2 years ago

1.3.39

2 years ago

1.3.38

2 years ago

1.3.37

2 years ago

1.3.36

2 years ago