Skip to content

Fix date in LiveQuery event trigger #7174

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

Closed

Conversation

mtrezza
Copy link
Member

@mtrezza mtrezza commented Feb 10, 2021

New Pull Request Checklist

Issue Description

Property of type Date is undefined in LiveQuery afterEvent trigger.

Related issue: closes #7082

Approach

Unknown, only added failing test case.

TODOs before merging

  • Add test cases
  • Add entry to changelog
  • Add changes to documentation (guides, repository pages, in-code descriptions)

mtrezza and others added 15 commits November 19, 2020 01:05
* commit 'ccb045b68c5b4d983a90fa125513fc476e4e2387':
  fix: upgrade @graphql-tools/links from 6.2.4 to 6.2.5 (parse-community#7007)
  fix: upgrade pg-promise from 10.7.0 to 10.7.1 (parse-community#7009)
  fix: upgrade jwks-rsa from 1.10.1 to 1.11.0 (parse-community#7008)
  fix: upgrade graphql from 15.3.0 to 15.4.0 (parse-community#7011)
  update stale bot (parse-community#6998)
  fix(beforeSave/afterSave): Return value instead of Parse.Op for nested fields (parse-community#7005)
  fix(beforeSave): Skip Sanitizing Database results (parse-community#7003)
  Fix includeAll for querying a Pointer and Pointer array (parse-community#7002)
  Init (parse-community#6999)
* commit '7f47b0427ea56214d9b0199f0fcfa4af38794e02':
  Add page localization (parse-community#7128)
  Improve contribution guide (parse-community#7075)
  fix: upgrade pg-promise from 10.9.0 to 10.9.1 (parse-community#7170)
  Add tests against multiple MongoDB versions (parse-community#7161)
  fix: upgrade mime from 2.4.7 to 2.5.0 (parse-community#7166)
  fix: upgrade pg-promise from 10.8.7 to 10.9.0 (parse-community#7168)
  fix: upgrade apollo-server-express from 2.19.1 to 2.19.2 (parse-community#7165)
  Upgrade @node-rs/bcrypt to latest version (parse-community#7159)
  Run Prettier after Definitions (parse-community#7164)
@mtrezza mtrezza changed the title Fix date in livequery trigger Fix date in LiveQuery trigger Feb 10, 2021
@mtrezza mtrezza changed the title Fix date in LiveQuery trigger Fix date in LiveQuery event trigger Feb 10, 2021
@dblythy
Copy link
Member

dblythy commented Feb 11, 2021

I'm not sure with the current Parse JS SDK can use fromJSON to save data:

it('from json save data', async (done) => {
    const json = {
      className : 'TestObject',
      date: new Date(),
      array: [],
      object: {},
      string: ''
    }
    const obj = Parse.Object.fromJSON(json);
    expect(obj.get('date')).toBeDefined()
    expect(obj.get('date')).toBeInstanceOf(Date);
    expect(obj.get('array')).toBeDefined()
    expect(obj.get('array')).toBeInstanceOf(Array);
    expect(obj.get('object')).toBeDefined()
    expect(obj.get('object')).toBeInstanceOf(Object);
    expect(obj.get('string')).toBeDefined()
    expect(obj.get('string')).toBeInstanceOf(String);
    // date, array, object, and string will be set
    await obj.save();

    await obj.fetch();
    // date, array, object, and string will be null
    expect(obj.get('date')).toBeDefined()
    expect(obj.get('date')).toBeInstanceOf(Date);
    expect(obj.get('array')).toBeDefined()
    expect(obj.get('array')).toBeInstanceOf(Array);
    expect(obj.get('object')).toBeDefined()
    expect(obj.get('object')).toBeInstanceOf(Object);
    expect(obj.get('string')).toBeDefined()
    expect(obj.get('string')).toBeInstanceOf(String);
    done();
  });

Related to this PR

@dblythy
Copy link
Member

dblythy commented Feb 11, 2021

Should be fixed by using the JS SDK master and:

const object = Parse.Object.fromJSON(json, false, true);

@dplewis
Copy link
Member

dplewis commented Feb 11, 2021

@dblythy I'll do a JS SDK release this weekend.

@dplewis
Copy link
Member

dplewis commented Feb 25, 2021

@mtrezza The SDK has been merged. Can you update from master?

@mtrezza
Copy link
Member Author

mtrezza commented Feb 25, 2021

Still fails, because of issue 1, but that may just be because of how the test case is written (toJSON -> fromJSON).

@dblythy
Copy link
Member

dblythy commented Feb 25, 2021

If you rewrite your test using the third parameter of fromJSON (dirty - whether keys should be saved), the test will pass:

it('afterEvent should work with date', async done => {
    await reconfigureServer({
      liveQuery: {
        classNames: ['TestObject'],
      },
      startLiveQueryServer: true,
    });
    Parse.Cloud.afterLiveQueryEvent('TestObject', request => {
      const date = request.object.get('date');
      expect(date).toBeDefined();
      expect(date).toBeInstanceOf(Date);
      done();
    });
  
    const query = new Parse.Query(TestObject);
    await query.subscribe();
  
    let object = new TestObject();
    object.set('date', new Date());
    const json = object.toJSON();
    json.className = 'TestObject';
    json.__type = 'Object';
    object = Parse.Object.fromJSON(json, false, true); // this is the key line
    await object.save();
  });

@mtrezza
Copy link
Member Author

mtrezza commented Feb 25, 2021

I wasn't even aware that parameter exists, which surfaces yet another issue: TypeScript, still says:
fromJSON(json: any, override?: boolean): T;

The test passes only with:

json.className = 'TestObject';
json.__type = 'Object';

but that is not something we should have in a test, because it is actually a workaround for another issue.

To make the test pass, the JSON could be created manually:

const json = 
  {
    date: {
      __type: "Date",
      iso: "2021-02-25T12:45:08.875Z",
    },
    className: "TestObject",
    __type: "Object",
  };

but that is also not something we should keep in this test, because it incorrectly assumes that the JSON syntax of Parse.Object will never change. Since this test is not intended to test the JSON syntax of Parse.Object, my suggestion is to fix the other issue first, to make this test pass in its entirety.

What do you suggest?

@dblythy
Copy link
Member

dblythy commented Feb 25, 2021

It is a new parameter recently released with V3 of the JS SDK, so that fromJSON can save data. Without it, no keys are set to dirty, so no data will be saved. This is as fromJSON was previously not to be used with saving data.

I think this one will be fixed, but i'm not sure how to test. The issue happened when the decoder was passed a raw date instead of an Object with __type date. The object decoder had no provision to return the date, so instead it returned an empty object. This was happening at the fromJSON before the afterLiveQueryEvent trigger.

See more here

@mtrezza
Copy link
Member Author

mtrezza commented Sep 3, 2021

⚠️ Important change for merging PRs from Parse Server 5.0 onwards!

We are planning to release the first beta version of Parse Server 5.0 in October 2021.

If a PR contains a breaking change and is not merged before the beta release of Parse Server 5.0, it cannot be merged until the end of 2022. Instead it has to follow the Deprecation Policy and phase-in breaking changes to be merged during the course of 2022.

One of the most voiced community feedbacks was the demand for predictability in breaking changes to make it easy to upgrade Parse Server. We have made a first step towards this by introducing the Deprecation Policy in February 2021 that assists to phase-in breaking changes, giving developers time to adapt. We will follow-up with the introduction of Release Automation and a branch model that will allow breaking changes only with a new major release, scheduled for the beginning of each calendar year.

We understand that some PRs are a long time in the making and we very much appreciate your contribution. We want to make it easy for PRs that contain a breaking change and were created before the introduction of the Deprecation Policy. These PRs can be merged with a breaking change without being phased-in before the beta release of Parse Server 5.0. We are making this exception because we appreciate that this is a time of transition that requires additional effort from contributors to adapt. We encourage everyone to prepare their PRs until the end of September and account for review time and possible adaptions.

If a PR contains a breaking change and should be merged before the beta release, please mention @parse-community/server-maintenance and we will coordinate with you to merge the PR.

Thanks for your contribution and support during this transition to Parse Server release automation!

@dblythy
Copy link
Member

dblythy commented Sep 8, 2022

Closed via parse-community/Parse-SDK-JS#1293

@dblythy dblythy closed this Sep 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

afterLiveQueryEvent hook returns empty date object
3 participants