Skip to content

Commit

Permalink
Merge pull request #3893 from mathesar-foundation/exploration_saving
Browse files Browse the repository at this point in the history
Get explorations CRUD working again
  • Loading branch information
pavish authored Sep 30, 2024
2 parents c9f2a6f + aa165ab commit 036fdf3
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 176 deletions.
8 changes: 4 additions & 4 deletions mathesar/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from rest_framework.response import Response

from mathesar.rpc.databases.configured import list_ as databases_list
from mathesar.rpc.explorations import list_ as explorations_list
from mathesar.rpc.schemas import list_ as schemas_list
from mathesar.rpc.servers.configured import list_ as get_servers_list
from mathesar.rpc.tables import list_with_metadata as tables_list
Expand Down Expand Up @@ -44,9 +45,8 @@ def get_table_list(request, database_id, schema_oid):
return []


def get_queries_list(request, schema_id):
# TODO_BETA: Fill this method
return []
def get_queries_list(request, database_id, schema_id):
return explorations_list(request=request, database_id=database_id, schema_oid=schema_id)


def get_ui_type_list(request, database_id):
Expand Down Expand Up @@ -104,7 +104,7 @@ def get_common_data(request, database_id=None, schema_id=None):
return {
**_get_base_data_all_routes(request, database_id, schema_id),
'tables': get_table_list(request, database_id, schema_id),
'queries': get_queries_list(request, schema_id),
'queries': get_queries_list(request, database_id, schema_id),
'routing_context': 'normal',
}

Expand Down
58 changes: 47 additions & 11 deletions mathesar_ui/src/api/rpc/explorations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,53 @@ export type QueryInstanceTransformation =
| QueryInstanceHideTransformation
| QueryInstanceSortTransformation;

export interface SavedExploration {
id: number;
/** The data an exploration contains when the data explorer opens */
export interface InitialExploration {
database_id: number;
name: string;
description?: string;
schema_oid: number;
}

/** The data needed to run an exploration without it being saved */
export interface AnonymousExploration extends InitialExploration {
base_table_oid: number;
initial_columns: InitialColumn[];
transformations?: QueryInstanceTransformation[];
display_names?: Record<string, string> | null;
display_options?: unknown[];
}

export type UnsavedExploration = Partial<SavedExploration> & {
database_id: number;
};
export interface AddableExploration extends AnonymousExploration {
name: string;
description?: string;
}

export type AnonymousExploration = Omit<SavedExploration, 'id' | 'name'>;
export interface SavedExploration extends AddableExploration {
id: number;
}

export type MaybeSavedExploration = Partial<SavedExploration> &
InitialExploration;

export function explorationIsAddable(
e: MaybeSavedExploration,
): e is MaybeSavedExploration & AddableExploration {
return (
'name' in e &&
e.name !== '' &&
e.name !== undefined &&
'base_table_oid' in e &&
e.base_table_oid !== undefined &&
'initial_columns' in e &&
e.initial_columns !== undefined &&
e.initial_columns.length > 0
);
}

export function explorationIsSaved(
e: MaybeSavedExploration,
): e is SavedExploration {
return explorationIsAddable(e) && 'id' in e && e.id !== undefined;
}

export interface ExplorationRunParams {
exploration_def: AnonymousExploration;
Expand Down Expand Up @@ -164,17 +194,23 @@ export interface ExplorationResult {
}

export const explorations = {
list: rpcMethodTypeContainer<{ database_id: number }, SavedExploration[]>(),
list: rpcMethodTypeContainer<
{ database_id: number; schema_oid: number },
SavedExploration[]
>(),

get: rpcMethodTypeContainer<{ exploration_id: number }, SavedExploration>(),

add: rpcMethodTypeContainer<AnonymousExploration & { name: string }, void>(),
add: rpcMethodTypeContainer<
{ exploration_def: AddableExploration },
SavedExploration
>(),

delete: rpcMethodTypeContainer<{ exploration_id: number }, void>(),

replace: rpcMethodTypeContainer<
{ new_exploration: SavedExploration },
void
SavedExploration
>(),

run: rpcMethodTypeContainer<ExplorationRunParams, ExplorationResult>(),
Expand Down
4 changes: 2 additions & 2 deletions mathesar_ui/src/pages/schema/SchemaOverview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import type { Schema } from '@mathesar/models/Schema';
import type { Table } from '@mathesar/models/Table';
import { getDataExplorerPageUrl } from '@mathesar/routes/urls';
import { refetchQueriesForSchema } from '@mathesar/stores/queries';
import { refetchExplorationsForSchema } from '@mathesar/stores/queries';
import { refetchTablesForSchema } from '@mathesar/stores/tables';
import { AnchorButton, Button } from '@mathesar-component-library';
Expand Down Expand Up @@ -88,7 +88,7 @@
<div>
<SpinnerButton
onClick={async () => {
await refetchQueriesForSchema(schema.oid);
await refetchExplorationsForSchema(schema);
}}
label={$_('retry')}
icon={iconRefresh}
Expand Down
11 changes: 6 additions & 5 deletions mathesar_ui/src/routes/DataExplorerRoute.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import { router } from 'tinro';
import type {
MaybeSavedExploration,
SavedExploration,
UnsavedExploration,
} from '@mathesar/api/rpc/explorations';
import type { CancellablePromise } from '@mathesar/component-library';
import AppendBreadcrumb from '@mathesar/components/breadcrumb/AppendBreadcrumb.svelte';
Expand All @@ -19,7 +19,7 @@
getExplorationEditorPageUrl,
} from '@mathesar/routes/urls';
import { abstractTypesMap } from '@mathesar/stores/abstract-types';
import { getQuery } from '@mathesar/stores/queries';
import { getExploration } from '@mathesar/stores/queries';
import {
QueryManager,
QueryModel,
Expand All @@ -36,7 +36,7 @@
let queryLoadPromise: CancellablePromise<SavedExploration>;
let query: Readable<QueryModel | undefined> = readable(undefined);
function createQueryManager(queryInstance: UnsavedExploration) {
function createQueryManager(queryInstance: MaybeSavedExploration) {
queryManager?.destroy();
queryManager = new QueryManager({
query: new QueryModel(queryInstance),
Expand Down Expand Up @@ -80,6 +80,7 @@
router.location.hash.clear();
createQueryManager({
database_id: database.id,
schema_oid: schema.oid,
...(newQueryModel ?? {}),
});
return;
Expand All @@ -88,7 +89,7 @@
console.error('Unable to create query model from hash', hash);
}
}
createQueryManager({ database_id: database.id });
createQueryManager({ database_id: database.id, schema_oid: schema.oid });
}
async function loadSavedQuery(_queryId: number) {
Expand All @@ -103,7 +104,7 @@
}
queryLoadPromise?.cancel();
queryLoadPromise = getQuery(_queryId);
queryLoadPromise = getExploration(_queryId);
try {
const queryInstance = await queryLoadPromise;
createQueryManager(queryInstance);
Expand Down
Loading

0 comments on commit 036fdf3

Please sign in to comment.