parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

DefinedSchema: Indexes not created correctly for Pointer types

Open sadortun opened this issue 2 years ago • 12 comments

New Issue Checklist

/cc @Moumouls

Issue Description

When creating indexes on Pointer fields, the index is not created on the correct field name in MongoDB

Parse will add a _p_ prefix to the field name, but DefinedSchema won't. This have for consequence to leave the Pointer field without an index, and creating an index on a non-existant field.

Had a chat with @Moumouls about this, here is his comment:

The correct version indeed is the version with the _p_, it is a quick fix that was pushed during the pr of the defined schemas. I remember it needed some refactor to allow just the field name without the p because of validators.

Steps to reproduce

For example:

export default SchemaMigrations.makeSchema('Message', {
   fields: {
      recipient: { type: 'Pointer', targetClass: '_User', required: true },
   },
   indexes: {
       recipient: { recipient: 1 },
   },

Looking at the indexes:

db.getCollection('Message').stats()
{
   "_id_": new NumberInt("749568"),
    "recipient": new NumberInt("90112"),
}

In this case, Parse will create a DB field named _p_recipient, not recipient.

This have for effect of leaving the _p_recipient un-indexed, and creating an unused index recipient

Actual Outcome

Index is created on unused field recipient

Expected Outcome

Index need to be created on _p_recipient

Environment

Server

  • Parse Server version: 5
  • Operating system: w11
  • Local or remote host (AWS, Azure, Google Cloud, Heroku, Digital Ocean, etc): gcloud

Database

  • System (MongoDB or Postgres): MongoDB
  • Database version: 4.4
  • Local or remote host (MongoDB Atlas, mLab, AWS, Azure, Google Cloud, etc): gcloud

Client

  • SDK (iOS, Android, JavaScript, PHP, Unity, etc): javascript
  • SDK version: 4

Logs

sadortun avatar Mar 02 '23 20:03 sadortun

Thanks for opening this issue!

  • 🚀 You can help us to fix this issue faster by opening a pull request with a failing test. See our Contribution Guide for how to make a pull request, or read our New Contributor's Guide if this is your first time contributing.

I can work on this, but in order to advance, I need additional details about the problem. Any advice or suggestions for me?

rgunindi avatar Mar 09 '23 22:03 rgunindi

      const server = await reconfigureServer();
      const indexes = { complex: {  createdAt: 1, updatedAt: 1, recipient:1 } };
      const schemas = {
        definitions: [{ className: 'Test', fields: { recipient: { type: 'Pointer', targetClass:'_User',require:true } }, indexes }],
      };
      await new DefinedSchemas(schemas, server.config).execute();
      const schema = await new Parse.Schema('Test').get();
      cleanUpIndexes(schema);
      expect(schema.indexes).withContext("Schema indexes did not match expected indexes");
    }) 

I ran this test case for this state but couldn't find a bug. Are you certain about this?

. ✓ should create index with p prefix for pointer

@sadortun

rgunindi avatar Mar 10 '23 20:03 rgunindi

@rgunindi

Can you check :

  1. schema.indexes contains _p_recipient

  2. MongoDB console

db.getCollection('Test').stats()
{
   "_id_": new NumberInt("1234"),
    "_p_recipient": new NumberInt("1234"),
}

sadortun avatar Mar 10 '23 20:03 sadortun

@sadortun

image

        definitions: [{
          className: 'Test', fields: {
            recipient: { type: 'Pointer', targetClass: '_User', required: true },
          },
          indexes: {
            recipient: { recipient: 1 },
          }
        }
        ],
      };

I re-run and the result same

image

where is the _p_recipient ?

rgunindi avatar Mar 11 '23 09:03 rgunindi

@rgunindi

If you add data, you'll see in MongoDB the pointer field will be in the _p_recipient field. But the index is created on recipient.

So, the actual pointer will not be indexed.

sadortun avatar Mar 11 '23 20:03 sadortun

Hi @sadortun @rgunindi , @sadortun is right using a "classic" index on Pointers in a Parse Schema will not work.

I added a quick fix when i worked on Defined Schemas, it's documented in the index section of the Defined Schema docs see: https://docs.parseplatform.org/defined-schema/guide/#indexes

I don't remember exactly but it was a quick fix and the PR to correctly support indexes on pointers without "p" was a little bit heavier than supporting the "p"

But feel free to open a PR to correctly support the pointer index, i'll be happy to review it !

Moumouls avatar Apr 08 '23 11:04 Moumouls

@Moumouls @sadortun resim

That's the way it should be, isn't it?

rgunindi avatar May 13 '23 21:05 rgunindi

Yes @rgunindi if you have a Parse Pointer on the recipient field !

Moumouls avatar May 13 '23 22:05 Moumouls

@Moumouls I have applied the fix for this issue and opened pr. Thank you for your patience. Please report deficiencies

Pr Link

rgunindi avatar May 13 '23 22:05 rgunindi

@mtrezza i think this could be closed, a fix is documented about using indexes on pointers, it's not the best DX, but it works fine and is documented: https://docs.parseplatform.org/defined-schema/guide/#indexes

Moumouls avatar Oct 24 '24 06:10 Moumouls

What about the PR that has been submitted?

mtrezza avatar Oct 24 '24 22:10 mtrezza