Skip to content

Commit d71d80b

Browse files
committed
add computed cols to templates
1 parent 75d45cc commit d71d80b

File tree

7 files changed

+26278
-5
lines changed

7 files changed

+26278
-5
lines changed

packages/app/src/actions/data/templates/applyTemplate.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
actionResult,
1717
runActionServer,
1818
} from "@/lib/actions/utils";
19+
import { ComputedColumn } from "@common/db/schema/query";
1920

2021
type ApplyTemplateParams = {
2122
projectId: string;
@@ -24,6 +25,7 @@ type ApplyTemplateParams = {
2425
plotName?: string;
2526
plotDefinition?: PlotDefinitionWithoutSource;
2627
folderId?: string | null;
28+
queryCustomColumns?: ComputedColumn[];
2729
};
2830

2931
export async function applyTemplateAction({
@@ -33,6 +35,7 @@ export async function applyTemplateAction({
3335
plotName,
3436
plotDefinition,
3537
folderId,
38+
queryCustomColumns,
3639
}: ApplyTemplateParams) {
3740
return runActionServer(async () => {
3841
const user = await getServerUser();
@@ -56,6 +59,7 @@ export async function applyTemplateAction({
5659
},
5760
]),
5861
),
62+
computedColumns: queryCustomColumns || [],
5963
});
6064

6165
revalidateQueryCache(newQuery.id, projectId);

packages/app/src/actions/data/templates/createTemplate.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
QueryWithoutFilters,
1212
PlotDefinitionWithoutSource,
1313
} from "@common/db/schema/template";
14+
import { ComputedColumn } from "@common/db/schema/query";
1415
import {
1516
runActionServer,
1617
actionError,
@@ -45,6 +46,7 @@ export async function createTemplateAction({
4546

4647
let queryDefinition: QueryWithoutFilters | null = null;
4748
let plotDefinition: PlotDefinitionWithoutSource | null = null;
49+
let computedColumns: ComputedColumn[] = [];
4850

4951
// Get query definition if provided
5052
if (queryId) {
@@ -53,7 +55,10 @@ export async function createTemplateAction({
5355
return actionError("Query not found or has no definition");
5456
}
5557

58+
computedColumns = query.computedColumns || [];
59+
5660
// Remove filters from the definition for the template
61+
// Create a partial record without filters
5762
queryDefinition = Object.fromEntries(
5863
Object.entries(query.definition).map(([tableName, tableDef]) => [
5964
tableName,
@@ -62,7 +67,7 @@ export async function createTemplateAction({
6267
filters: undefined, // Remove filters
6368
},
6469
]),
65-
) as Record<TableName, any>;
70+
) as Partial<Record<TableName, any>> as QueryWithoutFilters;
6671
}
6772

6873
// Get plot definition if provided
@@ -89,7 +94,7 @@ export async function createTemplateAction({
8994
filters: undefined, // Remove filters
9095
},
9196
]),
92-
) as Record<TableName, any>;
97+
) as Partial<Record<TableName, any>> as QueryWithoutFilters;
9398
}
9499

95100
// If we still don't have a query definition, try to get it from the plot's definition.dataSource
@@ -143,7 +148,10 @@ export async function createTemplateAction({
143148
const newTemplate = await createTemplate({
144149
name,
145150
ownerId: user.id,
146-
queryDefinition: queryDefinition || ({} as QueryWithoutFilters),
151+
queryDefinition:
152+
queryDefinition ||
153+
({} as Partial<Record<TableName, any>> as QueryWithoutFilters),
154+
queryCustomColumns: computedColumns,
147155
plotDefinition: plotDefinition || null,
148156
scope,
149157
});

packages/app/src/components/data/templates/templates-modal.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export function TemplatesModal({
115115
? selectedTemplate.plotDefinition || undefined
116116
: undefined,
117117
folderId: undefined,
118+
queryCustomColumns: selectedTemplate.queryCustomColumns,
118119
});
119120

120121
toast.success(
@@ -252,7 +253,7 @@ export function TemplatesModal({
252253
<ul className="ml-6 space-y-1 text-sm text-muted-foreground">
253254
<li>• All configuration settings from the template</li>
254255
<li>• Custom name and description</li>
255-
<li>• No data source (you'll need to select one)</li>
256+
256257
{selectedTemplate.isPlot && (
257258
<li>• Plot type and visualization settings</li>
258259
)}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE "template" ADD COLUMN "query_custom_columns" jsonb DEFAULT '[]'::jsonb NOT NULL;

0 commit comments

Comments
 (0)