From fcb1facece785c0c6ff121ddba6871aeb286eba6 Mon Sep 17 00:00:00 2001 From: kirillgroshkov Date: Sun, 21 Jan 2024 13:26:47 +0100 Subject: [PATCH] feat: BaseDBEntity now has all fields required (id/created/updated) Same as what SavedDBEntity was before. --- .../jsonSchemaBuilder.test.ts.snap | 6 +++++ src/json-schema/jsonSchemaBuilder.test.ts | 6 ++--- src/json-schema/jsonSchemaBuilder.ts | 7 +---- src/json-schema/jsonSchemas.ts | 14 +++++----- src/types.test.ts | 7 +++-- src/types.ts | 27 +++---------------- src/zod/zod.shared.schemas.ts | 8 +++--- src/zod/zod.test.ts | 22 +++++++-------- 8 files changed, 38 insertions(+), 59 deletions(-) diff --git a/src/json-schema/__snapshots__/jsonSchemaBuilder.test.ts.snap b/src/json-schema/__snapshots__/jsonSchemaBuilder.test.ts.snap index d6144c55..6806190b 100644 --- a/src/json-schema/__snapshots__/jsonSchemaBuilder.test.ts.snap +++ b/src/json-schema/__snapshots__/jsonSchemaBuilder.test.ts.snap @@ -44,6 +44,9 @@ exports[`addressBMJsonSchema 1`] = ` "address1", "city", "countryCode", + "created", + "id", + "updated", "zip", ], "type": "object", @@ -94,6 +97,9 @@ exports[`addressDBMJsonSchema 1`] = ` "address1", "city", "countryCode", + "created", + "id", + "updated", "zip", ], "type": "object", diff --git a/src/json-schema/jsonSchemaBuilder.test.ts b/src/json-schema/jsonSchemaBuilder.test.ts index 12488380..9568c338 100644 --- a/src/json-schema/jsonSchemaBuilder.test.ts +++ b/src/json-schema/jsonSchemaBuilder.test.ts @@ -37,9 +37,9 @@ const addressDBMJsonSchema2 = baseDBEntityJsonSchema.extend(addressJsonSchema) // alternative 2 const addressBMJsonSchema3 = addressJsonSchema.extend( jsonSchema.object({ - id: jsonSchema.string().optional(), - created: jsonSchema.unixTimestamp2000().optional(), - updated: jsonSchema.unixTimestamp2000().optional(), + id: jsonSchema.string(), + created: jsonSchema.unixTimestamp2000(), + updated: jsonSchema.unixTimestamp2000(), }), ) diff --git a/src/json-schema/jsonSchemaBuilder.ts b/src/json-schema/jsonSchemaBuilder.ts index 9fba3390..ce8fe0b0 100644 --- a/src/json-schema/jsonSchemaBuilder.ts +++ b/src/json-schema/jsonSchemaBuilder.ts @@ -5,7 +5,6 @@ import type { JsonSchemaArray, JsonSchemaOneOf, JsonSchemaTuple, - SavedDBEntity, AnyObject, } from '../index' import { mergeJsonSchemaObjects, _deepCopy, _sortObject } from '../index' @@ -375,11 +374,7 @@ export class JsonSchemaObjectBuilder extends JsonSchemaAnyB updated: { type: 'number', format: 'unixTimestamp2000' }, }) - return this - } - - savedDBEntity(): JsonSchemaObjectBuilder { - return this.baseDBEntity().addRequired(['id', 'created', 'updated']) as any + return this.addRequired(['id', 'created', 'updated']) as any } extend(s2: JsonSchemaObjectBuilder): JsonSchemaObjectBuilder { diff --git a/src/json-schema/jsonSchemas.ts b/src/json-schema/jsonSchemas.ts index 4b66c304..470c76b1 100644 --- a/src/json-schema/jsonSchemas.ts +++ b/src/json-schema/jsonSchemas.ts @@ -1,14 +1,14 @@ -import type { BaseDBEntity, SavedDBEntity } from '../types' +import type { BaseDBEntity } from '../types' import { jsonSchema } from './jsonSchemaBuilder' export const baseDBEntityJsonSchema = jsonSchema.object({ - id: jsonSchema.string().optional(), - created: jsonSchema.unixTimestamp2000().optional(), - updated: jsonSchema.unixTimestamp2000().optional(), -}) - -export const savedDBEntityJsonSchema = jsonSchema.object({ id: jsonSchema.string(), created: jsonSchema.unixTimestamp2000(), updated: jsonSchema.unixTimestamp2000(), }) + +// export const savedDBEntityJsonSchema = jsonSchema.object({ +// id: jsonSchema.string(), +// created: jsonSchema.unixTimestamp2000(), +// updated: jsonSchema.unixTimestamp2000(), +// }) diff --git a/src/types.test.ts b/src/types.test.ts index 87d3db14..e29170cc 100644 --- a/src/types.test.ts +++ b/src/types.test.ts @@ -4,7 +4,6 @@ import type { Reviver, StringMap, BaseDBEntity, - Saved, Unsaved, UnsavedId, AnyObject, @@ -29,7 +28,7 @@ interface Item extends BaseDBEntity { a?: number } -interface ItemDBM extends Saved {} +interface ItemDBM extends Item {} const _ym: MonthId = '2021-01' @@ -44,7 +43,7 @@ test('saved/unsaved', () => { a: number }>() - const item: Item = {} + const item = {} as Unsaved delete item.a delete item.id delete item.created @@ -177,7 +176,7 @@ test('_typeCast', () => { }) test('_objectAssign', () => { - const item: Item = {} + const item = {} as Item // No TypeScript error here Object.assign(item, { diff --git a/src/types.ts b/src/types.ts index 04585885..fc7cd239 100644 --- a/src/types.ts +++ b/src/types.ts @@ -47,7 +47,7 @@ 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 = { +export type BaseDBEntity = { id: string /** @@ -61,31 +61,10 @@ export type SavedDBEntity = { updated: UnixTimestampNumber } -/** - * Base interface for any Entity that can be saved to DB. - * This interface fits when entity was NOT YET saved to DB, - * hence `id`, `created` and `updated` fields CAN BE undefined (yet). - * 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?: string - - /** - * unixTimestamp of when the entity was first created (in the DB). - */ - created?: UnixTimestampNumber - - /** - * unixTimestamp of when the entity was last updated (in the DB). - */ - updated?: UnixTimestampNumber -} - -export type Saved = T & SavedDBEntity +export type Saved = T & BaseDBEntity export type Unsaved = T extends AnyObject - ? Omit & BaseDBEntity + ? Omit & Partial : T export type UnsavedId = Omit & { diff --git a/src/zod/zod.shared.schemas.ts b/src/zod/zod.shared.schemas.ts index a514569f..0e01f72f 100644 --- a/src/zod/zod.shared.schemas.ts +++ b/src/zod/zod.shared.schemas.ts @@ -93,10 +93,10 @@ export const zSlug = z export const zBaseDBEntity = z .object({ - id: z.string().optional(), - created: zUnixTimestamp2000.optional(), - updated: zUnixTimestamp2000.optional(), + id: z.string(), + created: zUnixTimestamp2000, + updated: zUnixTimestamp2000, }) .describe('BaseDBEntity') -export const zSavedDBEntity = zBaseDBEntity.required().describe('SavedDBEntity') +// export const zSavedDBEntity = zBaseDBEntity.required().describe('SavedDBEntity') diff --git a/src/zod/zod.test.ts b/src/zod/zod.test.ts index 8dcd2509..075f8f88 100644 --- a/src/zod/zod.test.ts +++ b/src/zod/zod.test.ts @@ -1,26 +1,26 @@ import { _expectedError } from '../error/try' -import { zBaseDBEntity, zEmail, zIsoDateString, zSavedDBEntity } from './zod.shared.schemas' +import { zBaseDBEntity, zEmail, zIsoDateString } from './zod.shared.schemas' import { ZodValidationError, zSafeValidate, zValidate } from './zod.util' test('basic', () => { - const err = _expectedError(() => zValidate({} as any, zSavedDBEntity), ZodValidationError) + const err = _expectedError(() => zValidate({} as any, zBaseDBEntity), ZodValidationError) expect(err).toBeInstanceOf(Error) expect(err).toBeInstanceOf(ZodValidationError) expect(err.name).toBe('ZodError') expect(err.annotate()).toBe(err.message) expect(err.toString()).toBe(err.message) - expect(err.stack!.split('\n')[0]).toMatchInlineSnapshot(`"ZodError: Invalid SavedDBEntity"`) + expect(err.stack!.split('\n')[0]).toMatchInlineSnapshot(`"ZodError: Invalid BaseDBEntity"`) expect(err.message).toMatchInlineSnapshot(` - "Invalid SavedDBEntity +"Invalid BaseDBEntity - Input: - {} +Input: +{} - 3 issues: - id: Required - created: Required - updated: Required" - `) +3 issues: +id: Required +created: Required +updated: Required" +`) expect(zSafeValidate(' a' as any, zBaseDBEntity).error!.message).toMatchInlineSnapshot(` "Invalid BaseDBEntity