Skip to content

Commit e0e2dc8

Browse files
dplewismontymxb
authored andcommitted
Add Indexes via Schema (#357)
* Add Indexes via Schema * support for multiple update * Documentation and Test Fix * Typos and test updates
1 parent b8fdecc commit e0e2dc8

File tree

3 files changed

+155
-8
lines changed

3 files changed

+155
-8
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Please note that this documentation contains the latest changes that may as of y
4141
- [Version](#version)
4242
- [Features](#features)
4343
- [Schema](#schema)
44+
- [Index](#index)
4445
- [Purge](#purge)
4546
- [Logs](#logs)
4647
- [Contributing / Testing](#contributing--testing)
@@ -679,6 +680,23 @@ A schema can be removed via `delete`, but it must be empty first.
679680
```php
680681
$mySchema->delete();
681682
```
683+
#### Index
684+
Indexes support efficient execution of queries from the database. MasterKey is required.
685+
```php
686+
// To add an index, the field must exist before you create an index
687+
$schema->addString('field');
688+
$index = [ 'field' => 1 ];
689+
$schema->addIndex('index_name', $index);
690+
$schema->save();
691+
692+
// Delete an index
693+
$schema->deleteIndex('index_name');
694+
$schema->save();
695+
696+
// If indexes exist, you can retrieve them
697+
$result = $schema->get();
698+
$indexes = $result['indexes'];
699+
```
682700

683701
#### Purge
684702
All objects can be purged from a schema (class) via `purge`. But be careful! This can be considered an irreversible action.

src/Parse/ParseSchema.php

+57-5
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ class ParseSchema
110110
*/
111111
private $fields = [];
112112

113+
/**
114+
* Indexes to create.
115+
*
116+
* @var array
117+
*/
118+
private $indexes = [];
119+
113120
/**
114121
* Force to use master key in Schema Methods.
115122
*
@@ -215,6 +222,10 @@ public function save()
215222
$schema['fields'] = $this->fields;
216223
}
217224

225+
if (!empty($this->indexes)) {
226+
$schema['indexes'] = $this->indexes;
227+
}
228+
218229
$result = ParseClient::_request(
219230
'POST',
220231
'schemas/'.$this->className,
@@ -247,14 +258,18 @@ public function update()
247258
}
248259

249260
// Schema
250-
$Schema['className'] = $this->className;
251-
$Schema['fields'] = $this->fields;
261+
$schema['className'] = $this->className;
262+
$schema['fields'] = $this->fields;
263+
$schema['indexes'] = $this->indexes;
264+
265+
$this->fields = [];
266+
$this->indexes = [];
252267

253268
$result = ParseClient::_request(
254269
'PUT',
255270
'schemas/'.$this->className,
256271
$sessionToken,
257-
json_encode($Schema),
272+
json_encode($schema),
258273
$this->useMasterKey
259274
);
260275

@@ -323,7 +338,7 @@ public function delete()
323338
/**
324339
* Adding a Field to Create / Update a Schema.
325340
*
326-
* @param string $fieldName Name of the field will created on Parse
341+
* @param string $fieldName Name of the field that will be created on Parse
327342
* @param string $fieldType Can be a (String|Number|Boolean|Date|File|GeoPoint|Array|Object|Pointer|Relation)
328343
*
329344
* @throws \Exception
@@ -348,10 +363,35 @@ public function addField($fieldName = null, $fieldType = 'String')
348363
return $this;
349364
}
350365

366+
/**
367+
* Adding an Index to Create / Update a Schema.
368+
*
369+
* @param string $indexName Name of the index that will be created on Parse
370+
* @param string $index Key / Value to be saved
371+
*
372+
* @throws \Exception
373+
*
374+
* @return ParseSchema indexes return self to create index on Parse
375+
*/
376+
public function addIndex($indexName, $index)
377+
{
378+
if (!$indexName) {
379+
throw new Exception('index name may not be null.', 105);
380+
}
381+
382+
if (!$index) {
383+
throw new Exception('index may not be null.', 105);
384+
}
385+
386+
$this->indexes[$indexName] = $index;
387+
388+
return $this;
389+
}
390+
351391
/**
352392
* Adding String Field.
353393
*
354-
* @param string $fieldName Name of the field will created on Parse
394+
* @param string $fieldName Name of the field that will be created on Parse
355395
*
356396
* @throws \Exception
357397
*
@@ -614,6 +654,18 @@ public function deleteField($fieldName = null)
614654
];
615655
}
616656

657+
/**
658+
* Deleting an Index to Update on a Schema.
659+
*
660+
* @param string $indexName Name of the index that will be deleted
661+
*/
662+
public function deleteIndex($indexName = null)
663+
{
664+
$this->indexes[$indexName] = [
665+
'__op' => 'Delete',
666+
];
667+
}
668+
617669
/**
618670
* Assert if ClassName has filled.
619671
*

tests/Parse/ParseSchemaTest.php

+80-3
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,24 @@ public function testUpdateSchemaCurl()
190190
}
191191
}
192192

193+
public function testUpdateMultipleNoDuplicateFields()
194+
{
195+
$schema = self::$schema;
196+
$schema->save();
197+
$schema->addString('name');
198+
$schema->update();
199+
200+
$getSchema = new ParseSchema('SchemaTest');
201+
$result = $getSchema->get();
202+
$this->assertEquals(count($result['fields']), 5);
203+
204+
$schema->update();
205+
206+
$getSchema = new ParseSchema('SchemaTest');
207+
$result = $getSchema->get();
208+
$this->assertEquals(count($result['fields']), 5);
209+
}
210+
193211
public function testUpdateWrongFieldType()
194212
{
195213
$this->setExpectedException('Exception', 'WrongType is not a valid type.');
@@ -411,7 +429,7 @@ public function testBadSchemaGet()
411429
*/
412430
public function testBadSchemaSave()
413431
{
414-
$this->setExpectedException('\Parse\ParseException');
432+
$this->setExpectedException('\Exception');
415433

416434
$user = new ParseUser();
417435
$user->setUsername('schema-user');
@@ -427,7 +445,7 @@ public function testBadSchemaSave()
427445
*/
428446
public function testBadSchemaUpdate()
429447
{
430-
$this->setExpectedException('\Parse\ParseException');
448+
$this->setExpectedException('\Exception');
431449

432450
$user = new ParseUser();
433451
$user->setUsername('schema-user');
@@ -449,8 +467,67 @@ public function testBadSchemaDelete()
449467
$user->setUsername('schema-user');
450468
$user->setPassword('basicpassword');
451469
$user->signUp();
452-
453470
$schema = new ParseSchema(self::$badClassName);
454471
$schema->delete();
455472
}
473+
474+
public function testCreateIndexSchema()
475+
{
476+
$schema = self::$schema;
477+
$schema->addString('name');
478+
$index = [ 'name' => 1 ];
479+
$schema->addIndex('test_index', $index);
480+
$schema->save();
481+
482+
$getSchema = new ParseSchema('SchemaTest');
483+
$result = $getSchema->get();
484+
$this->assertNotNull($result['indexes']['test_index']);
485+
}
486+
487+
public function testUpdateIndexSchema()
488+
{
489+
$schema = self::$schema;
490+
$schema->save();
491+
$schema->addString('name');
492+
$index = [ 'name' => 1 ];
493+
$schema->addIndex('test_index', $index);
494+
$schema->update();
495+
496+
$getSchema = new ParseSchema('SchemaTest');
497+
$result = $getSchema->get();
498+
$this->assertNotNull($result['indexes']['test_index']);
499+
}
500+
501+
public function testDeleteIndexSchema()
502+
{
503+
$schema = self::$schema;
504+
$schema->save();
505+
$schema->addString('name');
506+
$index = [ 'name' => 1 ];
507+
$schema->addIndex('test_index', $index);
508+
$schema->update();
509+
510+
$getSchema = new ParseSchema('SchemaTest');
511+
$result = $getSchema->get();
512+
$this->assertNotNull($result['indexes']['test_index']);
513+
514+
$schema->deleteIndex('test_index');
515+
$schema->update();
516+
$result = $getSchema->get();
517+
$this->assertEquals(array_key_exists('text_index', $result['indexes']), false);
518+
}
519+
520+
public function testIndexNameException()
521+
{
522+
$schema = self::$schema;
523+
$this->setExpectedException('\Exception', 'index name may not be null.');
524+
$schema->addIndex(null, null);
525+
}
526+
527+
public function testIndexException()
528+
{
529+
$schema = self::$schema;
530+
$this->setExpectedException('\Exception', 'index may not be null.');
531+
$schema->addIndex('name', null);
532+
}
456533
}

0 commit comments

Comments
 (0)