From 6e9cabe50bcf4d47978e2a31b5914ad415bba205 Mon Sep 17 00:00:00 2001 From: Darcy Ye Date: Wed, 12 Jan 2022 16:22:33 +0800 Subject: [PATCH] chore: rename all `\w+DBEntry` as `\w+Update` to eliminate understanding (#173) --- packages/core/src/database/insert-into.test.ts | 4 ++-- packages/core/src/database/update-where.test.ts | 4 ++-- packages/core/src/oidc/adapter.ts | 4 ++-- packages/core/src/queries/application.ts | 10 +++++----- packages/core/src/queries/connector.ts | 4 ++-- packages/core/src/queries/oidc-model-instance.ts | 4 ++-- packages/core/src/queries/resources.ts | 8 ++++---- packages/core/src/queries/scopes.ts | 4 ++-- packages/core/src/queries/user-log.ts | 6 +++--- packages/core/src/queries/user.ts | 8 ++++---- packages/schemas/src/db-entries/application.ts | 6 +++--- packages/schemas/src/db-entries/connector.ts | 6 +++--- packages/schemas/src/db-entries/oidc-model-instance.ts | 6 +++--- packages/schemas/src/db-entries/resource-scope.ts | 6 +++--- packages/schemas/src/db-entries/resource.ts | 6 +++--- packages/schemas/src/db-entries/setting.ts | 6 +++--- packages/schemas/src/db-entries/user-log.ts | 6 +++--- packages/schemas/src/db-entries/user.ts | 6 +++--- packages/schemas/src/gen/schema.ts | 2 +- packages/schemas/src/types/user.ts | 6 +++--- 20 files changed, 56 insertions(+), 56 deletions(-) diff --git a/packages/core/src/database/insert-into.test.ts b/packages/core/src/database/insert-into.test.ts index 04b45159b..71c2b4143 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 { UserDBEntry, Users } from '@logto/schemas'; +import { UserUpdate, 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: UserDBEntry = { id: 'foo', username: '123', primaryEmail: 'foo@bar.com' }; + const user: UserUpdate = { 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 c0d928541..65d1cf9ba 100644 --- a/packages/core/src/database/update-where.test.ts +++ b/packages/core/src/database/update-where.test.ts @@ -1,4 +1,4 @@ -import { UserDBEntry, Users } from '@logto/schemas'; +import { UserUpdate, 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: UserDBEntry = { id: 'foo', username: '123', primaryEmail: 'foo@bar.com' }; + const user: UserUpdate = { 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 e71627bb5..091ecba4d 100644 --- a/packages/core/src/oidc/adapter.ts +++ b/packages/core/src/oidc/adapter.ts @@ -1,4 +1,4 @@ -import { ApplicationDBEntry } from '@logto/schemas'; +import { ApplicationUpdate } from '@logto/schemas'; import dayjs from 'dayjs'; import { AdapterFactory, AllClientMetadata } from 'oidc-provider'; import snakecaseKeys from 'snakecase-keys'; @@ -23,7 +23,7 @@ export default function postgresAdapter(modelName: string): ReturnType ({ + }: ApplicationUpdate): 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 f8de23a16..3b1749e1d 100644 --- a/packages/core/src/queries/application.ts +++ b/packages/core/src/queries/application.ts @@ -1,4 +1,4 @@ -import { Application, ApplicationDBEntry, Applications } from '@logto/schemas'; +import { Application, ApplicationUpdate, 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( 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 e7d9b68b8..4b6a3350a 100644 --- a/packages/core/src/queries/connector.ts +++ b/packages/core/src/queries/connector.ts @@ -1,4 +1,4 @@ -import { Connector, ConnectorDBEntry, Connectors, ConnectorType } from '@logto/schemas'; +import { Connector, ConnectorUpdate, Connectors, ConnectorType } from '@logto/schemas'; import { sql } from 'slonik'; import { buildInsertInto } from '@/database/insert-into'; @@ -14,6 +14,6 @@ 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, }); diff --git a/packages/core/src/queries/oidc-model-instance.ts b/packages/core/src/queries/oidc-model-instance.ts index 07d34cdb9..75891982a 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, - OidcModelInstanceDBEntry, + OidcModelInstanceUpdate, 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/resources.ts b/packages/core/src/queries/resources.ts index 7a02d1477..3822c204e 100644 --- a/packages/core/src/queries/resources.ts +++ b/packages/core/src/queries/resources.ts @@ -1,4 +1,4 @@ -import { Resource, ResourceDBEntry, Resources } from '@logto/schemas'; +import { Resource, ResourceUpdate, Resources } from '@logto/schemas'; import { sql } from 'slonik'; import { buildInsertInto } from '@/database/insert-into'; @@ -43,15 +43,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/scopes.ts b/packages/core/src/queries/scopes.ts index 3e219cbf3..2de5b843b 100644 --- a/packages/core/src/queries/scopes.ts +++ b/packages/core/src/queries/scopes.ts @@ -1,4 +1,4 @@ -import { ResourceScope, ResourceScopeDBEntry, ResourceScopes } from '@logto/schemas'; +import { ResourceScope, ResourceScopeUpdate, 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/user-log.ts b/packages/core/src/queries/user-log.ts index 80f99c426..e8ee2862d 100644 --- a/packages/core/src/queries/user-log.ts +++ b/packages/core/src/queries/user-log.ts @@ -1,4 +1,4 @@ -import { UserLogDBEntry, UserLogs } from '@logto/schemas'; +import { UserLogUpdate, 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 4cbf8a26a..72d81e0bc 100644 --- a/packages/core/src/queries/user.ts +++ b/packages/core/src/queries/user.ts @@ -1,4 +1,4 @@ -import { User, UserDBEntry, Users } from '@logto/schemas'; +import { User, UserUpdate, 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/schemas/src/db-entries/application.ts b/packages/schemas/src/db-entries/application.ts index 81021267b..1cc38d97a 100644 --- a/packages/schemas/src/db-entries/application.ts +++ b/packages/schemas/src/db-entries/application.ts @@ -10,7 +10,7 @@ import { } from '../foundations'; import { ApplicationType } from './custom-types'; -export type ApplicationDBEntry = { +export type ApplicationUpdate = { id: string; name: string; description?: string | null; @@ -32,7 +32,7 @@ export type Application = { createdAt: number; }; -const guard: Guard = z.object({ +const guard: Guard = z.object({ id: z.string(), name: z.string(), description: z.string().optional(), @@ -43,7 +43,7 @@ const guard: Guard = z.object({ createdAt: z.number().optional(), }); -export const Applications: GeneratedSchema = Object.freeze({ +export const Applications: GeneratedSchema = Object.freeze({ table: 'applications', tableSingular: 'application', fields: { diff --git a/packages/schemas/src/db-entries/connector.ts b/packages/schemas/src/db-entries/connector.ts index fdf349241..0f6f0f74f 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 ConnectorDBEntry = { +export type ConnectorUpdate = { id: string; enabled?: boolean; type: ConnectorType; @@ -21,7 +21,7 @@ export type Connector = { createdAt: number; }; -const guard: Guard = z.object({ +const guard: 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: { diff --git a/packages/schemas/src/db-entries/oidc-model-instance.ts b/packages/schemas/src/db-entries/oidc-model-instance.ts index 9ba3210e8..e021083e6 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 OidcModelInstanceDBEntry = { +export type OidcModelInstanceUpdate = { modelName: string; id: string; payload: OidcModelInstancePayload; @@ -25,7 +25,7 @@ export type OidcModelInstance = { consumedAt: number | null; }; -const guard: Guard = z.object({ +const guard: Guard = z.object({ modelName: z.string(), id: z.string(), payload: oidcModelInstancePayloadGuard, @@ -33,7 +33,7 @@ const guard: Guard = z.object({ consumedAt: z.number().optional(), }); -export const OidcModelInstances: GeneratedSchema = Object.freeze({ +export const OidcModelInstances: GeneratedSchema = Object.freeze({ table: 'oidc_model_instances', tableSingular: 'oidc_model_instance', fields: { diff --git a/packages/schemas/src/db-entries/resource-scope.ts b/packages/schemas/src/db-entries/resource-scope.ts index 2e761f54a..2e0a39b43 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 ResourceScopeDBEntry = { +export type ResourceScopeUpdate = { id: string; name: string; description: string; @@ -18,14 +18,14 @@ export type ResourceScope = { resourceId: string; }; -const guard: Guard = z.object({ +const guard: 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: { diff --git a/packages/schemas/src/db-entries/resource.ts b/packages/schemas/src/db-entries/resource.ts index d0a149a0c..3106b0892 100644 --- a/packages/schemas/src/db-entries/resource.ts +++ b/packages/schemas/src/db-entries/resource.ts @@ -5,7 +5,7 @@ import { z } from 'zod'; import { GeneratedSchema, Guard } from '../foundations'; import { AccessTokenFormatType, SignAlgorithmType } from './custom-types'; -export type ResourceDBEntry = { +export type ResourceUpdate = { id: string; name: string; identifier: string; @@ -23,7 +23,7 @@ export type Resource = { signAlgorithm: SignAlgorithmType; }; -const guard: Guard = z.object({ +const guard: Guard = z.object({ id: z.string(), name: z.string(), identifier: z.string(), @@ -32,7 +32,7 @@ const guard: Guard = z.object({ signAlgorithm: z.nativeEnum(SignAlgorithmType).optional(), }); -export const Resources: GeneratedSchema = Object.freeze({ +export const Resources: GeneratedSchema = Object.freeze({ table: 'resources', tableSingular: 'resource', fields: { diff --git a/packages/schemas/src/db-entries/setting.ts b/packages/schemas/src/db-entries/setting.ts index bb06e3240..cf9c98a66 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 SettingDBEntry = { +export type SettingUpdate = { id: string; customDomain?: string | null; adminConsole: AdminConsoleConfig; @@ -21,13 +21,13 @@ export type Setting = { adminConsole: AdminConsoleConfig; }; -const guard: Guard = z.object({ +const guard: Guard = z.object({ id: z.string(), customDomain: z.string().optional(), adminConsole: adminConsoleConfigGuard, }); -export const Settings: GeneratedSchema = Object.freeze({ +export const Settings: GeneratedSchema = Object.freeze({ table: 'settings', tableSingular: 'setting', fields: { diff --git a/packages/schemas/src/db-entries/user-log.ts b/packages/schemas/src/db-entries/user-log.ts index f8f83c676..cc5a50ff3 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 UserLogDBEntry = { +export type UserLogUpdate = { id: string; userId: string; type: UserLogType; @@ -23,7 +23,7 @@ export type UserLog = { createdAt: number; }; -const guard: Guard = z.object({ +const guard: 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: { diff --git a/packages/schemas/src/db-entries/user.ts b/packages/schemas/src/db-entries/user.ts index b80ca1cd5..0a75340b4 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 UserDBEntry = { +export type UserUpdate = { id: string; username?: string | null; primaryEmail?: string | null; @@ -25,7 +25,7 @@ export type User = { passwordEncryptionSalt: string | null; }; -const guard: Guard = z.object({ +const guard: Guard = z.object({ id: z.string(), username: z.string().optional(), primaryEmail: z.string().optional(), @@ -35,7 +35,7 @@ const guard: Guard = z.object({ passwordEncryptionSalt: z.string().optional(), }); -export const Users: GeneratedSchema = Object.freeze({ +export const Users: GeneratedSchema = Object.freeze({ table: 'users', tableSingular: 'user', fields: { diff --git a/packages/schemas/src/gen/schema.ts b/packages/schemas/src/gen/schema.ts index 9ad9f679e..494b8c984 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}DBEntry`; + const databaseEntryType = `${modelName}Update`; return [ `export type ${databaseEntryType} = {`, ...fields.map( diff --git a/packages/schemas/src/types/user.ts b/packages/schemas/src/types/user.ts index b58c756b8..a7acd4e65 100644 --- a/packages/schemas/src/types/user.ts +++ b/packages/schemas/src/types/user.ts @@ -1,4 +1,4 @@ -import { UserDBEntry } from '../db-entries'; +import { UserUpdate } 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< - UserDBEntry, +export type UserInfo = Pick< + UserUpdate, Keys >;