ChuckJonas / ts-force

A Salesforce REST Client written in Typescript for Typescript
88 stars 21 forks source link

Danger of updating with stale in memory data #45

Closed ChuckJonas closed 6 years ago

ChuckJonas commented 6 years ago

Right now this library is very susceptible to causing "stale in memory" overwrites.

Repo:

  1. let acc = (await Account.Retrieve(SELECT Name FROM Account WHERE Id = 'abc123'))[0] 2: Open salesforce and update the name of account abc123 3: acc.update()

Even though you never specified any changes on the account, since we selected the Name field, it will cause your change from step 2 to get overwritten!

ChuckJonas commented 6 years ago

This could easily be achieved using ES6 proxies, but that would make the library incompatible with IE11 (does it even work in the first place?).

We would just need to update the constructor similar to this:

export class Account extends RestObject implements AccountFields {
    constructor (fields?: AccountFields) {
        super('Account');
        Object.assign(this, fields);
        return new Proxy(this, {
            set: (obj, key, value) => {
                obj[key] = value;
                if (typeof key === 'string') {
                    let decorator = getSFieldProps(obj, key);
                    if (decorator) {
                        this._modified.add(decorator.apiName);
                    }
                }
                return true;
            }
        });
    }
//...
}

The other option, which isn't as clean but would support IE11, would be to replace the properties with getters and setters. The setter would track modified.

ChuckJonas commented 6 years ago

fixed in 2.0