Skip to content

Commit

Permalink
auto-register blocks for each sheet/table (#282)
Browse files Browse the repository at this point in the history
* auto-register blocks for each sheet/table

- Moves the query generation to [Airtable/GoogleSheets]Integration from
  the data source files
- Auto-register blocks for each sheet/table instead of just the first
  sheet/table in the Airtable or Google Sheets data source
- Auto-register loop blocks for Airtable in addition to single entity
  blocks

* simplify data source meta display

Remove sheet/table names from the meta column in data sources list view.
With the recent addition of multiple sheet/table selection support for
Airtable and Google Sheets, displaying individual sheet names created
visual clutter. Now shows only the base spreadsheet/base name for a
cleaner UI.

---------

Co-authored-by: Max Schmeling <[email protected]>
  • Loading branch information
shekharnwagh and maxschmeling authored Jan 11, 2025
1 parent a93b35e commit 15f0f01
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 209 deletions.
69 changes: 0 additions & 69 deletions inc/Integrations/Airtable/AirtableDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
namespace RemoteDataBlocks\Integrations\Airtable;

use RemoteDataBlocks\Config\DataSource\HttpDataSource;
use RemoteDataBlocks\Config\Query\HttpQuery;
use RemoteDataBlocks\Validation\Types;
use WP_Error;

class AirtableDataSource extends HttpDataSource {
protected const SERVICE_NAME = REMOTE_DATA_BLOCKS_AIRTABLE_SERVICE;
Expand Down Expand Up @@ -47,71 +45,4 @@ protected static function map_service_config( array $service_config ): array {
],
];
}

public function ___temp_get_query(): HttpQuery|WP_Error {
$input_schema = [
'record_id' => [
'name' => 'Record ID',
'type' => 'id',
],
];

$output_schema = [
'is_collection' => false,
'type' => [
'id' => [
'name' => 'Record ID',
'path' => '$.id',
'type' => 'id',
],
],
];

foreach ( $this->config['service_config']['tables'][0]['output_query_mappings'] as $mapping ) {
$mapping_key = $mapping['key'];
$output_schema['type'][ $mapping_key ] = [
'name' => $mapping['name'] ?? $mapping_key,
'path' => $mapping['path'] ?? '$.fields["' . $mapping_key . '"]',
'type' => $mapping['type'] ?? 'string',
];
}

return HttpQuery::from_array( [
'data_source' => $this,
'endpoint' => function ( array $input_variables ): string {
return $this->get_endpoint() . '/' . $this->config['service_config']['tables'][0]['id'] . '/' . $input_variables['record_id'];
},
'input_schema' => $input_schema,
'output_schema' => $output_schema,
] );
}

public function ___temp_get_list_query(): HttpQuery|WP_Error {
$output_schema = [
'is_collection' => true,
'path' => '$.records[*]',
'type' => [
'record_id' => [
'name' => 'Record ID',
'path' => '$.id',
'type' => 'id',
],
],
];

foreach ( $this->config['service_config']['tables'][0]['output_query_mappings'] as $mapping ) {
$output_schema['type'][ $mapping['name'] ] = [
'name' => $mapping['name'],
'path' => '$.fields.' . $mapping['name'],
'type' => $mapping['type'] ?? 'string',
];
}

return HttpQuery::from_array( [
'data_source' => $this,
'endpoint' => $this->get_endpoint() . '/' . $this->config['service_config']['tables'][0]['id'],
'input_schema' => [],
'output_schema' => $output_schema,
] );
}
}
140 changes: 112 additions & 28 deletions inc/Integrations/Airtable/AirtableIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace RemoteDataBlocks\Integrations\Airtable;

use RemoteDataBlocks\WpdbStorage\DataSourceCrud;
use RemoteDataBlocks\Config\Query\HttpQuery;
use WP_Error;

