From 2d574330c77ebd005a26aa322868bb61616f82ec Mon Sep 17 00:00:00 2001 From: kirillgroshkov Date: Thu, 28 Dec 2023 17:26:41 +0100 Subject: [PATCH] feat: BaseDBEntity.id only supports `string` now Previously it had a generic ID type that could be string or number. This change is a step towards simplification. A trade-off towards simplicity, against flexibility. --- src/json-schema/jsonSchemaBuilder.ts | 12 ++++-------- src/types.test.ts | 14 +++++++------- src/types.ts | 25 +++++++++++-------------- 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/json-schema/jsonSchemaBuilder.ts b/src/json-schema/jsonSchemaBuilder.ts index 07752d43..9fba3390 100644 --- a/src/json-schema/jsonSchemaBuilder.ts +++ b/src/json-schema/jsonSchemaBuilder.ts @@ -368,11 +368,9 @@ export class JsonSchemaObjectBuilder extends JsonSchemaAnyB return this } - baseDBEntity( - idType = 'string', - ): JsonSchemaObjectBuilder> { + baseDBEntity(): JsonSchemaObjectBuilder { Object.assign(this.schema.properties, { - id: { type: idType }, + id: { type: 'string' }, created: { type: 'number', format: 'unixTimestamp2000' }, updated: { type: 'number', format: 'unixTimestamp2000' }, }) @@ -380,10 +378,8 @@ export class JsonSchemaObjectBuilder extends JsonSchemaAnyB return this } - savedDBEntity( - idType = 'string', - ): JsonSchemaObjectBuilder> { - return this.baseDBEntity(idType).addRequired(['id', 'created', 'updated']) as any + savedDBEntity(): JsonSchemaObjectBuilder { + return this.baseDBEntity().addRequired(['id', 'created', 'updated']) as any } extend(s2: JsonSchemaObjectBuilder): JsonSchemaObjectBuilder { diff --git a/src/types.test.ts b/src/types.test.ts index 7f263caf..87d3db14 100644 --- a/src/types.test.ts +++ b/src/types.test.ts @@ -25,7 +25,7 @@ import { } from './types' import type { AppError } from '.' -interface Item extends BaseDBEntity { +interface Item extends BaseDBEntity { a?: number } @@ -52,13 +52,13 @@ test('saved/unsaved', () => { expectTypeOf(item).toEqualTypeOf<{ a?: number - id?: number + id?: string created?: number updated?: number }>() const itemDBM: ItemDBM = { - id: 5, // should only allow number, but not string + id: '5', // should only allow string, but not number created: 1, updated: 1, a: 5, @@ -67,7 +67,7 @@ test('saved/unsaved', () => { delete itemDBM.a expectTypeOf(itemDBM).toEqualTypeOf<{ - id: number + id: string created: number updated: number a?: number @@ -81,7 +81,7 @@ test('saved/unsaved', () => { expectTypeOf(unsavedItem).toEqualTypeOf<{ a?: number - id?: number + id?: string created?: number updated?: number }>() @@ -97,7 +97,7 @@ test('saved/unsaved', () => { expectTypeOf(unsavedItemDBM).toEqualTypeOf<{ a?: number - id?: number + id?: string created?: number updated?: number }>() @@ -106,7 +106,7 @@ test('saved/unsaved', () => { delete unsavedItemId.id expectTypeOf(unsavedItemId).toEqualTypeOf<{ - id?: number + id?: string created: number updated: number a?: number diff --git a/src/types.ts b/src/types.ts index 02bd5690..71c335e9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -25,26 +25,23 @@ export type CreatedUpdated = { updated: number } -export interface CreatedUpdatedId - extends CreatedUpdated { - id: ID +export interface CreatedUpdatedId extends CreatedUpdated { + id: string } // eslint-disable-next-line @typescript-eslint/consistent-type-definitions -export type ObjectWithId = { - id: ID +export type ObjectWithId = { + id: string } -export interface AnyObjectWithId - extends AnyObject, - ObjectWithId {} +export interface AnyObjectWithId extends AnyObject, ObjectWithId {} /** * Base interface for any Entity that was saved to DB. */ // eslint-disable-next-line @typescript-eslint/consistent-type-definitions -export type SavedDBEntity = { - id: ID +export type SavedDBEntity = { + id: string /** * unixTimestamp of when the entity was first created (in the DB). @@ -64,8 +61,8 @@ export type SavedDBEntity = { * When it's known to be saved - `SavedDBEntity` interface can be used instead. */ // eslint-disable-next-line @typescript-eslint/consistent-type-definitions -export type BaseDBEntity = { - id?: ID +export type BaseDBEntity = { + id?: string /** * unixTimestamp of when the entity was first created (in the DB). @@ -79,11 +76,11 @@ export type BaseDBEntity = { } export type Saved> = T extends AnyObject - ? Omit & SavedDBEntity> + ? Omit & SavedDBEntity : T export type Unsaved> = T extends AnyObject - ? Omit & BaseDBEntity> + ? Omit & BaseDBEntity : T export type UnsavedId> = Omit & {