moleculerjs / moleculerjs/database

Populate.keyField ignored?

Open
#51 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
33
Forks
15
Avg merge
15m
Merged PRs (30d)
1

Description

We are trying to use the keyField parameter to use _id instead of id


_id: { type: "string", primaryKey: true },
country: {
        type: "string",
        required: true,
        populate: {
          keyField: "_id",
          action: "countries.get",
        },
      },

But we are getting validation errors on countries.get saying that _id is undefined. Seems like id is hardcoded?
https://github.com/moleculerjs/database/blob/3e962ec89f191d8c3a4de25eb8e7cfb2390e9716/src/transform.js#L186-L190

Here's a workaround we found:

country: {
        type: "string",
        required: true,
        populate: async (ctx, values, entities, field) => {
          return Promise.all(
            entities.map(
              async (entity) =>
                (entity.postCount = await ctx.call(
                  "countries.get",
                  { _id: entity.country }
                ))
            )
          );
        },
      },

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/transform.js around lines 186-190 and trace how populate.keyField is used when building the countries.get call. Reproduce the schema with _id as the primary key; done means keyField: "_id" is honored without the validation error described in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
databases
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.