import type { Session } from "neo4j-driver" ; import { isInt , integer } from "neo4j-driver" ; const isoDateRE = /(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/ ; function isDate ( value ) { return value && isoDateRE . test ( value ) && ! isNaN ( Date . parse ( value ) ) ; } export const format = { /** Takes a plain old JavaScript object and turns it into a Neo4j compatible object */ to ( object ) { const newObject = { } ; for ( const key in object ) { const value = object [ key ] ; if ( value instanceof Date ) newObject [ key ] = value . toISOString ( ) ; else newObject [ key ] = value ; } return newObject ; } , /** Takes a Neo4j object and returns a plain old JavaScript object */ from < T = Record < string , unknown > > ( object ? ) { const newObject = { } ; if ( ! object ) return null ; for ( const key in object ) { const value = object [ key ] ; if ( isDate ( value ) ) { newObject [ key ] = new Date ( value ) ; } else if ( isInt ( value ) ) { if ( integer . inSafeRange ( value ) ) newObject [ key ] = value . toNumber ( ) ; else newObject [ key ] = value . toString ( ) ; } else { newObject [ key ] = value ; } } return newObject as T ; } , } ; export function client ( session ) { return { /** Reads values from the database */ async read < T > ( statement , values ? ) { const result = await session . readTransaction ( ( tx ) => tx . run ( statement , values ) ) ; return format . from < T > ( result ?. records [ 0 ] ?. get ( 0 ) ) ?? null ; } , /**
         * Reads/writes values from/to the database.
         * Properties are available under `$data`
         */ async write < T > ( statement , values ) { const result = await session . writeTransaction ( ( tx ) => tx . run ( statement , { data : format . to ( values ) } ) ) ; return format . from < T > ( result ?. records [ 0 ] ?. toObject ( ) ) ; } , } ; }