Skip to content

Commit

Permalink
feat: BaseDBEntity.id only supports string now
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kirillgroshkov committed Dec 28, 2023
1 parent 9f3249b commit 2d57433
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
12 changes: 4 additions & 8 deletions src/json-schema/jsonSchemaBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,22 +368,18 @@ export class JsonSchemaObjectBuilder<T extends AnyObject> extends JsonSchemaAnyB
return this
}

baseDBEntity<ID extends string | number = string>(
idType = 'string',
): JsonSchemaObjectBuilder<T & BaseDBEntity<ID>> {
baseDBEntity(): JsonSchemaObjectBuilder<T & BaseDBEntity> {
Object.assign(this.schema.properties, {
id: { type: idType },
id: { type: 'string' },
created: { type: 'number', format: 'unixTimestamp2000' },
updated: { type: 'number', format: 'unixTimestamp2000' },
})

return this
}

savedDBEntity<ID extends string | number = string>(
idType = 'string',
): JsonSchemaObjectBuilder<T & SavedDBEntity<ID>> {
return this.baseDBEntity(idType).addRequired(['id', 'created', 'updated']) as any
savedDBEntity(): JsonSchemaObjectBuilder<T & SavedDBEntity> {
return this.baseDBEntity().addRequired(['id', 'created', 'updated']) as any
}

extend<T2 extends AnyObject>(s2: JsonSchemaObjectBuilder<T2>): JsonSchemaObjectBuilder<T & T2> {
Expand Down
14 changes: 7 additions & 7 deletions src/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
} from './types'
import type { AppError } from '.'

interface Item extends BaseDBEntity<number> {
interface Item extends BaseDBEntity {
a?: number
}

Expand All @@ -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,
Expand All @@ -67,7 +67,7 @@ test('saved/unsaved', () => {
delete itemDBM.a

expectTypeOf(itemDBM).toEqualTypeOf<{
id: number
id: string
created: number
updated: number
a?: number
Expand All @@ -81,7 +81,7 @@ test('saved/unsaved', () => {

expectTypeOf(unsavedItem).toEqualTypeOf<{
a?: number
id?: number
id?: string
created?: number
updated?: number
}>()
Expand All @@ -97,7 +97,7 @@ test('saved/unsaved', () => {

expectTypeOf(unsavedItemDBM).toEqualTypeOf<{
a?: number
id?: number
id?: string
created?: number
updated?: number
}>()
Expand All @@ -106,7 +106,7 @@ test('saved/unsaved', () => {
delete unsavedItemId.id

expectTypeOf(unsavedItemId).toEqualTypeOf<{
id?: number
id?: string
created: number
updated: number
a?: number
Expand Down
25 changes: 11 additions & 14 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,23 @@ export type CreatedUpdated = {
updated: number
}

export interface CreatedUpdatedId<ID extends string | number = string | number>
extends CreatedUpdated {
id: ID
export interface CreatedUpdatedId extends CreatedUpdated {
id: string
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type ObjectWithId<ID extends string | number = string | number> = {
id: ID
export type ObjectWithId = {
id: string
}

export interface AnyObjectWithId<ID extends string | number = string | number>
extends AnyObject,
ObjectWithId<ID> {}
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 extends string | number = string> = {
id: ID
export type SavedDBEntity = {
id: string

/**
* unixTimestamp of when the entity was first created (in the DB).
Expand All @@ -64,8 +61,8 @@ export type SavedDBEntity<ID extends string | number = string> = {
* 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 extends string | number = string> = {
id?: ID
export type BaseDBEntity = {
id?: string

/**
* unixTimestamp of when the entity was first created (in the DB).
Expand All @@ -79,11 +76,11 @@ export type BaseDBEntity<ID extends string | number = string> = {
}

export type Saved<T extends Partial<ObjectWithId>> = T extends AnyObject
? Omit<T, 'id' | 'created' | 'updated'> & SavedDBEntity<NonNullable<T['id']>>
? Omit<T, 'id' | 'created' | 'updated'> & SavedDBEntity
: T

export type Unsaved<T extends Partial<ObjectWithId>> = T extends AnyObject
? Omit<T, 'id' | 'created' | 'updated'> & BaseDBEntity<NonNullable<T['id']>>
? Omit<T, 'id' | 'created' | 'updated'> & BaseDBEntity
: T

export type UnsavedId<T extends Partial<ObjectWithId>> = Omit<T, 'id'> & {
Expand Down

0 comments on commit 2d57433

Please sign in to comment.