Search code examples
typescriptmongoosefeathersjs

Is there a mistake in FeathersJS generator?


When generating a service with feathersjs, the model file looks like this

import { Application } from '../declarations';

export default function (app: Application) {
  const modelName = 'comments';
  const mongooseClient = app.get('mongooseClient');
  const { Schema } = mongooseClient;
  const schema = new Schema({
    text: { type: String, required: true }
  }, {
    timestamps: true
  });

  // This is necessary to avoid model compilation errors in watch mode
  // see https://mongoosejs.com/docs/api/connection.html#connection_Connection-deleteModel
  if (mongooseClient.modelNames().includes(modelName)) {
    mongooseClient.deleteModel(modelName);
  }
  return mongooseClient.model(modelName, schema);
}

The mongooseClient is of type Mongoose and the documentation says deleteModel is under the Connection.prototype.deleteModel()

Shouldn't the line say mongooseClient.connection.deleteModel(modelName); ??


Solution

  • No it shouldn't

    If you look at the file mongoose.js under root folder you can see 'mongooseClient' is setting after mongoose.connect :

    const mongoose = require('mongoose');
    
        module.exports = function (app) {
          mongoose.connect(
            app.get('mongodb'),
            { useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true }
          );
          mongoose.Promise = global.Promise;
        
          app.set('mongooseClient', mongoose);
        };
    

    So it is part of Connection.prototype.