diff --git a/README.md b/README.md index a363fd0a..725518d3 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ Please note that this documentation contains the latest changes that may as of y - [Version](#version) - [Features](#features) - [Schema](#schema) + - [Index](#index) - [Purge](#purge) - [Logs](#logs) - [Contributing / Testing](#contributing--testing) @@ -679,6 +680,23 @@ A schema can be removed via `delete`, but it must be empty first. ```php $mySchema->delete(); ``` +#### Index +Indexes support efficient execution of queries from the database. MasterKey is required. +```php +// To add an index, the field must exist before you create an index +$schema->addString('field'); +$index = [ 'field' => 1 ]; +$schema->addIndex('index_name', $index); +$schema->save(); + +// Delete an index +$schema->deleteIndex('index_name'); +$schema->save(); + +// If indexes exist, you can retrieve them +$result = $schema->get(); +$indexes = $result['indexes']; + ``` #### Purge All objects can be purged from a schema (class) via `purge`. But be careful! This can be considered an irreversible action. diff --git a/src/Parse/ParseSchema.php b/src/Parse/ParseSchema.php index 2d22d259..5d250c0c 100644 --- a/src/Parse/ParseSchema.php +++ b/src/Parse/ParseSchema.php @@ -110,6 +110,13 @@ class ParseSchema */ private $fields = []; + /** + * Indexes to create. + * + * @var array + */ + private $indexes = []; + /** * Force to use master key in Schema Methods. * @@ -215,6 +222,10 @@ public function save() $schema['fields'] = $this->fields; } + if (!empty($this->indexes)) { + $schema['indexes'] = $this->indexes; + } + $result = ParseClient::_request( 'POST', 'schemas/'.$this->className, @@ -247,14 +258,18 @@ public function update() } // Schema - $Schema['className'] = $this->className; - $Schema['fields'] = $this->fields; + $schema['className'] = $this->className; + $schema['fields'] = $this->fields; + $schema['indexes'] = $this->indexes; + + $this->fields = []; + $this->indexes = []; $result = ParseClient::_request( 'PUT', 'schemas/'.$this->className, $sessionToken, - json_encode($Schema), + json_encode($schema), $this->useMasterKey ); @@ -323,7 +338,7 @@ public function delete() /** * Adding a Field to Create / Update a Schema. * - * @param string $fieldName Name of the field will created on Parse + * @param string $fieldName Name of the field that will be created on Parse * @param string $fieldType Can be a (String|Number|Boolean|Date|File|GeoPoint|Array|Object|Pointer|Relation) * * @throws \Exception @@ -348,10 +363,35 @@ public function addField($fieldName = null, $fieldType = 'String') return $this; } + /** + * Adding an Index to Create / Update a Schema. + * + * @param string $indexName Name of the index that will be created on Parse + * @param string $index Key / Value to be saved + * + * @throws \Exception + * + * @return ParseSchema indexes return self to create index on Parse + */ + public function addIndex($indexName, $index) + { + if (!$indexName) { + throw new Exception('index name may not be null.', 105); + } + + if (!$index) { + throw new Exception('index may not be null.', 105); + } + + $this->indexes[$indexName] = $index; + + return $this; + } + /** * Adding String Field. * - * @param string $fieldName Name of the field will created on Parse + * @param string $fieldName Name of the field that will be created on Parse * * @throws \Exception * @@ -614,6 +654,18 @@ public function deleteField($fieldName = null) ]; } + /** + * Deleting an Index to Update on a Schema. + * + * @param string $indexName Name of the index that will be deleted + */ + public function deleteIndex($indexName = null) + { + $this->indexes[$indexName] = [ + '__op' => 'Delete', + ]; + } + /** * Assert if ClassName has filled. * diff --git a/tests/Parse/ParseSchemaTest.php b/tests/Parse/ParseSchemaTest.php index c953508a..e17287b9 100644 --- a/tests/Parse/ParseSchemaTest.php +++ b/tests/Parse/ParseSchemaTest.php @@ -190,6 +190,24 @@ public function testUpdateSchemaCurl() } } + public function testUpdateMultipleNoDuplicateFields() + { + $schema = self::$schema; + $schema->save(); + $schema->addString('name'); + $schema->update(); + + $getSchema = new ParseSchema('SchemaTest'); + $result = $getSchema->get(); + $this->assertEquals(count($result['fields']), 5); + + $schema->update(); + + $getSchema = new ParseSchema('SchemaTest'); + $result = $getSchema->get(); + $this->assertEquals(count($result['fields']), 5); + } + public function testUpdateWrongFieldType() { $this->setExpectedException('Exception', 'WrongType is not a valid type.'); @@ -411,7 +429,7 @@ public function testBadSchemaGet() */ public function testBadSchemaSave() { - $this->setExpectedException('\Parse\ParseException'); + $this->setExpectedException('\Exception'); $user = new ParseUser(); $user->setUsername('schema-user'); @@ -427,7 +445,7 @@ public function testBadSchemaSave() */ public function testBadSchemaUpdate() { - $this->setExpectedException('\Parse\ParseException'); + $this->setExpectedException('\Exception'); $user = new ParseUser(); $user->setUsername('schema-user'); @@ -449,8 +467,67 @@ public function testBadSchemaDelete() $user->setUsername('schema-user'); $user->setPassword('basicpassword'); $user->signUp(); - $schema = new ParseSchema(self::$badClassName); $schema->delete(); } + + public function testCreateIndexSchema() + { + $schema = self::$schema; + $schema->addString('name'); + $index = [ 'name' => 1 ]; + $schema->addIndex('test_index', $index); + $schema->save(); + + $getSchema = new ParseSchema('SchemaTest'); + $result = $getSchema->get(); + $this->assertNotNull($result['indexes']['test_index']); + } + + public function testUpdateIndexSchema() + { + $schema = self::$schema; + $schema->save(); + $schema->addString('name'); + $index = [ 'name' => 1 ]; + $schema->addIndex('test_index', $index); + $schema->update(); + + $getSchema = new ParseSchema('SchemaTest'); + $result = $getSchema->get(); + $this->assertNotNull($result['indexes']['test_index']); + } + + public function testDeleteIndexSchema() + { + $schema = self::$schema; + $schema->save(); + $schema->addString('name'); + $index = [ 'name' => 1 ]; + $schema->addIndex('test_index', $index); + $schema->update(); + + $getSchema = new ParseSchema('SchemaTest'); + $result = $getSchema->get(); + $this->assertNotNull($result['indexes']['test_index']); + + $schema->deleteIndex('test_index'); + $schema->update(); + $result = $getSchema->get(); + $this->assertEquals(array_key_exists('text_index', $result['indexes']), false); + } + + public function testIndexNameException() + { + $schema = self::$schema; + $this->setExpectedException('\Exception', 'index name may not be null.'); + $schema->addIndex(null, null); + } + + public function testIndexException() + { + $schema = self::$schema; + $this->setExpectedException('\Exception', 'index may not be null.'); + $schema->addIndex('name', null); + } }