| 170 | } |
| 171 | |
| 172 | export function mapArg(arg: unknown, argType: ArgType, options?: PrismaLibSqlOptions): InValue { |
| 173 | if (arg === null) { |
| 174 | return null |
| 175 | } |
| 176 | |
| 177 | if (typeof arg === 'string' && argType.scalarType === 'bigint') { |
| 178 | return BigInt(arg) |
| 179 | } |
| 180 | |
| 181 | if (typeof arg === 'string' && argType.scalarType === 'decimal') { |
| 182 | // This can lose precision, but SQLite does not have a native decimal type. |
| 183 | // This is how we have historically handled it. |
| 184 | return Number.parseFloat(arg) |
| 185 | } |
| 186 | |
| 187 | if (typeof arg === 'string' && argType.scalarType === 'datetime') { |
| 188 | arg = new Date(arg) |
| 189 | } |
| 190 | |
| 191 | if (arg instanceof Date) { |
| 192 | const format = options?.timestampFormat ?? 'iso8601' |
| 193 | switch (format) { |
| 194 | case 'unixepoch-ms': |
| 195 | return arg.getTime() |
| 196 | case 'iso8601': |
| 197 | return arg.toISOString().replace('Z', '+00:00') |
| 198 | default: |
| 199 | throw new Error(`Unknown timestamp format: ${format}`) |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | if (typeof arg === 'string' && argType.scalarType === 'bytes') { |
| 204 | return Buffer.from(arg, 'base64') |
| 205 | } |
| 206 | |
| 207 | return arg as InValue |
| 208 | } |