11import * as flech from "@uwdata/flechette" ;
2- // @ts -types="../deps/mosaic-core.d.ts"
32import {
43 type Coordinator ,
5- type FieldInfo ,
6- type FieldRequest ,
74 MosaicClient ,
5+ queryFieldInfo ,
86 Selection ,
97} from "@uwdata/mosaic-core" ;
10- // @ts -types="../deps/mosaic-sql.d.ts"
11- import { desc , Query , type SQLExpression } from "@uwdata/mosaic-sql" ;
8+ import {
9+ asc ,
10+ desc ,
11+ type FilterExpr ,
12+ Query ,
13+ type SelectQuery ,
14+ } from "@uwdata/mosaic-sql" ;
1215import * as signals from "@preact/signals-core" ;
1316import { html } from "htl" ;
1417
@@ -21,6 +24,7 @@ import { signal } from "@preact/signals-core";
2124
2225import stylesString from "./styles.css.ts" ;
2326import { StatusBar } from "./StatusBar.ts" ;
27+ import { isFlechetteTable } from "../utils/guards.ts" ;
2428
2529interface DataTableOptions {
2630 table : string ;
@@ -55,7 +59,9 @@ export async function datatable(
5559 . from ( table )
5660 . select ( options . columns ?? [ "*" ] )
5761 . limit ( 0 ) ,
62+ { type : "arrow" } ,
5863 ) ;
64+ assert ( isFlechetteTable ( empty ) , "Expected a flechette table" ) ;
5965 let client = new DataTable ( {
6066 table,
6167 schema : empty . schema ,
@@ -156,17 +162,6 @@ export class DataTable extends MosaicClient {
156162 return this . #sql;
157163 }
158164
159- /**
160- * Mosaic function. Client defines the fields to be requested from the coordinator.
161- */
162- override fields ( ) : Array < FieldRequest > {
163- return this . #columns. map ( ( column ) => ( {
164- table : this . #meta. table ,
165- column,
166- stats : [ ] ,
167- } ) ) ;
168- }
169-
170165 node ( ) : HTMLElement {
171166 return this . #root;
172167 }
@@ -187,10 +182,10 @@ export class DataTable extends MosaicClient {
187182 * @param filter - The filter predicates to apply to the query
188183 * @returns The query to be executed
189184 */
190- override query ( filter : Array < unknown > = [ ] ) : Query {
185+ override query ( filter ?: FilterExpr | null | undefined ) : SelectQuery {
191186 let query = Query . from ( this . #meta. table )
192187 . select ( this . #columns)
193- . where ( filter )
188+ . where ( filter ?? [ ] )
194189 . orderby (
195190 this . #orderby
196191 . filter ( ( o ) => o . order !== "unset" )
@@ -227,7 +222,7 @@ export class DataTable extends MosaicClient {
227222 /**
228223 * Mosaic lifecycle function called after `queryResult`
229224 */
230- update ( ) : this {
225+ override update ( ) : this {
231226 if ( ! this . #pendingInternalRequest) {
232227 // on the first update, populate the table with initial data
233228 this . #appendRows( this . #rows * 2 ) ;
@@ -244,21 +239,26 @@ export class DataTable extends MosaicClient {
244239 this . requestQuery ( query ) ;
245240
246241 // prefetch subsequent data batch
247- this . coordinator . prefetch ( query . clone ( ) . offset ( offset + this . #limit) ) ;
242+ this . coordinator ! . prefetch ( query . clone ( ) . offset ( offset + this . #limit) ) ;
248243 }
249244
250- /**
251- * Mosaic lifecycle function called when the client is connected to a coordinator.
252- */
253- override fieldInfo ( infos : Array < FieldInfo > ) : this {
245+ override async prepare ( ) : Promise < void > {
246+ const infos = await queryFieldInfo (
247+ this . coordinator ! ,
248+ this . #columns. map ( ( column ) => ( {
249+ table : this . #meta. table ,
250+ column,
251+ stats : [ ] ,
252+ } ) ) ,
253+ ) ;
254254 let classes = classof ( this . #meta. schema ) ;
255255
256256 {
257257 let statusBar = new StatusBar ( {
258258 table : this . #meta. table ,
259259 filterBy : this . filterBy ,
260260 } ) ;
261- this . coordinator . connect ( statusBar ) ;
261+ this . coordinator ! . connect ( statusBar ) ;
262262 this . #shadowRoot. querySelector ( ".quak" ) ?. appendChild (
263263 statusBar . node ( ) ,
264264 ) ;
@@ -290,7 +290,7 @@ export class DataTable extends MosaicClient {
290290 } ) ;
291291 }
292292 let th = thcol ( field , this . #columnWidth, vis ) ;
293- this . coordinator . connect ( vis ) ;
293+ this . coordinator ! . connect ( vis ) ;
294294 return th ;
295295 } ) ;
296296
@@ -334,8 +334,6 @@ export class DataTable extends MosaicClient {
334334 }
335335 } ) ;
336336 }
337-
338- return this ;
339337 }
340338
341339 /** Number of rows to append */
@@ -557,23 +555,6 @@ function shouldGrayoutValue(value: string) {
557555 ) ;
558556}
559557
560- /**
561- * A mosaic SQL expression for ascending order
562- *
563- * The normal behavior in SQL is to sort nulls first when sorting in ascending order.
564- * This function returns an expression that sorts nulls last (i.e., `NULLS LAST`),
565- * like the `desc` function.
566- *
567- * @param field
568- */
569- function asc ( field : string ) : SQLExpression {
570- // doesn't sort nulls for asc
571- let expr = desc ( field ) ;
572- // @ts -expect-error - private field
573- expr . _expr [ 0 ] = expr . _expr [ 0 ] . replace ( "DESC" , "ASC" ) ;
574- return expr ;
575- }
576-
577558/**
578559 * Adds custom wheel behavior to an HTML element, allowing either horizontal or vertical scrolling based on the scroll input.
579560 * Prevents default scrolling to stop event propagation to parent elements.
0 commit comments