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
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# mongoose-plugin-autoinc
# mongoose-plugin-autoinc-fix
[![](https://img.shields.io/npm/v/mongoose-plugin-autoinc.svg)](https://www.npmjs.com/package/mongoose-plugin-autoinc)
[![codecov coverage](https://img.shields.io/codecov/c/github/nodkz/mongoose-plugin-autoinc.svg)](https://codecov.io/github/nodkz/mongoose-plugin-autoinc)
[![Travis](https://img.shields.io/travis/nodkz/mongoose-plugin-autoinc.svg?maxAge=2592000)](https://travis-ci.org/nodkz/mongoose-plugin-autoinc)
Expand All @@ -7,7 +7,15 @@

---

This is a fork of [mongoose-auto-increment](https://github.com/chevex-archived/mongoose-auto-increment) which has not been maintained in a while. Also used fixes and changes from [dashride fork](https://github.com/Dashride/mongoose-auto-increment). This fork addresses the following issues:
This is a fork of [nodkz/mongoose-auto-increment](https://github.com/nodkz/mongoose-auto-increment) which has not been maintained in a while.This fork addresses the following issues:
- fix errors during testing
- After `run test`, `run flow` output error `'The first parameter is incompatible'`. However, it was the same as nodz's version.
- fix errors in README.md `call nextCount and resetCount methods wrongly`
- tested with Mongoose 5
- upload to yarnpkg.com as `'mongoose-plugin-autoinc-fix'`
- increment version number started from nodkz's version

The following issues were fixed by [nodkz](https://github.com/nodkz/mongoose-plugin-autoinc).Also used fixes and changes from [dashride fork](https://github.com/Dashride/mongoose-auto-increment).
- fix error `'required' is not valid for an index specification` for Mongoose 4
- does not require established connection for initialization **(deprecate `initialize()` method)**
- include Flowtype and Typescript declarations
Expand All @@ -16,17 +24,19 @@ This is a fork of [mongoose-auto-increment](https://github.com/chevex-archived/m

## Getting Started

> npm install mongoose-plugin-autoinc
> npm install mongoose-plugin-autoinc-fix

> yarn add mongoose-plugin-autoinc-fix

Once you have the plugin installed it is very simple to use. Just pass `autoIncrement` to the `plugin()` function on your schema.

> Note: You only need to initialize MAI once.

````js
import mongoose from 'mongoose';
import { autoIncrement } from 'mongoose-plugin-autoinc';
import { autoIncrement } from 'mongoose-plugin-autoinc-fix';

const connection = mongoose.createConnection("mongodb://localhost/myDatabase");
mongoose.connect("mongodb://localhost/myDatabase");

const BookSchema = new mongoose.Schema({
author: { type: Schema.Types.ObjectId, ref: 'Author' },
Expand All @@ -36,7 +46,7 @@ const BookSchema = new mongoose.Schema({
});

BookSchema.plugin(autoIncrement, 'Book');
const Book = connection.model('Book', BookSchema);
const Book = mongoose.model('Book', BookSchema);
````

That's it. Now you can create book entities at will and they will have an `_id` field added of type `Number` and will automatically increment with each new document. Even declaring references is easy, just remember to change the reference property's type to `Number` instead of `ObjectId` if the referenced model is also using the plugin.
Expand Down Expand Up @@ -80,7 +90,7 @@ Your first book document would have a `bookId` equal to `100`. Your second book

````js
const Book = connection.model('Book', BookSchema);
Book.nextCount((err, count) => {
Book.nextCount().then(count => {

// count === 0 -> true

Expand All @@ -89,7 +99,7 @@ Book.nextCount((err, count) => {

// book._id === 0 -> true

book.nextCount((err2, count) => {
book.nextCount().then(count => {

// count === 1 -> true

Expand All @@ -116,11 +126,11 @@ book.save(err => {

// book._id === 100 -> true

book.nextCount((err1, count) => {
book.nextCount().then(count => {

// count === 101 -> true

book.resetCount((err2, nextCount) => {
book.resetCount().then(nextCount => {

// nextCount === 100 -> true

Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "mongoose-plugin-autoinc",
"version": "0.0.0-development",
"description": "This plugin allows you to auto-increment any field on any mongoose schema that you wish (forked mongoose-auto-increment in 2018).",
"name": "mongoose-plugin-autoinc-fix",
"version": "1.2.0",
"description": "==fix-bug==This plugin allows you to auto-increment any field on any mongoose schema that you wish (forked mongoose-auto-increment in 2018).",
"files": [
"lib"
],
"main": "lib/index.js",
"types": "lib/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/nodkz/mongoose-plugin-autoinc.git"
"url": "https://github.com/bigdong89/mongoose-plugin-autoinc.git"
},
"keywords": [
"mongoose",
Expand All @@ -21,7 +21,7 @@
"bugs": {
"url": "https://github.com/nodkz/mongoose-plugin-autoinc/issues"
},
"homepage": "https://github.com/nodkz/mongoose-plugin-autoinc",
"homepage": "https://github.com/bigdong89/mongoose-plugin-autoinc",
"peerDependencies": {
"mongoose": ">=4.0.0 || >=5.0.0"
},
Expand Down Expand Up @@ -80,5 +80,6 @@
"flow": "./node_modules/.bin/flow",
"test": "npm run coverage && npm run lint && npm run flow",
"semantic-release": "semantic-release"
}
},
"dependencies": {}
}
33 changes: 17 additions & 16 deletions src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ beforeAll(async () => {
mongoServer = new MongodbMemoryServer();
const mongoUrl = await mongoServer.getConnectionString();

connection = await mongoose
.createConnection(mongoUrl, {
mongoose
.connect(mongoUrl, {
autoReconnect: true,
reconnectInterval: 100,
reconnectTries: Number.MAX_VALUE,
})
// $FlowFixMe
.catch(() => {});
connection = mongoose.connection;
// connection.on('error', (...args) => console.error(...args));
});

Expand Down Expand Up @@ -56,7 +57,7 @@ describe('mongoose-auto-increment', () => {
dept: String,
});
UserSchema.plugin(autoIncrement, 'User');
const User = connection.model('User', UserSchema);
const User = mongoose.model('User', UserSchema);
await User.ensureIndexes();

const user1 = new User({ name: 'Charlie', dept: 'Support' });
Expand All @@ -76,7 +77,7 @@ describe('mongoose-auto-increment', () => {
});

UserSchema.plugin(autoIncrement, 'User');
const User = connection.model('User', UserSchema);
const User = mongoose.model('User', UserSchema);
await User.ensureIndexes();

const user1 = new User({ name: 'Charlie', dept: 'Support' });
Expand All @@ -95,7 +96,7 @@ describe('mongoose-auto-increment', () => {
});

UserSchema.plugin(autoIncrement, { model: 'User', field: 'userId' });
const User = connection.model('User', UserSchema);
const User = mongoose.model('User', UserSchema);
await User.ensureIndexes();

const user1 = new User({ name: 'Charlie', dept: 'Support' });
Expand All @@ -114,7 +115,7 @@ describe('mongoose-auto-increment', () => {
dept: String,
});
UserSchema.plugin(autoIncrement, { model: 'User', field: 'userId' });
const User = connection.model('User', UserSchema);
const User = mongoose.model('User', UserSchema);
await User.ensureIndexes();

const user1 = new User({ name: 'Charlie', dept: 'Support' });
Expand All @@ -138,7 +139,7 @@ describe('mongoose-auto-increment', () => {
});

UserSchema.plugin(autoIncrement, { model: 'User', startAt: 3 });
const User = connection.model('User', UserSchema);
const User = mongoose.model('User', UserSchema);
await User.ensureIndexes();

const user1 = new User({ name: 'Charlie', dept: 'Support' });
Expand All @@ -157,7 +158,7 @@ describe('mongoose-auto-increment', () => {
dept: String,
});
UserSchema.plugin(autoIncrement, { model: 'User', incrementBy: 5 });
const User = connection.model('User', UserSchema);
const User = mongoose.model('User', UserSchema);
await User.ensureIndexes();

const user1 = new User({ name: 'Charlie', dept: 'Support' });
Expand Down Expand Up @@ -185,7 +186,7 @@ describe('mongoose-auto-increment', () => {
field: 'userId',
groupingField: 'dept',
});
const User = connection.model('User', UserSchema);
const User = mongoose.model('User', UserSchema);
await User.ensureIndexes();

const user1 = new User({ name: 'Charlie', dept: 'Support' });
Expand Down Expand Up @@ -230,7 +231,7 @@ describe('mongoose-auto-increment', () => {
dept: String,
});
UserSchema.plugin(autoIncrement, 'User');
const User = connection.model('User', UserSchema);
const User = mongoose.model('User', UserSchema);
await User.ensureIndexes();

const user1 = new User({ name: 'Charlie', dept: 'Support' });
Expand All @@ -255,7 +256,7 @@ describe('mongoose-auto-increment', () => {
dept: String,
});
UserSchema.plugin(autoIncrement, 'User');
const User = connection.model('User', UserSchema);
const User = mongoose.model('User', UserSchema);
await User.ensureIndexes();

const user1 = new User({ name: 'Charlie', dept: 'Support' });
Expand Down Expand Up @@ -284,7 +285,7 @@ describe('mongoose-auto-increment', () => {
field: 'userId',
groupingField: 'dept',
});
const User = connection.model('User', UserSchema);
const User = mongoose.model('User', UserSchema);
await User.ensureIndexes();

const user1 = new User({ name: 'Charlie', dept: 'Support' });
Expand All @@ -311,7 +312,7 @@ describe('mongoose-auto-increment', () => {
dept: String,
});
UserSchema.plugin(autoIncrement, 'User');
const User = connection.model('User', UserSchema);
const User = mongoose.model('User', UserSchema);
await User.ensureIndexes();

const user = new User({ name: 'Charlie', dept: 'Support' });
Expand All @@ -333,7 +334,7 @@ describe('mongoose-auto-increment', () => {
dept: String,
});
UserSchema.plugin(autoIncrement, 'User');
const User = connection.model('User', UserSchema);
const User = mongoose.model('User', UserSchema);
await User.ensureIndexes();

const user = new User({ name: 'Charlie', dept: 'Support' });
Expand Down Expand Up @@ -361,7 +362,7 @@ describe('mongoose-auto-increment', () => {
field: 'orderNumber',
outputFilter: value => value * 100,
});
const User = connection.model('User', UserSchema);
const User = mongoose.model('User', UserSchema);
await User.ensureIndexes();

const user1 = new User({ name: 'Charlie', dept: 'Support' });
Expand Down Expand Up @@ -406,7 +407,7 @@ describe('mongoose-auto-increment', () => {
model: 'User',
field: 'orderNumber',
});
const User = connection.model('User', UserSchema);
const User = mongoose.model('User', UserSchema);
await User.ensureIndexes();

const user1 = new User({ name: 'Charlie', dept: 'Support' });
Expand Down
15 changes: 9 additions & 6 deletions src/__tests__/parallel-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;
mongoose.Promise = global.Promise;

let mongoServer;
let connection;
// let connection;

beforeAll(async () => {
mongoServer = new MongodbMemoryServer();
const mongoUrl = await mongoServer.getConnectionString();

connection = await mongoose
.createConnection(mongoUrl, {
mongoose
.connect(mongoUrl, {
autoReconnect: true,
reconnectInterval: 100,
reconnectTries: Number.MAX_VALUE,
})
// $FlowFixMe
.catch(() => {});
connection.on('error', (...args) => console.error(...args));
// connection = mongoose.connection;
/* connection.on('error', (...args) => {
// console.error(...args);
}); */
});

afterAll(() => {
Expand All @@ -40,7 +43,7 @@ describe('parallel writing', () => {
dept: String,
});
UserSchema.plugin(autoIncrement, 'User');
const User = connection.model('User', UserSchema);
const User = mongoose.model('User', UserSchema);

async function createUserAsync() {
return User.create({
Expand Down Expand Up @@ -72,7 +75,7 @@ describe('parallel writing', () => {
dept: String,
});
UserPidSchema.plugin(autoIncrement, { model: 'UserPid', field: 'pid' });
const UserPid = connection.model('UserPid', UserPidSchema);
const UserPid = mongoose.model('UserPid', UserPidSchema);

async function createUserPidAsync() {
return UserPid.create({
Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ counterSchema.index(
);

export function initialize(): void {
console.log(
/* console.log(
`MongooseAutoIncrement.initialize() method is deprecated. ` +
`Just remove this method, it not required anymore.`
);
); */
}

function isMongoDuplicateError(e: any): boolean {
Expand Down Expand Up @@ -315,9 +315,9 @@ export function autoIncrement(
});

// Every time documents in this schema are saved, run this logic.
schema.pre('validate', function(next: Function) {
schema.post('validate', (doc: MongooseDocument, next: Function) => {
// Get reference to the document being saved.
const doc: MongooseDocument = this;
// const doc: MongooseDocument = this;
// $FlowFixMe
const alreadyGetId = doc.__maiRanOnce === true;

Expand Down
Loading