From a55beed4078d4bf1e69647f67d45cd01325d6e6c Mon Sep 17 00:00:00 2001 From: Jeff Gu Kang Date: Fri, 11 Oct 2019 05:04:46 +0900 Subject: [PATCH] [parse] Add Schema class (#39024) --- types/parse/index.d.ts | 136 +++++++++++++++++++++++++++++++++++++ types/parse/parse-tests.ts | 58 ++++++++++++++++ 2 files changed, 194 insertions(+) diff --git a/types/parse/index.d.ts b/types/parse/index.d.ts index f2cb6b0deb..2e30c2e754 100644 --- a/types/parse/index.d.ts +++ b/types/parse/index.d.ts @@ -14,6 +14,7 @@ // Yago Tomé // Thibault MOCELLIN // Raschid JF Rafaelly +// Jeff Gu Kang // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.4 @@ -781,6 +782,141 @@ subscription.on('close', () => {}); _linkWith(provider: any, options: { authData?: AuthData }, saveOpts?: FullOptions): Promise; } + /** + * A Parse.Schema object is for handling schema data from Parse. + * All the schemas methods require MasterKey. + * + * @param {String} className Parse Class string + * + * https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html + * + * ``` + * const schema = new Parse.Schema('MyClass'); + * schema.addString('field'); + * schema.addIndex('index_name', { field: 1 }); + * schema.save(); + * ``` + */ + class Schema { + + constructor(className: string) + + /** + * Static method to get all schemas + * @param options Valid options are: + * - useMasterKey: In Cloud Code and Node only, causes the Master Key to be used for this request. + * - sessionToken: A valid session token, used for making a request on behalf of a specific user. + */ + static all(options?: ScopeOptions): Promise + + addArray(name: string): this + addBoolean(name: string): this + addDate(name: string): this + addField(name: string, type?: Schema.TYPE): this + addFile(name: string): this + addGeoPoint(name: string): this + + /** + * Adding an Index to Create / Update a Schema + * @param {String} name Name of the field that will be created on Parse + * @param {Schema.Index} index { 'field': value } `field` should exist in the schema before using addIndex. `value` can be a (String|Number|Boolean|Date|Parse.File|Parse.GeoPoint|Array|Object|Pointer|Parse.Relation) + * @return Returns the schema, so you can chain this call. + * @example + * ``` + * schema.addIndex('index_name', {'field': 1}); + * ``` + */ + addIndex(name: string, index: Schema.Index): this + + addNumber(name: string): this + addObject(name: string): this + + /** + * Adding Pointer Field + * @param {String} name Name of the field that will be created on Parse + * @param {String} targetClass Name of the target Pointer Class + * @return Returns the schema, so you can chain this call. + */ + addPointer(name: string, targetClass: string): this + + addPolygon(name: string): this + + /** + * Adding Relation Field + * @param {String} name Name of the field that will be created on Parse + * @param {String} targetClass Name of the target Pointer Class + * @return Returns the schema, so you can chain this call. + */ + addRelation(name: string, targetClass: string): this + + addString(name: string): this + + /** + * Removing a Schema from Parse Can only be used on Schema without objects + * @param options + * Valid options are: + * - useMasterKey: In Cloud Code and Node only, causes the Master Key to be used for this request. + * - sessionToken: A valid session token, used for making a request on behalf of a specific user. + * @returns {Promise} A promise that is resolved with the result when the query completes. + */ + // @TODO Fix Promise + delete(options?: ScopeOptions): Promise + + /** + * Deleting a Field to Update on a Schema + * @param name Name of the field + * @return Returns the schema, so you can chain this call. + */ + deleteField(name: string): this + + /** + * Deleting a Index Field to Update on a Schema + * @param name Name of the index field + * @return Returns the schema, so you can chain this call. + */ + deleteIndex(name: string): this + + /** + * Get the Schema from Parse + */ + get(options?: ScopeOptions): Promise + + /** + * Removes all objects from a Schema (class) in Parse. EXERCISE CAUTION, running this will delete all objects for this schema and cannot be reversed + */ + // TODO Fix Promise + purge(): Promise + + /** + * Create a new Schema on Parse + */ + save(options?: ScopeOptions): Promise + + /** + * Update a Schema on Parse + */ + update(options?: ScopeOptions): Promise + } + + namespace Schema { + + type TYPE = + | string + | number + | boolean + | Date + | File + | GeoPoint + | Array + | object + | Pointer + | Relation + + interface Index { + [fieldName: string]: TYPE + } + } + namespace Analytics { diff --git a/types/parse/parse-tests.ts b/types/parse/parse-tests.ts index 8a04216f1d..75ac46778b 100644 --- a/types/parse/parse-tests.ts +++ b/types/parse/parse-tests.ts @@ -617,3 +617,61 @@ async function test_local_datastore() { Parse.setLocalDatastoreController({}); } + +async function test_schema() { + + Parse.Schema.all({ useMasterKey: true }) + Parse.Schema.all({ sessionToken: '' }) + + const schema = new Parse.Schema('TestSchema'); + + schema.addField('defaultFieldString') + schema.addString('stringField') + schema.addNumber('numberField') + schema.addBoolean('booleanField') + schema.addDate('dateField') + schema.addFile('fileField') + schema.addGeoPoint('geoPointField') + schema.addPolygon('polygonField') + schema.addArray('arrayField') + schema.addObject('objectField') + schema.addPointer('pointerField', '_User') + schema.addRelation('relationField', '_User'); + + schema.addIndex('testIndex', { stringField: 1 }); + + schema.deleteField('defaultFieldString') + schema.deleteIndex('testIndex') + + // Master Key + schema.delete({ useMasterKey: true }) + .then((results) => { }); + + schema.get({ useMasterKey: true }) + .then((results) => { }) + + schema.purge() + .then((results) => { }) + + schema.save({ useMasterKey: true }) + .then((results) => { }) + + schema.update({ useMasterKey: true }) + .then((results) => { }) + + // Session Token + schema.delete({ sessionToken: '' }) + .then((results) => { }); + + schema.get({ sessionToken: '' }) + .then((results) => { }) + + schema.purge() + .then((results) => { }) + + schema.save({ sessionToken: '' }) + .then((results) => { }) + + schema.update({ sessionToken: '' }) + .then((results) => { }) +}