Skip to content

Adds Purge & Polygon to Parse.Schema #544

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 4 commits into from
Jan 28, 2018
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
28 changes: 28 additions & 0 deletions integration/test/ParseSchemaTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('Schema', () => {
.addDate('dateField')
.addFile('fileField')
.addGeoPoint('geoPointField')
.addPolygon('polygonField')
.addArray('arrayField')
.addObject('objectField')
.addPointer('pointerField', '_User')
Expand All @@ -62,6 +63,7 @@ describe('Schema', () => {
assert.equal(result.fields.dateField.type, 'Date');
assert.equal(result.fields.fileField.type, 'File');
assert.equal(result.fields.geoPointField.type, 'GeoPoint');
assert.equal(result.fields.polygonField.type, 'Polygon');
assert.equal(result.fields.arrayField.type, 'Array');
assert.equal(result.fields.objectField.type, 'Object');
assert.equal(result.fields.pointerField.type, 'Pointer');
Expand Down Expand Up @@ -132,6 +134,32 @@ describe('Schema', () => {
});
});

it('purge', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
const obj = new Parse.Object('SchemaTest');
obj.save().then(() => {
return testSchema.delete().then(() => {
// Should never reach here
assert.equal(true, false);
}).catch((error) => {
assert.equal(error.code, Parse.Error.INVALID_SCHEMA_OPERATION);
assert.equal(error.message, 'Class SchemaTest is not empty, contains 1 objects, cannot drop schema.');
return Parse.Promise.as();
});
}).then(() => {
return testSchema.purge();
}).then(() => {
const query = new Parse.Query('SchemaTest');
return query.count();
}).then((count) => {
assert.equal(count, 0);
// Delete only works on empty schema, extra check
return testSchema.delete();
}).then(() => {
done();
});
});

it('save index', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
const index = {
Expand Down
3 changes: 2 additions & 1 deletion src/CoreManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type RESTController = {
ajax: (method: string, url: string, data: any, headers?: any) => ParsePromise;
};
type SchemaController = {
purge: (className: string) => ParsePromise;
get: (className: string, options: RequestOptions) => ParsePromise;
delete: (className: string, options: RequestOptions) => ParsePromise;
create: (className: string, params: any, options: RequestOptions) => ParsePromise;
Expand Down Expand Up @@ -295,7 +296,7 @@ module.exports = {
},

setSchemaController(controller: SchemaController) {
requireMethods('SchemaController', ['get', 'create', 'update', 'delete', 'send'], controller);
requireMethods('SchemaController', ['get', 'create', 'update', 'delete', 'send', 'purge'], controller);
config['SchemaController'] = controller;
},

Expand Down
10 changes: 9 additions & 1 deletion src/ParseError.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ParseError {
this.code = code;
this.message = message;
}

toString() {
return 'ParseError: ' + this.code + ' ' + this.message;
}
Expand Down Expand Up @@ -466,6 +466,14 @@ ParseError.INVALID_LINKED_SESSION = 251;
*/
ParseError.UNSUPPORTED_SERVICE = 252;

/**
* Error code indicating an invalid operation occured on schema
* @property INVALID_SCHEMA_OPERATION
* @static
* @final
*/
ParseError.INVALID_SCHEMA_OPERATION = 255;

/**
* Error code indicating that there were multiple errors. Aggregate errors
* have an "errors" property, which is an array of error objects with more
Expand Down
51 changes: 50 additions & 1 deletion src/ParseSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import ParsePromise from './ParsePromise';

import type { RequestOptions, FullOptions } from './RESTController';

const FIELD_TYPES = ['String', 'Number', 'Boolean', 'Date', 'File', 'GeoPoint', 'Array', 'Object', 'Pointer', 'Relation'];
const FIELD_TYPES = ['String', 'Number', 'Boolean', 'Date', 'File', 'GeoPoint', 'Polygon', 'Array', 'Object', 'Pointer', 'Relation'];

/**
* A Parse.Schema object is for handling schema data from Parse.
Expand Down Expand Up @@ -181,6 +181,7 @@ class ParseSchema {

/**
* Removing a Schema from Parse
* Can only be used on Schema without objects
*
* @param {Object} options A Backbone-style options object.
* Valid options are:<ul>
Expand All @@ -207,6 +208,34 @@ class ParseSchema {
})._thenRunCallbacks(options);
}

/**
* Removes all objects from a Schema (class) in Parse.
* EXERCISE CAUTION, running this will delete all objects for this schema and cannot be reversed
*
* @param {Object} options A Backbone-style options object.
* Valid options are:<ul>
* <li>success: A Backbone-style success callback
* <li>error: An Backbone-style error callback.
* <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
* be used for this request.
* <li>sessionToken: A valid session token, used for making a request on
* behalf of a specific user.
* </ul>
*
* @return {Parse.Promise} A promise that is resolved with the result when
* the query completes.
*/
purge(options: FullOptions) {
this.assertClassName();

const controller = CoreManager.getSchemaController();

return controller.purge(this.className)
.then((response) => {
return response;
})._thenRunCallbacks(options);
}

/**
* Assert if ClassName has been filled
* @private
Expand Down Expand Up @@ -319,6 +348,16 @@ class ParseSchema {
return this.addField(name, 'GeoPoint');
}

/**
* Adding Polygon Field
*
* @param {String} name Name of the field that will be created on Parse
* @return {Parse.Schema} Returns the schema, so you can chain this call.
*/
addPolygon(name: string) {
return this.addField(name, 'Polygon');
}

/**
* Adding Array Field
*
Expand Down Expand Up @@ -437,6 +476,16 @@ const DefaultController = {

delete(className: string, options: RequestOptions): ParsePromise {
return this.send(className, 'DELETE', {}, options);
},

purge(className: string): ParsePromise {
const RESTController = CoreManager.getRESTController();
return RESTController.request(
'DELETE',
`purge/${className}`,
{},
{ useMasterKey: true }
);
}
};

Expand Down
6 changes: 4 additions & 2 deletions src/__tests__/CoreManager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ describe('CoreManager', () => {
get: function() {},
create: function() {},
update: function() {},
delete: function() {}
delete: function() {},
purge: function() {},
})).not.toThrow();
});

Expand All @@ -338,7 +339,8 @@ describe('CoreManager', () => {
get: function() {},
create: function() {},
update: function() {},
delete: function() {}
delete: function() {},
purge: function() {},
};

CoreManager.setSchemaController(controller);
Expand Down
46 changes: 39 additions & 7 deletions src/__tests__/ParseSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('ParseSchema', () => {
.addDate('dateField')
.addFile('fileField')
.addGeoPoint('geoPointField')
.addPolygon('polygonField')
.addArray('arrayField')
.addObject('objectField')
.addPointer('pointerField', '_User')
Expand All @@ -59,6 +60,7 @@ describe('ParseSchema', () => {
expect(schema._fields.dateField.type, 'Date');
expect(schema._fields.fileField.type, 'File');
expect(schema._fields.geoPointField.type, 'GeoPoint');
expect(schema._fields.polygonField.type, 'Polygon');
expect(schema._fields.arrayField.type, 'Array');
expect(schema._fields.objectField.type, 'Object');
expect(schema._fields.pointerField.type, 'Pointer');
Expand Down Expand Up @@ -162,19 +164,13 @@ describe('ParseSchema', () => {
done();
});

// CoreManager.setSchemaController({
// send() {},
// get() {},
// create() {},
// update() {},
// delete() {},
// });
it('can save schema', (done) => {
CoreManager.setSchemaController({
send() {},
get() {},
update() {},
delete() {},
purge() {},
create(className, params, options) {
expect(className).toBe('SchemaTest');
expect(params).toEqual({
Expand Down Expand Up @@ -202,6 +198,7 @@ describe('ParseSchema', () => {
get() {},
create() {},
delete() {},
purge() {},
update(className, params, options) {
expect(className).toBe('SchemaTest');
expect(params).toEqual({
Expand Down Expand Up @@ -229,6 +226,7 @@ describe('ParseSchema', () => {
create() {},
update() {},
get() {},
purge() {},
delete(className, options) {
expect(className).toBe('SchemaTest');
expect(options).toEqual({});
Expand All @@ -243,12 +241,33 @@ describe('ParseSchema', () => {
});
});

it('can purge schema', (done) => {
CoreManager.setSchemaController({
send() {},
create() {},
update() {},
get() {},
delete() {},
purge(className) {
expect(className).toBe('SchemaTest');
return ParsePromise.as([]);
},
});

var schema = new ParseSchema('SchemaTest');
schema.purge().then((results) => {
expect(results).toEqual([]);
done();
});
});

it('can get schema', (done) => {
CoreManager.setSchemaController({
send() {},
create() {},
update() {},
delete() {},
purge() {},
get(className, options) {
expect(className).toBe('SchemaTest');
expect(options).toEqual({});
Expand All @@ -269,6 +288,7 @@ describe('ParseSchema', () => {
create() {},
update() {},
delete() {},
purge() {},
get(className, options) {
expect(className).toBe('SchemaTest');
expect(options).toEqual({ sessionToken: 1234 });
Expand All @@ -289,6 +309,7 @@ describe('ParseSchema', () => {
create() {},
update() {},
delete() {},
purge() {},
get(className, options) {
expect(className).toBe('SchemaTest');
expect(options).toEqual({});
Expand All @@ -313,6 +334,7 @@ describe('ParseSchema', () => {
create() {},
update() {},
delete() {},
purge() {},
get(className, options) {
expect(className).toBe('');
expect(options).toEqual({});
Expand All @@ -334,6 +356,7 @@ describe('ParseSchema', () => {
create() {},
update() {},
delete() {},
purge() {},
get(className, options) {
expect(className).toBe('');
expect(options).toEqual({ sessionToken: 1234 });
Expand All @@ -355,6 +378,7 @@ describe('ParseSchema', () => {
create() {},
update() {},
delete() {},
purge() {},
get(className, options) {
expect(className).toBe('');
expect(options).toEqual({});
Expand Down Expand Up @@ -418,4 +442,12 @@ describe('SchemaController', () => {
done();
});
});

it('purge schema', (done) => {
var schema = new ParseSchema('SchemaTest');
schema.purge().then((results) => {
expect(results).toEqual([]);
done();
});
});
});