Skip to content

Commit 436f4a4

Browse files
author
Femi Novia Lina
committed
feat: add query variable
1 parent 26939b9 commit 436f4a4

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/datasource.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
import { DataSourceInstanceSettings, CoreApp, ScopedVars } from '@grafana/data';
1+
import { DataSourceInstanceSettings, CoreApp, ScopedVars, VariableSupportType, DataQueryRequest } from '@grafana/data';
22
import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime';
33

44
import { MCQuery, MCConfig, defaultMCSQLQuery } from './types';
5+
import { SQLEditor } from './components/SQLEditor'
6+
import { uniqueId } from 'lodash';
57

68
export class DataSource extends DataSourceWithBackend<MCQuery, MCConfig> {
79
constructor(instanceSettings: DataSourceInstanceSettings<MCConfig>) {
810
super(instanceSettings);
11+
this.variables = {
12+
getType: () => VariableSupportType.Custom,
13+
editor: SQLEditor as any,
14+
query: (request: DataQueryRequest<MCQuery>) => {
15+
const queries = request.targets.map((query) => {
16+
return { ...query, refId: query.refId || uniqueId('tempVar') };
17+
});
18+
return this.query({ ...request, targets: queries });
19+
}
20+
};
921
}
1022

1123
getDefaultQuery(_: CoreApp): Partial<MCQuery> {
@@ -26,6 +38,5 @@ export class DataSource extends DataSourceWithBackend<MCQuery, MCConfig> {
2638
...query,
2739
rawSql: rawQuery
2840
}
29-
3041
}
3142
}

0 commit comments

Comments
 (0)