0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-03-17 22:31:28 -05:00

refactor: update SchemaValuePrimitive and add docs for convertToPrimitive()

This commit is contained in:
Gao Sun 2021-08-18 00:45:46 +08:00
parent b192b7d17c
commit acc2031e82
No known key found for this signature in database
GPG key ID: 0F0EFA2E36639F31
2 changed files with 12 additions and 3 deletions

View file

@ -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') {

View file

@ -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;