Giter Site home page Giter Site logo

meteor-mongo-counter's Introduction

osv-mongo-counter

Atomic counters stored in MongoDB.

Synopsis

Mydb.insert({
  _id: incrementCounter('countCollection', 'counterName'),
  // ... other data
});

Description

Incrementing a counter returns consecutive integers (1, 2, 3...), with the counter stored in the database.

It is safe to make concurrent calls to incrementCounter and decrementCounter. If the current value of a counter is 6 and two Meteor methods call incrementCounter at the same time, one will receive 7 and the other 8.

This package is called mongo-counter because it works directly with MongoDB's facilities for atomic updates; it doesn't go through Meteor's collection code. (Thus it only works with Meteor deployments that use a MongoDB database. If, for example, a SQL database was being used instead, there would need to be a "sql-counter" package to implement atomic counters in SQL).

Note that counters are not stored in a Meteor document: this package doesn't increment a field in a document inside of a Meteor collection. But when you increment or decrement a counter, you can take the new value of the counter that is returned to you and store that value in a Meteor document if you want.

Counters are not themselves a reactive data source, but you can store the counter value into a reactive data source such as a Meteor document whenever you increment or decrement a counter.

mrt:mongo-counter

Warning, API changed, if you have used mrt:mongo-counter before and want use this package now, you must migrate your counters (see collection awwx_mongo_counter):

Fork history

  • Original mrt:mongo-counter, use not clean name for count collection awwx_mongo_counter and not maintained.
  • Fork konecty:mongo-counter - it too "Konecty" specified.

Version

It designed for using Meteor >= 1.0.4, for early version you can use konecty:[email protected] that have similar API (but not later)

API

Note that this API is defined on the server only, and that these functions are not defined in the client. To access them from the client, create a Meteor method on the server that calls the API, and then call the method from the client.

incrementCounter

incrementCounter(countCollection, name, [amount])   server

Increments a database counter and returns the new value.

Arguments

countCollection: string
Name of collection where counter is stored.
name: string
The name of the counter to increment.
amount: integer
The amount to increment the counter by, defaulting to one.

Increments the counter named name in the database, and atomically returns the new value. New counters conceptually start at zero, so if you increment a new counter by one you will receive one on the first call.

decrementCounter

decrementCounter(countCollection, name, [amount])   server

Decrements a database counter and returns the new value.

Arguments

countCollection: string
Name of collection where counter is stored.
name: string
The name of the counter to decrement.
amount: integer
The amount to decrement the counter by, defaulting to one.

Decrements the counter named name in the database, and atomically returns the new value.

setCounter

setCounter(countCollection, name, value)   server

Sets a counter.

Arguments

countCollection: string
Name of collection where counter is stored.
name: string
The name of the counter to set.
value: integer
The value to set the counter to.

Sets the counter to the specified value.

This is primarily useful for setting a new counter to an initial value. (If a counter was currently 10 and one method called incrementCounter while another simultaneously called setCounter with a value of 0, it would be indeterminate whether the first method received 11 or 1).

Using a counter for a humanly readable id

Counters can be used when you want to create a humanly readable identifier that is guaranteed to be unique, such as an order or invoice number.

Typically you wouldn't want to use a counter number as the _id of a document, unless the document is only ever created on the server.

A pattern that works with creating documents in the client is to allow Meteor to create its usual random string _id field, and then put the humanly readable identifier in another field (such as "orderNumber") with a method call to the server. This allows the counter field to be filled in when the client has a connection to the server, without making the client wait if the Internet connection is momentarily slow or disconnected.

Implementation

This package implements the "Counters Collection" technique described in the MongoDB documentation Create an Auto-Incrementing Sequence Field.

Using the Mongo findAndModify method makes incrementing the counter and reading the new value atomically safe. (If we first incremented the counter field using update and then read the new value using find, two simultaneous calls to incrementCounter could increment the counter twice and then both return the same doubly incremented number).

Since Meteor doesn't yet support Mongo's findAndModify, the implementation accesses Mongo directly without going through a Meteor Collection.

Accessing this collection with a Meteor Collection isn't recommended, because changes made by incrementCounter aren't reported back to Meteor.

Hire

You can hire original author of this packages :) Click http://awwx.ws/hire-me

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.