* Knex returns `datetime` and `date` columns as Date.. This is wrong for date / datetime, as those * shouldn't return with time / timezone info respectively
(
fieldEntries: [string, FieldOverview][],
payloads: Partial<Record<string, any>>[],
action: PayloadAction,
aliasMap: Record<string, string> = {},
aggregate: Aggregate = {},
)
| 414 | * shouldn't return with time / timezone info respectively |
| 415 | */ |
| 416 | processDates( |
| 417 | fieldEntries: [string, FieldOverview][], |
| 418 | payloads: Partial<Record<string, any>>[], |
| 419 | action: PayloadAction, |
| 420 | aliasMap: Record<string, string> = {}, |
| 421 | aggregate: Aggregate = {}, |
| 422 | ): Partial<Record<string, any>>[] { |
| 423 | // Include aggegation e.g. "count->id" in alias map |
| 424 | const aggregateMapped = Object.fromEntries( |
| 425 | Object.entries(aggregate).reduce<string[][]>((acc, [key, values]) => { |
| 426 | acc.push(...values.map((value) => [`${key}->${value}`, value])); |
| 427 | return acc; |
| 428 | }, []), |
| 429 | ); |
| 430 | |
| 431 | const aliasFields = { ...aliasMap, ...aggregateMapped }; |
| 432 | |
| 433 | for (const aliasField in aliasFields) { |
| 434 | const schemaField = aliasFields[aliasField]; |
| 435 | const field = this.schema.collections[this.collection]!.fields[schemaField!]; |
| 436 | |
| 437 | if (field) { |
| 438 | fieldEntries.push([ |
| 439 | aliasField, |
| 440 | { |
| 441 | ...field, |
| 442 | field: aliasField, |
| 443 | }, |
| 444 | ]); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | const dateColumns = fieldEntries.filter(([_name, field]) => ['dateTime', 'date', 'timestamp'].includes(field.type)); |
| 449 | |
| 450 | const timeColumns = fieldEntries.filter(([_name, field]) => { |
| 451 | return field.type === 'time'; |
| 452 | }); |
| 453 | |
| 454 | if (dateColumns.length === 0 && timeColumns.length === 0) return payloads; |
| 455 | |
| 456 | for (const [name, dateColumn] of dateColumns) { |
| 457 | for (const payload of payloads) { |
| 458 | let value: number | string | Date = payload[name]; |
| 459 | |
| 460 | if (value === null || (typeof value === 'string' && /^[.0 :-]{10,}$/.test(value))) { |
| 461 | payload[name] = null; |
| 462 | continue; |
| 463 | } |
| 464 | |
| 465 | if (!value) continue; |
| 466 | |
| 467 | if (action === 'read') { |
| 468 | if (typeof value === 'number' || typeof value === 'string') { |
| 469 | value = new Date(value); |
| 470 | } |
| 471 | |
| 472 | if (dateColumn.type === 'timestamp') { |
| 473 | const newValue = this.helpers.date.readTimestampString(value.toISOString()); |
no test coverage detected