🌳
pt0/serverF/dbF/pgLuxonPatchAI.mts
1import pg from 'pg'
3const isLuxonDateTime = (val: unknown): val is { toISO: () => string } =>
4 typeof val === 'object' && val !== null && (val as any).isLuxonDateTime === true
6const mapLuxonValues = (values: unknown[] | undefined): unknown[] | undefined => {
7 if (!values) return values
8 let mapped: unknown[] | undefined
9 for (let i = 0; i < values.length; i++) {
10 const v = values[i]
11 if (isLuxonDateTime(v)) {
12 (mapped ||= values.slice())[i] = v.toISO()
13 }
14 }
15 return mapped || values
18const origClientQuery = (pg as any).Client.prototype.query
19if (origClientQuery && !origClientQuery._luxonPatched) {
20 ;(pg as any).Client.prototype.query = function(config: any, values: any, callback: any) {
21 if (config && config.values) {
22 config.values = mapLuxonValues(config.values)
23 } else if (Array.isArray(values)) {
24 values = mapLuxonValues(values)
25 }
26 return origClientQuery.call(this, config, values, callback)
27 }
28 ;(pg as any).Client.prototype.query._luxonPatched = true