1

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?

Nick Hodges
  • 16,902
  • 11
  • 68
  • 130
  • 2
    `import Inc from "mongoose-sequence"; const AutoIncrement = Inc(mongoose) `; – Shanon Jackson Sep 16 '19 at 01:23
  • @ShanonJackson -- Thanks for your interest in my problem. However, the `import` statement you gave won't compile. :-( – Nick Hodges Sep 16 '19 at 01:44
  • it will compile, its the equivalent to require syntax and also how you import someones default export. The issue may be that the typings aren't working correctly. try add // @ts-ignore above it and see if it compiles/works if it does its a typings issue – Shanon Jackson Sep 16 '19 at 02:06
  • @ShanonJackson Where is `Inc` exported by mongoose-sequence? – Nick Hodges Sep 16 '19 at 02:09
  • @ShanonJackson I get `TypeError: mongoose_sequence_1.Inc is not a function` even with the @ts-ignore – Nick Hodges Sep 16 '19 at 02:11
  • The typings say its a function so its typed possibly for the wrong version of the library. Reading through the typings it says the default export is a function that takes a mongoose schema NOT mongoose itself. Give me a second to figure this out – Shanon Jackson Sep 16 '19 at 02:25

4 Answers4

2

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.

Shanon Jackson
  • 5,873
  • 1
  • 19
  • 39
1

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.

Loránd Péter
  • 184
  • 1
  • 5
0

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],
  },
]
0

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.

Yaz Rae
  • 36
  • 3