Nick Hodges
Nick Hodges

Reputation: 17138

How do I use the mongoose-sequence project in TypeScript?

I am trying to build a REST API server with an auto-incrementing field with Mongoose, Express, and TypeScript using the mongoose-sequence project. (This project seems like the most popular and supported of the ones out there.)

I have run npm install --save @types\mongoose-sequence to import the types for TypeScript.

However, I can't seem to figure out how to correctly make use of it.

The code in the README says to do the following:

const AutoIncrement = require('mongoose-sequence')(mongoose);

How does that translate into TypeScript?

Can someone explain the basics to get me rolling?

Upvotes: 1

Views: 4051

Answers (4)

Yaz Rae
Yaz Rae

Reputation: 36

mongoose-auto-increment package could be used here for the same object as follows

 import { createConnection, Schema } from 'mongoose';
 import autoIncrement from 'mongoose-auto-increment';

 const connection = await mongoose.createConnection(DATABASE_URL);
 autoIncrement.initialize(connection);

 const modelSchema = new Schema({...});

 modelSchema.plugin(autoIncrement.plugin, 'Model');

 export default model('Model', modelSchema);

mongoose.createConnection() could be imported if the data base connection was established in another definition.

Upvotes: 0

dieson1111
dieson1111

Reputation: 1

This is how I use the mongoose-sequence for Nest.js

import mongoose, { Connection } from 'mongoose'
import { MatchEntity } from './entities/match.entity'
import { DB_CONNECTION, MATCH_MODEL, MATCH_NAME } from '../constants'

export const matchesProviders = [
  {
    provide: MATCH_MODEL,
    useFactory: (connection: Connection) => {
      const inc = require('mongoose-sequence')(mongoose)
      MatchEntity.plugin(inc, { inc_field: 'id' })
      return connection.model(MATCH_NAME, MatchEntity)
    },
    inject: [DB_CONNECTION],
  },
]

Upvotes: 0

Loránd Péter
Loránd Péter

Reputation: 314

My solution is the following based on Shanon's answer (passing the mongoose instance instead of a schema):

import mongoose from "mongoose";
// @ts-ignore
import Inc from "mongoose-sequence";

const AutoIncrement = Inc(mongoose); 

// define your schema

UserSchema.plugin(AutoIncrement, { id: "user_id", inc_field: "id" });

I've tried many approaches and realised that using types with @types/mongoose-sequence (version "^3.0.6") breaks the code becauese it says that the library exports a void function and of course you cannot use a void value later. So that's why the @ts-ignore is present.

Upvotes: 3

Shanon Jackson
Shanon Jackson

Reputation: 6581

This code works for me both on a type-level and a value level.

import Inc from "mongoose-sequence";
import { userSchema } from "../SOME-MONGOOSE-SCHEMA.ts";
const AutoIncrement = Inc(userSchema);

Note that this contradicts the documentation, which says you should pass it a mongoose instance but the typings say it takes a mongoose schema.

Upvotes: 2

Related Questions