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

Fix date in LiveQuery event trigger

Open mtrezza opened this issue 4 years ago • 9 comments
trafficstars

New Pull Request Checklist

  • [x] I am not disclosing a vulnerability.
  • [x] I am creating this PR in reference to an issue.

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 avatar Feb 10 '21 15:02 mtrezza

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 avatar Feb 11 '21 03:02 dblythy

Should be fixed by using the JS SDK master and:

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

dblythy avatar Feb 11 '21 04:02 dblythy

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

dplewis avatar Feb 11 '21 06:02 dplewis

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

dplewis avatar Feb 25 '21 01:02 dplewis

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

mtrezza avatar Feb 25 '21 01:02 mtrezza

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();
  });

dblythy avatar Feb 25 '21 02:02 dblythy

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?

mtrezza avatar Feb 25 '21 12:02 mtrezza

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

dblythy avatar Feb 25 '21 13:02 dblythy

⚠️ 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!

mtrezza avatar Sep 03 '21 00:09 mtrezza

Closed via https://github.com/parse-community/Parse-SDK-JS/pull/1293

dblythy avatar Sep 08 '22 14:09 dblythy