To use mongoose-auto-increment with TypeScript, follow these steps:
-
Install the extend package as a dependency using npm:
npm i extend
-
Uninstall the mongoose-auto-increment package if it's already installed:
npm uninstall mongoose-auto-increment
-
Create a new file named
mongoose-auto-increment.ts
(you can choose any name you want as long as it has a .ts extension). This file will contain the TypeScript types and definitions formongoose-auto-increment
. -
In the mongoose-auto-increment.ts file, import the necessary packages and define the TypeScript types:
import { Schema, Connection } from "mongoose";
import extend from "extend";
export interface AutoIncrementSettings {
model: string;
field: string;
startAt?: number;
incrementBy?: number;
}
export interface AutoIncrementPlugin {
(schema: Schema<any>, options?: AutoIncrementSettings): void;
}
export interface AutoIncrementModule {
(connection: Connection): void;
plugin: AutoIncrementPlugin;
}
- Export the AutoIncrementModule function and the plugin function from the mongoose-auto-increment module (Just use provided files):
const autoIncrement: AutoIncrementModule = (connection: Connection) => {
extend(true, connection.base.options, {
useUnifiedTopology: true,
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
});
};
const plugin: AutoIncrementPlugin = function (schema: Schema<any>, options?: AutoIncrementSettings) {
const fields = {};
fields[options.field] = {
type: Number,
default: 0,
unique: true,
};
schema.add(fields);
schema.pre("save", function (this: any, next) {
const self = this;
if (self.isNew) {
connection
.model(options.model)
.findOne({})
.sort({ [options.field]: -1 })
.exec(function (err, doc) {
if (err) {
return next(err);
}
self[options.field] = doc ? doc[options.field] + options.incrementBy : options.startAt;
next();
});
} else {
next();
}
});
};
export default autoIncrement;
export { plugin };
- In your schema file, import mongoose-auto-increment and your AutoIncrementSettings interface:
import autoIncrement, { AutoIncrementSettings } from "./mongoose-auto-increment";
- Call the autoIncrement function and pass in your Mongoose connection:
autoIncrement(connection);
- Define your schema and pass in the AutoIncrementSettings object to the plugin method:
const UserSchema = new Schema(schema, {
timestamps: { createdAt: "created_at", updatedAt: "updated_at" },
});
UserSchema.plugin(plugin, {
model: "user",
field: "unq_id",
startAt: 1000,
incrementBy: 1,
});
- Export your model with the TypeScript interface:
export default connection.model<UserInput>("user", UserSchema) as any;