@plyaz/types 1.13.0 → 1.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/db/DatabaseAdapter.d.ts +182 -0
- package/dist/db/DatabaseService.d.ts +268 -0
- package/dist/db/EventEmitter.d.ts +64 -0
- package/dist/db/Transaction.d.ts +49 -0
- package/dist/db/adapter.d.ts +91 -0
- package/dist/db/config.types.d.ts +57 -0
- package/dist/db/database.types.d.ts +186 -0
- package/dist/db/enhanced-config.types.d.ts +311 -0
- package/dist/db/event.types.d.ts +158 -0
- package/dist/db/index.cjs +4 -0
- package/dist/db/index.cjs.map +1 -0
- package/dist/db/index.d.ts +9 -0
- package/dist/db/index.js +3 -0
- package/dist/db/index.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/package.json +6 -1
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview DatabaseAdapter interface - Core adapter contract
|
|
3
|
+
*
|
|
4
|
+
* Defines the standardized interface that all database adapters must implement
|
|
5
|
+
* to ensure consistent behavior across different database systems. This interface
|
|
6
|
+
* serves as the foundation for the adapter pattern implementation, enabling
|
|
7
|
+
* seamless switching between different database technologies.
|
|
8
|
+
*
|
|
9
|
+
* **Application Flow Context:**
|
|
10
|
+
* ```
|
|
11
|
+
* Extension Chain → DatabaseAdapterType → Concrete Adapter → Database
|
|
12
|
+
* ↓ ↓ ↓ ↓
|
|
13
|
+
* AuditAdapter → Interface Contract → DrizzleAdapter → PostgreSQL
|
|
14
|
+
* SoftDelete → Method Signatures → SupabaseAdapter → Supabase
|
|
15
|
+
* Encryption → Type Safety → SQLAdapter → Raw SQL
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* **Interface Responsibilities:**
|
|
19
|
+
* - **Connection Management**: Initialize, connect, disconnect operations
|
|
20
|
+
* - **CRUD Operations**: Create, read, update, delete with type safety
|
|
21
|
+
* - **Query Operations**: Complex queries with filtering and pagination
|
|
22
|
+
* - **Transaction Support**: Atomic operations with rollback capabilities
|
|
23
|
+
* - **Health Monitoring**: Connection status and performance metrics
|
|
24
|
+
* - **Table Management**: Dynamic table registration and schema handling
|
|
25
|
+
*
|
|
26
|
+
* **Implementation Requirements:**
|
|
27
|
+
* All adapters must implement every method in this interface to ensure
|
|
28
|
+
* compatibility with the extension system and service layer. The interface
|
|
29
|
+
* uses generic types to maintain type safety across different data models.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* // Adapter implementation example
|
|
34
|
+
* class CustomAdapter implements DatabaseAdapterType {
|
|
35
|
+
* async initialize(): Promise<DatabaseResult<void>> {
|
|
36
|
+
* // Initialize connection and validate configuration
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* async findById<T>(table: string, id: string): Promise<DatabaseResult<T | null>> {
|
|
40
|
+
* // Implement type-safe record retrieval
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* // ... implement all other interface methods
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
*/
|
|
48
|
+
import type { DatabaseResult, PaginatedResult, QueryOptions, Filter, HealthStatus } from './database.types';
|
|
49
|
+
import type { Transaction } from './Transaction';
|
|
50
|
+
/**
|
|
51
|
+
* @interface DatabaseAdapterType
|
|
52
|
+
* @description
|
|
53
|
+
* Core interface defining the contract that all database adapters must implement.
|
|
54
|
+
*
|
|
55
|
+
* This interface ensures consistent behavior across different database implementations
|
|
56
|
+
* by defining a standardized set of methods for database operations. All adapters
|
|
57
|
+
* (Drizzle, Supabase, SQL, etc.) must implement this interface to be compatible
|
|
58
|
+
* with the database service and extension system.
|
|
59
|
+
*
|
|
60
|
+
* **Method Categories:**
|
|
61
|
+
* - **Lifecycle**: initialize, connect, disconnect
|
|
62
|
+
* - **CRUD**: create, findById, findMany, update, delete
|
|
63
|
+
* - **Queries**: count, exists, query (raw SQL)
|
|
64
|
+
* - **Transactions**: transaction with rollback support
|
|
65
|
+
* - **Management**: registerTable, healthCheck
|
|
66
|
+
*
|
|
67
|
+
* **Type Safety:**
|
|
68
|
+
* The interface uses generic types extensively to maintain compile-time type
|
|
69
|
+
* safety while allowing flexibility for different data models and database schemas.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* // Using the adapter interface
|
|
74
|
+
* const adapter: DatabaseAdapterType = new DrizzleAdapter(config);
|
|
75
|
+
*
|
|
76
|
+
* // All operations are type-safe
|
|
77
|
+
* await adapter.initialize();
|
|
78
|
+
* adapter.registerTable('users', usersTable, usersTable.id);
|
|
79
|
+
*
|
|
80
|
+
* const user = await adapter.findById<User>('users', '123');
|
|
81
|
+
* const users = await adapter.findMany<User>('users', {
|
|
82
|
+
* filter: { field: 'status', operator: 'eq', value: 'active' }
|
|
83
|
+
* });
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export interface DatabaseAdapterType {
|
|
87
|
+
/**
|
|
88
|
+
* Initialize the database adapter with configuration
|
|
89
|
+
* @returns Promise resolving to DatabaseResult indicating success or failure
|
|
90
|
+
*/
|
|
91
|
+
initialize(): Promise<DatabaseResult<void>>;
|
|
92
|
+
/**
|
|
93
|
+
* Connect to the database
|
|
94
|
+
* @returns Promise that resolves when connected
|
|
95
|
+
*/
|
|
96
|
+
connect(): Promise<void>;
|
|
97
|
+
/**
|
|
98
|
+
* Disconnect from the database
|
|
99
|
+
* @returns Promise that resolves when disconnected
|
|
100
|
+
*/
|
|
101
|
+
disconnect(): Promise<void>;
|
|
102
|
+
/**
|
|
103
|
+
* Get the underlying database client
|
|
104
|
+
* @returns The underlying database client
|
|
105
|
+
*/
|
|
106
|
+
getClient(): object;
|
|
107
|
+
/**
|
|
108
|
+
* Execute a raw SQL query
|
|
109
|
+
* @param sql - SQL query string
|
|
110
|
+
* @param params - Query parameters
|
|
111
|
+
* @returns Promise resolving to query results
|
|
112
|
+
*/
|
|
113
|
+
query<T>(sql: string, params?: T[]): Promise<T[]>;
|
|
114
|
+
/**
|
|
115
|
+
* Register a table with the adapter
|
|
116
|
+
* @param name - Logical name for the table
|
|
117
|
+
* @param table - Table object or name
|
|
118
|
+
* @param idColumn - Optional ID column name
|
|
119
|
+
*/
|
|
120
|
+
registerTable<TTable, TIdColumn>(name: string, table: TTable, idColumn?: TIdColumn): void;
|
|
121
|
+
/**
|
|
122
|
+
* Find a single record by ID
|
|
123
|
+
* @param table - Table name
|
|
124
|
+
* @param id - Record ID
|
|
125
|
+
* @returns Promise resolving to DatabaseResult containing the record or null
|
|
126
|
+
*/
|
|
127
|
+
findById<T>(table: string, id: string): Promise<DatabaseResult<T | null>>;
|
|
128
|
+
/**
|
|
129
|
+
* Find multiple records with filtering and pagination
|
|
130
|
+
* @param table - Table name
|
|
131
|
+
* @param options - Query options including filters, sorting, and pagination
|
|
132
|
+
* @returns Promise resolving to DatabaseResult containing paginated data
|
|
133
|
+
*/
|
|
134
|
+
findMany<T>(table: string, options?: QueryOptions): Promise<DatabaseResult<PaginatedResult<T>>>;
|
|
135
|
+
/**
|
|
136
|
+
* Create a new record
|
|
137
|
+
* @param table - Table name
|
|
138
|
+
* @param data - Record data to create
|
|
139
|
+
* @returns Promise resolving to DatabaseResult containing the created record
|
|
140
|
+
*/
|
|
141
|
+
create<T>(table: string, data: T): Promise<DatabaseResult<T>>;
|
|
142
|
+
/**
|
|
143
|
+
* Update an existing record
|
|
144
|
+
* @param table - Table name
|
|
145
|
+
* @param id - Record ID
|
|
146
|
+
* @param data - Partial record data to update
|
|
147
|
+
* @returns Promise resolving to DatabaseResult containing the updated record
|
|
148
|
+
*/
|
|
149
|
+
update<T>(table: string, id: string, data: Partial<T>): Promise<DatabaseResult<T>>;
|
|
150
|
+
/**
|
|
151
|
+
* Delete a record
|
|
152
|
+
* @param table - Table name
|
|
153
|
+
* @param id - Record ID
|
|
154
|
+
* @returns Promise resolving to DatabaseResult indicating success or failure
|
|
155
|
+
*/
|
|
156
|
+
delete(table: string, id: string): Promise<DatabaseResult<void>>;
|
|
157
|
+
/**
|
|
158
|
+
* Execute a transaction with multiple operations
|
|
159
|
+
* @param callback - Function containing transaction operations
|
|
160
|
+
* @returns Promise resolving to DatabaseResult containing the transaction result
|
|
161
|
+
*/
|
|
162
|
+
transaction<T>(callback: (trx: Transaction) => Promise<T>): Promise<DatabaseResult<T>>;
|
|
163
|
+
/**
|
|
164
|
+
* Check if a record exists
|
|
165
|
+
* @param table - Table name
|
|
166
|
+
* @param id - Record ID
|
|
167
|
+
* @returns Promise resolving to DatabaseResult containing boolean indicating existence
|
|
168
|
+
*/
|
|
169
|
+
exists(table: string, id: string): Promise<DatabaseResult<boolean>>;
|
|
170
|
+
/**
|
|
171
|
+
* Count records matching a filter
|
|
172
|
+
* @param table - Table name
|
|
173
|
+
* @param filter - Filter conditions
|
|
174
|
+
* @returns Promise resolving to DatabaseResult containing the count
|
|
175
|
+
*/
|
|
176
|
+
count(table: string, filter?: Filter): Promise<DatabaseResult<number>>;
|
|
177
|
+
/**
|
|
178
|
+
* Perform health check on the database connection
|
|
179
|
+
* @returns Promise resolving to DatabaseResult containing health status
|
|
180
|
+
*/
|
|
181
|
+
healthCheck(): Promise<DatabaseResult<HealthStatus>>;
|
|
182
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public interface for DatabaseService
|
|
3
|
+
* This is what consumers interact with - matches the documentation exactly
|
|
4
|
+
*/
|
|
5
|
+
import type { DatabaseResult, PaginatedResult, QueryOptions, HealthStatus, Filter, CreateInput, UpdateInput, BatchUpdate } from './database.types';
|
|
6
|
+
import type { TransactionFn, TransactionOptions } from './Transaction';
|
|
7
|
+
import type { OperationConfig } from './enhanced-config.types';
|
|
8
|
+
import type { DatabaseEvent } from './event.types';
|
|
9
|
+
/**
|
|
10
|
+
* Table name type - centralized table constants
|
|
11
|
+
*/
|
|
12
|
+
export type TableName = string;
|
|
13
|
+
/**
|
|
14
|
+
* Audit context for tracking user actions
|
|
15
|
+
*/
|
|
16
|
+
export interface AuditContext {
|
|
17
|
+
userId?: string;
|
|
18
|
+
requestId?: string;
|
|
19
|
+
ipAddress?: string;
|
|
20
|
+
userAgent?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Service status information
|
|
24
|
+
*/
|
|
25
|
+
export interface ServiceStatus {
|
|
26
|
+
isHealthy: boolean;
|
|
27
|
+
adapter: string;
|
|
28
|
+
uptime: number;
|
|
29
|
+
lastHealthCheck: Date;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* MAIN DATABASE SERVICE INTERFACE - Public API Surface
|
|
33
|
+
*
|
|
34
|
+
* This is the ONLY interface that applications interact with.
|
|
35
|
+
* Provides all database operations with type safety and configuration overrides.
|
|
36
|
+
*
|
|
37
|
+
* **Application Flow Position:**
|
|
38
|
+
* Repository Layer → **DatabaseService** → DatabaseService → Adapter Chain
|
|
39
|
+
*
|
|
40
|
+
* **What this interface provides:**
|
|
41
|
+
* - Type-safe CRUD operations with per-operation configuration
|
|
42
|
+
* - Batch operations for high-performance scenarios
|
|
43
|
+
* - Transaction support with ACID guarantees
|
|
44
|
+
* - Event system for monitoring and side effects
|
|
45
|
+
* - Audit context management for compliance
|
|
46
|
+
* - Health monitoring and status reporting
|
|
47
|
+
*
|
|
48
|
+
* **Used by:** Repository layer (BaseRepository, UserRepository, etc.)
|
|
49
|
+
* **Implemented by:** DatabaseService (internal)
|
|
50
|
+
* **Created by:** createDatabaseService() factory
|
|
51
|
+
*
|
|
52
|
+
* **Key Features:**
|
|
53
|
+
* - Per-operation config overrides (skipAudit, includeSoftDeleted, etc.)
|
|
54
|
+
* - Automatic extension handling (encryption, soft delete, audit)
|
|
55
|
+
* - Event hooks for application integration
|
|
56
|
+
* - Type-safe operations with proper error handling
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ### Basic Usage in Repository
|
|
60
|
+
* ```typescript
|
|
61
|
+
* class UserRepository extends BaseRepository<User> {
|
|
62
|
+
* constructor(private db: DatabaseService) {
|
|
63
|
+
* super(db, Tables.USERS);
|
|
64
|
+
* }
|
|
65
|
+
*
|
|
66
|
+
* async findByEmail(email: string) {
|
|
67
|
+
* return this.db.query(Tables.USERS, {
|
|
68
|
+
* filter: { field: 'email', operator: 'eq', value: email }
|
|
69
|
+
* });
|
|
70
|
+
* }
|
|
71
|
+
* }
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ### Per-Operation Configuration
|
|
76
|
+
* ```typescript
|
|
77
|
+
* // Admin view - include soft-deleted records
|
|
78
|
+
* const allUsers = await db.list(Tables.USERS, {}, {
|
|
79
|
+
* includeSoftDeleted: true,
|
|
80
|
+
* forceAdapter: 'primary',
|
|
81
|
+
* cache: { enabled: false }
|
|
82
|
+
* });
|
|
83
|
+
*
|
|
84
|
+
* // Bulk import - skip audit and caching
|
|
85
|
+
* const result = await db.batchCreate(Tables.USERS, users, {
|
|
86
|
+
* skipAudit: true,
|
|
87
|
+
* timestamps: { enabled: false },
|
|
88
|
+
* cache: { enabled: false }
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export interface DatabaseService {
|
|
93
|
+
/**
|
|
94
|
+
* Get a single record by ID
|
|
95
|
+
* @param table - Table name
|
|
96
|
+
* @param id - Record ID
|
|
97
|
+
* @param config - Optional per-operation configuration overrides
|
|
98
|
+
* @returns Promise resolving to the record or null
|
|
99
|
+
*/
|
|
100
|
+
get<T>(table: TableName, id: string, config?: OperationConfig): Promise<DatabaseResult<T | null>>;
|
|
101
|
+
/**
|
|
102
|
+
* List records with optional filtering, sorting, and pagination
|
|
103
|
+
* @param table - Table name
|
|
104
|
+
* @param options - Query options
|
|
105
|
+
* @param config - Optional per-operation configuration overrides
|
|
106
|
+
* @returns Promise resolving to paginated results
|
|
107
|
+
*/
|
|
108
|
+
list<T>(table: TableName, options?: QueryOptions, config?: OperationConfig): Promise<DatabaseResult<PaginatedResult<T>>>;
|
|
109
|
+
/**
|
|
110
|
+
* Create a new record
|
|
111
|
+
* @param table - Table name
|
|
112
|
+
* @param input - Record data to create
|
|
113
|
+
* @param config - Optional per-operation configuration overrides
|
|
114
|
+
* @returns Promise resolving to the created record
|
|
115
|
+
*/
|
|
116
|
+
create<T>(table: TableName, input: CreateInput<T>, config?: OperationConfig): Promise<DatabaseResult<T>>;
|
|
117
|
+
/**
|
|
118
|
+
* Update an existing record
|
|
119
|
+
* @param table - Table name
|
|
120
|
+
* @param id - Record ID
|
|
121
|
+
* @param input - Updated fields
|
|
122
|
+
* @param config - Optional per-operation configuration overrides
|
|
123
|
+
* @returns Promise resolving to the updated record
|
|
124
|
+
*/
|
|
125
|
+
update<T>(table: TableName, id: string, input: UpdateInput<T>, config?: OperationConfig): Promise<DatabaseResult<T>>;
|
|
126
|
+
/**
|
|
127
|
+
* Delete a record
|
|
128
|
+
* @param table - Table name
|
|
129
|
+
* @param id - Record ID
|
|
130
|
+
* @param config - Optional per-operation configuration overrides
|
|
131
|
+
* @returns Promise resolving to void on success
|
|
132
|
+
*/
|
|
133
|
+
delete(table: TableName, id: string, config?: OperationConfig): Promise<DatabaseResult<void>>;
|
|
134
|
+
/**
|
|
135
|
+
* Create multiple records in a batch
|
|
136
|
+
* @param table - Table name
|
|
137
|
+
* @param inputs - Array of records to create
|
|
138
|
+
* @param config - Optional per-operation configuration overrides
|
|
139
|
+
* @returns Promise resolving to array of created records
|
|
140
|
+
*/
|
|
141
|
+
batchCreate<T>(table: TableName, inputs: CreateInput<T>[], config?: OperationConfig): Promise<DatabaseResult<T[]>>;
|
|
142
|
+
/**
|
|
143
|
+
* Update multiple records in a batch
|
|
144
|
+
* @param table - Table name
|
|
145
|
+
* @param updates - Array of update operations
|
|
146
|
+
* @param config - Optional per-operation configuration overrides
|
|
147
|
+
* @returns Promise resolving to array of updated records
|
|
148
|
+
*/
|
|
149
|
+
batchUpdate<T>(table: TableName, updates: BatchUpdate<T>[], config?: OperationConfig): Promise<DatabaseResult<T[]>>;
|
|
150
|
+
/**
|
|
151
|
+
* Delete multiple records in a batch
|
|
152
|
+
* @param table - Table name
|
|
153
|
+
* @param ids - Array of record IDs to delete
|
|
154
|
+
* @param config - Optional per-operation configuration overrides
|
|
155
|
+
* @returns Promise resolving to void on success
|
|
156
|
+
*/
|
|
157
|
+
batchDelete(table: TableName, ids: string[], config?: OperationConfig): Promise<DatabaseResult<void>>;
|
|
158
|
+
/**
|
|
159
|
+
* Execute a complex query with filters, sorting, and pagination
|
|
160
|
+
* @param table - Table name
|
|
161
|
+
* @param query - Query configuration
|
|
162
|
+
* @param config - Optional per-operation configuration overrides
|
|
163
|
+
* @returns Promise resolving to paginated results
|
|
164
|
+
*/
|
|
165
|
+
query<T>(table: TableName, query: QueryOptions, config?: OperationConfig): Promise<DatabaseResult<PaginatedResult<T>>>;
|
|
166
|
+
/**
|
|
167
|
+
* Count records matching a filter
|
|
168
|
+
* @param table - Table name
|
|
169
|
+
* @param filter - Optional filter conditions
|
|
170
|
+
* @param config - Optional per-operation configuration overrides
|
|
171
|
+
* @returns Promise resolving to the count
|
|
172
|
+
*/
|
|
173
|
+
count(table: TableName, filter?: Filter, config?: OperationConfig): Promise<DatabaseResult<number>>;
|
|
174
|
+
/**
|
|
175
|
+
* Find a single record by filter conditions
|
|
176
|
+
* @param table - Table name
|
|
177
|
+
* @param filter - Filter conditions
|
|
178
|
+
* @param config - Optional per-operation configuration overrides
|
|
179
|
+
* @returns Promise resolving to the first matching record or null
|
|
180
|
+
*/
|
|
181
|
+
findOne<T>(table: TableName, filter: Filter, config?: OperationConfig): Promise<DatabaseResult<T | null>>;
|
|
182
|
+
/**
|
|
183
|
+
* Soft delete a record (logical deletion)
|
|
184
|
+
* @param table - Table name
|
|
185
|
+
* @param id - Record ID
|
|
186
|
+
* @param config - Optional per-operation configuration overrides
|
|
187
|
+
* @returns Promise resolving to void on success
|
|
188
|
+
*/
|
|
189
|
+
softDelete(table: TableName, id: string, config?: OperationConfig): Promise<DatabaseResult<void>>;
|
|
190
|
+
/**
|
|
191
|
+
* Execute operations within a transaction
|
|
192
|
+
* @param fn - Function containing transactional operations
|
|
193
|
+
* @param options - Optional transaction configuration
|
|
194
|
+
* @returns Promise resolving to the transaction result
|
|
195
|
+
*/
|
|
196
|
+
transaction<T>(fn: TransactionFn<T>, options?: TransactionOptions): Promise<DatabaseResult<T>>;
|
|
197
|
+
/**
|
|
198
|
+
* Set audit context for tracking user actions
|
|
199
|
+
* @param context - Audit context information
|
|
200
|
+
* @returns Promise resolving to void on success
|
|
201
|
+
*/
|
|
202
|
+
setAuditContext(context: AuditContext): Promise<DatabaseResult<void>>;
|
|
203
|
+
/**
|
|
204
|
+
* Subscribe to database events
|
|
205
|
+
* @param event - Event type to listen for
|
|
206
|
+
* @param handler - Event handler function
|
|
207
|
+
*/
|
|
208
|
+
on<T extends DatabaseEvent['type']>(event: T, handler: (event: Extract<DatabaseEvent, {
|
|
209
|
+
type: T;
|
|
210
|
+
}>) => void | Promise<void>): void;
|
|
211
|
+
/**
|
|
212
|
+
* Unsubscribe from database events
|
|
213
|
+
* @param event - Event type to stop listening for
|
|
214
|
+
* @param handler - Event handler function to remove
|
|
215
|
+
*/
|
|
216
|
+
off<T extends DatabaseEvent['type']>(event: T, handler: (event: Extract<DatabaseEvent, {
|
|
217
|
+
type: T;
|
|
218
|
+
}>) => void | Promise<void>): void;
|
|
219
|
+
/**
|
|
220
|
+
* Perform health check on the database connection
|
|
221
|
+
* @returns Promise resolving to health status
|
|
222
|
+
*/
|
|
223
|
+
healthCheck(): Promise<DatabaseResult<HealthStatus>>;
|
|
224
|
+
/**
|
|
225
|
+
* Get current service status
|
|
226
|
+
* @returns Current service status information
|
|
227
|
+
*/
|
|
228
|
+
getStatus(): ServiceStatus;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Legacy interface for backward compatibility
|
|
232
|
+
* @deprecated Use DatabaseService instead
|
|
233
|
+
*/
|
|
234
|
+
export interface DatabaseServiceType extends DatabaseService {
|
|
235
|
+
/**
|
|
236
|
+
* @deprecated Use get() instead
|
|
237
|
+
*/
|
|
238
|
+
findById<T>(table: string, id: string): Promise<DatabaseResult<T | null>>;
|
|
239
|
+
/**
|
|
240
|
+
* @deprecated Use list() instead
|
|
241
|
+
*/
|
|
242
|
+
findMany<T>(table: string, options?: QueryOptions): Promise<DatabaseResult<PaginatedResult<T>>>;
|
|
243
|
+
/**
|
|
244
|
+
* Initialize the database service
|
|
245
|
+
* @deprecated Initialization is handled by factory
|
|
246
|
+
*/
|
|
247
|
+
initialize(): Promise<DatabaseResult<void>>;
|
|
248
|
+
/**
|
|
249
|
+
* Register a table with the adapter
|
|
250
|
+
* @deprecated Tables are registered automatically
|
|
251
|
+
*/
|
|
252
|
+
registerTable<T>(name: string, table: T, idColumn?: keyof T): void;
|
|
253
|
+
/**
|
|
254
|
+
* Check if a record exists
|
|
255
|
+
* @deprecated Use count() with filter instead
|
|
256
|
+
*/
|
|
257
|
+
exists(table: string, id: string): Promise<DatabaseResult<boolean>>;
|
|
258
|
+
/**
|
|
259
|
+
* Find a single record by filter conditions
|
|
260
|
+
* @deprecated Use findOne() from main interface instead
|
|
261
|
+
*/
|
|
262
|
+
findOne<T>(table: string, filter: Filter): Promise<DatabaseResult<T | null>>;
|
|
263
|
+
/**
|
|
264
|
+
* Soft delete a record (logical deletion)
|
|
265
|
+
* @deprecated Use softDelete() from main interface instead
|
|
266
|
+
*/
|
|
267
|
+
softDelete(table: string, id: string): Promise<DatabaseResult<void>>;
|
|
268
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event emitter interface
|
|
3
|
+
* Defines the contract for database event emission
|
|
4
|
+
*/
|
|
5
|
+
import type { DatabaseEvent, DBEventHandler, DatabaseOperationType } from './event.types';
|
|
6
|
+
import type { DatabaseError } from './database.types';
|
|
7
|
+
import type { ContextType } from '@nestjs/common';
|
|
8
|
+
/**
|
|
9
|
+
* Emit a query error event
|
|
10
|
+
*/
|
|
11
|
+
interface EmitQueryErrorOptions {
|
|
12
|
+
table: string;
|
|
13
|
+
operation: DatabaseOperationType;
|
|
14
|
+
error: Error;
|
|
15
|
+
params?: Record<string, object>;
|
|
16
|
+
context?: ContextType;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Interface for database event emission
|
|
20
|
+
*/
|
|
21
|
+
export interface DatabaseEventEmitterType {
|
|
22
|
+
/**
|
|
23
|
+
* Register an event handler for a specific event type
|
|
24
|
+
*/
|
|
25
|
+
on<T extends DatabaseEvent>(eventType: T['type'], handler: DBEventHandler<T>): void;
|
|
26
|
+
/**
|
|
27
|
+
* Remove an event handler for a specific event type
|
|
28
|
+
*/
|
|
29
|
+
off<T extends DatabaseEvent>(eventType: T['type'], handler: DBEventHandler<T>): void;
|
|
30
|
+
/**
|
|
31
|
+
* Emit an event to all registered handlers
|
|
32
|
+
*/
|
|
33
|
+
emit<T extends DatabaseEvent>(event: T): void;
|
|
34
|
+
/**
|
|
35
|
+
* Emit a before query event
|
|
36
|
+
*/
|
|
37
|
+
emitBeforeQuery<T extends object = object>(table: string, operation: DatabaseOperationType, params?: Partial<T>, context?: Record<string, string | number | boolean | null>): void;
|
|
38
|
+
/**
|
|
39
|
+
* Emit an after query event
|
|
40
|
+
*/
|
|
41
|
+
emitAfterQuery(table: string, operation: DatabaseOperationType, duration: number, affectedRows?: number): void;
|
|
42
|
+
/**
|
|
43
|
+
* Emit a query error event
|
|
44
|
+
*/
|
|
45
|
+
emitQueryError<T extends object = object>(table: string, operation: DatabaseOperationType, error: DatabaseError, params?: Partial<T>, context?: Record<string, string | number | boolean | null>): void;
|
|
46
|
+
/**
|
|
47
|
+
* Emit a before transaction event
|
|
48
|
+
*/
|
|
49
|
+
emitBeforeTransaction(transactionId: string, context?: Record<string, string | number | boolean | null>): void;
|
|
50
|
+
/**
|
|
51
|
+
* Emit an after transaction event
|
|
52
|
+
*/
|
|
53
|
+
emitAfterTransaction(transactionId: string, duration: number): void;
|
|
54
|
+
/**
|
|
55
|
+
* Emit a transaction rollback event
|
|
56
|
+
*/
|
|
57
|
+
emitTransactionRollback(transactionId: string, error?: DatabaseError): void;
|
|
58
|
+
/**
|
|
59
|
+
* Emit a health change event
|
|
60
|
+
*/
|
|
61
|
+
emitHealthChange<TDetails extends object = object>(previousStatus: boolean, currentStatus: boolean, details?: TDetails): void;
|
|
62
|
+
emitQueryError(options: EmitQueryErrorOptions): void;
|
|
63
|
+
}
|
|
64
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transaction interface
|
|
3
|
+
* Defines the contract for database transactions
|
|
4
|
+
*/
|
|
5
|
+
import type { DatabaseResult } from './database.types';
|
|
6
|
+
/**
|
|
7
|
+
* Interface for database transaction operations
|
|
8
|
+
*/
|
|
9
|
+
export interface Transaction {
|
|
10
|
+
/**
|
|
11
|
+
* Find a single record within the transaction
|
|
12
|
+
*/
|
|
13
|
+
findById<T>(table: string, id: string): Promise<DatabaseResult<T | null>>;
|
|
14
|
+
/**
|
|
15
|
+
* Create a record within the transaction
|
|
16
|
+
*/
|
|
17
|
+
create<T>(table: string, data: T): Promise<DatabaseResult<T>>;
|
|
18
|
+
/**
|
|
19
|
+
* Update a record within the transaction
|
|
20
|
+
*/
|
|
21
|
+
update<T>(table: string, id: string, data: Partial<T>): Promise<DatabaseResult<T>>;
|
|
22
|
+
/**
|
|
23
|
+
* Delete a record within the transaction
|
|
24
|
+
*/
|
|
25
|
+
delete(table: string, id: string): Promise<DatabaseResult<void>>;
|
|
26
|
+
/**
|
|
27
|
+
* Commit the transaction
|
|
28
|
+
*/
|
|
29
|
+
commit(): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Rollback the transaction
|
|
32
|
+
*/
|
|
33
|
+
rollback(): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Transaction function type
|
|
37
|
+
*/
|
|
38
|
+
export type TransactionFn<T> = (trx: Transaction) => Promise<T>;
|
|
39
|
+
/**
|
|
40
|
+
* Transaction options
|
|
41
|
+
*/
|
|
42
|
+
export interface TransactionOptions {
|
|
43
|
+
/** Transaction isolation level */
|
|
44
|
+
isolationLevel?: 'READ_UNCOMMITTED' | 'READ_COMMITTED' | 'REPEATABLE_READ' | 'SERIALIZABLE';
|
|
45
|
+
/** Transaction timeout in milliseconds */
|
|
46
|
+
timeout?: number;
|
|
47
|
+
/** Whether to use savepoints for nested transactions */
|
|
48
|
+
useSavepoints?: boolean;
|
|
49
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Database adapter type definitions
|
|
3
|
+
*
|
|
4
|
+
* Defines the enumeration of supported database adapter types used throughout
|
|
5
|
+
* the database package. This enum provides type-safe identification of different
|
|
6
|
+
* database integrations and is used in configuration, factory methods, and
|
|
7
|
+
* adapter selection logic.
|
|
8
|
+
*
|
|
9
|
+
* **Application Flow Context:**
|
|
10
|
+
* ```
|
|
11
|
+
* Configuration → ADAPTERS Enum → AdapterFactory → Concrete Adapter
|
|
12
|
+
* ↓ ↓ ↓ ↓
|
|
13
|
+
* User Config → Type Safety → Adapter Creation → Database Connection
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* **Adapter Types:**
|
|
17
|
+
* - **DRIZZLE**: Type-safe ORM with excellent TypeScript support
|
|
18
|
+
* - **SUPABASE**: Hosted PostgreSQL with real-time capabilities
|
|
19
|
+
* - **SQL**: Raw SQL execution for maximum control
|
|
20
|
+
* - **DATABASE**: Generic fallback adapter
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* // Adapter selection in configuration
|
|
25
|
+
* const config = {
|
|
26
|
+
* adapter: ADAPTERS.DRIZZLE,
|
|
27
|
+
* connectionString: process.env.DATABASE_URL
|
|
28
|
+
* };
|
|
29
|
+
*
|
|
30
|
+
* // Type-safe adapter factory usage
|
|
31
|
+
* const adapter = AdapterFactory.create(ADAPTERS.SUPABASE, supabaseConfig);
|
|
32
|
+
*
|
|
33
|
+
* // Switch statement with exhaustive checking
|
|
34
|
+
* switch (config.adapter) {
|
|
35
|
+
* case ADAPTERS.DRIZZLE:
|
|
36
|
+
* // Handle Drizzle-specific logic
|
|
37
|
+
* break;
|
|
38
|
+
* case ADAPTERS.SUPABASE:
|
|
39
|
+
* // Handle Supabase-specific logic
|
|
40
|
+
* break;
|
|
41
|
+
* case ADAPTERS.SQL:
|
|
42
|
+
* // Handle SQL-specific logic
|
|
43
|
+
* break;
|
|
44
|
+
* default:
|
|
45
|
+
* // TypeScript ensures all cases are handled
|
|
46
|
+
* throw new Error(`Unsupported adapter: ${config.adapter}`);
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
*/
|
|
51
|
+
/**
|
|
52
|
+
* @enum ADAPTERS
|
|
53
|
+
* @description
|
|
54
|
+
* Enumeration of supported database adapter types.
|
|
55
|
+
*
|
|
56
|
+
* This enum provides type-safe identification of different database integrations
|
|
57
|
+
* and is used throughout the package for configuration, factory methods, and
|
|
58
|
+
* adapter selection. Each adapter type represents a different approach to
|
|
59
|
+
* database connectivity and operations.
|
|
60
|
+
*
|
|
61
|
+
* **Adapter Characteristics:**
|
|
62
|
+
* - **DATABASE**: Generic fallback, minimal functionality
|
|
63
|
+
* - **DRIZZLE**: Full ORM with type safety and query building
|
|
64
|
+
* - **SUPABASE**: Hosted solution with real-time and auth features
|
|
65
|
+
* - **SQL**: Raw SQL for performance-critical applications
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* // Configuration with adapter selection
|
|
70
|
+
* const configs = {
|
|
71
|
+
* development: { adapter: ADAPTERS.DRIZZLE },
|
|
72
|
+
* production: { adapter: ADAPTERS.SUPABASE },
|
|
73
|
+
* performance: { adapter: ADAPTERS.SQL }
|
|
74
|
+
* };
|
|
75
|
+
*
|
|
76
|
+
* // Type-safe adapter validation
|
|
77
|
+
* function validateAdapter(adapter: ADAPTERS): boolean {
|
|
78
|
+
* return Object.values(ADAPTERS).includes(adapter);
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export declare enum ADAPTERS {
|
|
83
|
+
/** Generic database adapter (default when no specific integration is set) */
|
|
84
|
+
DATABASE = "database",
|
|
85
|
+
/** Drizzle ORM adapter (PostgreSQL, MySQL, SQLite, etc.) */
|
|
86
|
+
DRIZZLE = "drizzle",
|
|
87
|
+
/** Supabase adapter (PostgreSQL backend with REST + Realtime APIs) */
|
|
88
|
+
SUPABASE = "supabase",
|
|
89
|
+
/** Raw SQL adapter (direct database queries without ORM) */
|
|
90
|
+
SQL = "sql"
|
|
91
|
+
}
|