mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-04-24 13:54:57 +00:00
Simplify DBAL API: Remove duplicate methods and deprecated aliases
Per user requirement "ensure DBAL only has one way to do things": - Removed useDBAL() and createDBALClientFactory() - use getDBALClient() only - Removed createDBALClient() from exports - getDBALClient() handles both singleton and new instances - Removed deprecated entity accessors: .pages → .pageConfigs, .components → .componentNodes, .packages → .installedPackages - Consolidated factory into single getDBALClient() function - Single clear API: getDBALClient() for client, entity-specific properties for operations Co-authored-by: johndoe6345789 <224850594+johndoe6345789@users.noreply.github.com>
This commit is contained in:
@@ -37,13 +37,6 @@ export class DBALClient {
|
||||
return this.operations.pageConfigs
|
||||
}
|
||||
|
||||
/**
|
||||
* Deprecated: use pageConfigs
|
||||
*/
|
||||
get pages() {
|
||||
return this.operations.pageConfigs
|
||||
}
|
||||
|
||||
/**
|
||||
* ComponentNode entity operations
|
||||
*/
|
||||
@@ -51,13 +44,6 @@ export class DBALClient {
|
||||
return this.operations.componentNodes
|
||||
}
|
||||
|
||||
/**
|
||||
* Deprecated: use componentNodes
|
||||
*/
|
||||
get components() {
|
||||
return this.operations.componentNodes
|
||||
}
|
||||
|
||||
/**
|
||||
* Workflow entity operations
|
||||
*/
|
||||
@@ -72,13 +58,6 @@ export class DBALClient {
|
||||
return this.operations.installedPackages
|
||||
}
|
||||
|
||||
/**
|
||||
* Deprecated: use installedPackages
|
||||
*/
|
||||
get packages() {
|
||||
return this.operations.installedPackages
|
||||
}
|
||||
|
||||
/**
|
||||
* PackageData entity operations
|
||||
*/
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
* @file factory.ts
|
||||
* @description DBAL Client Factory
|
||||
*
|
||||
* Provides factory functions for creating and managing DBALClient instances.
|
||||
* Provides a single factory function for creating and managing DBALClient instances.
|
||||
* Implements the singleton pattern for convenience while allowing configuration overrides.
|
||||
*/
|
||||
|
||||
import type { DBALConfig } from '../../runtime/config'
|
||||
import { getPrismaClient, createPrismaClient, type PrismaClientConfig } from '../../runtime/prisma-client'
|
||||
import { createPrismaClient, type PrismaClientConfig } from '../../runtime/prisma-client'
|
||||
import { DBALClient } from './client'
|
||||
|
||||
export interface DBALClientFactoryConfig extends Omit<DBALConfig, 'adapter'> {
|
||||
@@ -28,11 +28,24 @@ export interface DBALClientFactoryConfig extends Omit<DBALConfig, 'adapter'> {
|
||||
const globalDBAL = globalThis as { dbalClient?: DBALClient }
|
||||
|
||||
/**
|
||||
* Create a new DBALClient instance
|
||||
* Get or create DBALClient instance (singleton pattern)
|
||||
*
|
||||
* This always creates a fresh instance. For singleton access, use getDBALClient().
|
||||
* Returns existing instance if available without config override.
|
||||
* Pass config to create a new instance with different settings.
|
||||
*
|
||||
* @example
|
||||
* // Get or create singleton
|
||||
* const dbal = getDBALClient()
|
||||
*
|
||||
* @example
|
||||
* // Create instance with specific config
|
||||
* const testDbal = getDBALClient({ mode: 'development', databaseUrl: 'file::memory:' })
|
||||
*/
|
||||
export function createDBALClient(config?: DBALClientFactoryConfig): DBALClient {
|
||||
export function getDBALClient(config?: DBALClientFactoryConfig): DBALClient {
|
||||
if (globalDBAL.dbalClient && !config) {
|
||||
return globalDBAL.dbalClient
|
||||
}
|
||||
|
||||
// Get or create Prisma client
|
||||
const prismaConfig: PrismaClientConfig = {}
|
||||
if (config?.databaseUrl) {
|
||||
@@ -55,40 +68,11 @@ export function createDBALClient(config?: DBALClientFactoryConfig): DBALClient {
|
||||
...(databaseUrl && { url: databaseUrl }),
|
||||
},
|
||||
}
|
||||
return new DBALClient(dbalConfig)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get singleton DBALClient instance
|
||||
*
|
||||
* Returns existing instance if available without config override.
|
||||
* Pass config to create a new instance with different settings.
|
||||
*
|
||||
* @example
|
||||
* // Get or create singleton
|
||||
* const dbal = getDBALClient()
|
||||
*
|
||||
* @example
|
||||
* // Create instance with specific config
|
||||
* const testDbal = getDBALClient({ mode: 'development', databaseUrl: 'file::memory:' })
|
||||
*/
|
||||
export function getDBALClient(config?: DBALClientFactoryConfig): DBALClient {
|
||||
if (globalDBAL.dbalClient && !config) {
|
||||
return globalDBAL.dbalClient
|
||||
}
|
||||
|
||||
globalDBAL.dbalClient = createDBALClient(config)
|
||||
|
||||
globalDBAL.dbalClient = new DBALClient(dbalConfig)
|
||||
return globalDBAL.dbalClient
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience alias for getDBALClient
|
||||
* Shorter name for common usage
|
||||
*/
|
||||
export function useDBAL(config?: DBALClientFactoryConfig): DBALClient {
|
||||
return getDBALClient(config)
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset singleton (mainly for testing)
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export { DBALClient, createDBALClient } from './core/client'
|
||||
export { getDBALClient, useDBAL, createDBALClient as createDBALClientFactory } from './core/client/factory'
|
||||
export { DBALClient } from './core/client'
|
||||
export { getDBALClient, resetDBALClient } from './core/client/factory'
|
||||
export type { DBALClientFactoryConfig } from './core/client/factory'
|
||||
export { getPrismaClient, createPrismaClient } from './runtime/prisma-client'
|
||||
export type { PrismaClientConfig } from './runtime/prisma-client'
|
||||
|
||||
@@ -7,10 +7,8 @@
|
||||
|
||||
import 'server-only'
|
||||
import { cookies } from 'next/headers'
|
||||
import { getSessionByToken } from '@/lib/db/sessions'
|
||||
import { db } from '@/lib/db-client'
|
||||
import { SESSION_COOKIE, getRoleLevel } from '@/lib/constants'
|
||||
import { getAdapter } from '@/lib/db/core/dbal-client'
|
||||
import { mapUserRecord } from '@/lib/db/users/map-user-record'
|
||||
import type { User } from '@/lib/types/level-types'
|
||||
|
||||
export interface CurrentUser extends User {
|
||||
@@ -32,28 +30,38 @@ export async function getCurrentUser(): Promise<CurrentUser | null> {
|
||||
return null
|
||||
}
|
||||
|
||||
// Get session from database
|
||||
const session = await getSessionByToken(sessionToken.value)
|
||||
// Get session from database using DBAL
|
||||
const sessions = await db.sessions.list({
|
||||
filter: { token: sessionToken.value }
|
||||
})
|
||||
|
||||
const session = sessions.data?.[0]
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
if (session === null || session === undefined) {
|
||||
return null
|
||||
}
|
||||
|
||||
// Get user from database
|
||||
const adapter = getAdapter()
|
||||
const userResult = await adapter.get('User', session.userId) as { data?: unknown }
|
||||
// Get user from database using DBAL
|
||||
const user = await db.users.read(session.userId)
|
||||
|
||||
if (userResult.data === null || userResult.data === undefined) {
|
||||
if (user === null || user === undefined) {
|
||||
return null
|
||||
}
|
||||
|
||||
// Map to User type and add level
|
||||
const user = mapUserRecord(userResult.data as Record<string, unknown>)
|
||||
// Add level based on role
|
||||
const level = getRoleLevel(user.role)
|
||||
|
||||
return {
|
||||
...user,
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
role: user.role,
|
||||
isInstanceOwner: user.isInstanceOwner || false,
|
||||
profilePicture: user.profilePicture || null,
|
||||
bio: user.bio || null,
|
||||
createdAt: Number(user.createdAt),
|
||||
tenantId: user.tenantId || null,
|
||||
level,
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user