1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
import {
ColumnFiltersState,
SortingState,
PaginationState,
GroupingState,
ColumnDef
} from "@tanstack/react-table";
import {
SQL,
and,
or,
eq,
ilike,
gt,
lt,
gte,
lte,
inArray,
asc,
desc,
getTableColumns,
} from "drizzle-orm";
import { PgTable, PgView, PgColumn } from "drizzle-orm/pg-core";
// Helper to detect if value is empty or undefined
const isEmpty = (value: any) => value === undefined || value === null || value === "";
export interface DrizzleTableState {
sorting?: SortingState;
columnFilters?: ColumnFiltersState;
globalFilter?: string;
pagination?: PaginationState;
grouping?: GroupingState;
}
export class DrizzleTableAdapter<TData> {
private columnMap: Map<string, PgColumn>;
constructor(
private table: PgTable | PgView,
private columns: ColumnDef<TData, any>[]
) {
// Create a map of accessorKey -> Drizzle Column for fast lookup
this.columnMap = new Map();
// @ts-ignore - getTableColumns works on views in newer drizzle versions or we can cast
const drizzleColumns = getTableColumns(table as any);
columns.forEach(col => {
// We currently only support accessorKey which maps directly to a DB column
if ('accessorKey' in col && typeof col.accessorKey === 'string') {
const dbCol = drizzleColumns[col.accessorKey];
if (dbCol) {
this.columnMap.set(col.accessorKey, dbCol);
}
}
});
}
private getColumn(columnId: string): PgColumn | undefined {
return this.columnMap.get(columnId);
}
/**
* Build the WHERE clause based on column filters and global filter
*/
getWhere(columnFilters?: ColumnFiltersState, globalFilter?: string): SQL | undefined {
const conditions: SQL[] = [];
// 1. Column Filters
if (columnFilters) {
for (const filter of columnFilters) {
const column = this.getColumn(filter.id);
if (!column) continue;
const value = filter.value;
if (isEmpty(value)) continue;
// Handle Array (range or multiple select)
if (Array.isArray(value)) {
// Range filter (e.g. [min, max])
if (value.length === 2 && (typeof value[0] === 'number' || typeof value[0] === 'string')) {
const [min, max] = value;
if (!isEmpty(min) && !isEmpty(max)) {
conditions.push(and(gte(column, min), lte(column, max))!);
} else if (!isEmpty(min)) {
conditions.push(gte(column, min)!);
} else if (!isEmpty(max)) {
conditions.push(lte(column, max)!);
}
}
// Multi-select (IN)
else if (value.length > 0) {
conditions.push(inArray(column, value)!);
}
}
// Boolean
else if (typeof value === 'boolean') {
conditions.push(eq(column, value)!);
}
// Number
else if (typeof value === 'number') {
conditions.push(eq(column, value)!);
}
// String (Search)
else if (typeof value === 'string') {
conditions.push(ilike(column, `%${value}%`)!);
}
}
}
// 2. Global Filter
if (globalFilter) {
const searchConditions: SQL[] = [];
this.columnMap.forEach((column) => {
// Implicitly supports only text-compatible columns for ilike
// Drizzle might throw if type mismatch, so user should be aware
searchConditions.push(ilike(column, `%${globalFilter}%`));
});
if (searchConditions.length > 0) {
conditions.push(or(...searchConditions)!);
}
}
return conditions.length > 0 ? and(...conditions) : undefined;
}
/**
* Build the ORDER BY clause
*/
getOrderBy(sorting?: SortingState): SQL[] {
if (!sorting || !sorting.length) return [];
return sorting.map((sort) => {
const column = this.getColumn(sort.id);
if (!column) return null;
return sort.desc ? desc(column) : asc(column);
}).filter(Boolean) as SQL[];
}
/**
* Build the GROUP BY clause
*/
getGroupBy(grouping?: GroupingState): SQL[] {
if (!grouping || !grouping.length) return [];
return grouping.map(g => this.getColumn(g)).filter(Boolean) as SQL[];
}
/**
* Get Limit and Offset
*/
getPagination(pagination?: PaginationState) {
if (!pagination) return { limit: 10, offset: 0 };
return {
limit: pagination.pageSize,
offset: pagination.pageIndex * pagination.pageSize,
};
}
/**
* Helper to apply all state to a query builder.
* Returns the modifier objects that can be passed to drizzle query builder.
*/
getQueryParts(state: DrizzleTableState) {
return {
where: this.getWhere(state.columnFilters, state.globalFilter),
orderBy: this.getOrderBy(state.sorting),
groupBy: this.getGroupBy(state.grouping),
...this.getPagination(state.pagination)
};
}
}
|