Skip to content
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

feat(ParseObject): enable subclasses to set initial values for props #924

Open
wants to merge 7 commits into
base: alpha
Choose a base branch
from
51 changes: 51 additions & 0 deletions integration/test/ParseObjectTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1774,4 +1774,55 @@ describe('Parse Object', () => {
const fetched = await query.get(user.id);
assert.equal(fetched.isDataAvailable(), true);
});

it('retrieve subclass objects', async (done) => {
try {
class MySubclass extends Parse.Object {
constructor(attr) {
super('MySubclass', attr);
this.defaultProp = attr && attr.defaultProp || 'default';
}
get defaultProp() { return this.get('defaultProp'); }
set defaultProp(val) { this.set('defaultProp', val); }
}
Parse.Object.registerSubclass('MySubclass', MySubclass);

await new MySubclass({defaultProp: 'foo'}).save();
const result = await new Parse.Query(MySubclass).first();
expect(result.defaultProp).toBe('foo');

done();
} catch(e) {
done.fail(e);
}
});

it('conver from JSON', async (done) => {
try {

// Object without constructor
const o = Parse.Object.fromJSON({
className: 'FooObject',
name: 'foo'
}, true);

await o.save();
let result = await new Parse.Query('FooObject').first();
expect(result.get('name')).toBe('foo');

// Object with constructor `TestObject`
const to = Parse.Object.fromJSON({
className: 'TestObject',
name: 'foo'
}, true);

await to.save();
result = await new Parse.Query('TestObject').first();
expect(result.get('name')).toBe('foo');

done();
} catch(e) {
done.fail(e);
}
});
});
2 changes: 1 addition & 1 deletion integration/test/helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ParseServer = require('parse-server').ParseServer;

jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
beforeAll((done) => {
const { app } = require('../server');
const httpServer = require('http').createServer(app);
Expand Down
17 changes: 16 additions & 1 deletion src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -1741,8 +1741,23 @@ class ParseObject {
if (!json.className) {
throw new Error('Cannot create an object without a className');
}

const constructor = classMap[json.className];
const o = constructor ? new constructor() : new ParseObject(json.className);

// Remove reserved fields
const attributes = {}
Object.assign({}, json);
if(constructor && constructor.readOnlyAttributes) {
constructor.readOnlyAttributes().forEach(attr => {
try { delete attributes[attr]; }
catch { 0; }
});
}
if(attributes && attributes.__type) delete attributes.__type;
if(attributes && attributes.className) delete attributes.className;

const o = constructor ? new constructor(attributes) : new ParseObject(json.className, attributes);

const otherAttributes = {};
for (const attr in json) {
if (attr !== 'className' && attr !== '__type') {
Expand Down