class AirtableIntegration {
public static function init(): void {
Expand All @@ -16,48 +18,130 @@ public static function register_blocks(): void {

foreach ( $data_source_configs as $config ) {
$data_source = AirtableDataSource::from_array( $config );
self::register_block_for_airtable_data_source( $data_source );
self::register_blocks_for_airtable_data_source( $data_source );
self::register_loop_blocks_for_airtable_data_source( $data_source );
}
}

public static function register_block_for_airtable_data_source(
public static function register_blocks_for_airtable_data_source(
AirtableDataSource $data_source,
array $block_overrides = []
): void {
register_remote_data_block(
array_merge(
[
'title' => $data_source->get_display_name(),
'render_query' => [
'query' => $data_source->___temp_get_query(),
],
'selection_queries' => [
[
'query' => $data_source->___temp_get_list_query(),
'type' => 'list',
$tables = $data_source->to_array()['service_config']['tables'];

foreach ( $tables as $table ) {
$query = self::get_query( $data_source, $table );
$list_query = self::get_list_query( $data_source, $table );

register_remote_data_block(
array_merge(
[
'title' => $data_source->get_display_name() . '/' . $table['name'],
'render_query' => [
'query' => $query,
],
'selection_queries' => [
[
'query' => $list_query,
'type' => 'list',
],
],
],
],
$block_overrides
)
);
$block_overrides
)
);
}
}

public static function register_loop_block_for_airtable_data_source(
public static function register_loop_blocks_for_airtable_data_source(
AirtableDataSource $data_source,
array $block_overrides = []
): void {
register_remote_data_block(
array_merge(
[
'title' => sprintf( '%s Loop', $data_source->get_display_name() ),
'render_query' => [
'loop' => true,
'query' => $data_source->___temp_get_list_query(),
$tables = $data_source->to_array()['service_config']['tables'];

foreach ( $tables as $table ) {
$list_query = self::get_list_query( $data_source, $table );

register_remote_data_block(
array_merge(
[
'title' => sprintf( '%s/%s Loop', $data_source->get_display_name(), $table['name'] ),
'render_query' => [
'loop' => true,
'query' => $list_query,
],
],
$block_overrides
)
);
}
}

private static function get_query( AirtableDataSource $data_source, array $table ): HttpQuery|WP_Error {
$input_schema = [
'record_id' => [
'name' => 'Record ID',
'type' => 'id',
],
];

$output_schema = [
'is_collection' => false,
'type' => [
'id' => [
'name' => 'Record ID',
'path' => '$.id',
'type' => 'id',
],
$block_overrides
)
);
],
];

foreach ( $table['output_query_mappings'] as $mapping ) {
$mapping_key = $mapping['key'];
$output_schema['type'][ $mapping_key ] = [
'name' => $mapping['name'] ?? $mapping_key,
'path' => $mapping['path'] ?? '$.fields["' . $mapping_key . '"]',
'type' => $mapping['type'] ?? 'string',
];
}

return HttpQuery::from_array( [
'data_source' => $data_source,
'endpoint' => function ( array $input_variables ) use ( $data_source, $table ): string {
return $data_source->get_endpoint() . '/' . $table['id'] . '/' . $input_variables['record_id'];
},
'input_schema' => $input_schema,
'output_schema' => $output_schema,
] );
}

private static function get_list_query( AirtableDataSource $data_source, array $table ): HttpQuery|WP_Error {
$output_schema = [
'is_collection' => true,
'path' => '$.records[*]',
'type' => [
'record_id' => [
'name' => 'Record ID',
'path' => '$.id',
'type' => 'id',
],
],
];

foreach ( $table['output_query_mappings'] as $mapping ) {
$mapping_key = $mapping['key'];
$output_schema['type'][ $mapping_key ] = [
'name' => $mapping['name'] ?? $mapping_key,
'path' => $mapping['path'] ?? '$.fields["' . $mapping_key . '"]',
'type' => $mapping['type'] ?? 'string',
];
}

return HttpQuery::from_array( [
'data_source' => $data_source,
'endpoint' => $data_source->get_endpoint() . '/' . $table['id'],
'input_schema' => [],
'output_schema' => $output_schema,
] );
}
}
81 changes: 0 additions & 81 deletions inc/Integrations/Google/Sheets/GoogleSheetsDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace RemoteDataBlocks\Integrations\Google\Sheets;

use RemoteDataBlocks\Config\DataSource\HttpDataSource;
use RemoteDataBlocks\Config\Query\HttpQuery;
use RemoteDataBlocks\Integrations\Google\Auth\GoogleAuth;
use RemoteDataBlocks\Validation\Types;

Expand Down Expand Up @@ -70,86 +69,6 @@ protected static function map_service_config( array $service_config ): array {
];
}

public function ___temp_get_query(): HttpQuery {
$service_config = $this->config['service_config'];

$input_schema = [
'row_id' => [
'name' => 'Row ID',
'type' => 'id',
],
];

$output_schema = [
'is_collection' => false,
'type' => [
'row_id' => [
'name' => 'Row ID',
'path' => '$.RowId',
'type' => 'id',
],
],
];

foreach ( $service_config['sheets'][0]['output_query_mappings'] as $mapping ) {
$mapping_key = $mapping['key'];
$output_schema['type'][ $mapping_key ] = [
'name' => $mapping['name'] ?? $mapping_key,
'path' => $mapping['path'] ?? '$.fields["' . $mapping_key . '"]',
'type' => $mapping['type'] ?? 'string',
];
}

return HttpQuery::from_array( [
'data_source' => $this,
'endpoint' => function (): string {
return $this->get_endpoint() . '/values/' . $this->config['service_config']['sheets'][0]['name'];
},
'input_schema' => $input_schema,
'output_schema' => $output_schema,
'preprocess_response' => function ( mixed $response_data, array $input_variables ): array {
return GoogleSheetsDataSource::preprocess_get_response( $response_data, $input_variables );
},
] );
}

public function ___temp_get_list_query(): HttpQuery {
$service_config = $this->config['service_config'];

$output_schema = [
'is_collection' => true,
'path' => '$.values[*]',
'type' => [
'row_id' => [
'name' => 'Row ID',
'path' => '$.RowId',
'type' => 'id',
],
],
];

foreach ( $service_config['sheets'][0]['output_query_mappings'] as $mapping ) {
$mapping_key = $mapping['key'];
$output_schema['type'][ $mapping_key ] = [
'name' => $mapping['name'] ?? $mapping_key,
'path' => $mapping['path'] ?? '$.fields["' . $mapping_key . '"]',
'type' => $mapping['type'] ?? 'string',
];
}

return HttpQuery::from_array( [
'data_source' => $this,
'endpoint' => function (): string {
return $this->get_endpoint() . '/values/' . $this->config['service_config']['sheets'][0]['name'];
},
'input_schema' => [],
'output_schema' => $output_schema,
'preprocess_response' => function ( mixed $response_data ): array {
return GoogleSheetsDataSource::preprocess_list_response( $response_data );
},
] );
}

public static function preprocess_list_response( array $response_data ): array {
if ( isset( $response_data['values'] ) && is_array( $response_data['values'] ) ) {
$values = $response_data['values'];
Expand Down
Loading

0 comments on commit 15f0f01

Please sign in to comment.