sqlite-hub 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,145 @@
1
+ const { ValidationError } = require("../../utils/errors");
2
+
3
+ const SUPPORTED_QUERY_HISTORY_CHART_TYPES = ["bar", "line", "pie", "scatter"];
4
+ const SUPPORTED_RESULT_COLUMN_TYPES = ["number", "text", "datetime", "unknown"];
5
+
6
+ function normalizeChartName(value = "") {
7
+ return String(value ?? "")
8
+ .replace(/\s+/g, " ")
9
+ .trim();
10
+ }
11
+
12
+ function normalizeChartType(value = "") {
13
+ const normalized = String(value ?? "")
14
+ .trim()
15
+ .toLowerCase();
16
+
17
+ if (!SUPPORTED_QUERY_HISTORY_CHART_TYPES.includes(normalized)) {
18
+ throw new ValidationError(`Unsupported chart type: ${value}`);
19
+ }
20
+
21
+ return normalized;
22
+ }
23
+
24
+ function normalizeBooleanFlag(value, fallback = false) {
25
+ if (typeof value === "boolean") {
26
+ return value;
27
+ }
28
+
29
+ if (value === null || value === undefined || value === "") {
30
+ return fallback;
31
+ }
32
+
33
+ return ["1", "true", "yes", "on"].includes(
34
+ String(value)
35
+ .trim()
36
+ .toLowerCase()
37
+ );
38
+ }
39
+
40
+ function normalizeOptionalColumn(value) {
41
+ const normalized = String(value ?? "").trim();
42
+ return normalized ? normalized : null;
43
+ }
44
+
45
+ function normalizeRequiredColumn(value, label) {
46
+ const normalized = normalizeOptionalColumn(value);
47
+
48
+ if (!normalized) {
49
+ throw new ValidationError(`${label} is required.`);
50
+ }
51
+
52
+ return normalized;
53
+ }
54
+
55
+ function normalizeSortDirection(value, fallback = "asc") {
56
+ return String(value ?? "").trim().toLowerCase() === "desc" ? "desc" : fallback;
57
+ }
58
+
59
+ function normalizeChartConfig(chartType, config = {}) {
60
+ const source =
61
+ config && typeof config === "object" && !Array.isArray(config) ? config : {};
62
+
63
+ switch (normalizeChartType(chartType)) {
64
+ case "bar":
65
+ return {
66
+ x_column: normalizeRequiredColumn(source.x_column, "Bar chart x column"),
67
+ y_column: normalizeRequiredColumn(source.y_column, "Bar chart y column"),
68
+ show_legend: normalizeBooleanFlag(source.show_legend, true),
69
+ show_labels: normalizeBooleanFlag(source.show_labels, false),
70
+ sort_direction: normalizeSortDirection(source.sort_direction, "asc"),
71
+ };
72
+ case "line":
73
+ return {
74
+ x_column: normalizeRequiredColumn(source.x_column, "Line chart x column"),
75
+ y_column: normalizeRequiredColumn(source.y_column, "Line chart y column"),
76
+ show_legend: normalizeBooleanFlag(source.show_legend, true),
77
+ show_labels: normalizeBooleanFlag(source.show_labels, false),
78
+ sort_direction: normalizeSortDirection(source.sort_direction, "asc"),
79
+ smooth: normalizeBooleanFlag(source.smooth, false),
80
+ };
81
+ case "pie":
82
+ return {
83
+ label_column: normalizeRequiredColumn(source.label_column, "Pie chart label column"),
84
+ value_column: normalizeRequiredColumn(source.value_column, "Pie chart value column"),
85
+ show_legend: normalizeBooleanFlag(source.show_legend, true),
86
+ show_labels: normalizeBooleanFlag(source.show_labels, true),
87
+ donut: normalizeBooleanFlag(source.donut, false),
88
+ };
89
+ case "scatter":
90
+ return {
91
+ x_column: normalizeRequiredColumn(source.x_column, "Scatter chart x column"),
92
+ y_column: normalizeRequiredColumn(source.y_column, "Scatter chart y column"),
93
+ size_column: normalizeOptionalColumn(source.size_column),
94
+ series_column: normalizeOptionalColumn(source.series_column),
95
+ show_legend: normalizeBooleanFlag(source.show_legend, true),
96
+ };
97
+ default:
98
+ throw new ValidationError(`Unsupported chart type: ${chartType}`);
99
+ }
100
+ }
101
+
102
+ function normalizeResultColumns(resultColumns = []) {
103
+ if (!Array.isArray(resultColumns)) {
104
+ return [];
105
+ }
106
+
107
+ const seen = new Set();
108
+
109
+ return resultColumns
110
+ .map((column) => {
111
+ if (!column || typeof column !== "object") {
112
+ return null;
113
+ }
114
+
115
+ const name = String(column.name ?? "").trim();
116
+
117
+ if (!name || seen.has(name)) {
118
+ return null;
119
+ }
120
+
121
+ seen.add(name);
122
+
123
+ const type = SUPPORTED_RESULT_COLUMN_TYPES.includes(column.type)
124
+ ? column.type
125
+ : "unknown";
126
+
127
+ return { name, type };
128
+ })
129
+ .filter(Boolean);
130
+ }
131
+
132
+ function buildDefaultChartName(chartType, queryName) {
133
+ const normalizedType = normalizeChartType(chartType);
134
+ const normalizedQueryName = normalizeChartName(queryName) || "Query";
135
+ return `${normalizedType[0].toUpperCase()}${normalizedType.slice(1)}_${normalizedQueryName}`;
136
+ }
137
+
138
+ module.exports = {
139
+ SUPPORTED_QUERY_HISTORY_CHART_TYPES,
140
+ buildDefaultChartName,
141
+ normalizeChartConfig,
142
+ normalizeChartName,
143
+ normalizeChartType,
144
+ normalizeResultColumns,
145
+ };