From e78e8b6ec1205288aed7e417a2f8045c744a4752 Mon Sep 17 00:00:00 2001 From: Sergey Ukustov Date: Tue, 11 Apr 2023 18:14:55 +0300 Subject: [PATCH] Replace `toISOString` calls with io-ts codec (#990) --- src/__tests__/knex-timestamps.test.ts | 5 +++-- src/models/request.ts | 8 ++++---- src/repositories/request-repository.ts | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/__tests__/knex-timestamps.test.ts b/src/__tests__/knex-timestamps.test.ts index 1b0e9cce1..35d182463 100644 --- a/src/__tests__/knex-timestamps.test.ts +++ b/src/__tests__/knex-timestamps.test.ts @@ -2,6 +2,7 @@ import { test, expect, beforeAll, afterAll } from '@jest/globals' import type { Knex } from 'knex' import { createDbConnection } from '../db-connection.js' import { isClose, randomStreamID, seconds } from './test-utils.js' +import * as te from '../ancillary/io-ts-extra.js' let dbConnection: Knex @@ -41,9 +42,9 @@ test('JS-to-PG conversion ignores timezone', async () => { .insert({ streamId: randomStreamID().toString(), metadata: {}, - usedAt: withTimezone.toISOString(), + usedAt: te.date.encode(withTimezone), }) .returning('usedAt') .then((rows) => rows[0].usedAt as Date) - expect(pgTimestamp1.toISOString()).toEqual('2023-01-01T19:04:05.678Z') + expect(te.date.encode(pgTimestamp1)).toEqual('2023-01-01T19:04:05.678Z') }) diff --git a/src/models/request.ts b/src/models/request.ts index 1013bab9b..db4c429ed 100644 --- a/src/models/request.ts +++ b/src/models/request.ts @@ -17,7 +17,7 @@ export type IDBRequest = { streamId: string message: string pinned: boolean - timestamp: string + timestamp?: string createdAt?: string updatedAt?: string origin?: string @@ -87,9 +87,9 @@ export class Request { streamId: this.streamId.toString(), message: this.message, pinned: this.pinned, - createdAt: this.createdAt?.toISOString(), - updatedAt: this.updatedAt?.toISOString(), - timestamp: this.timestamp?.toISOString(), + createdAt: this.createdAt ? te.date.encode(this.createdAt) : undefined, + updatedAt: this.updatedAt ? te.date.encode(this.updatedAt) : undefined, + timestamp: this.timestamp ? te.date.encode(this.timestamp) : undefined, origin: this.origin, } } diff --git a/src/repositories/request-repository.ts b/src/repositories/request-repository.ts index 93b975b84..c034e16c7 100644 --- a/src/repositories/request-repository.ts +++ b/src/repositories/request-repository.ts @@ -149,7 +149,7 @@ export class RequestRepository { message: fields.message, status: fields.status, pinned: fields.pinned, - updatedAt: updatedAt.toISOString(), + updatedAt: te.date.encode(updatedAt), }) .whereIn('id', ids)