import Knex from 'knex' ; import knexConfig from './config/knexfile' ; import { DateTime } from 'luxon' ; import { types } from 'pg' ; import { Query } from 'express-serve-static-core' ; let connectionString ; if ( typeof knexConfig . connection === 'string' ) { connectionString = knexConfig . connection ; } else if ( Array . isArray ( knexConfig . connection ) && 'user' in knexConfig . connection && 'database' in knexConfig . connection ) { connectionString = knexConfig . connection . user + '@' + knexConfig . connection . database ; } else if ( Array . isArray ( knexConfig . connection ) && 'filename' in knexConfig . connection ) { connectionString = knexConfig . connection . filename ; } else { connectionString = 'custom connection (see knexfile)' ; } console . log ( `[DB] DB connection set to ${ connectionString }` ) ; types . setTypeParser ( 1700 , 'text' , parseFloat ) ; export const db = Knex ( knexConfig ) ; export async function dbConnect ( ) { return db . raw ( 'SELECT 1+1 AS result' ) ; } export async function dbDestroy ( ) { return db . destroy ( ) ; } const sortReg = /^(-?)(.+)$/ ; export function sort2order ( sort ) { const sortItems = sort . split ( ',' ) ; return sortItems . map ( item => { const match = sortReg . exec ( item ) ; if ( ! match || match . length < 3 ) { return { column : '' , order : '' } ; } return { column : match [ 2 ] , order : match [ 1 ] === '-' ? ( 'DESC' as const ) : ( 'ASC' as const ) , } ; } ) . filter ( item => { return item . column . length > 0 && item . order . length > 0 ; } ) ; } const validDateRegexp = /^(\d{4}-\d{2}-\d{2})$/ ; export function isValidDateString ( original ) { if ( typeof original === 'string' ) { return original . match ( validDateRegexp ) !== null ; } return false ; } export function getValidDateString ( original , returnNullOnInvalid = true ) { if ( typeof original === 'string' ) { if ( original . match ( validDateRegexp ) !== null ) { return original ; } } if ( returnNullOnInvalid ) { return null ; } return DateTime . local ( ) . toSQLDate ( ) ; }