Skip to content

Commit 741958a

Browse files
dplewisflovilmart
authored andcommitted
Adds Purge & Polygon to Parse.Schema (#544)
* Purge & Polygon to Parse.Schema * Purge & Polygon to Parse.Schema * Revert "Purge & Polygon to Parse.Schema" This reverts commit 8d95e05. * fix error
1 parent 7ef1b0a commit 741958a

File tree

6 files changed

+132
-12
lines changed

6 files changed

+132
-12
lines changed

integration/test/ParseSchemaTest.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ describe('Schema', () => {
4747
.addDate('dateField')
4848
.addFile('fileField')
4949
.addGeoPoint('geoPointField')
50+
.addPolygon('polygonField')
5051
.addArray('arrayField')
5152
.addObject('objectField')
5253
.addPointer('pointerField', '_User')
@@ -62,6 +63,7 @@ describe('Schema', () => {
6263
assert.equal(result.fields.dateField.type, 'Date');
6364
assert.equal(result.fields.fileField.type, 'File');
6465
assert.equal(result.fields.geoPointField.type, 'GeoPoint');
66+
assert.equal(result.fields.polygonField.type, 'Polygon');
6567
assert.equal(result.fields.arrayField.type, 'Array');
6668
assert.equal(result.fields.objectField.type, 'Object');
6769
assert.equal(result.fields.pointerField.type, 'Pointer');
@@ -132,6 +134,32 @@ describe('Schema', () => {
132134
});
133135
});
134136

137+
it('purge', (done) => {
138+
const testSchema = new Parse.Schema('SchemaTest');
139+
const obj = new Parse.Object('SchemaTest');
140+
obj.save().then(() => {
141+
return testSchema.delete().then(() => {
142+
// Should never reach here
143+
assert.equal(true, false);
144+
}).catch((error) => {
145+
assert.equal(error.code, Parse.Error.INVALID_SCHEMA_OPERATION);
146+
assert.equal(error.message, 'Class SchemaTest is not empty, contains 1 objects, cannot drop schema.');
147+
return Parse.Promise.as();
148+
});
149+
}).then(() => {
150+
return testSchema.purge();
151+
}).then(() => {
152+
const query = new Parse.Query('SchemaTest');
153+
return query.count();
154+
}).then((count) => {
155+
assert.equal(count, 0);
156+
// Delete only works on empty schema, extra check
157+
return testSchema.delete();
158+
}).then(() => {
159+
done();
160+
});
161+
});
162+
135163
it('save index', (done) => {
136164
const testSchema = new Parse.Schema('SchemaTest');
137165
const index = {

src/CoreManager.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ type RESTController = {
7878
ajax: (method: string, url: string, data: any, headers?: any) => ParsePromise;
7979
};
8080
type SchemaController = {
81+
purge: (className: string) => ParsePromise;
8182
get: (className: string, options: RequestOptions) => ParsePromise;
8283
delete: (className: string, options: RequestOptions) => ParsePromise;
8384
create: (className: string, params: any, options: RequestOptions) => ParsePromise;
@@ -295,7 +296,7 @@ module.exports = {
295296
},
296297

297298
setSchemaController(controller: SchemaController) {
298-
requireMethods('SchemaController', ['get', 'create', 'update', 'delete', 'send'], controller);
299+
requireMethods('SchemaController', ['get', 'create', 'update', 'delete', 'send', 'purge'], controller);
299300
config['SchemaController'] = controller;
300301
},
301302

src/ParseError.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ParseError {
2020
this.code = code;
2121
this.message = message;
2222
}
23-
23+
2424
toString() {
2525
return 'ParseError: ' + this.code + ' ' + this.message;
2626
}
@@ -466,6 +466,14 @@ ParseError.INVALID_LINKED_SESSION = 251;
466466
*/
467467
ParseError.UNSUPPORTED_SERVICE = 252;
468468

469+
/**
470+
* Error code indicating an invalid operation occured on schema
471+
* @property INVALID_SCHEMA_OPERATION
472+
* @static
473+
* @final
474+
*/
475+
ParseError.INVALID_SCHEMA_OPERATION = 255;
476+
469477
/**
470478
* Error code indicating that there were multiple errors. Aggregate errors
471479
* have an "errors" property, which is an array of error objects with more

src/ParseSchema.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import ParsePromise from './ParsePromise';
1414

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

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

1919
/**
2020
* A Parse.Schema object is for handling schema data from Parse.
@@ -181,6 +181,7 @@ class ParseSchema {
181181

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

211+
/**
212+
* Removes all objects from a Schema (class) in Parse.
213+
* EXERCISE CAUTION, running this will delete all objects for this schema and cannot be reversed
214+
*
215+
* @param {Object} options A Backbone-style options object.
216+
* Valid options are:<ul>
217+
* <li>success: A Backbone-style success callback
218+
* <li>error: An Backbone-style error callback.
219+
* <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
220+
* be used for this request.
221+
* <li>sessionToken: A valid session token, used for making a request on
222+
* behalf of a specific user.
223+
* </ul>
224+
*
225+
* @return {Parse.Promise} A promise that is resolved with the result when
226+
* the query completes.
227+
*/
228+
purge(options: FullOptions) {
229+
this.assertClassName();
230+
231+
const controller = CoreManager.getSchemaController();
232+
233+
return controller.purge(this.className)
234+
.then((response) => {
235+
return response;
236+
})._thenRunCallbacks(options);
237+
}
238+
210239
/**
211240
* Assert if ClassName has been filled
212241
* @private
@@ -319,6 +348,16 @@ class ParseSchema {
319348
return this.addField(name, 'GeoPoint');
320349
}
321350

351+
/**
352+
* Adding Polygon Field
353+
*
354+
* @param {String} name Name of the field that will be created on Parse
355+
* @return {Parse.Schema} Returns the schema, so you can chain this call.
356+
*/
357+
addPolygon(name: string) {
358+
return this.addField(name, 'Polygon');
359+
}
360+
322361
/**
323362
* Adding Array Field
324363
*
@@ -437,6 +476,16 @@ const DefaultController = {
437476

438477
delete(className: string, options: RequestOptions): ParsePromise {
439478
return this.send(className, 'DELETE', {}, options);
479+
},
480+
481+
purge(className: string): ParsePromise {
482+
const RESTController = CoreManager.getRESTController();
483+
return RESTController.request(
484+
'DELETE',
485+
`purge/${className}`,
486+
{},
487+
{ useMasterKey: true }
488+
);
440489
}
441490
};
442491

src/__tests__/CoreManager-test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ describe('CoreManager', () => {
328328
get: function() {},
329329
create: function() {},
330330
update: function() {},
331-
delete: function() {}
331+
delete: function() {},
332+
purge: function() {},
332333
})).not.toThrow();
333334
});
334335

@@ -338,7 +339,8 @@ describe('CoreManager', () => {
338339
get: function() {},
339340
create: function() {},
340341
update: function() {},
341-
delete: function() {}
342+
delete: function() {},
343+
purge: function() {},
342344
};
343345

344346
CoreManager.setSchemaController(controller);

src/__tests__/ParseSchema-test.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ describe('ParseSchema', () => {
4747
.addDate('dateField')
4848
.addFile('fileField')
4949
.addGeoPoint('geoPointField')
50+
.addPolygon('polygonField')
5051
.addArray('arrayField')
5152
.addObject('objectField')
5253
.addPointer('pointerField', '_User')
@@ -59,6 +60,7 @@ describe('ParseSchema', () => {
5960
expect(schema._fields.dateField.type, 'Date');
6061
expect(schema._fields.fileField.type, 'File');
6162
expect(schema._fields.geoPointField.type, 'GeoPoint');
63+
expect(schema._fields.polygonField.type, 'Polygon');
6264
expect(schema._fields.arrayField.type, 'Array');
6365
expect(schema._fields.objectField.type, 'Object');
6466
expect(schema._fields.pointerField.type, 'Pointer');
@@ -162,19 +164,13 @@ describe('ParseSchema', () => {
162164
done();
163165
});
164166

165-
// CoreManager.setSchemaController({
166-
// send() {},
167-
// get() {},
168-
// create() {},
169-
// update() {},
170-
// delete() {},
171-
// });
172167
it('can save schema', (done) => {
173168
CoreManager.setSchemaController({
174169
send() {},
175170
get() {},
176171
update() {},
177172
delete() {},
173+
purge() {},
178174
create(className, params, options) {
179175
expect(className).toBe('SchemaTest');
180176
expect(params).toEqual({
@@ -202,6 +198,7 @@ describe('ParseSchema', () => {
202198
get() {},
203199
create() {},
204200
delete() {},
201+
purge() {},
205202
update(className, params, options) {
206203
expect(className).toBe('SchemaTest');
207204
expect(params).toEqual({
@@ -229,6 +226,7 @@ describe('ParseSchema', () => {
229226
create() {},
230227
update() {},
231228
get() {},
229+
purge() {},
232230
delete(className, options) {
233231
expect(className).toBe('SchemaTest');
234232
expect(options).toEqual({});
@@ -243,12 +241,33 @@ describe('ParseSchema', () => {
243241
});
244242
});
245243

244+
it('can purge schema', (done) => {
245+
CoreManager.setSchemaController({
246+
send() {},
247+
create() {},
248+
update() {},
249+
get() {},
250+
delete() {},
251+
purge(className) {
252+
expect(className).toBe('SchemaTest');
253+
return ParsePromise.as([]);
254+
},
255+
});
256+
257+
var schema = new ParseSchema('SchemaTest');
258+
schema.purge().then((results) => {
259+
expect(results).toEqual([]);
260+
done();
261+
});
262+
});
263+
246264
it('can get schema', (done) => {
247265
CoreManager.setSchemaController({
248266
send() {},
249267
create() {},
250268
update() {},
251269
delete() {},
270+
purge() {},
252271
get(className, options) {
253272
expect(className).toBe('SchemaTest');
254273
expect(options).toEqual({});
@@ -269,6 +288,7 @@ describe('ParseSchema', () => {
269288
create() {},
270289
update() {},
271290
delete() {},
291+
purge() {},
272292
get(className, options) {
273293
expect(className).toBe('SchemaTest');
274294
expect(options).toEqual({ sessionToken: 1234 });
@@ -289,6 +309,7 @@ describe('ParseSchema', () => {
289309
create() {},
290310
update() {},
291311
delete() {},
312+
purge() {},
292313
get(className, options) {
293314
expect(className).toBe('SchemaTest');
294315
expect(options).toEqual({});
@@ -313,6 +334,7 @@ describe('ParseSchema', () => {
313334
create() {},
314335
update() {},
315336
delete() {},
337+
purge() {},
316338
get(className, options) {
317339
expect(className).toBe('');
318340
expect(options).toEqual({});
@@ -334,6 +356,7 @@ describe('ParseSchema', () => {
334356
create() {},
335357
update() {},
336358
delete() {},
359+
purge() {},
337360
get(className, options) {
338361
expect(className).toBe('');
339362
expect(options).toEqual({ sessionToken: 1234 });
@@ -355,6 +378,7 @@ describe('ParseSchema', () => {
355378
create() {},
356379
update() {},
357380
delete() {},
381+
purge() {},
358382
get(className, options) {
359383
expect(className).toBe('');
360384
expect(options).toEqual({});
@@ -418,4 +442,12 @@ describe('SchemaController', () => {
418442
done();
419443
});
420444
});
445+
446+
it('purge schema', (done) => {
447+
var schema = new ParseSchema('SchemaTest');
448+
schema.purge().then((results) => {
449+
expect(results).toEqual([]);
450+
done();
451+
});
452+
});
421453
});

0 commit comments

Comments
 (0)