Skip to content

Commit dc570b6

Browse files
committed
Merge branch 'release/3.0.2'
2 parents 57621ab + cf50e63 commit dc570b6

File tree

3 files changed

+52
-41
lines changed

3 files changed

+52
-41
lines changed

Changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# 3.0.2
2+
3+
- Updated readme
4+
+ added coverall badge
5+
+ added installation instructions
6+
+ some section have benn rewrote
7+
- Updated dependencies
8+
19
# 3.0.1
210

311
- Fixed tests for Travis-CI environment

README.md

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Mongoose sequence plugin
22

33
[![Build Status](https://travis-ci.org/ramiel/mongoose-sequence.svg?branch=master)](https://travis-ci.org/ramiel/mongoose-sequence)
4+
[![Coverage Status](https://coveralls.io/repos/github/ramiel/mongoose-sequence/badge.svg?branch=master)](https://coveralls.io/github/ramiel/mongoose-sequence?branch=master)
45

56
This plugin let you create fields which autoincrement their value:
67
- every time a new document is inserted in a collection
@@ -17,13 +18,17 @@ Multiple counter can be set for a collection.
1718

1819
This plugin need mongoose version 4.0.0 or above
1920

21+
## Installation
22+
23+
`npm install --save mongoose-sequence`
24+
2025
## Global sequences
2126

2227
Let's say you want to have an `id` field in your `user` collection which has an unique auto-incremented value.
2328

2429
The user schema is something like this:
2530

26-
```javascript
31+
```js
2732
UserSchema = mongoose.Schema({
2833
name: String
2934
});
@@ -33,7 +38,7 @@ mongoose.model('User', UserSchema);
3338

3439
You don't need to define the `id` field in your schema because the plugin automatically set it for you. The only thing you have to do is to call:
3540

36-
```javascript
41+
```js
3742
UserSchema.plugin(AutoIncrement, {inc_field: 'id'});
3843
```
3944

@@ -42,7 +47,7 @@ A commodity collection named `counters` is created for you. You can override the
4247

4348
If you want to increment the `_id` field which is special to mongoose, you have to explicitly specify it as a Number and tell mongoose to not interfer:
4449

45-
```javascript
50+
```js
4651
UserSchema = mongoose.Schema({
4752
_id: Number,
4853
name: String
@@ -54,55 +59,53 @@ In this case you don't have to specify `inc_field` because the default value is
5459

5560
## Not automatic sequences
5661

57-
Let say our user model has a `like` field which counts how many likes the user has in our amazing application. This field is of course a sequence but has to be incremented everytime an event occours. Because we have concurrent access to database we want to be sure that the increment of this counter happens safely.
62+
Let say our user model has a `rank` field which cgives the rank of the user in a tournament. So it saves the arrival order of a user to the end of our amazing game. This field is of course a sequence but has to be incremented everytime an event occours. Because we have concurrent access to database we want to be sure that the increment of this counter happens safely.
5863
Let's start modify our schema
5964

60-
```javascript
65+
```js
6166
UserSchema = mongoose.Schema({
6267
name: String,
63-
like: Number
68+
rank: Number
6469
});
6570
```
6671

67-
this time we specified explicitly the field `like`. There is no difference between defining and omitting the field specification. The only constraints is that the field has to be of type `Number`, otherwise the plugin will raise an error.
68-
So, let's say to the plugin we want the `like` field to be a safe counter
72+
this time we specified explicitly the field `rank`. There is no difference between defining and omitting the field specification. The only constraints is that the field has to be of type `Number`, otherwise the plugin will raise an error.
73+
So, let's say to the plugin we want the `rank` field to be a safe counter
6974

70-
```javascript
71-
UserSchema.plugin(AutoIncrement, {inc_field: 'like', disable_hooks: true});
75+
```js
76+
UserSchema.plugin(AutoIncrement, {inc_field: 'rank', disable_hooks: true});
7277
```
7378

74-
We specified `disable_hooks`. This avoid the field to be incremented when a new document is saved. So, how to increment this field? Your models has a new method: `setNext`. You must specify which sequence you want to increment and a callback. Here an example:
79+
We specified `disable_hooks`. This avoid the field to be incremented when a new document is saved. So, how to increment this field? Your models have a new method: **setNext**. You must specify which sequence you want to increment and a callback. Here an example:
7580

76-
```javascript
81+
```js
7782
User.findOne({name:'George'}, function(err, user){
78-
user.setNext('like', function(err, user){
79-
if(err) console.log('Cannot increment the likes because',err);
83+
user.setNext('rank', function(err, user){
84+
if(err) console.log('Cannot increment the rank because',err);
8085
});
8186
});
8287
```
8388

8489
You noticed that the method `setNext` takes, as argument, the counter field name. Is possible to give a name to the counter and use it as reference. For the previous example we can define the counter like this:
8590

86-
```javascript
87-
UserSchema.plugin(AutoIncrement, {id:'like_counter', inc_field: 'like', disable_hooks: true});
91+
```js
92+
UserSchema.plugin(AutoIncrement, {id:'rank_counter', inc_field: 'rank', disable_hooks: true});
8893
```
8994

9095
and then using
9196

92-
```javascript
93-
user.setNext('like_counter', function(err, user){
97+
```js
98+
user.setNext('rank_counter', function(err, user){
9499
...
95100
});
96101
```
97102

98-
So, if you not specify the `id`, the field name is used. Even if you're not forced to specify an id, its use is strongly suggested. This because if you have two different counters, which refers to fields with the same name, they will collide and incrementing one, will increment the other too.
99-
So use unique id to be sure to avoid collision. In any case any collision led to an error by the plugin.
103+
So, if you not specify the `id`, the field name is used. Even if you're not forced to specify an id, its use is strongly suggested. This because if you have two different counters, which refers to fields with the same name, they will collide and incrementing one, will increment the other too. Counters are not bound to the schema they refer too, so two counters for two different schemas can collide.
104+
So use unique id to be sure to avoid collision. In case of collision the plugin will raise an error.
100105

101106
As we will see, the use of an id for the counter is mandatory when you're are defining a `scoped counter`.
102107

103-
**NOTE**: When you call `setNext` the document is automatically saved. This behavior is different from version <=2.0.0
104-
105-
108+
**NOTE**: When you call `setNext` the document is automatically saved. This behavior has changed since version 3.0.0. If you use a prior version you have to call save by yourself.
106109

107110
## Advanced
108111

@@ -111,24 +114,24 @@ As we will see, the use of an id for the counter is mandatory when you're are de
111114
Let say our users are organized for `country` and `city`. And we want to save the `inhabitant_number` according to the two informations.
112115
The schema is like this:
113116

114-
```javascript
117+
```js
115118
UserSchema = mongoose.Schema({
116119
name: String,
117120
country: String,
118121
city: String,
119-
inhabitants: Number
122+
inhabitant_number: Number
120123
});
121124
```
122125

123-
Every time a new Parisian is added the counting of Parisians have to be incremented. The inhabitants of New York must not interfer and have their separated counting. We should define a __scoped__ counter which says, increment the counter, depending on the value of other fields.
126+
Every time a new Parisian is added the counting of Parisians have to be incremented. The inhabitants of New York must not interfer and have their separated counting. We should define a __scoped__ counter which increment the counter depending on the value of other fields.
124127

125-
```
126-
UserSchema.plugin(AutoIncrement, {id: 'inhabitant_seq', inc_field: 'inhabitants', reference_fields: ['country','city'] });
128+
```js
129+
UserSchema.plugin(AutoIncrement, {id: 'inhabitant_seq', inc_field: 'inhabitant_number', reference_fields: ['country','city'] });
127130
```
128131

129132
Notice that we have to use an id for our sequence, otherwise the plugin will raise an error.
130133
Now save a new user
131-
```
134+
```js
132135
var user = new User({
133136
name: 'Patrice',
134137
country: 'France',
@@ -137,25 +140,25 @@ var user = new User({
137140
user.save();
138141
```
139142

140-
This user will have the `inhabitants` counter to 1.
141-
If now we add a new inhabitant from New York, this will have the counter to 1 also, because the counter is referenced to the value of the fields country and city.
143+
This user will have the `inhabitant_number` counter to 1.
144+
If now we add a new inhabitant from New York, this will have the counter to 1 also, because the counter is referred to the value of the fields `country` and `city`.
142145

143146
If we want to increment manually this counter we have to specify the id of the sequence in the `setNext` method
144147

145-
```javascript
148+
```js
146149
user.setNext('inhabitant_seq', function(err, user){
147-
user.inhabitants; // the counter value
150+
user.inhabitant_number; // the counter value
148151
});
149152
```
150153

151-
Of course this example is a bit forced and this is for sure not the perfect use case. The field country and city have to be present and must not change during the life of the document because no automatic hook are set on the change of those values. But there are situation when you want a similar behaviour.
154+
Of course this example is a bit forced and this is for sure not the perfect use case. The field country and city have to be present and must not change during the life of the document because no automatic hook are set on the change of those values. But there are situations when you want a similar behavior.
152155

153156
### Options
154157

155158
This plugin accept a series of options.
156159

157-
- __inc_field__: The name of the field to increment. Mandatory, default is `_id`
158-
- __id__: Id of the sequence. Is mandatory only for scoped sequences but its use is strongly encouraged.
159-
- __reference_fields__: The field to reference for a scoped counter. Optional
160-
- __disable_hooks__: If true, the counter will not be incremented on saving a new document
161-
- __collection_name__: By default the collection name for the counters is `counters`. You can override it using this option
160+
- **inc_field**: The name of the field to increment. Mandatory, default is `_id`
161+
- **id**: Id of the sequence. Is mandatory only for scoped sequences but its use is strongly encouraged.
162+
- **reference_fields**: The field to reference for a scoped counter. Optional
163+
- **disable_hooks**: If true, the counter will not be incremented on saving a new document. Default to `false`
164+
- **collection_name**: By default the collection name to mantain the status of the counters is `counters`. You can override it using this option

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mongoose-sequence",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"description": "Very generic autoincrement plugin for mongoose",
55
"main": "index.js",
66
"scripts": {
@@ -23,7 +23,7 @@
2323
},
2424
"license": "GPL-2.0",
2525
"dependencies": {
26-
"lodash": "^3.10.1"
26+
"lodash": "^4.6.1"
2727
},
2828
"peerDependencies": {
2929
"mongoose": ">=4"

0 commit comments

Comments
 (0)