Skip to content
Open
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
2 changes: 2 additions & 0 deletions src/Typesense.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Typesense plugin for Craft CMS 5.x
*
Expand Down Expand Up @@ -307,6 +308,7 @@ protected function customAdminCpRoutes(): array
'typesense/save-collection' => 'typesense/collections/save-collection',
'typesense/sync-collection' => 'typesense/collections/sync-collection',
'typesense/flush-collection' => 'typesense/collections/flush-collection',
'typesense/update-schema' => 'typesense/collections/update-schema',
];
}

Expand Down
6 changes: 6 additions & 0 deletions src/console/controllers/DefaultController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Typesense plugin for Craft CMS 5.x
*
Expand Down Expand Up @@ -92,6 +93,11 @@ public function actionFlush()
}
}

public function actionUpdateSchema()
{
Typesense::$plugin->getCollections()->updateSchema();
}

public function actionSync()
{
$indexes = Typesense::$plugin->getSettings()->collections;
Expand Down
33 changes: 30 additions & 3 deletions src/services/CollectionService.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @author CraftPulse
* @since 4.0.0
Expand All @@ -9,9 +10,7 @@
use craft\base\MemoizableArray;
use craft\db\Query;
use Craft;

use percipiolondon\typesense\models\CollectionModel as Collection;

use percipiolondon\typesense\Typesense;
use Throwable;
use yii\base\Component;
Expand Down Expand Up @@ -56,7 +55,6 @@ public function saveCollections(): void
}
}


private function _verifyClient(): bool
{
$client = Typesense::$plugin->getClient()->client();
Expand All @@ -66,4 +64,33 @@ private function _verifyClient(): bool

return true;
}

/**
* Update the schema in Typesense based on the configuration in PHP
*
* @return void
*/
public function updateSchema(): void
{
$indexes = Typesense::$plugin->getSettings()->collections;

foreach ($indexes as $index) {

print('Updating schema ' . $index->indexName);
print(PHP_EOL);

$updateSchema = ['fields' => []];
foreach ($index->schema['fields'] as $field) {
$updateSchema['fields'][] = [
'name' => $field['name'],
'drop' => true
];
$updateSchema['fields'][] = $field;
}
Typesense::$plugin->getClient()->client()->collections[$index->indexName]->update($updateSchema);

print('Updated schema ' . $index->indexName);
print(PHP_EOL);
}
}
}