Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling schema.index(...) in service ctor throws an exception #2346

Closed
1 task done
orgads opened this issue Dec 25, 2024 · 4 comments
Closed
1 task done

Calling schema.index(...) in service ctor throws an exception #2346

orgads opened this issue Dec 25, 2024 · 4 comments

Comments

@orgads
Copy link
Contributor

orgads commented Dec 25, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I have a schema for which I need some compound indexes. I create the indexes in the service ctor.

When running unit tests, the ctor is called multiple times, and apparently at some point the same schema instance is injected. When I call schema.index(...) it throws an exception: MongooseError: Schema already has an index.

This started with mongoose 8.9.x. See related Automattic/mongoose#15109.

Minimum reproduction code

import { Injectable } from '@nestjs/common';
import { InjectModel, Prop, Schema } from '@nestjs/mongoose';
import { IsString } from 'class-validator';
import { Model } from 'mongoose';

@Schema({ versionKey: false })
export class Foo {
  @IsString()
  @Prop({ index: true, type: String })
  bar!: string;
  @IsString()
  @Prop({ index: true, type: String })
  baz!: string;
}

@Injectable()
export class FooService {
  constructor(@InjectModel(Foo.name) private fooModel: Model<Foo>) {
    const { schema } = fooModel;
    schema.index({ bar: 1, baz: 1 }, { unique: true }); // This throws on second call
  }
}```

### Steps to reproduce

Instantiate FooService multiple times. The ctor is called, and the same schema is injected, then `index` fails.

### Expected behavior

Not sure what is the recommended to create the index only once. Can you advise?

### Package version

10.1.0

### mongoose version

8.9.2

### NestJS version

10.4.13

### Node.js version

22.11.0

### In which operating systems have you tested?

- [ ] macOS
- [X] Windows
- [X] Linux

### Other

_No response_
@kamilmysliwiec
Copy link
Member

Why not register the index where you create the schema instead of doing it in the service constructor?

@orgads
Copy link
Contributor Author

orgads commented Dec 27, 2024

@kamilmysliwiec It is created using a class decorator. Where can I initialize the indexes?

@kamilmysliwiec
Copy link
Member

https://docs.nestjs.com/techniques/mongodb#model-injection

createForClass returns a schema instance that you can call index() on

@orgads
Copy link
Contributor Author

orgads commented Dec 27, 2024

Thank you. This works.

@orgads orgads closed this as completed Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants