import * as firebase from "firebase/app" ; import { Query } from "./types" ; export const createFirestoreQuery = < T > ( collection , query ? ) : firebase . firestore . Query => { let ref = collection ; if ( query != null ) { if ( query . where ) { for ( const [ field , op , value ] of query . where ) { ref = ref . where ( field . toString ( ) , op , value ) ; } } if ( query . orderBy ) { for ( const orderBy of query . orderBy ) { ref = ref . orderBy ( orderBy [ 0 ] . toString ( ) , orderBy . length === 2 ? orderBy [ 1 ] : undefined ) ; } } if ( query . limit ) { ref = ref . limit ( query . limit ) ; } } return ref ; } ; export const createQueryName = < T > ( collectionName , query ? ) : string => { if ( query == null ) { return `${ collectionName }-all` ; } let name = collectionName ; if ( query . where ) { name = query . where . reduce ( ( name , [ field , op , value ] ) => `${ name }-${ field }${ op }${ value . toString ( ) }` , name ) ; } if ( query . orderBy ) { name = query . orderBy . reduce ( ( name , orderBy ) => `${ name }-${ orderBy [ 0 ] }${ orderBy . length === 2 ? "-" + orderBy [ 1 ] : "" }` , name ) ; } if ( query . limit ) { name = `${ name }-${ query . limit }` ; } return name ; } ;