Skip to content

feat(ParseObject): Add option cascadeSave to save() #881

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions integration/test/ParseObjectTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,44 @@ describe('Parse Object', () => {
});
});

it('can skip cascade saving as per request', async(done) => {
const Parent = Parse.Object.extend('Parent');
const Child = Parse.Object.extend('Child');

const parent = new Parent();
const child1 = new Child();
const child2 = new Child();
const child3 = new Child();

child1.set('name', 'rob');
child2.set('name', 'sansa');
child3.set('name', 'john');
parent.set('children', [child1, child2]);
parent.set('bastard', child3);

expect(parent.save).toThrow();
let results = await new Parse.Query(Child).find();
assert.equal(results.length, 0);

await parent.save(null, { cascadeSave: true });
results = await new Parse.Query(Child).find();
assert.equal(results.length, 3);

parent.set('dead', true);
child1.set('dead', true);
await parent.save(null);
const rob = await new Parse.Query(Child).equalTo('name', 'rob').first();
expect(rob.get('dead')).toBe(true);

parent.set('lastname', 'stark');
child3.set('lastname', 'stark');
await parent.save(null, { cascadeSave: false });
const john = await new Parse.Query(Child).doesNotExist('lastname').first();
expect(john.get('lastname')).toBeUndefined();

done();
});

it('can do two saves at the same time', (done) => {
const object = new TestObject();
let firstSave = true;
Expand Down
12 changes: 9 additions & 3 deletions src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ type SaveParams = {
body: AttributeMap;
};

type SaveOptions = FullOptions & {
cascadeSave?: boolean
}

const DEFAULT_BATCH_SIZE = 20;

// Mapping of class names to constructors, so we can populate objects from the
Expand Down Expand Up @@ -1131,6 +1135,7 @@ class ParseObject {
* be used for this request.
* <li>sessionToken: A valid session token, used for making a request on
* behalf of a specific user.
* <li>cascadeSave: If `false`, nested objects will not be saved (default is `true`).
* </ul>
* </li>
* </ul>
Expand All @@ -1143,15 +1148,16 @@ class ParseObject {
* be used for this request.
* <li>sessionToken: A valid session token, used for making a request on
* behalf of a specific user.
* <li>cascadeSave: If `false`, nested objects will not be saved (default is `true`).
* </ul>
*
* @return {Promise} A promise that is fulfilled when the save
* completes.
*/
save(
arg1: ?string | { [attr: string]: mixed },
arg2: FullOptions | mixed,
arg3?: FullOptions
arg2: SaveOptions | mixed,
arg3?: SaveOptions
): Promise {
let attrs;
let options;
Expand Down Expand Up @@ -1200,7 +1206,7 @@ class ParseObject {
saveOptions.sessionToken = options.sessionToken;
}
const controller = CoreManager.getObjectController();
const unsaved = unsavedChildren(this);
const unsaved = options.cascadeSave !== false ? unsavedChildren(this) : null;
return controller.save(unsaved, saveOptions).then(() => {
return controller.save(this, saveOptions);
});
Expand Down