Skip to content

Commit cfc976b

Browse files
committed
Merge branch 'next' of github.com:devforth/adminforth into next
2 parents e27b8e4 + 3c815a2 commit cfc976b

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

adminforth/dataConnectors/sqlite.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ class SQLiteConnector extends AdminForthBaseConnector implements IAdminForthData
99

1010
async setupClient(url: string): Promise<void> {
1111
this.client = betterSqlite3(url.replace('sqlite://', ''));
12+
this.client.aggregate('median', {
13+
start: (): number[] => [],
14+
step: (acc: number[], val: any) => {
15+
if (val != null) acc.push(Number(val));
16+
},
17+
result: (acc: number[]): number | null => {
18+
if (acc.length === 0) return null;
19+
const sorted = acc.slice().sort((a, b) => a - b);
20+
const mid = Math.floor(sorted.length / 2);
21+
return sorted.length % 2 === 0
22+
? (sorted[mid - 1] + sorted[mid]) / 2
23+
: sorted[mid];
24+
},
25+
});
1226
}
1327
async getAllTables(): Promise<Array<string>> {
1428
const stmt = this.client.prepare(
@@ -358,7 +372,7 @@ class SQLiteConnector extends AdminForthBaseConnector implements IAdminForthData
358372
case 'avg': selectParts.push(`AVG("${rule.field}") AS "${alias}"`); break;
359373
case 'min': selectParts.push(`MIN("${rule.field}") AS "${alias}"`); break;
360374
case 'max': selectParts.push(`MAX("${rule.field}") AS "${alias}"`); break;
361-
case 'median': throw new Error('Aggregates.median() with GroupBy.Field is not supported in SQLite.');
375+
case 'median': selectParts.push(`median("${rule.field}") AS "${alias}"`); break;
362376
}
363377
}
364378

0 commit comments

Comments
 (0)