diff --git a/packages/core/src/database/insert-into.test.ts b/packages/core/src/database/insert-into.test.ts index 71c2b4143..8e5528400 100644 --- a/packages/core/src/database/insert-into.test.ts +++ b/packages/core/src/database/insert-into.test.ts @@ -1,5 +1,5 @@ /* eslint-disable sql/no-unsafe-query */ -import { UserUpdate, Users } from '@logto/schemas'; +import { CreateUser, Users } from '@logto/schemas'; import decamelize from 'decamelize'; import RequestError from '@/errors/RequestError'; @@ -42,7 +42,7 @@ describe('buildInsertInto()', () => { }); it('resolves a promise with single entity when `returning` is true', async () => { - const user: UserUpdate = { id: 'foo', username: '123', primaryEmail: 'foo@bar.com' }; + const user: CreateUser = { id: 'foo', username: '123', primaryEmail: 'foo@bar.com' }; const pool = createTestPool( [...expectInsertIntoSql, 'returning *'].join('\n'), (_, [id, username, primaryEmail]) => ({ diff --git a/packages/core/src/database/update-where.test.ts b/packages/core/src/database/update-where.test.ts index 65d1cf9ba..1f9553e24 100644 --- a/packages/core/src/database/update-where.test.ts +++ b/packages/core/src/database/update-where.test.ts @@ -1,4 +1,4 @@ -import { UserUpdate, Users } from '@logto/schemas'; +import { CreateUser, Users } from '@logto/schemas'; import RequestError from '@/errors/RequestError'; import { createTestPool } from '@/utils/test-utils'; @@ -20,7 +20,7 @@ describe('buildUpdateWhere()', () => { }); it('resolves a promise with single entity when `returning` is true', async () => { - const user: UserUpdate = { id: 'foo', username: '123', primaryEmail: 'foo@bar.com' }; + const user: CreateUser = { id: 'foo', username: '123', primaryEmail: 'foo@bar.com' }; const pool = createTestPool( 'update "users"\nset "username"=$1, "primary_email"=$2\nwhere "id"=$3\nreturning *', (_, [username, primaryEmail, id]) => ({ diff --git a/packages/core/src/oidc/adapter.ts b/packages/core/src/oidc/adapter.ts index 96477ac41..434cec3e5 100644 --- a/packages/core/src/oidc/adapter.ts +++ b/packages/core/src/oidc/adapter.ts @@ -1,4 +1,4 @@ -import { ApplicationUpdate } from '@logto/schemas'; +import { CreateApplication } from '@logto/schemas'; import dayjs from 'dayjs'; import { AdapterFactory, AllClientMetadata } from 'oidc-provider'; import snakecaseKeys from 'snakecase-keys'; @@ -24,7 +24,7 @@ export default function postgresAdapter(modelName: string): ReturnType ({ + }: CreateApplication): AllClientMetadata => ({ client_id, client_name, application_type: getApplicationTypeString(type), diff --git a/packages/core/src/queries/application.ts b/packages/core/src/queries/application.ts index 864d4b3e9..6cf8596b7 100644 --- a/packages/core/src/queries/application.ts +++ b/packages/core/src/queries/application.ts @@ -1,4 +1,4 @@ -import { Application, ApplicationUpdate, Applications } from '@logto/schemas'; +import { Application, CreateApplication, Applications } from '@logto/schemas'; import { sql } from 'slonik'; import { buildFindMany } from '@/database/find-many'; @@ -12,7 +12,7 @@ const { table, fields } = convertToIdentifiers(Applications); export const findTotalNumberOfApplications = async () => getTotalRowCount(table); -const findApplicationMany = buildFindMany(pool, Applications); +const findApplicationMany = buildFindMany(pool, Applications); export const findAllApplications = async (limit: number, offset: number) => findApplicationMany({ limit, offset }); @@ -24,7 +24,7 @@ export const findApplicationById = async (id: string) => where ${fields.id}=${id} `); -export const insertApplication = buildInsertInto( +export const insertApplication = buildInsertInto( pool, Applications, { @@ -32,7 +32,7 @@ export const insertApplication = buildInsertInto } ); -const updateApplication = buildUpdateWhere( +const updateApplication = buildUpdateWhere( pool, Applications, true @@ -40,7 +40,7 @@ const updateApplication = buildUpdateWhere( export const updateApplicationById = async ( id: string, - set: Partial> + set: Partial> ) => updateApplication({ set, where: { id } }); export const deleteApplicationById = async (id: string) => { diff --git a/packages/core/src/queries/connector.ts b/packages/core/src/queries/connector.ts index ee995d29d..211554beb 100644 --- a/packages/core/src/queries/connector.ts +++ b/packages/core/src/queries/connector.ts @@ -1,4 +1,4 @@ -import { Connector, ConnectorUpdate, Connectors, ConnectorType } from '@logto/schemas'; +import { Connector, CreateConnector, Connectors, ConnectorType } from '@logto/schemas'; import { sql } from 'slonik'; import { buildInsertInto } from '@/database/insert-into'; @@ -15,8 +15,8 @@ export const findConnectorByIdAndType = async (id: string, type: ConnectorType) where ${fields.id}=${id} and ${fields.type}=${type} `); -export const insertConnector = buildInsertInto(pool, Connectors, { +export const insertConnector = buildInsertInto(pool, Connectors, { returning: true, }); -export const updateConnector = buildUpdateWhere(pool, Connectors); +export const updateConnector = buildUpdateWhere(pool, Connectors); diff --git a/packages/core/src/queries/oidc-model-instance.ts b/packages/core/src/queries/oidc-model-instance.ts index 75891982a..f663d92cb 100644 --- a/packages/core/src/queries/oidc-model-instance.ts +++ b/packages/core/src/queries/oidc-model-instance.ts @@ -1,6 +1,6 @@ import { OidcModelInstance, - OidcModelInstanceUpdate, + CreateOidcModelInstance, OidcModelInstancePayload, OidcModelInstances, } from '@logto/schemas'; @@ -24,7 +24,7 @@ const withConsumed = (data: T, consumedAt?: number | null): WithConsumed = const convertResult = (result: QueryResult | null) => conditional(result && withConsumed(result.payload, result.consumedAt)); -export const upsertInstance = buildInsertInto(pool, OidcModelInstances, { +export const upsertInstance = buildInsertInto(pool, OidcModelInstances, { onConflict: { fields: [fields.modelName, fields.id], setExcludedFields: [fields.payload, fields.expiresAt], diff --git a/packages/core/src/queries/resource.ts b/packages/core/src/queries/resource.ts index 893ba10e4..8a986f672 100644 --- a/packages/core/src/queries/resource.ts +++ b/packages/core/src/queries/resource.ts @@ -1,4 +1,4 @@ -import { Resource, ResourceUpdate, Resources } from '@logto/schemas'; +import { Resource, CreateResource, Resources } from '@logto/schemas'; import { sql } from 'slonik'; import { buildFindMany } from '@/database/find-many'; @@ -12,7 +12,7 @@ const { table, fields } = convertToIdentifiers(Resources); export const findTotalNumberOfResources = async () => getTotalRowCount(table); -const findResourcesMany = buildFindMany(pool, Resources); +const findResourcesMany = buildFindMany(pool, Resources); export const findAllResources = async (limit: number, offset: number) => findResourcesMany({ limit, offset }); @@ -31,15 +31,15 @@ export const findResourceById = async (id: string) => where ${fields.id}=${id} `); -export const insertResource = buildInsertInto(pool, Resources, { +export const insertResource = buildInsertInto(pool, Resources, { returning: true, }); -const updateResource = buildUpdateWhere(pool, Resources, true); +const updateResource = buildUpdateWhere(pool, Resources, true); export const updateResourceById = async ( id: string, - set: Partial> + set: Partial> ) => updateResource({ set, where: { id } }); export const deleteResourceById = async (id: string) => { diff --git a/packages/core/src/queries/scope.ts b/packages/core/src/queries/scope.ts index c567d6517..aac44808f 100644 --- a/packages/core/src/queries/scope.ts +++ b/packages/core/src/queries/scope.ts @@ -1,4 +1,4 @@ -import { ResourceScope, ResourceScopeUpdate, ResourceScopes } from '@logto/schemas'; +import { ResourceScope, CreateResourceScope, ResourceScopes } from '@logto/schemas'; import { sql } from 'slonik'; import { buildInsertInto } from '@/database/insert-into'; @@ -15,7 +15,7 @@ export const findAllScopesWithResourceId = async (resourceId: string) => where ${fields.resourceId}=${resourceId} `); -export const insertScope = buildInsertInto( +export const insertScope = buildInsertInto( pool, ResourceScopes, { diff --git a/packages/core/src/queries/setting.ts b/packages/core/src/queries/setting.ts index 62a3c80f6..512dfc1f6 100644 --- a/packages/core/src/queries/setting.ts +++ b/packages/core/src/queries/setting.ts @@ -1,4 +1,4 @@ -import { Setting, SettingUpdate, Settings } from '@logto/schemas'; +import { Setting, CreateSetting, Settings } from '@logto/schemas'; import { sql } from 'slonik'; import pool from '@/database/pool'; @@ -16,8 +16,8 @@ export const getSetting = async () => where ${fields.id}=${defaultSettingId} `); -export const updateSetting = async (setting: Partial>) => { - return buildUpdateWhere( +export const updateSetting = async (setting: Partial>) => { + return buildUpdateWhere( pool, Settings, true diff --git a/packages/core/src/queries/user-log.ts b/packages/core/src/queries/user-log.ts index e8ee2862d..e4b8b7e97 100644 --- a/packages/core/src/queries/user-log.ts +++ b/packages/core/src/queries/user-log.ts @@ -1,4 +1,4 @@ -import { UserLogUpdate, UserLogs } from '@logto/schemas'; +import { CreateUserLog, UserLogs } from '@logto/schemas'; import { sql } from 'slonik'; import { buildInsertInto } from '@/database/insert-into'; @@ -7,10 +7,10 @@ import { convertToIdentifiers } from '@/database/utils'; const { table, fields } = convertToIdentifiers(UserLogs); -export const insertUserLog = buildInsertInto(pool, UserLogs); +export const insertUserLog = buildInsertInto(pool, UserLogs); export const findLogsByUserId = async (userId: string) => - pool.many(sql` + pool.many(sql` select ${sql.join(Object.values(fields), sql`,`)} from ${table} where ${fields.userId}=${userId} diff --git a/packages/core/src/queries/user.ts b/packages/core/src/queries/user.ts index 428dc4d8e..4bfbf446d 100644 --- a/packages/core/src/queries/user.ts +++ b/packages/core/src/queries/user.ts @@ -1,4 +1,4 @@ -import { User, UserUpdate, Users } from '@logto/schemas'; +import { User, CreateUser, Users } from '@logto/schemas'; import { sql } from 'slonik'; import { buildInsertInto } from '@/database/insert-into'; @@ -37,7 +37,7 @@ export const hasUserWithId = async (id: string) => where ${fields.id}=${id} `); -export const insertUser = buildInsertInto(pool, Users, { returning: true }); +export const insertUser = buildInsertInto(pool, Users, { returning: true }); export const findAllUsers = async () => pool.many(sql` @@ -45,9 +45,9 @@ export const findAllUsers = async () => from ${table} `); -const updateUser = buildUpdateWhere(pool, Users, true); +const updateUser = buildUpdateWhere(pool, Users, true); -export const updateUserById = async (id: string, set: Partial>) => +export const updateUserById = async (id: string, set: Partial>) => updateUser({ set, where: { id } }); export const deleteUserById = async (id: string) => { diff --git a/packages/core/src/routes/application.ts b/packages/core/src/routes/application.ts index 615cb9b71..0f72649e6 100644 --- a/packages/core/src/routes/application.ts +++ b/packages/core/src/routes/application.ts @@ -37,10 +37,10 @@ export default function applicationRoutes(router: T) { router.post( '/applications', koaGuard({ - body: Applications.guard + body: Applications.createGuard .omit({ id: true, createdAt: true }) .partial() - .merge(Applications.guard.pick({ name: true, type: true })), + .merge(Applications.createGuard.pick({ name: true, type: true })), }), async (ctx, next) => { const { name, type, oidcClientMetadata, ...rest } = ctx.guard.body; @@ -75,7 +75,7 @@ export default function applicationRoutes(router: T) { '/applications/:id', koaGuard({ params: object({ id: string().min(1) }), - body: Applications.guard.omit({ id: true, createdAt: true }).partial(), + body: Applications.createGuard.omit({ id: true, createdAt: true }).partial(), }), async (ctx, next) => { const { diff --git a/packages/core/src/routes/resource.ts b/packages/core/src/routes/resource.ts index bea38428a..e6d9bf9b5 100644 --- a/packages/core/src/routes/resource.ts +++ b/packages/core/src/routes/resource.ts @@ -38,7 +38,7 @@ export default function resourceRoutes(router: T) { router.post( '/resources', koaGuard({ - body: Resources.guard.omit({ id: true }), + body: Resources.createGuard.omit({ id: true }), }), async (ctx, next) => { const resource = await insertResource({ @@ -74,7 +74,7 @@ export default function resourceRoutes(router: T) { '/resources/:id', koaGuard({ params: object({ id: string().min(1) }), - body: Resources.guard.omit({ id: true }).partial(), + body: Resources.createGuard.omit({ id: true }).partial(), }), async (ctx, next) => { const { diff --git a/packages/core/src/routes/setting.ts b/packages/core/src/routes/setting.ts index 7de9c488b..3560c2a54 100644 --- a/packages/core/src/routes/setting.ts +++ b/packages/core/src/routes/setting.ts @@ -16,7 +16,7 @@ export default function settingRoutes(router: T) { router.patch( '/settings', koaGuard({ - body: Settings.guard.omit({ id: true }).partial(), + body: Settings.createGuard.omit({ id: true }).partial(), }), async (ctx, next) => { const { body: setting } = ctx.guard; diff --git a/packages/schemas/src/db-entries/application.ts b/packages/schemas/src/db-entries/application.ts index 610e8bc94..0836d8b1d 100644 --- a/packages/schemas/src/db-entries/application.ts +++ b/packages/schemas/src/db-entries/application.ts @@ -12,7 +12,7 @@ import { } from '../foundations'; import { ApplicationType } from './custom-types'; -export type ApplicationUpdate = { +export type CreateApplication = { id: string; name: string; description?: string | null; @@ -32,17 +32,17 @@ export type Application = { createdAt: number; }; -const guard: Guard = z.object({ +const createGuard: Guard = z.object({ id: z.string(), name: z.string(), - description: z.string().optional(), + description: z.string().nullable().optional(), type: z.nativeEnum(ApplicationType), oidcClientMetadata: oidcClientMetadataGuard, customClientMetadata: customClientMetadataGuard.optional(), createdAt: z.number().optional(), }); -export const Applications: GeneratedSchema = Object.freeze({ +export const Applications: GeneratedSchema = Object.freeze({ table: 'applications', tableSingular: 'application', fields: { @@ -63,5 +63,5 @@ export const Applications: GeneratedSchema = Object.freeze({ 'customClientMetadata', 'createdAt', ], - guard, + createGuard, }); diff --git a/packages/schemas/src/db-entries/connector.ts b/packages/schemas/src/db-entries/connector.ts index 0f6f0f74f..cadf6e525 100644 --- a/packages/schemas/src/db-entries/connector.ts +++ b/packages/schemas/src/db-entries/connector.ts @@ -5,7 +5,7 @@ import { z } from 'zod'; import { ConnectorConfig, connectorConfigGuard, GeneratedSchema, Guard } from '../foundations'; import { ConnectorType } from './custom-types'; -export type ConnectorUpdate = { +export type CreateConnector = { id: string; enabled?: boolean; type: ConnectorType; @@ -21,7 +21,7 @@ export type Connector = { createdAt: number; }; -const guard: Guard = z.object({ +const createGuard: Guard = z.object({ id: z.string(), enabled: z.boolean().optional(), type: z.nativeEnum(ConnectorType), @@ -29,7 +29,7 @@ const guard: Guard = z.object({ createdAt: z.number().optional(), }); -export const Connectors: GeneratedSchema = Object.freeze({ +export const Connectors: GeneratedSchema = Object.freeze({ table: 'connectors', tableSingular: 'connector', fields: { @@ -40,5 +40,5 @@ export const Connectors: GeneratedSchema = Object.freeze({ createdAt: 'created_at', }, fieldKeys: ['id', 'enabled', 'type', 'config', 'createdAt'], - guard, + createGuard, }); diff --git a/packages/schemas/src/db-entries/oidc-model-instance.ts b/packages/schemas/src/db-entries/oidc-model-instance.ts index e021083e6..d37730a91 100644 --- a/packages/schemas/src/db-entries/oidc-model-instance.ts +++ b/packages/schemas/src/db-entries/oidc-model-instance.ts @@ -9,7 +9,7 @@ import { Guard, } from '../foundations'; -export type OidcModelInstanceUpdate = { +export type CreateOidcModelInstance = { modelName: string; id: string; payload: OidcModelInstancePayload; @@ -25,15 +25,15 @@ export type OidcModelInstance = { consumedAt: number | null; }; -const guard: Guard = z.object({ +const createGuard: Guard = z.object({ modelName: z.string(), id: z.string(), payload: oidcModelInstancePayloadGuard, expiresAt: z.number(), - consumedAt: z.number().optional(), + consumedAt: z.number().nullable().optional(), }); -export const OidcModelInstances: GeneratedSchema = Object.freeze({ +export const OidcModelInstances: GeneratedSchema = Object.freeze({ table: 'oidc_model_instances', tableSingular: 'oidc_model_instance', fields: { @@ -44,5 +44,5 @@ export const OidcModelInstances: GeneratedSchema = Obje consumedAt: 'consumed_at', }, fieldKeys: ['modelName', 'id', 'payload', 'expiresAt', 'consumedAt'], - guard, + createGuard, }); diff --git a/packages/schemas/src/db-entries/resource-scope.ts b/packages/schemas/src/db-entries/resource-scope.ts index 2e0a39b43..8ac901f4f 100644 --- a/packages/schemas/src/db-entries/resource-scope.ts +++ b/packages/schemas/src/db-entries/resource-scope.ts @@ -4,7 +4,7 @@ import { z } from 'zod'; import { GeneratedSchema, Guard } from '../foundations'; -export type ResourceScopeUpdate = { +export type CreateResourceScope = { id: string; name: string; description: string; @@ -18,14 +18,14 @@ export type ResourceScope = { resourceId: string; }; -const guard: Guard = z.object({ +const createGuard: Guard = z.object({ id: z.string(), name: z.string(), description: z.string(), resourceId: z.string(), }); -export const ResourceScopes: GeneratedSchema = Object.freeze({ +export const ResourceScopes: GeneratedSchema = Object.freeze({ table: 'resource_scopes', tableSingular: 'resource_scope', fields: { @@ -35,5 +35,5 @@ export const ResourceScopes: GeneratedSchema = Object.freez resourceId: 'resource_id', }, fieldKeys: ['id', 'name', 'description', 'resourceId'], - guard, + createGuard, }); diff --git a/packages/schemas/src/db-entries/resource.ts b/packages/schemas/src/db-entries/resource.ts index efe473f1d..bdc67a893 100644 --- a/packages/schemas/src/db-entries/resource.ts +++ b/packages/schemas/src/db-entries/resource.ts @@ -4,7 +4,7 @@ import { z } from 'zod'; import { GeneratedSchema, Guard } from '../foundations'; -export type ResourceUpdate = { +export type CreateResource = { id: string; name: string; identifier: string; @@ -18,14 +18,14 @@ export type Resource = { accessTokenTtl: number; }; -const guard: Guard = z.object({ +const createGuard: Guard = z.object({ id: z.string(), name: z.string(), identifier: z.string(), accessTokenTtl: z.number().optional(), }); -export const Resources: GeneratedSchema = Object.freeze({ +export const Resources: GeneratedSchema = Object.freeze({ table: 'resources', tableSingular: 'resource', fields: { @@ -35,5 +35,5 @@ export const Resources: GeneratedSchema = Object.freeze({ accessTokenTtl: 'access_token_ttl', }, fieldKeys: ['id', 'name', 'identifier', 'accessTokenTtl'], - guard, + createGuard, }); diff --git a/packages/schemas/src/db-entries/setting.ts b/packages/schemas/src/db-entries/setting.ts index cf9c98a66..b1b4081eb 100644 --- a/packages/schemas/src/db-entries/setting.ts +++ b/packages/schemas/src/db-entries/setting.ts @@ -9,7 +9,7 @@ import { Guard, } from '../foundations'; -export type SettingUpdate = { +export type CreateSetting = { id: string; customDomain?: string | null; adminConsole: AdminConsoleConfig; @@ -21,13 +21,13 @@ export type Setting = { adminConsole: AdminConsoleConfig; }; -const guard: Guard = z.object({ +const createGuard: Guard = z.object({ id: z.string(), - customDomain: z.string().optional(), + customDomain: z.string().nullable().optional(), adminConsole: adminConsoleConfigGuard, }); -export const Settings: GeneratedSchema = Object.freeze({ +export const Settings: GeneratedSchema = Object.freeze({ table: 'settings', tableSingular: 'setting', fields: { @@ -36,5 +36,5 @@ export const Settings: GeneratedSchema = Object.freeze({ adminConsole: 'admin_console', }, fieldKeys: ['id', 'customDomain', 'adminConsole'], - guard, + createGuard, }); diff --git a/packages/schemas/src/db-entries/user-log.ts b/packages/schemas/src/db-entries/user-log.ts index cc5a50ff3..f9284f2f8 100644 --- a/packages/schemas/src/db-entries/user-log.ts +++ b/packages/schemas/src/db-entries/user-log.ts @@ -5,7 +5,7 @@ import { z } from 'zod'; import { UserLogPayload, userLogPayloadGuard, GeneratedSchema, Guard } from '../foundations'; import { UserLogType, UserLogResult } from './custom-types'; -export type UserLogUpdate = { +export type CreateUserLog = { id: string; userId: string; type: UserLogType; @@ -23,7 +23,7 @@ export type UserLog = { createdAt: number; }; -const guard: Guard = z.object({ +const createGuard: Guard = z.object({ id: z.string(), userId: z.string(), type: z.nativeEnum(UserLogType), @@ -32,7 +32,7 @@ const guard: Guard = z.object({ createdAt: z.number().optional(), }); -export const UserLogs: GeneratedSchema = Object.freeze({ +export const UserLogs: GeneratedSchema = Object.freeze({ table: 'user_logs', tableSingular: 'user_log', fields: { @@ -44,5 +44,5 @@ export const UserLogs: GeneratedSchema = Object.freeze({ createdAt: 'created_at', }, fieldKeys: ['id', 'userId', 'type', 'result', 'payload', 'createdAt'], - guard, + createGuard, }); diff --git a/packages/schemas/src/db-entries/user.ts b/packages/schemas/src/db-entries/user.ts index 0a75340b4..75df70a9d 100644 --- a/packages/schemas/src/db-entries/user.ts +++ b/packages/schemas/src/db-entries/user.ts @@ -5,7 +5,7 @@ import { z } from 'zod'; import { GeneratedSchema, Guard } from '../foundations'; import { PasswordEncryptionMethod } from './custom-types'; -export type UserUpdate = { +export type CreateUser = { id: string; username?: string | null; primaryEmail?: string | null; @@ -25,17 +25,17 @@ export type User = { passwordEncryptionSalt: string | null; }; -const guard: Guard = z.object({ +const createGuard: Guard = z.object({ id: z.string(), - username: z.string().optional(), - primaryEmail: z.string().optional(), - primaryPhone: z.string().optional(), - passwordEncrypted: z.string().optional(), - passwordEncryptionMethod: z.nativeEnum(PasswordEncryptionMethod).optional(), - passwordEncryptionSalt: z.string().optional(), + username: z.string().nullable().optional(), + primaryEmail: z.string().nullable().optional(), + primaryPhone: z.string().nullable().optional(), + passwordEncrypted: z.string().nullable().optional(), + passwordEncryptionMethod: z.nativeEnum(PasswordEncryptionMethod).nullable().optional(), + passwordEncryptionSalt: z.string().nullable().optional(), }); -export const Users: GeneratedSchema = Object.freeze({ +export const Users: GeneratedSchema = Object.freeze({ table: 'users', tableSingular: 'user', fields: { @@ -56,5 +56,5 @@ export const Users: GeneratedSchema = Object.freeze({ 'passwordEncryptionMethod', 'passwordEncryptionSalt', ], - guard, + createGuard, }); diff --git a/packages/schemas/src/foundations/schemas.ts b/packages/schemas/src/foundations/schemas.ts index 0316a1da7..2624208fc 100644 --- a/packages/schemas/src/foundations/schemas.ts +++ b/packages/schemas/src/foundations/schemas.ts @@ -20,6 +20,6 @@ export type GeneratedSchema = keyof Schema extends st [key in keyof Schema]: string; }; fieldKeys: ReadonlyArray; - guard: Guard; + createGuard: Guard; }> : never; diff --git a/packages/schemas/src/gen/schema.ts b/packages/schemas/src/gen/schema.ts index 494b8c984..88458e941 100644 --- a/packages/schemas/src/gen/schema.ts +++ b/packages/schemas/src/gen/schema.ts @@ -6,7 +6,7 @@ import { TableWithType } from './types'; export const generateSchema = ({ name, fields }: TableWithType) => { const modelName = pluralize(camelcase(name, { pascalCase: true }), 1); - const databaseEntryType = `${modelName}Update`; + const databaseEntryType = `Create${modelName}`; return [ `export type ${databaseEntryType} = {`, ...fields.map( @@ -28,19 +28,19 @@ export const generateSchema = ({ name, fields }: TableWithType) => { ), '};', '', - `const guard: Guard<${databaseEntryType}> = z.object({`, + `const createGuard: Guard<${databaseEntryType}> = z.object({`, ...fields.map(({ name, type, isArray, isEnum, nullable, hasDefaultValue, tsType }) => { if (tsType) { return ` ${camelcase(name)}: ${camelcase(tsType)}Guard${conditionalString( - (nullable || hasDefaultValue) && '.optional()' - )},`; + nullable && !hasDefaultValue && '.nullable()' + )}${conditionalString((nullable || hasDefaultValue) && '.optional()')},`; } return ` ${camelcase(name)}: z.${ isEnum ? `nativeEnum(${type})` : `${type}()` }${conditionalString(isArray && '.array()')}${conditionalString( - (nullable || hasDefaultValue) && '.optional()' - )},`; + nullable && !hasDefaultValue && '.nullable()' + )}${conditionalString((nullable || hasDefaultValue) && '.optional()')},`; }), ' });', '', @@ -55,7 +55,7 @@ export const generateSchema = ({ name, fields }: TableWithType) => { ' fieldKeys: [', ...fields.map(({ name }) => ` '${camelcase(name)}',`), ' ],', - ' guard,', + ' createGuard,', '});', ].join('\n'); }; diff --git a/packages/schemas/src/types/user.ts b/packages/schemas/src/types/user.ts index a7acd4e65..81c658010 100644 --- a/packages/schemas/src/types/user.ts +++ b/packages/schemas/src/types/user.ts @@ -1,4 +1,4 @@ -import { UserUpdate } from '../db-entries'; +import { CreateUser } from '../db-entries'; export const userInfoSelectFields = Object.freeze([ 'id', @@ -7,7 +7,7 @@ export const userInfoSelectFields = Object.freeze([ 'primaryPhone', ] as const); -export type UserInfo = Pick< - UserUpdate, +export type UserInfo = Pick< + CreateUser, Keys >;