@@ -6,7 +6,7 @@ import { buildWhere } from './rows'
66
77export const totalQuery = createQuery ( {
88 type : type ( {
9- count : 'string ' ,
9+ count : 'number ' ,
1010 isEstimated : 'boolean' ,
1111 } ) ,
1212 query : ( {
@@ -24,50 +24,43 @@ export const totalQuery = createQuery({
2424 if ( ! exact && ! filters ?. length ) {
2525 const estimate = await db
2626 . withSchema ( 'pg_catalog' )
27- . withTables < {
28- pg_class : { reltuples : number , relname : string , relnamespace : number }
29- pg_namespace : { oid : number , nspname : string }
30- } > ( )
31- . selectFrom ( 'pg_class' )
32- . innerJoin ( 'pg_namespace' , 'pg_namespace.oid' , 'pg_class.relnamespace' )
33- . select ( 'pg_class.reltuples as count' )
34- . where ( 'pg_namespace.nspname' , '=' , schema )
35- . where ( 'pg_class.relname' , '=' , table )
27+ . selectFrom ( 'pg_catalog.pg_class' )
28+ . innerJoin ( 'pg_catalog.pg_namespace' , 'pg_catalog.pg_namespace.oid' , 'pg_catalog.pg_class.relnamespace' )
29+ . select ( 'pg_catalog.pg_class.reltuples as count' )
30+ . where ( 'pg_catalog.pg_namespace.nspname' , '=' , schema )
31+ . where ( 'pg_catalog.pg_class.relname' , '=' , table )
3632 . executeTakeFirst ( )
3733
38- if ( estimate && Number ( estimate . count ) >= 0 ) {
34+ if ( estimate && estimate . count >= 0 ) {
3935 return {
40- count : String ( Math . round ( Number ( estimate . count ) ) ) ,
36+ count : Math . round ( estimate . count ) ,
4137 isEstimated : true ,
4238 }
4339 }
4440 }
41+
4542 const query = await db
4643 . withSchema ( schema )
4744 . withTables < { [ table ] : Record < string , unknown > } > ( )
4845 . selectFrom ( table )
4946 . select ( db . fn . countAll ( ) . as ( 'total' ) )
5047 . $if ( filters !== undefined , qb => qb . where ( eb => buildWhere ( eb , filters ! ) ) )
51- . execute ( )
48+ . executeTakeFirst ( )
5249
53- return { count : String ( query [ 0 ] ?. total ?? 0 ) , isEstimated : false }
50+ return { count : Number ( query ?. total ?? 0 ) , isEstimated : false }
5451 } ,
55-
5652 mysql : async ( db ) => {
5753 if ( ! exact && ! filters ?. length ) {
5854 const estimate = await db
5955 . withSchema ( 'information_schema' )
60- . withTables < {
61- TABLES : { TABLE_SCHEMA : string , TABLE_NAME : string , TABLE_ROWS : number }
62- } > ( )
63- . selectFrom ( 'TABLES' )
56+ . selectFrom ( 'information_schema.TABLES' )
6457 . select ( 'TABLE_ROWS as count' )
6558 . where ( 'TABLE_SCHEMA' , '=' , schema )
6659 . where ( 'TABLE_NAME' , '=' , table )
6760 . executeTakeFirst ( )
6861
69- if ( estimate && Number ( estimate . count ) >= 0 ) {
70- return { count : String ( estimate . count ) , isEstimated : true }
62+ if ( estimate && estimate . count >= 0 ) {
63+ return { count : estimate . count , isEstimated : true }
7164 }
7265 }
7366
@@ -77,9 +70,9 @@ export const totalQuery = createQuery({
7770 . selectFrom ( table )
7871 . select ( db . fn . countAll ( ) . as ( 'total' ) )
7972 . $if ( filters !== undefined , qb => qb . where ( eb => buildWhere ( eb , filters ! ) ) )
80- . execute ( )
73+ . executeTakeFirst ( )
8174
82- return { count : String ( query [ 0 ] ?. total ?? 0 ) , isEstimated : false }
75+ return { count : Number ( query ?. total ?? 0 ) , isEstimated : false }
8376 } ,
8477
8578 mssql : async ( db ) => {
@@ -89,10 +82,10 @@ export const totalQuery = createQuery({
8982 . selectFrom ( table )
9083 . select ( db . fn . countAll ( ) . as ( 'total' ) )
9184 . $if ( filters !== undefined , qb => qb . where ( eb => buildWhere ( eb , filters ! ) ) )
92- . execute ( )
85+ . executeTakeFirst ( )
9386
9487 return {
95- count : String ( query [ 0 ] ?. total ?? 0 ) ,
88+ count : Number ( query ?. total ?? 0 ) ,
9689 isEstimated : false ,
9790 }
9891 } ,
@@ -101,18 +94,15 @@ export const totalQuery = createQuery({
10194 if ( ! exact && ! filters ?. length ) {
10295 const estimate = await db
10396 . withSchema ( 'system' )
104- . withTables < {
105- parts : { database : string , table : string , rows : number , active : number }
106- } > ( )
107- . selectFrom ( 'parts' )
97+ . selectFrom ( 'system.parts' )
10898 . select ( db . fn . sum ( sql . ref ( 'rows' ) ) . as ( 'count' ) )
10999 . where ( 'database' , '=' , schema )
110100 . where ( 'table' , '=' , table )
111101 . where ( 'active' , '=' , 1 )
112102 . executeTakeFirst ( )
113103
114104 if ( estimate && Number ( estimate . count ) >= 0 ) {
115- return { count : String ( estimate . count ) , isEstimated : true }
105+ return { count : Number ( estimate . count ) , isEstimated : true }
116106 }
117107 }
118108
@@ -122,9 +112,9 @@ export const totalQuery = createQuery({
122112 . selectFrom ( table )
123113 . select ( db . fn . countAll ( ) . as ( 'total' ) )
124114 . $if ( filters !== undefined , qb => qb . where ( eb => buildWhere ( eb , filters ! ) ) )
125- . execute ( )
115+ . executeTakeFirst ( )
126116
127- return { count : String ( query [ 0 ] ?. total ?? 0 ) , isEstimated : false }
117+ return { count : Number ( query ?. total ?? 0 ) , isEstimated : false }
128118 } ,
129119 } ) ,
130120} )
0 commit comments