moleculerjs / moleculer-db

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

populate not working in moleculer-db-adapter-sequelize #296

Open capcaicah opened 2 years ago

capcaicah commented 2 years ago

HI, i try use populate to connect my detail to my master, but our populate not display. maybe can you help me

this is my code:

user_detail.js

brokerUser.createService({
  // Define service name
  name: "user_detail",

  mixins: [DbService],

  adapter : new SqlAdapter('node-user', 'root', '', {
            host: 'localhost',
            dialect:'mysql',
          }),

  model: {
    name: "user_details",
    define: {
        birth_date: Sequelize.DATE,
    },
    options:{
      timestamps: false,
      createdAt: false,
      updatedAt: false,
    }

  }
});

user.js

brokerUser.createService({
  // Define service name
  name: "user",

  mixins: [DbService],

  adapter : new SqlAdapter('node-user', 'root', '', {
            host: 'localhost',
            dialect:'mysql',
          }),

  model: {
    name: "users",
    define: {
        name: Sequelize.STRING,
        email: Sequelize.STRING,
    },
    options:{
      timestamps: false,
      createdAt: false,
      updatedAt: false,
    }

  },
  settings: {
    populates:{
      'birth_date':'user_detail.get'
    }
  }
});

index.js

brokerUsers.createService({
  // Define service name
  name: "users",

  actions:{
    list(ctx){
      return brokerUsers.call('user.list',{populate:['birth_date']});
    }
  }
});
icebob commented 2 years ago

Please create an exact repro code on repl.it.

capcaicah commented 2 years ago

hi @icebob this is th repl.it:

https://replit.com/@BothGameplay/-ms-users

icebob commented 2 years ago

I mean a working example that works on repl.it without transporter and MySQL (use sqlite)

aheadrox commented 2 years ago

Work with the _id field.

irzhywau commented 2 years ago

@aheadrox What do you mean here exactly? I'm facing to same issue, using postgresql with this settings in the service

// membership.service
...
settings: {
  fields: ["id", "role", "userId", "communityId", "user"],
  populates: {
    "user": {
      field: "userId",
      action: "users.get",
      params: {
        fields: ["id", "name", "email", "avatar"]
      }
    }
  }
}
...

when calling it I have this piece of code

return ctx.call('membership.find', {
  query: {
    communityId
  },
  populate: ["user"]
});
irzhywau commented 2 years ago

@icebob I confirm this issue as stated in my previous comment What I find weird is that when I remove the query section and only let populate in the call args it seems working. but when a query is added into the args the populate doesn't work

With query

mol $ call membership.find '{"populate":["user"], "query": {"communityId": 1}}'
>> Call 'membership.find' with params: { populate: [ 'user' ], query: { communityId: 1 } }
>> Execution time:18ms
>> Response:
[
  {
    id: 1,
    userId: 4,
    communityId: 1,
    role: 'citizen',
  }
]

Without query

mol $ call membership.find '{"populate":["user"]}'
>> Call 'membership.find' with params: { populate: [ 'user' ] }
>> Execution time:2ms
>> Response:
[
  {
    id: 1,
    userId: 4,
    communityId: 1,
    role: 'citizen',
    user: {
      id: 4,
      name: 'Tester',
      email: 'user@email.co',
      surname: null,
      avatar: null,
    }
  },
  {
    id: 2,
    userId: 1,
    communityId: 2,
    role: 'citizen',
    user: {
...

And this only works for find method, not working on list and get neither

Any idea?

icebob commented 2 years ago

I need a repro example (without any external tools and schemas) what I can debug to investigate the problem.

irzhywau commented 2 years ago

I think you can use the replit you setup, just need to define 2 different service which are related My setup contains something else that are not really involved in this issue

irzhywau commented 2 years ago

@icebob here is a full reproduction https://replit.com/@irzhywau/issue-296 you can run npm run dev to run it

Im still wondering what Im doing wrong, either the library is buggy

AndreMaz commented 2 years ago

@irzhywau tested your repro and, after setting the userId@memberships.db to same value as _id@users.db, got the same result with both call membership.find '{"populate":["user"], "query": {"communityId": 1}}' and call membership.find '{"populate":["user"]}'

image

It seems that user field was properly populated, right? Can you please provide more info about the issue that you're facing?