From b29208e156db93dd6318784fa215f9be591c7fe2 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 8 Feb 2024 13:34:28 -0500 Subject: [PATCH] refactor: type `false` instead of type `never` --- packages/db/src/core/types.ts | 38 ++++++++++++++++------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/packages/db/src/core/types.ts b/packages/db/src/core/types.ts index 39cb446a61..7de3422346 100644 --- a/packages/db/src/core/types.ts +++ b/packages/db/src/core/types.ts @@ -25,29 +25,25 @@ const booleanFieldSchema = baseFieldSchema.extend({ const numberFieldBaseSchema = baseFieldSchema.omit({ optional: true }).and( z.union([ z.object({ - primaryKey: z.literal(false).optional().default(false), + primaryKey: z.literal(false).optional(), optional: z.boolean().optional(), default: z.union([z.number(), z.instanceof(SQL)]).optional(), }), - z - .object({ - // `integer primary key` uses ROWID as the default value. - // `optional` and `default` do not have an effect, - // so omit these config options for primary keys. - primaryKey: z.literal(true), - }) - .transform((val) => ({ ...val, optional: false, default: undefined })), + z.object({ + // `integer primary key` uses ROWID as the default value. + // `optional` and `default` do not have an effect, + // so disable these config options for primary keys. + primaryKey: z.literal(true), + optional: z.literal(false).optional(), + default: z.literal(undefined).optional(), + }), ]) ); const numberFieldOptsSchema: z.ZodType< - z.output & { + z.infer & { // ReferenceableField creates a circular type. Define ZodType to resolve. - references?: () => NumberFieldInput; - }, - ZodTypeDef, - z.input & { - references?: () => NumberFieldInput; + references?: () => NumberField; } > = numberFieldBaseSchema.and( z.object({ @@ -58,8 +54,6 @@ const numberFieldOptsSchema: z.ZodType< }) ); -export type NumberFieldOpts = z.input; - const numberFieldSchema = numberFieldOptsSchema.and( z.object({ type: z.literal('number'), @@ -110,7 +104,7 @@ const fieldSchema = z.union([ dateFieldSchema, jsonFieldSchema, ]); -export const referenceableFieldSchema = z.union([textFieldBaseSchema, numberFieldBaseSchema]); +export const referenceableFieldSchema = z.union([textFieldBaseSchema, numberFieldSchema]); export type ReferenceableField = z.input; const fieldsSchema = z.record(fieldSchema); @@ -151,7 +145,6 @@ export const collectionSchema = z.union([readableCollectionSchema, writableColle export const collectionsSchema = z.record(collectionSchema); export type BooleanField = z.infer; -export type NumberFieldInput = z.input; export type NumberField = z.infer; export type TextField = z.infer; export type DateField = z.infer; @@ -167,7 +160,7 @@ export type FieldType = | JsonField['type']; export type DBField = z.infer; -export type DBFieldInput = DateFieldInput | BooleanField | NumberFieldInput | TextField | JsonField; +export type DBFieldInput = DateFieldInput | BooleanField | NumberField | TextField | JsonField; export type DBFields = z.infer; export type DBCollection = z.infer< typeof readableCollectionSchema | typeof writableCollectionSchema @@ -299,10 +292,13 @@ export function defineWritableCollection( export type AstroConfigWithDB = z.infer; type FieldOpts = Omit; +// We cannot use `Omit`, +// since Omit collapses our union type on primary key. +type NumberFieldOpts = z.input; export const field = { number: (opts: T = {} as T) => { - return { type: 'number', ...opts } satisfies NumberFieldInput; + return { type: 'number', ...opts } satisfies NumberField; }, boolean: >(opts: T = {} as T) => { return { type: 'boolean', ...opts } satisfies BooleanField;