Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,58 @@ user.setNext('inhabitant_seq', function(err, user){

Of course this example is a bit forced and this is for sure not the perfect use case. The fields `country` and `city` have to be present and must not change during the life of the document because no automatic hooks are set on the change of those values. But there are situations when you want a similar behavior.

### Shared counters

Let say we want to share counter between User and Person document.
The schema is like this:

```js
UserSchema = mongoose.Schema({
name: String,
country: String,
city: String,
inhabitant_number: Number
});

PersonSchema = mongoose.Schema({
name: String,
country: String,
city: String,
inhabitant_number: Number
});
```

Every time a new User or Person is added, we want the inhabitant_number to be incremented and to be shared between the two collection. We do so by setting duplicate and super field.

```js
UserSchema.plugin(AutoIncrement, {id: 'inhabitant_seq', inc_field: 'inhabitant_number', reference_fields: ['country'] , duplicate: true, super: true});

PersonSchema.plugin(AutoIncrement, {id: 'inhabitant_seq', inc_field: 'inhabitant_number', reference_fields: ['country'] , duplicate: true, super: false});
```

Notice that we have to set super to only one of the Schema, and duplicate to all the schemas for we want a shared counter.

Now save a new user and a person:
```js
var user = new User({
name: 'Patrice',
country: 'France',
city: 'Paris'
});
user.save();

var person = new PersonSchema({
name: 'Patrice',
country: 'France',
city: 'Paris'
});
person.save();
```

This user will have the `inhabitant_number` counter set to 1 and person will have the `inhabitant_number` counter set to 1 .

Important Note: For the shared counter to work as inteneded, reference field property must be same and have type in all the models.

### Reset a counter

It's possible to programmatically reset a counter through the Model's static method `counterReset(id, reference, callback)`. The method takes these parameters:
Expand Down
26 changes: 26 additions & 0 deletions incrementScopePrep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017";
const dbName = 'test';
const CounterCollection = "counters";

const id = 'member_id';
const suffix = '_old';

try {
MongoClient.connect(url, async(err, client) => {
if (err) console.log(err);

const db = client.db(dbName);
const Counter = db.collection(CounterCollection);

const counters = await Counter.find({id}).toArray();

for (let index = 0; index < counters.length; index++) {
const counter = counters[index];
await Counter.update({_id: counter._id}, {$set: {id : `${counter.id}_${suffix}`}});
}
console.log('done')
});
} catch (error) {
console.log(error)
}
Loading