import invariant from "tiny-invariant" ; import traverse from "traverse" ; export const prepareStatement = ( input , type = "insert" ) : [ string , any [ ] ] => { if ( type === "insert" ) { const cols = Object . keys ( input ) . join ( "," ) ; const bindings = Object . keys ( input ) . map ( ( ) => "?" ) . join ( "," ) ; const vals = Object . values ( input ) ; const statement = `(${ cols }) VALUES (${ bindings })` ; return [ statement , vals ] ; } if ( type === "update" ) { const statement = Object . keys ( input ) . map ( ( k ) => { return `${ k } = ?` ; } ) . join ( "," ) ; const vals = Object . values ( input ) ; return [ statement , vals ] ; } invariant ( false , "Unsupported query type" ) ; } ; export const getType = ( type ) : string => { const types = { "number" : "INTEGER" , "string" : "TEXT" , "boolean" : "INTEGER" } ; return types [ type ] as string ; } ; param rows export const flattenResultSet = ( rows ) : any [ ] => { const result = [ ] ; for ( let i = 0 ; i < rows . length ; i ++ ) { const row = rows . item ( i ) ; result . push ( row ) ; } return result ; } ; export function serializeData ( input ) { return traverse ( input ) . map ( function ( item ) { if ( this . isRoot ) { return ; } if ( ( Array . isArray ( item ) || typeof item === "object" ) ) { this . update ( `serialized:${ JSON . stringify ( item ) }` ) ; } } ) ; } export function deserializeData ( data ) { return traverse ( data ) . map ( function ( d ) { if ( typeof d === "string" ) { const item = d . split ( "serialized:" ) ; if ( item . length > 1 ) { this . update ( JSON . parse ( item [ 1 ] ) ) ; } } } ) ; }