@plyaz/types 1.13.11 → 1.13.13
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/api/index.cjs +1 -1
- package/dist/api/index.cjs.map +1 -1
- package/dist/db/config.types.d.ts +57 -0
- package/dist/db/databaseAdapter.d.ts +183 -0
- package/dist/db/databaseService.d.ts +261 -0
- package/dist/db/eventEmitter.d.ts +68 -0
- package/dist/db/index.d.ts +4 -0
- package/dist/db/replica.types.d.ts +2 -2
- package/dist/errors/index.cjs +1 -1
- package/dist/errors/index.cjs.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration types
|
|
3
|
+
* Types for database configuration
|
|
4
|
+
*/
|
|
5
|
+
import type { ADAPTERS } from './adapter';
|
|
6
|
+
/**
|
|
7
|
+
* Base adapter interface with common configuration options
|
|
8
|
+
*/
|
|
9
|
+
export interface BaseAdapter {
|
|
10
|
+
/** Database adapter type */
|
|
11
|
+
adapter: ADAPTERS;
|
|
12
|
+
/** Database connection string */
|
|
13
|
+
connectionString?: string;
|
|
14
|
+
/** Connection pool configuration */
|
|
15
|
+
pool?: {
|
|
16
|
+
/** Minimum number of connections to maintain in the pool */
|
|
17
|
+
min?: number;
|
|
18
|
+
/** Maximum number of connections allowed in the pool */
|
|
19
|
+
max?: number;
|
|
20
|
+
/** Time in milliseconds after which idle connections are closed */
|
|
21
|
+
idleTimeoutMillis?: number;
|
|
22
|
+
};
|
|
23
|
+
/** Maximum time in milliseconds to wait for a query to complete */
|
|
24
|
+
queryTimeout?: number;
|
|
25
|
+
/** Whether to enable SSL for database connections */
|
|
26
|
+
ssl?: boolean;
|
|
27
|
+
/** Database schema name */
|
|
28
|
+
schema?: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Drizzle adapter configuration
|
|
32
|
+
*/
|
|
33
|
+
export interface DrizzleAdapterConfig extends BaseAdapter {
|
|
34
|
+
adapter: ADAPTERS.DRIZZLE;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Supabase adapter configuration
|
|
38
|
+
*/
|
|
39
|
+
export interface SupabaseAdapterConfig extends BaseAdapter {
|
|
40
|
+
adapter: ADAPTERS.SUPABASE;
|
|
41
|
+
/** Supabase project URL */
|
|
42
|
+
supabaseUrl: string;
|
|
43
|
+
/** Supabase anonymous key */
|
|
44
|
+
supabaseAnonKey?: string;
|
|
45
|
+
/** Supabase service role key */
|
|
46
|
+
supabaseServiceKey?: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* SQL adapter configuration
|
|
50
|
+
*/
|
|
51
|
+
export interface SQLAdapterConfig extends BaseAdapter {
|
|
52
|
+
adapter: ADAPTERS.SQL;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Database configuration as a union of all adapter configurations
|
|
56
|
+
*/
|
|
57
|
+
export type DatabaseConfig = DrizzleAdapterConfig | SupabaseAdapterConfig | SQLAdapterConfig;
|
|
@@ -0,0 +1,183 @@
|
|
|
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, QueryOptions, Filter, DatabaseHealthStatus } from './database.types';
|
|
49
|
+
import type { PaginatedResult } from './databsePagination';
|
|
50
|
+
import type { Transaction } from './Transaction';
|
|
51
|
+
/**
|
|
52
|
+
* @interface DatabaseAdapterType
|
|
53
|
+
* @description
|
|
54
|
+
* Core interface defining the contract that all database adapters must implement.
|
|
55
|
+
*
|
|
56
|
+
* This interface ensures consistent behavior across different database implementations
|
|
57
|
+
* by defining a standardized set of methods for database operations. All adapters
|
|
58
|
+
* (Drizzle, Supabase, SQL, etc.) must implement this interface to be compatible
|
|
59
|
+
* with the database service and extension system.
|
|
60
|
+
*
|
|
61
|
+
* **Method Categories:**
|
|
62
|
+
* - **Lifecycle**: initialize, connect, disconnect
|
|
63
|
+
* - **CRUD**: create, findById, findMany, update, delete
|
|
64
|
+
* - **Queries**: count, exists, query (raw SQL)
|
|
65
|
+
* - **Transactions**: transaction with rollback support
|
|
66
|
+
* - **Management**: registerTable, healthCheck
|
|
67
|
+
*
|
|
68
|
+
* **Type Safety:**
|
|
69
|
+
* The interface uses generic types extensively to maintain compile-time type
|
|
70
|
+
* safety while allowing flexibility for different data models and database schemas.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* // Using the adapter interface
|
|
75
|
+
* const adapter: DatabaseAdapterType = new DrizzleAdapter(config);
|
|
76
|
+
*
|
|
77
|
+
* // All operations are type-safe
|
|
78
|
+
* await adapter.initialize();
|
|
79
|
+
* adapter.registerTable('users', usersTable, usersTable.id);
|
|
80
|
+
*
|
|
81
|
+
* const user = await adapter.findById<User>('users', '123');
|
|
82
|
+
* const users = await adapter.findMany<User>('users', {
|
|
83
|
+
* filter: { field: 'status', operator: 'eq', value: 'active' }
|
|
84
|
+
* });
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export interface DatabaseAdapterType {
|
|
88
|
+
/**
|
|
89
|
+
* Initialize the database adapter with configuration
|
|
90
|
+
* @returns Promise resolving to DatabaseResult indicating success or failure
|
|
91
|
+
*/
|
|
92
|
+
initialize(): Promise<DatabaseResult<void>>;
|
|
93
|
+
/**
|
|
94
|
+
* Connect to the database
|
|
95
|
+
* @returns Promise that resolves when connected
|
|
96
|
+
*/
|
|
97
|
+
connect(): Promise<void>;
|
|
98
|
+
/**
|
|
99
|
+
* Disconnect from the database
|
|
100
|
+
* @returns Promise that resolves when disconnected
|
|
101
|
+
*/
|
|
102
|
+
disconnect(): Promise<void>;
|
|
103
|
+
/**
|
|
104
|
+
* Get the underlying database client
|
|
105
|
+
* @returns The underlying database client
|
|
106
|
+
*/
|
|
107
|
+
getClient(): object;
|
|
108
|
+
/**
|
|
109
|
+
* Execute a raw SQL query
|
|
110
|
+
* @param sql - SQL query string
|
|
111
|
+
* @param params - Query parameters
|
|
112
|
+
* @returns Promise resolving to query results
|
|
113
|
+
*/
|
|
114
|
+
query<T>(sql: string, params?: T[]): Promise<T[]>;
|
|
115
|
+
/**
|
|
116
|
+
* Register a table with the adapter
|
|
117
|
+
* @param name - Logical name for the table
|
|
118
|
+
* @param table - Table object or name
|
|
119
|
+
* @param idColumn - Optional ID column name
|
|
120
|
+
*/
|
|
121
|
+
registerTable<TTable, TIdColumn>(name: string, table: TTable, idColumn?: TIdColumn): void;
|
|
122
|
+
/**
|
|
123
|
+
* Find a single record by ID
|
|
124
|
+
* @param table - Table name
|
|
125
|
+
* @param id - Record ID
|
|
126
|
+
* @returns Promise resolving to DatabaseResult containing the record or null
|
|
127
|
+
*/
|
|
128
|
+
findById<T>(table: string, id: string): Promise<DatabaseResult<T | null>>;
|
|
129
|
+
/**
|
|
130
|
+
* Find multiple records with filtering and pagination
|
|
131
|
+
* @param table - Table name
|
|
132
|
+
* @param options - Query options including filters, sorting, and pagination
|
|
133
|
+
* @returns Promise resolving to DatabaseResult containing paginated data
|
|
134
|
+
*/
|
|
135
|
+
findMany<T>(table: string, options?: QueryOptions): Promise<DatabaseResult<PaginatedResult<T>>>;
|
|
136
|
+
/**
|
|
137
|
+
* Create a new record
|
|
138
|
+
* @param table - Table name
|
|
139
|
+
* @param data - Record data to create
|
|
140
|
+
* @returns Promise resolving to DatabaseResult containing the created record
|
|
141
|
+
*/
|
|
142
|
+
create<T>(table: string, data: T): Promise<DatabaseResult<T>>;
|
|
143
|
+
/**
|
|
144
|
+
* Update an existing record
|
|
145
|
+
* @param table - Table name
|
|
146
|
+
* @param id - Record ID
|
|
147
|
+
* @param data - Partial record data to update
|
|
148
|
+
* @returns Promise resolving to DatabaseResult containing the updated record
|
|
149
|
+
*/
|
|
150
|
+
update<T>(table: string, id: string, data: Partial<T>): Promise<DatabaseResult<T>>;
|
|
151
|
+
/**
|
|
152
|
+
* Delete a record
|
|
153
|
+
* @param table - Table name
|
|
154
|
+
* @param id - Record ID
|
|
155
|
+
* @returns Promise resolving to DatabaseResult indicating success or failure
|
|
156
|
+
*/
|
|
157
|
+
delete(table: string, id: string): Promise<DatabaseResult<void>>;
|
|
158
|
+
/**
|
|
159
|
+
* Execute a transaction with multiple operations
|
|
160
|
+
* @param callback - Function containing transaction operations
|
|
161
|
+
* @returns Promise resolving to DatabaseResult containing the transaction result
|
|
162
|
+
*/
|
|
163
|
+
transaction<T>(callback: (trx: Transaction) => Promise<T>): Promise<DatabaseResult<T>>;
|
|
164
|
+
/**
|
|
165
|
+
* Check if a record exists
|
|
166
|
+
* @param table - Table name
|
|
167
|
+
* @param id - Record ID
|
|
168
|
+
* @returns Promise resolving to DatabaseResult containing boolean indicating existence
|
|
169
|
+
*/
|
|
170
|
+
exists(table: string, id: string): Promise<DatabaseResult<boolean>>;
|
|
171
|
+
/**
|
|
172
|
+
* Count records matching a filter
|
|
173
|
+
* @param table - Table name
|
|
174
|
+
* @param filter - Filter conditions
|
|
175
|
+
* @returns Promise resolving to DatabaseResult containing the count
|
|
176
|
+
*/
|
|
177
|
+
count(table: string, filter?: Filter): Promise<DatabaseResult<number>>;
|
|
178
|
+
/**
|
|
179
|
+
* Perform health check on the database connection
|
|
180
|
+
* @returns Promise resolving to DatabaseResult containing health status
|
|
181
|
+
*/
|
|
182
|
+
healthCheck(): Promise<DatabaseResult<DatabaseHealthStatus>>;
|
|
183
|
+
}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public interface for DatabaseService
|
|
3
|
+
* This is what consumers interact with - matches the documentation exactly
|
|
4
|
+
*/
|
|
5
|
+
import type { DatabaseResult, QueryOptions, DatabaseHealthStatus, Filter, CreateInput, UpdateInput, BatchUpdate } from './database.types';
|
|
6
|
+
import type { TransactionFn, TransactionOptions } from './Transaction';
|
|
7
|
+
import type { OperationConfig } from './features-config.types';
|
|
8
|
+
import type { DatabaseEvent } from './event.types';
|
|
9
|
+
import type { PaginatedResult } from './databsePagination';
|
|
10
|
+
import type { AuditContext } from './audit.types';
|
|
11
|
+
/**
|
|
12
|
+
* Table name type - centralized table constants
|
|
13
|
+
*/
|
|
14
|
+
export type TableName = string;
|
|
15
|
+
/**
|
|
16
|
+
* Service status information
|
|
17
|
+
*/
|
|
18
|
+
export interface ServiceStatus {
|
|
19
|
+
isHealthy: boolean;
|
|
20
|
+
adapter: string;
|
|
21
|
+
uptime: number;
|
|
22
|
+
lastHealthCheck: Date;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* MAIN DATABASE SERVICE INTERFACE - Public API Surface
|
|
26
|
+
*
|
|
27
|
+
* This is the ONLY interface that applications interact with.
|
|
28
|
+
* Provides all database operations with type safety and configuration overrides.
|
|
29
|
+
*
|
|
30
|
+
* **Application Flow Position:**
|
|
31
|
+
* Repository Layer → **DatabaseService** → DatabaseService → Adapter Chain
|
|
32
|
+
*
|
|
33
|
+
* **What this interface provides:**
|
|
34
|
+
* - Type-safe CRUD operations with per-operation configuration
|
|
35
|
+
* - Batch operations for high-performance scenarios
|
|
36
|
+
* - Transaction support with ACID guarantees
|
|
37
|
+
* - Event system for monitoring and side effects
|
|
38
|
+
* - Audit context management for compliance
|
|
39
|
+
* - Health monitoring and status reporting
|
|
40
|
+
*
|
|
41
|
+
* **Used by:** Repository layer (BaseRepository, UserRepository, etc.)
|
|
42
|
+
* **Implemented by:** DatabaseService (internal)
|
|
43
|
+
* **Created by:** createDatabaseService() factory
|
|
44
|
+
*
|
|
45
|
+
* **Key Features:**
|
|
46
|
+
* - Per-operation config overrides (skipAudit, includeSoftDeleted, etc.)
|
|
47
|
+
* - Automatic extension handling (encryption, soft delete, audit)
|
|
48
|
+
* - Event hooks for application integration
|
|
49
|
+
* - Type-safe operations with proper error handling
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ### Basic Usage in Repository
|
|
53
|
+
* ```typescript
|
|
54
|
+
* class UserRepository extends BaseRepository<User> {
|
|
55
|
+
* constructor(private db: DatabaseService) {
|
|
56
|
+
* super(db, Tables.USERS);
|
|
57
|
+
* }
|
|
58
|
+
*
|
|
59
|
+
* async findByEmail(email: string) {
|
|
60
|
+
* return this.db.query(Tables.USERS, {
|
|
61
|
+
* filter: { field: 'email', operator: 'eq', value: email }
|
|
62
|
+
* });
|
|
63
|
+
* }
|
|
64
|
+
* }
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ### Per-Operation Configuration
|
|
69
|
+
* ```typescript
|
|
70
|
+
* // Admin view - include soft-deleted records
|
|
71
|
+
* const allUsers = await db.list(Tables.USERS, {}, {
|
|
72
|
+
* includeSoftDeleted: true,
|
|
73
|
+
* forceAdapter: 'primary',
|
|
74
|
+
* cache: { enabled: false }
|
|
75
|
+
* });
|
|
76
|
+
*
|
|
77
|
+
* // Bulk import - skip audit and caching
|
|
78
|
+
* const result = await db.batchCreate(Tables.USERS, users, {
|
|
79
|
+
* skipAudit: true,
|
|
80
|
+
* timestamps: { enabled: false },
|
|
81
|
+
* cache: { enabled: false }
|
|
82
|
+
* });
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export interface DatabaseServiceInterface {
|
|
86
|
+
/**
|
|
87
|
+
* Get a single record by ID
|
|
88
|
+
* @param table - Table name
|
|
89
|
+
* @param id - Record ID
|
|
90
|
+
* @param config - Optional per-operation configuration overrides
|
|
91
|
+
* @returns Promise resolving to the record or null
|
|
92
|
+
*/
|
|
93
|
+
get<T>(table: TableName, id: string, config?: OperationConfig): Promise<DatabaseResult<T | null>>;
|
|
94
|
+
/**
|
|
95
|
+
* List records with optional filtering, sorting, and pagination
|
|
96
|
+
* @param table - Table name
|
|
97
|
+
* @param options - Query options
|
|
98
|
+
* @param config - Optional per-operation configuration overrides
|
|
99
|
+
* @returns Promise resolving to paginated results
|
|
100
|
+
*/
|
|
101
|
+
list<T>(table: TableName, options?: QueryOptions, config?: OperationConfig): Promise<DatabaseResult<PaginatedResult<T>>>;
|
|
102
|
+
/**
|
|
103
|
+
* Create a new record
|
|
104
|
+
* @param table - Table name
|
|
105
|
+
* @param input - Record data to create
|
|
106
|
+
* @param config - Optional per-operation configuration overrides
|
|
107
|
+
* @returns Promise resolving to the created record
|
|
108
|
+
*/
|
|
109
|
+
create<T>(table: TableName, input: CreateInput<T>, config?: OperationConfig): Promise<DatabaseResult<T>>;
|
|
110
|
+
/**
|
|
111
|
+
* Update an existing record
|
|
112
|
+
* @param table - Table name
|
|
113
|
+
* @param id - Record ID
|
|
114
|
+
* @param input - Updated fields
|
|
115
|
+
* @param config - Optional per-operation configuration overrides
|
|
116
|
+
* @returns Promise resolving to the updated record
|
|
117
|
+
*/
|
|
118
|
+
update<T>(table: TableName, id: string, input: UpdateInput<T>, config?: OperationConfig): Promise<DatabaseResult<T>>;
|
|
119
|
+
/**
|
|
120
|
+
* Delete a record
|
|
121
|
+
* @param table - Table name
|
|
122
|
+
* @param id - Record ID
|
|
123
|
+
* @param config - Optional per-operation configuration overrides
|
|
124
|
+
* @returns Promise resolving to void on success
|
|
125
|
+
*/
|
|
126
|
+
delete(table: TableName, id: string, config?: OperationConfig): Promise<DatabaseResult<void>>;
|
|
127
|
+
/**
|
|
128
|
+
* Create multiple records in a batch
|
|
129
|
+
* @param table - Table name
|
|
130
|
+
* @param inputs - Array of records to create
|
|
131
|
+
* @param config - Optional per-operation configuration overrides
|
|
132
|
+
* @returns Promise resolving to array of created records
|
|
133
|
+
*/
|
|
134
|
+
batchCreate<T>(table: TableName, inputs: CreateInput<T>[], config?: OperationConfig): Promise<DatabaseResult<T[]>>;
|
|
135
|
+
/**
|
|
136
|
+
* Update multiple records in a batch
|
|
137
|
+
* @param table - Table name
|
|
138
|
+
* @param updates - Array of update operations
|
|
139
|
+
* @param config - Optional per-operation configuration overrides
|
|
140
|
+
* @returns Promise resolving to array of updated records
|
|
141
|
+
*/
|
|
142
|
+
batchUpdate<T>(table: TableName, updates: BatchUpdate<T>[], config?: OperationConfig): Promise<DatabaseResult<T[]>>;
|
|
143
|
+
/**
|
|
144
|
+
* Delete multiple records in a batch
|
|
145
|
+
* @param table - Table name
|
|
146
|
+
* @param ids - Array of record IDs to delete
|
|
147
|
+
* @param config - Optional per-operation configuration overrides
|
|
148
|
+
* @returns Promise resolving to void on success
|
|
149
|
+
*/
|
|
150
|
+
batchDelete(table: TableName, ids: string[], config?: OperationConfig): Promise<DatabaseResult<void>>;
|
|
151
|
+
/**
|
|
152
|
+
* Execute a complex query with filters, sorting, and pagination
|
|
153
|
+
* @param table - Table name
|
|
154
|
+
* @param query - Query configuration
|
|
155
|
+
* @param config - Optional per-operation configuration overrides
|
|
156
|
+
* @returns Promise resolving to paginated results
|
|
157
|
+
*/
|
|
158
|
+
query<T>(table: TableName, query: QueryOptions, config?: OperationConfig): Promise<DatabaseResult<PaginatedResult<T>>>;
|
|
159
|
+
/**
|
|
160
|
+
* Count records matching a filter
|
|
161
|
+
* @param table - Table name
|
|
162
|
+
* @param filter - Optional filter conditions
|
|
163
|
+
* @param config - Optional per-operation configuration overrides
|
|
164
|
+
* @returns Promise resolving to the count
|
|
165
|
+
*/
|
|
166
|
+
count(table: TableName, filter?: Filter, config?: OperationConfig): Promise<DatabaseResult<number>>;
|
|
167
|
+
/**
|
|
168
|
+
* Find a single record by filter conditions
|
|
169
|
+
* @param table - Table name
|
|
170
|
+
* @param filter - Filter conditions
|
|
171
|
+
* @param config - Optional per-operation configuration overrides
|
|
172
|
+
* @returns Promise resolving to the first matching record or null
|
|
173
|
+
*/
|
|
174
|
+
findOne<T>(table: TableName, filter: Filter, config?: OperationConfig): Promise<DatabaseResult<T | null>>;
|
|
175
|
+
/**
|
|
176
|
+
* Soft delete a record (logical deletion)
|
|
177
|
+
* @param table - Table name
|
|
178
|
+
* @param id - Record ID
|
|
179
|
+
* @param config - Optional per-operation configuration overrides
|
|
180
|
+
* @returns Promise resolving to void on success
|
|
181
|
+
*/
|
|
182
|
+
softDelete(table: TableName, id: string, config?: OperationConfig): Promise<DatabaseResult<void>>;
|
|
183
|
+
/**
|
|
184
|
+
* Execute operations within a transaction
|
|
185
|
+
* @param fn - Function containing transactional operations
|
|
186
|
+
* @param options - Optional transaction configuration
|
|
187
|
+
* @returns Promise resolving to the transaction result
|
|
188
|
+
*/
|
|
189
|
+
transaction<T>(fn: TransactionFn<T>, options?: TransactionOptions): Promise<DatabaseResult<T>>;
|
|
190
|
+
/**
|
|
191
|
+
* Set audit context for tracking user actions
|
|
192
|
+
* @param context - Audit context information
|
|
193
|
+
* @returns Promise resolving to void on success
|
|
194
|
+
*/
|
|
195
|
+
setAuditContext(context: AuditContext): Promise<DatabaseResult<void>>;
|
|
196
|
+
/**
|
|
197
|
+
* Subscribe to database events
|
|
198
|
+
* @param event - Event type to listen for
|
|
199
|
+
* @param handler - Event handler function
|
|
200
|
+
*/
|
|
201
|
+
on<T extends DatabaseEvent['type']>(event: T, handler: (event: Extract<DatabaseEvent, {
|
|
202
|
+
type: T;
|
|
203
|
+
}>) => void | Promise<void>): void;
|
|
204
|
+
/**
|
|
205
|
+
* Unsubscribe from database events
|
|
206
|
+
* @param event - Event type to stop listening for
|
|
207
|
+
* @param handler - Event handler function to remove
|
|
208
|
+
*/
|
|
209
|
+
off<T extends DatabaseEvent['type']>(event: T, handler: (event: Extract<DatabaseEvent, {
|
|
210
|
+
type: T;
|
|
211
|
+
}>) => void | Promise<void>): void;
|
|
212
|
+
/**
|
|
213
|
+
* Perform health check on the database connection
|
|
214
|
+
* @returns Promise resolving to health status
|
|
215
|
+
*/
|
|
216
|
+
healthCheck(): Promise<DatabaseResult<DatabaseHealthStatus>>;
|
|
217
|
+
/**
|
|
218
|
+
* Get current service status
|
|
219
|
+
* @returns Current service status information
|
|
220
|
+
*/
|
|
221
|
+
getStatus(): ServiceStatus;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Legacy interface for backward compatibility
|
|
225
|
+
* @deprecated Use DatabaseService instead
|
|
226
|
+
*/
|
|
227
|
+
export interface DatabaseServiceType extends DatabaseServiceInterface {
|
|
228
|
+
/**
|
|
229
|
+
* @deprecated Use get() instead
|
|
230
|
+
*/
|
|
231
|
+
findById<T>(table: string, id: string): Promise<DatabaseResult<T | null>>;
|
|
232
|
+
/**
|
|
233
|
+
* @deprecated Use list() instead
|
|
234
|
+
*/
|
|
235
|
+
findMany<T>(table: string, options?: QueryOptions): Promise<DatabaseResult<PaginatedResult<T>>>;
|
|
236
|
+
/**
|
|
237
|
+
* Initialize the database service
|
|
238
|
+
* @deprecated Initialization is handled by factory
|
|
239
|
+
*/
|
|
240
|
+
initialize(): Promise<DatabaseResult<void>>;
|
|
241
|
+
/**
|
|
242
|
+
* Register a table with the adapter
|
|
243
|
+
* @deprecated Tables are registered automatically
|
|
244
|
+
*/
|
|
245
|
+
registerTable<T>(name: string, table: T, idColumn?: keyof T): void;
|
|
246
|
+
/**
|
|
247
|
+
* Check if a record exists
|
|
248
|
+
* @deprecated Use count() with filter instead
|
|
249
|
+
*/
|
|
250
|
+
exists(table: string, id: string): Promise<DatabaseResult<boolean>>;
|
|
251
|
+
/**
|
|
252
|
+
* Find a single record by filter conditions
|
|
253
|
+
* @deprecated Use findOne() from main interface instead
|
|
254
|
+
*/
|
|
255
|
+
findOne<T>(table: string, filter: Filter): Promise<DatabaseResult<T | null>>;
|
|
256
|
+
/**
|
|
257
|
+
* Soft delete a record (logical deletion)
|
|
258
|
+
* @deprecated Use softDelete() from main interface instead
|
|
259
|
+
*/
|
|
260
|
+
softDelete(table: string, id: string): Promise<DatabaseResult<void>>;
|
|
261
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
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 { ContextType } from '@nestjs/common';
|
|
7
|
+
/**
|
|
8
|
+
* Emit a query error event
|
|
9
|
+
*/
|
|
10
|
+
interface EmitQueryErrorOptions {
|
|
11
|
+
table: string;
|
|
12
|
+
operation: DatabaseOperationType;
|
|
13
|
+
error: Error;
|
|
14
|
+
params?: Record<string, object>;
|
|
15
|
+
context?: ContextType;
|
|
16
|
+
}
|
|
17
|
+
export interface DatabaseError extends Error {
|
|
18
|
+
code?: string;
|
|
19
|
+
status?: number;
|
|
20
|
+
cause?: Error;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Interface for database event emission
|
|
24
|
+
*/
|
|
25
|
+
export interface DatabaseEventEmitterType {
|
|
26
|
+
/**
|
|
27
|
+
* Register an event handler for a specific event type
|
|
28
|
+
*/
|
|
29
|
+
on<T extends DatabaseEvent>(eventType: T['type'], handler: DBEventHandler<T>): void;
|
|
30
|
+
/**
|
|
31
|
+
* Remove an event handler for a specific event type
|
|
32
|
+
*/
|
|
33
|
+
off<T extends DatabaseEvent>(eventType: T['type'], handler: DBEventHandler<T>): void;
|
|
34
|
+
/**
|
|
35
|
+
* Emit an event to all registered handlers
|
|
36
|
+
*/
|
|
37
|
+
emit<T extends DatabaseEvent>(event: T): void;
|
|
38
|
+
/**
|
|
39
|
+
* Emit a before query event
|
|
40
|
+
*/
|
|
41
|
+
emitBeforeQuery<T extends object = object>(table: string, operation: DatabaseOperationType, params?: Partial<T>, context?: Record<string, string | number | boolean | null>): void;
|
|
42
|
+
/**
|
|
43
|
+
* Emit an after query event
|
|
44
|
+
*/
|
|
45
|
+
emitAfterQuery(table: string, operation: DatabaseOperationType, duration: number, affectedRows?: number): void;
|
|
46
|
+
/**
|
|
47
|
+
* Emit a query error event
|
|
48
|
+
*/
|
|
49
|
+
emitQueryError<T extends object = object>(table: string, operation: DatabaseOperationType, error: DatabaseError, params?: Partial<T>, context?: Record<string, string | number | boolean | null>): void;
|
|
50
|
+
/**
|
|
51
|
+
* Emit a before transaction event
|
|
52
|
+
*/
|
|
53
|
+
emitBeforeTransaction(transactionId: string, context?: Record<string, string | number | boolean | null>): void;
|
|
54
|
+
/**
|
|
55
|
+
* Emit an after transaction event
|
|
56
|
+
*/
|
|
57
|
+
emitAfterTransaction(transactionId: string, duration: number): void;
|
|
58
|
+
/**
|
|
59
|
+
* Emit a transaction rollback event
|
|
60
|
+
*/
|
|
61
|
+
emitTransactionRollback(transactionId: string, error?: DatabaseError): void;
|
|
62
|
+
/**
|
|
63
|
+
* Emit a health change event
|
|
64
|
+
*/
|
|
65
|
+
emitHealthChange<TDetails extends object = object>(previousStatus: boolean, currentStatus: boolean, details?: TDetails): void;
|
|
66
|
+
emitQueryError(options: EmitQueryErrorOptions): void;
|
|
67
|
+
}
|
|
68
|
+
export {};
|
package/dist/db/index.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
export type * from './databaseService';
|
|
2
|
+
export type * from './eventEmitter';
|
|
3
|
+
export type * from './config.types';
|
|
4
|
+
export type * from './databaseAdapter';
|
|
1
5
|
export type * from './features-config.types';
|
|
2
6
|
export type * from './database.types';
|
|
3
7
|
export * from './adapter';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { DatabaseAdapterType } from "./databaseAdapter";
|
|
2
2
|
import type { REPLICA_STRATEGY } from "./replicaStrategy";
|
|
3
3
|
/**
|
|
4
4
|
* Options for the @UseReplica decorator.
|
|
@@ -18,7 +18,7 @@ export interface ReadReplicaConfig {
|
|
|
18
18
|
/** Whether read replicas are enabled */
|
|
19
19
|
enabled: boolean;
|
|
20
20
|
/** Replica connection configs */
|
|
21
|
-
replicas:
|
|
21
|
+
replicas: DatabaseAdapterType[];
|
|
22
22
|
/** Load balancing strategy */
|
|
23
23
|
strategy?: 'round-robin' | 'random' | 'least-connections';
|
|
24
24
|
/** Fallback to primary if all replicas unhealthy */
|
package/dist/errors/index.cjs
CHANGED
|
@@ -4410,7 +4410,7 @@ var coerce = {
|
|
|
4410
4410
|
};
|
|
4411
4411
|
var NEVER = INVALID;
|
|
4412
4412
|
|
|
4413
|
-
// node_modules/.pnpm/@plyaz+types@1.13.
|
|
4413
|
+
// node_modules/.pnpm/@plyaz+types@1.13.12_@types+react@19.2.2_next@15.4.7_react-dom@19.2.0_react@19.2.0/node_modules/@plyaz/types/dist/index.js
|
|
4414
4414
|
var __defProp2 = Object.defineProperty;
|
|
4415
4415
|
var __name2 = /* @__PURE__ */ __name((target, value) => __defProp2(target, "name", { value, configurable: true }), "__name");
|
|
4416
4416
|
external_exports.object({
|