From acc2031e8200e03c49aa2c4a6392c305dff26825 Mon Sep 17 00:00:00 2001 From: Gao Sun <gao@silverhand.io> Date: Wed, 18 Aug 2021 00:45:46 +0800 Subject: [PATCH] refactor: update `SchemaValuePrimitive` and add docs for `convertToPrimitive()` --- packages/core/src/database/utils.ts | 13 +++++++++++-- packages/schemas/src/foundations.ts | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/core/src/database/utils.ts b/packages/core/src/database/utils.ts index 720a44d41..6dde30ea8 100644 --- a/packages/core/src/database/utils.ts +++ b/packages/core/src/database/utils.ts @@ -21,9 +21,18 @@ export const excludeAutoSetFields = <T extends string>(fields: readonly T[]) => ) ); -export const convertToPrimitive = (value: SchemaValue): SchemaValuePrimitive => { +/** + * Note `undefined` is removed from the acceptable list, + * since you should NOT call this function if ignoring the field is the desired behavior. + * Calling this function with `null` means an explicit `null` setting in database is expected. + * @param value The value to convert. + * @returns A primitive that can be saved into database. + */ +export const convertToPrimitive = ( + value: NonNullable<SchemaValue> | null +): NonNullable<SchemaValuePrimitive> | null => { if (value === null) { - return value; + return null; } if (typeof value === 'object') { diff --git a/packages/schemas/src/foundations.ts b/packages/schemas/src/foundations.ts index 6451ef660..526cfb99e 100644 --- a/packages/schemas/src/foundations.ts +++ b/packages/schemas/src/foundations.ts @@ -1,4 +1,4 @@ -export type SchemaValuePrimitive = string | number | boolean | null; +export type SchemaValuePrimitive = string | number | boolean | undefined; export type SchemaValue = SchemaValuePrimitive | Record<string, unknown>; export type SchemaLike<Key extends string> = { [key in Key]: SchemaValue;