moleculerjs / moleculer-db

:battery: Database access service mixins for Moleculer
https://moleculer.services/
MIT License
151 stars 122 forks source link

Unable to get moleculer-db mongoose working #21

Closed nurdism closed 6 years ago

nurdism commented 6 years ago

I'm running a very simple service, and I just can't get it to work, it just stops after 'Connected successfully...', no errors, it just stops, and I can't for the life of me figure out what is going on. Here's my test repo any help would be much appreciated.

$ node -v
v9.11.1
$ npm -v
5.8.0
icebob commented 6 years ago

Thanks. It seems, that problem is that, npm keeps an own mongoose version for adapter. Therefore, your Model instance is not inherited from the adapter mongoose version. Currently I don't know why...

image

icebob commented 6 years ago

A workaround if you uses schema & modelName in Service schem instead of model because in this case you shouldn't require the mongoose lib.

It works:

module.exports = {
  name: "test",
  mixins: [DbService],
  adapter: new MongooseAdapter("mongodb://localhost/test"),
  schema: {
    title: { type: String },
    content: { type: String },
    votes: { type: Number, default: 0}
  },
  modelName: "test",
  afterConnected() {
      this.logger.info("Connected successfully...");
      return this.adapter.clear().then(() => {
          this.logger.info("Seed Posts collection...");
          return this.adapter.insertMany([
              { title: "1st post", content: "First post content.", votes: 3 },
              { title: "2nd post", content: "Labore eum veritatis ut.", votes: 8 },
              { title: "3rd post", content: "Rerum deleniti repellendus error ea.", votes: 0 },
              { title: "4th post", content: "Normal post content.", votes: 4 },
              { title: "5th post", content: "Voluptatum praesentium voluptatibus est nesciunt fugiat." }
          ]);
      });
  }
}
nurdism commented 6 years ago

Wouldn't making mongoose a peer dependency fix that issue?

icebob commented 6 years ago

Yes, I will change it.

icebob commented 6 years ago

Fixed: https://github.com/moleculerjs/moleculer-db/releases/tag/moleculer-db-adapter-mongoose%400.7.0