@plyaz/types 1.12.4 → 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/api/client/types.d.ts +60 -1
- package/dist/api/endpoints/campaigns/types.d.ts +14 -0
- package/dist/api/endpoints/index.d.ts +1 -0
- package/dist/api/endpoints/infobip/endpoints.d.ts +115 -0
- package/dist/api/endpoints/infobip/generated/email.generated.d.ts +5384 -0
- package/dist/api/endpoints/infobip/index.d.ts +2 -0
- package/dist/api/endpoints/infobip/types.d.ts +265 -0
- package/dist/api/endpoints/types.d.ts +2 -1
- package/dist/api/errors/types.d.ts +2 -1
- package/dist/api/hooks/types.d.ts +85 -5
- package/dist/api/index.cjs +1 -1
- package/dist/api/index.cjs.map +1 -1
- package/dist/api/index.d.ts +1 -0
- 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.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js.map +1 -1
- package/dist/payments/index.cjs.map +1 -1
- package/dist/payments/index.js.map +1 -1
- package/package.json +9 -3
|
@@ -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,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Core database types and utilities
|
|
3
|
+
*
|
|
4
|
+
* Provides fundamental type definitions and utility functions used throughout
|
|
5
|
+
* the database package. These types ensure type safety, consistent data structures,
|
|
6
|
+
* and standardized error handling across all database operations and adapters.
|
|
7
|
+
*
|
|
8
|
+
* **Type Categories:**
|
|
9
|
+
* - **Input Types**: CreateInput, UpdateInput for data manipulation
|
|
10
|
+
* - **Query Types**: QueryOptions, Filter, SortOptions for data retrieval
|
|
11
|
+
* - **Result Types**: DatabaseResult, PaginatedResult for operation results
|
|
12
|
+
* - **Utility Types**: HealthStatus for monitoring
|
|
13
|
+
* - **Error Types**: DatabaseError for standardized error handling
|
|
14
|
+
*
|
|
15
|
+
* **Application Flow Context:**
|
|
16
|
+
* ```
|
|
17
|
+
* Application Data → Input Types → Database Operations → Result Types
|
|
18
|
+
* ↓ ↓ ↓ ↓
|
|
19
|
+
* User Input → Validation → Adapter Methods → Type-Safe Results
|
|
20
|
+
* Query Params → Type Safety → SQL Generation → Error Handling
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* **Key Features:**
|
|
24
|
+
* - **Type Safety**: Compile-time validation of data structures
|
|
25
|
+
* - **Result Pattern**: Consistent success/failure result handling
|
|
26
|
+
* - **Generic Support**: Flexible types for different data models
|
|
27
|
+
* - **Error Standardization**: Unified error handling across adapters
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* // Type-safe database operations
|
|
32
|
+
* interface User {
|
|
33
|
+
* id: string;
|
|
34
|
+
* name: string;
|
|
35
|
+
* email: string;
|
|
36
|
+
* createdAt: Date;
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* // Input types automatically exclude system fields
|
|
40
|
+
* const createData: CreateInput<User> = {
|
|
41
|
+
* name: 'John Doe',
|
|
42
|
+
* email: 'john@example.com'
|
|
43
|
+
* // id, createdAt automatically excluded
|
|
44
|
+
* };
|
|
45
|
+
*
|
|
46
|
+
* // Result pattern for consistent error handling
|
|
47
|
+
* const result = await service.create('users', createData);
|
|
48
|
+
* if (result.success) {
|
|
49
|
+
* console.log('Created user:', result.value);
|
|
50
|
+
* } else {
|
|
51
|
+
* console.error('Error:', result.error.message);
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @author Plyaz Engineering Team
|
|
56
|
+
* @since 1.0.0
|
|
57
|
+
*/
|
|
58
|
+
/**
|
|
59
|
+
* Input type for creating records
|
|
60
|
+
*/
|
|
61
|
+
export type CreateInput<T> = Omit<T, 'id' | 'createdAt' | 'updatedAt' | 'deletedAt'>;
|
|
62
|
+
/**
|
|
63
|
+
* Input type for updating records
|
|
64
|
+
*/
|
|
65
|
+
export type UpdateInput<T> = Partial<Omit<T, 'id' | 'createdAt'>>;
|
|
66
|
+
/**
|
|
67
|
+
* Batch update operation
|
|
68
|
+
*/
|
|
69
|
+
export interface BatchUpdate<T> {
|
|
70
|
+
/** Record ID to update */
|
|
71
|
+
id: string;
|
|
72
|
+
/** Fields to update */
|
|
73
|
+
data: UpdateInput<T>;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Query options for findMany operations
|
|
77
|
+
*/
|
|
78
|
+
export interface QueryOptions<TRecord extends object = object> {
|
|
79
|
+
/** Filter conditions */
|
|
80
|
+
filter?: Filter<TRecord>;
|
|
81
|
+
/** Sorting options */
|
|
82
|
+
sort?: SortOptions<TRecord>[];
|
|
83
|
+
/** Pagination options */
|
|
84
|
+
pagination?: PaginationOptions;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Filter conditions for queries
|
|
88
|
+
*/
|
|
89
|
+
export interface Filter<TRecord extends object = object> {
|
|
90
|
+
/** Field to filter on */
|
|
91
|
+
field: keyof TRecord & string;
|
|
92
|
+
/** Filter operator */
|
|
93
|
+
operator: 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte' | 'in' | 'notIn' | 'like' | 'between' | 'isNull' | 'isNotNull';
|
|
94
|
+
/** Value to compare against */
|
|
95
|
+
value: TRecord[keyof TRecord];
|
|
96
|
+
/** Logical operator to combine with other filters */
|
|
97
|
+
logical?: 'and' | 'or' | 'not';
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Sorting options for queries
|
|
101
|
+
*/
|
|
102
|
+
export interface SortOptions<TRecord extends object = object> {
|
|
103
|
+
/** Field to sort by */
|
|
104
|
+
field: keyof TRecord & string;
|
|
105
|
+
/** Sort direction */
|
|
106
|
+
direction: 'asc' | 'desc';
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Pagination options for queries
|
|
110
|
+
*/
|
|
111
|
+
export interface PaginationOptions {
|
|
112
|
+
/** Number of items to return */
|
|
113
|
+
limit?: number;
|
|
114
|
+
/** Number of items to skip */
|
|
115
|
+
offset?: number;
|
|
116
|
+
/** Cursor for cursor-based pagination */
|
|
117
|
+
cursor?: string;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Paginated result type
|
|
121
|
+
*/
|
|
122
|
+
export interface PaginatedResult<T> {
|
|
123
|
+
/** Array of results */
|
|
124
|
+
data: T[];
|
|
125
|
+
/** Total count of items */
|
|
126
|
+
total: number;
|
|
127
|
+
/** Pagination metadata */
|
|
128
|
+
pagination: {
|
|
129
|
+
/** Current page number */
|
|
130
|
+
page?: number;
|
|
131
|
+
/** Number of items per page */
|
|
132
|
+
limit?: number;
|
|
133
|
+
/** Total number of pages */
|
|
134
|
+
totalPages?: number;
|
|
135
|
+
/** Next cursor for cursor-based pagination */
|
|
136
|
+
nextCursor?: string;
|
|
137
|
+
/** Previous cursor for cursor-based pagination */
|
|
138
|
+
prevCursor?: string;
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Database health status
|
|
143
|
+
*/
|
|
144
|
+
export interface HealthStatus<TDetails extends object = object> {
|
|
145
|
+
/** Whether the database is healthy */
|
|
146
|
+
isHealthy: boolean;
|
|
147
|
+
/** Response time in milliseconds */
|
|
148
|
+
responseTime: number;
|
|
149
|
+
/** Additional health details */
|
|
150
|
+
details?: TDetails;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Database result type that wraps either a successful value or an error
|
|
154
|
+
*/
|
|
155
|
+
export interface DatabaseResult<T> {
|
|
156
|
+
/** Whether the operation was successful */
|
|
157
|
+
success: boolean;
|
|
158
|
+
/** The result value if successful */
|
|
159
|
+
value?: T;
|
|
160
|
+
/** The error if unsuccessful */
|
|
161
|
+
error?: Error;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Helper function to create a successful database result
|
|
165
|
+
*/
|
|
166
|
+
export declare function success<T>(value: T): DatabaseResult<T>;
|
|
167
|
+
/**
|
|
168
|
+
* Helper function to create a failed database result
|
|
169
|
+
*/
|
|
170
|
+
export declare function failure<T>(error: Error): DatabaseResult<T>;
|
|
171
|
+
export declare class DatabaseError extends Error {
|
|
172
|
+
code?: string;
|
|
173
|
+
status?: number;
|
|
174
|
+
cause?: Error;
|
|
175
|
+
constructor(message: string, cause?: Error);
|
|
176
|
+
}
|
|
177
|
+
export declare class BaseError extends DatabaseError {
|
|
178
|
+
code: string;
|
|
179
|
+
status: number;
|
|
180
|
+
constructor(code: string, status: number, message: string);
|
|
181
|
+
}
|
|
182
|
+
export declare class ValidationError extends DatabaseError {
|
|
183
|
+
code: string;
|
|
184
|
+
status: number;
|
|
185
|
+
constructor(code: string, status: number, message: string);
|
|
186
|
+
}
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Enhanced configuration types for database service
|
|
3
|
+
*
|
|
4
|
+
* Provides comprehensive, strictly-typed configuration interfaces for all
|
|
5
|
+
* database extensions and features. These types enable type-safe configuration
|
|
6
|
+
* of the entire database system, from basic adapter settings to advanced
|
|
7
|
+
* features like encryption, audit logging, and caching.
|
|
8
|
+
*
|
|
9
|
+
* **Configuration Hierarchy:**
|
|
10
|
+
* ```
|
|
11
|
+
* DatabaseServiceConfig (Root)
|
|
12
|
+
* ↓
|
|
13
|
+
* ├── AdapterConfig (Database Connection)
|
|
14
|
+
* ├── ExtensionConfigs (Features)
|
|
15
|
+
* │ ├── SoftDeleteConfig
|
|
16
|
+
* │ ├── DBEncryptionConfig
|
|
17
|
+
* │ ├── AuditConfig
|
|
18
|
+
* │ └── DBCacheConfig
|
|
19
|
+
* └── OperationConfig (Per-Operation Overrides)
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* **Configuration Features:**
|
|
23
|
+
* - **Type Safety**: Compile-time validation of all configuration options
|
|
24
|
+
* - **Hierarchical Overrides**: Global config with per-operation overrides
|
|
25
|
+
* - **Extension Integration**: Seamless configuration for all extensions
|
|
26
|
+
* - **Event Handling**: Type-safe event handler configuration
|
|
27
|
+
* - **Adapter Flexibility**: Support for multiple database adapters
|
|
28
|
+
*
|
|
29
|
+
* **Usage Patterns:**
|
|
30
|
+
* - Global configuration for application-wide settings
|
|
31
|
+
* - Per-operation configuration for specific requirements
|
|
32
|
+
* - Extension-specific configuration for feature customization
|
|
33
|
+
* - Event handler configuration for monitoring and logging
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* // Complete database service configuration
|
|
38
|
+
* const config: DatabaseServiceConfig = {
|
|
39
|
+
* adapter: 'drizzle',
|
|
40
|
+
* config: {
|
|
41
|
+
* connectionString: process.env.DATABASE_URL,
|
|
42
|
+
* poolSize: 10
|
|
43
|
+
* },
|
|
44
|
+
* encryption: {
|
|
45
|
+
* enabled: true,
|
|
46
|
+
* key: process.env.ENCRYPTION_KEY,
|
|
47
|
+
* fields: {
|
|
48
|
+
* users: ['ssn', 'taxId'],
|
|
49
|
+
* payments: ['cardNumber', 'cvv']
|
|
50
|
+
* }
|
|
51
|
+
* },
|
|
52
|
+
* audit: {
|
|
53
|
+
* enabled: true,
|
|
54
|
+
* retentionDays: 90,
|
|
55
|
+
* excludeTables: ['sessions', 'logs']
|
|
56
|
+
* },
|
|
57
|
+
* events: {
|
|
58
|
+
* onAfterWrite: async (event) => {
|
|
59
|
+
* console.log(`${event.operation} completed on ${event.table}`);
|
|
60
|
+
* }
|
|
61
|
+
* }
|
|
62
|
+
* };
|
|
63
|
+
*
|
|
64
|
+
* // Per-operation configuration override
|
|
65
|
+
* const operationConfig: OperationConfig = {
|
|
66
|
+
* skipAudit: true,
|
|
67
|
+
* cache: { ttl: 600 },
|
|
68
|
+
* timeout: 30000
|
|
69
|
+
* };
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
/**
|
|
73
|
+
* Configuration for soft delete functionality
|
|
74
|
+
*/
|
|
75
|
+
export interface SoftDeleteConfig {
|
|
76
|
+
/** Whether soft delete is enabled */
|
|
77
|
+
enabled: boolean;
|
|
78
|
+
/** Field name for soft delete timestamp (default: 'deletedAt') */
|
|
79
|
+
field?: string;
|
|
80
|
+
/** Tables to exclude from soft delete behavior */
|
|
81
|
+
excludeTables?: string[];
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Configuration for field-level encryption
|
|
85
|
+
*/
|
|
86
|
+
export interface DBEncryptionConfig {
|
|
87
|
+
/** Whether encryption is enabled */
|
|
88
|
+
enabled: boolean;
|
|
89
|
+
/** Base64 encoded encryption key */
|
|
90
|
+
key: string;
|
|
91
|
+
/** Fields to encrypt per table */
|
|
92
|
+
fields: Record<string, string[]>;
|
|
93
|
+
/** Encryption algorithm (default: 'aes-256-gcm') */
|
|
94
|
+
algorithm?: 'aes-256-gcm' | 'aes-256-cbc';
|
|
95
|
+
/** Use database-native encryption when available */
|
|
96
|
+
useDatabaseNative?: boolean;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Configuration for audit logging
|
|
100
|
+
*/
|
|
101
|
+
export interface AuditConfig {
|
|
102
|
+
/** Whether audit logging is enabled */
|
|
103
|
+
enabled: boolean;
|
|
104
|
+
/** Number of days to retain audit logs (default: 90) */
|
|
105
|
+
retentionDays?: number;
|
|
106
|
+
/** Fields to exclude from audit logs */
|
|
107
|
+
excludeFields?: string[];
|
|
108
|
+
/** Tables to exclude from audit logging */
|
|
109
|
+
excludeTables?: string[];
|
|
110
|
+
/** Custom audit event handler */
|
|
111
|
+
onAuditAfterWrite?: (event: AuditEvent) => void | Promise<void>;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Configuration for automatic timestamps
|
|
115
|
+
*/
|
|
116
|
+
export interface TimestampsConfig {
|
|
117
|
+
/** Whether automatic timestamps are enabled */
|
|
118
|
+
enabled: boolean;
|
|
119
|
+
/** Field name for creation timestamp (default: 'createdAt') */
|
|
120
|
+
createdAtField?: string;
|
|
121
|
+
/** Field name for update timestamp (default: 'updatedAt') */
|
|
122
|
+
updatedAtField?: string;
|
|
123
|
+
/** Automatically update timestamp on record changes */
|
|
124
|
+
autoUpdate?: boolean;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Configuration for query result caching
|
|
128
|
+
*/
|
|
129
|
+
export interface DBCacheConfig {
|
|
130
|
+
/** Whether caching is enabled */
|
|
131
|
+
enabled: boolean;
|
|
132
|
+
/** Default TTL in seconds (default: 300) */
|
|
133
|
+
ttl?: number;
|
|
134
|
+
/** Cache provider ('redis' | 'memory') */
|
|
135
|
+
provider?: string;
|
|
136
|
+
/** Cache invalidation strategy */
|
|
137
|
+
invalidation?: 'write' | 'ttl' | 'manual';
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Configuration for read replicas
|
|
141
|
+
*/
|
|
142
|
+
export interface ReadReplicaConfig {
|
|
143
|
+
/** Whether read replicas are enabled */
|
|
144
|
+
enabled: boolean;
|
|
145
|
+
/** Replica connection configs */
|
|
146
|
+
replicas: AdapterConfig[];
|
|
147
|
+
/** Load balancing strategy */
|
|
148
|
+
strategy?: 'round-robin' | 'random' | 'least-connections';
|
|
149
|
+
/** Fallback to primary if all replicas unhealthy */
|
|
150
|
+
fallbackToPrimary?: boolean;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Connection pool configuration
|
|
154
|
+
*/
|
|
155
|
+
export interface PoolConfig {
|
|
156
|
+
/** Minimum number of connections */
|
|
157
|
+
min?: number;
|
|
158
|
+
/** Maximum number of connections */
|
|
159
|
+
max?: number;
|
|
160
|
+
/** Idle connection timeout in milliseconds */
|
|
161
|
+
idleTimeoutMs?: number;
|
|
162
|
+
/** Connection acquisition timeout in milliseconds */
|
|
163
|
+
acquireTimeoutMs?: number;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Database event handlers
|
|
167
|
+
*/
|
|
168
|
+
export interface DatabaseEvents {
|
|
169
|
+
/** Called before any write operation */
|
|
170
|
+
onBeforeWrite?: (event: BeforeWriteEvent) => void | Promise<void>;
|
|
171
|
+
/** Called after successful write operation */
|
|
172
|
+
onAfterWrite?: (event: AfterWriteEvent) => void | Promise<void>;
|
|
173
|
+
/** Called before any read operation */
|
|
174
|
+
onBeforeRead?: (event: BeforeReadEvent) => void | Promise<void>;
|
|
175
|
+
/** Called after successful read operation */
|
|
176
|
+
onAfterRead?: (event: AfterReadEvent) => void | Promise<void>;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Event types for database operations
|
|
180
|
+
*/
|
|
181
|
+
export interface BeforeWriteEvent {
|
|
182
|
+
type: 'beforeWrite';
|
|
183
|
+
operation: 'CREATE' | 'UPDATE' | 'DELETE';
|
|
184
|
+
table: string;
|
|
185
|
+
data: Record<string, string | number | boolean | Date>;
|
|
186
|
+
timestamp: Date;
|
|
187
|
+
}
|
|
188
|
+
export interface AfterWriteEvent {
|
|
189
|
+
type: 'afterWrite';
|
|
190
|
+
operation: 'CREATE' | 'UPDATE' | 'DELETE';
|
|
191
|
+
table: string;
|
|
192
|
+
result: Record<string, string | number | boolean | Date>;
|
|
193
|
+
duration: number;
|
|
194
|
+
timestamp: Date;
|
|
195
|
+
}
|
|
196
|
+
export interface BeforeReadEvent {
|
|
197
|
+
type: 'beforeRead';
|
|
198
|
+
operation: 'READ';
|
|
199
|
+
table: string;
|
|
200
|
+
timestamp: Date;
|
|
201
|
+
}
|
|
202
|
+
export interface AfterReadEvent {
|
|
203
|
+
type: 'afterRead';
|
|
204
|
+
operation: 'READ';
|
|
205
|
+
table: string;
|
|
206
|
+
result: Record<string, string | number | boolean | Date>;
|
|
207
|
+
duration: number;
|
|
208
|
+
timestamp: Date;
|
|
209
|
+
}
|
|
210
|
+
export interface AuditEvent {
|
|
211
|
+
operation: string;
|
|
212
|
+
table: string;
|
|
213
|
+
recordId?: string;
|
|
214
|
+
userId?: string;
|
|
215
|
+
requestId?: string;
|
|
216
|
+
changes: {
|
|
217
|
+
before?: Record<string, string | number | boolean | Date>;
|
|
218
|
+
after?: Record<string, string | number | boolean | Date>;
|
|
219
|
+
fields?: string[];
|
|
220
|
+
};
|
|
221
|
+
timestamp: Date;
|
|
222
|
+
ipAddress?: string;
|
|
223
|
+
userAgent?: string;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Adapter-specific configuration types
|
|
227
|
+
*/
|
|
228
|
+
export interface DrizzleConfig {
|
|
229
|
+
connectionString?: string;
|
|
230
|
+
url?: string;
|
|
231
|
+
poolSize?: number;
|
|
232
|
+
ssl?: boolean;
|
|
233
|
+
}
|
|
234
|
+
export interface SupabaseConfig {
|
|
235
|
+
supabaseUrl: string;
|
|
236
|
+
supabaseAnonKey: string;
|
|
237
|
+
supabaseServiceKey?: string;
|
|
238
|
+
schema?: string;
|
|
239
|
+
}
|
|
240
|
+
export interface SqlConfig {
|
|
241
|
+
connectionString?: string;
|
|
242
|
+
url?: string;
|
|
243
|
+
dialect?: 'postgresql' | 'mysql' | 'sqlite';
|
|
244
|
+
}
|
|
245
|
+
export interface MockConfig {
|
|
246
|
+
logging?: boolean;
|
|
247
|
+
initialData?: Record<string, Record<string, string | number | boolean | Date>[]>;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Union type for adapter configurations
|
|
251
|
+
*/
|
|
252
|
+
export type AdapterConfig = DrizzleConfig | SupabaseConfig | SqlConfig | MockConfig;
|
|
253
|
+
/**
|
|
254
|
+
* Per-operation configuration overrides
|
|
255
|
+
*/
|
|
256
|
+
export interface OperationConfig {
|
|
257
|
+
/** Override soft delete settings for this operation */
|
|
258
|
+
softDelete?: Partial<SoftDeleteConfig>;
|
|
259
|
+
/** Override encryption settings for this operation */
|
|
260
|
+
encryption?: Partial<DBEncryptionConfig>;
|
|
261
|
+
/** Skip audit logging for this operation */
|
|
262
|
+
skipAudit?: boolean;
|
|
263
|
+
/** Override cache settings for this operation */
|
|
264
|
+
cache?: Partial<DBCacheConfig>;
|
|
265
|
+
/** Force specific adapter usage */
|
|
266
|
+
forceAdapter?: 'primary' | 'replica';
|
|
267
|
+
/** Include soft-deleted records in results */
|
|
268
|
+
includeSoftDeleted?: boolean;
|
|
269
|
+
/** Override query timeout for this operation */
|
|
270
|
+
timeout?: number;
|
|
271
|
+
/** Override timestamp settings for this operation */
|
|
272
|
+
timestamps?: Partial<TimestampsConfig>;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Complete database service configuration
|
|
276
|
+
*/
|
|
277
|
+
export interface DatabaseServiceConfig {
|
|
278
|
+
/** Database adapter type */
|
|
279
|
+
adapter: 'drizzle' | 'supabase' | 'sql' | 'mock';
|
|
280
|
+
/** Adapter-specific configuration */
|
|
281
|
+
config: AdapterConfig;
|
|
282
|
+
/** Connection pool settings */
|
|
283
|
+
pool?: PoolConfig;
|
|
284
|
+
/** Encryption configuration */
|
|
285
|
+
encryption?: DBEncryptionConfig;
|
|
286
|
+
/** Soft delete configuration */
|
|
287
|
+
softDelete?: SoftDeleteConfig;
|
|
288
|
+
/** Audit logging configuration */
|
|
289
|
+
audit?: AuditConfig;
|
|
290
|
+
/** Automatic timestamps configuration */
|
|
291
|
+
timestamps?: TimestampsConfig;
|
|
292
|
+
/** Query result caching configuration */
|
|
293
|
+
cache?: DBCacheConfig;
|
|
294
|
+
/** Read replica configuration */
|
|
295
|
+
readReplica?: ReadReplicaConfig;
|
|
296
|
+
/** Database event handlers */
|
|
297
|
+
events?: DatabaseEvents;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Resolved configuration after merging global and operation configs
|
|
301
|
+
*/
|
|
302
|
+
export interface ResolvedOperationConfig {
|
|
303
|
+
softDelete?: SoftDeleteConfig;
|
|
304
|
+
encryption?: DBEncryptionConfig;
|
|
305
|
+
cache?: DBCacheConfig;
|
|
306
|
+
timestamps?: TimestampsConfig;
|
|
307
|
+
skipAudit: boolean;
|
|
308
|
+
includeSoftDeleted: boolean;
|
|
309
|
+
forceAdapter?: 'primary' | 'replica';
|
|
310
|
+
timeout: number;
|
|
311
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event system types for database operations
|
|
3
|
+
* Matches the documentation's event system design
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Database operation types
|
|
7
|
+
*/
|
|
8
|
+
export type DatabaseOperationType = 'CREATE' | 'READ' | 'UPDATE' | 'DELETE';
|
|
9
|
+
/**
|
|
10
|
+
* Base database event interface
|
|
11
|
+
*/
|
|
12
|
+
export interface DatabaseEvent {
|
|
13
|
+
/** Event type */
|
|
14
|
+
type: string;
|
|
15
|
+
/** Timestamp when the event occurred */
|
|
16
|
+
timestamp: Date;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Event emitted before any write operation starts
|
|
20
|
+
*/
|
|
21
|
+
export interface DatabaseBeforeWriteEvent extends DatabaseEvent {
|
|
22
|
+
type: 'beforeWrite';
|
|
23
|
+
/** Database adapter name */
|
|
24
|
+
adapter: string;
|
|
25
|
+
/** Database operation type */
|
|
26
|
+
operation: 'CREATE' | 'UPDATE' | 'DELETE';
|
|
27
|
+
/** Table name */
|
|
28
|
+
table: string;
|
|
29
|
+
/** Data being written */
|
|
30
|
+
data: Record<string, string | number | boolean | Date>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Event emitted after successful write operation
|
|
34
|
+
*/
|
|
35
|
+
export interface DatabaseAfterWriteEvent extends DatabaseEvent {
|
|
36
|
+
type: 'afterWrite';
|
|
37
|
+
/** Database adapter name */
|
|
38
|
+
adapter: string;
|
|
39
|
+
/** Database operation type */
|
|
40
|
+
operation: 'CREATE' | 'UPDATE' | 'DELETE';
|
|
41
|
+
/** Table name */
|
|
42
|
+
table: string;
|
|
43
|
+
/** Result data */
|
|
44
|
+
result: Record<string, string | number | boolean | Date>;
|
|
45
|
+
/** Operation duration in milliseconds */
|
|
46
|
+
duration: number;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Event emitted before any read operation starts
|
|
50
|
+
*/
|
|
51
|
+
export interface DatabaseBeforeReadEvent extends DatabaseEvent {
|
|
52
|
+
type: 'beforeRead';
|
|
53
|
+
/** Database adapter name */
|
|
54
|
+
adapter: string;
|
|
55
|
+
/** Database operation type */
|
|
56
|
+
operation: 'READ';
|
|
57
|
+
/** Table name */
|
|
58
|
+
table: string;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Event emitted after successful read operation
|
|
62
|
+
*/
|
|
63
|
+
export interface DatabaseAfterReadEvent extends DatabaseEvent {
|
|
64
|
+
type: 'afterRead';
|
|
65
|
+
/** Database adapter name */
|
|
66
|
+
adapter: string;
|
|
67
|
+
/** Database operation type */
|
|
68
|
+
operation: 'READ';
|
|
69
|
+
/** Table name */
|
|
70
|
+
table: string;
|
|
71
|
+
/** Result data */
|
|
72
|
+
result: Record<string, string | number | boolean | Date>;
|
|
73
|
+
/** Operation duration in milliseconds */
|
|
74
|
+
duration: number;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Event emitted when any database operation fails
|
|
78
|
+
*/
|
|
79
|
+
export interface DBErrorEvent extends DatabaseEvent {
|
|
80
|
+
type: 'error';
|
|
81
|
+
/** Database adapter name */
|
|
82
|
+
adapter: string;
|
|
83
|
+
/** Database operation type */
|
|
84
|
+
operation: string;
|
|
85
|
+
/** Table name */
|
|
86
|
+
table: string;
|
|
87
|
+
/** Error that occurred */
|
|
88
|
+
error: Error;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Event emitted when health status changes
|
|
92
|
+
*/
|
|
93
|
+
export interface HealthChangeEvent extends DatabaseEvent {
|
|
94
|
+
type: 'healthChange';
|
|
95
|
+
/** Database adapter name */
|
|
96
|
+
adapter: string;
|
|
97
|
+
/** Previous health status */
|
|
98
|
+
previousStatus: boolean;
|
|
99
|
+
/** Current health status */
|
|
100
|
+
currentStatus: boolean;
|
|
101
|
+
/** Health check details */
|
|
102
|
+
details?: Record<string, string | number | boolean>;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Event emitted on slow queries
|
|
106
|
+
*/
|
|
107
|
+
export interface SlowQueryEvent extends DatabaseEvent {
|
|
108
|
+
type: 'slowQuery';
|
|
109
|
+
/** Table name */
|
|
110
|
+
table: string;
|
|
111
|
+
/** Database operation type */
|
|
112
|
+
operation: 'READ';
|
|
113
|
+
/** Query configuration */
|
|
114
|
+
query: Record<string, string | number | boolean>;
|
|
115
|
+
/** Query duration in milliseconds */
|
|
116
|
+
duration: number;
|
|
117
|
+
/** Slow query threshold that was exceeded */
|
|
118
|
+
threshold: number;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Audit event for compliance logging
|
|
122
|
+
*/
|
|
123
|
+
export interface DatabaseAuditEvent extends DatabaseEvent {
|
|
124
|
+
type: 'audit';
|
|
125
|
+
/** Database operation type */
|
|
126
|
+
operation: string;
|
|
127
|
+
/** User ID who performed the action */
|
|
128
|
+
userId?: string;
|
|
129
|
+
/** System action identifier */
|
|
130
|
+
systemAction?: string;
|
|
131
|
+
/** Request ID for tracing */
|
|
132
|
+
requestId: string;
|
|
133
|
+
/** Table name */
|
|
134
|
+
table: string;
|
|
135
|
+
/** Record ID that was affected */
|
|
136
|
+
recordId?: string;
|
|
137
|
+
/** Changes made to the record */
|
|
138
|
+
changes: {
|
|
139
|
+
/** State before the change */
|
|
140
|
+
before?: Record<string, string | number | boolean | Date>;
|
|
141
|
+
/** State after the change */
|
|
142
|
+
after?: Record<string, string | number | boolean | Date>;
|
|
143
|
+
/** Fields that were changed */
|
|
144
|
+
fields?: string[];
|
|
145
|
+
};
|
|
146
|
+
/** IP address of the user */
|
|
147
|
+
ipAddress?: string;
|
|
148
|
+
/** User agent string */
|
|
149
|
+
userAgent?: string;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Union type for all database events
|
|
153
|
+
*/
|
|
154
|
+
export type DatabaseEventUnion = DatabaseBeforeWriteEvent | DatabaseAfterWriteEvent | DatabaseBeforeReadEvent | DatabaseAfterReadEvent | DBErrorEvent | DatabaseAuditEvent | HealthChangeEvent | SlowQueryEvent;
|
|
155
|
+
/**
|
|
156
|
+
* Event handler function type
|
|
157
|
+
*/
|
|
158
|
+
export type DBEventHandler<T extends DatabaseEvent> = (event: T) => void | Promise<void>;
|