@plyaz/db 0.1.0 → 0.2.0
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/README.md +98 -134
- package/dist/adapters/drizzle/DrizzleAdapter.d.ts +109 -9
- package/dist/adapters/drizzle/DrizzleAdapter.d.ts.map +1 -1
- package/dist/adapters/index.d.ts +5 -0
- package/dist/adapters/index.d.ts.map +1 -1
- package/dist/adapters/mock/MockAdapter.d.ts +88 -0
- package/dist/adapters/mock/MockAdapter.d.ts.map +1 -0
- package/dist/adapters/sql/SQLAdapter.d.ts +29 -6
- package/dist/adapters/sql/SQLAdapter.d.ts.map +1 -1
- package/dist/adapters/supabase/SupabaseAdapter.d.ts +9 -2
- package/dist/adapters/supabase/SupabaseAdapter.d.ts.map +1 -1
- package/dist/advanced/multi-tenancy/TenantRepository.d.ts +1 -7
- package/dist/advanced/multi-tenancy/TenantRepository.d.ts.map +1 -1
- package/dist/advanced/read-replica/ReadReplicaAdapter.d.ts +3 -2
- package/dist/advanced/read-replica/ReadReplicaAdapter.d.ts.map +1 -1
- package/dist/cli/index.d.ts +27 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +9201 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/extensions/AuditExtension.d.ts +56 -9
- package/dist/extensions/AuditExtension.d.ts.map +1 -1
- package/dist/extensions/CachingAdapter.d.ts +5 -4
- package/dist/extensions/CachingAdapter.d.ts.map +1 -1
- package/dist/extensions/EncryptionExtension.d.ts +5 -4
- package/dist/extensions/EncryptionExtension.d.ts.map +1 -1
- package/dist/extensions/MultiReadExtension.d.ts +95 -0
- package/dist/extensions/MultiReadExtension.d.ts.map +1 -0
- package/dist/extensions/MultiWriteExtension.d.ts +67 -0
- package/dist/extensions/MultiWriteExtension.d.ts.map +1 -0
- package/dist/extensions/ReadReplicaAdapter.d.ts +4 -3
- package/dist/extensions/ReadReplicaAdapter.d.ts.map +1 -1
- package/dist/extensions/SoftDeleteExtension.d.ts +5 -4
- package/dist/extensions/SoftDeleteExtension.d.ts.map +1 -1
- package/dist/extensions/index.d.ts +4 -0
- package/dist/extensions/index.d.ts.map +1 -1
- package/dist/factory/AdapterFactory.d.ts.map +1 -1
- package/dist/factory/createDatabaseService.d.ts.map +1 -1
- package/dist/index.cjs +3298 -396
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +4441 -1562
- package/dist/index.mjs.map +1 -1
- package/dist/migrations/MigrationManager.d.ts +128 -0
- package/dist/migrations/MigrationManager.d.ts.map +1 -0
- package/dist/migrations/generateDownMigration.d.ts +25 -0
- package/dist/migrations/generateDownMigration.d.ts.map +1 -0
- package/dist/migrations/index.d.ts +10 -0
- package/dist/migrations/index.d.ts.map +1 -0
- package/dist/repository/BaseRepository.d.ts +109 -23
- package/dist/repository/BaseRepository.d.ts.map +1 -1
- package/dist/seeds/SeedManager.d.ts +120 -0
- package/dist/seeds/SeedManager.d.ts.map +1 -0
- package/dist/seeds/index.d.ts +10 -0
- package/dist/seeds/index.d.ts.map +1 -0
- package/dist/service/DatabaseService.d.ts +89 -13
- package/dist/service/DatabaseService.d.ts.map +1 -1
- package/dist/service/EventEmitter.d.ts +3 -14
- package/dist/service/EventEmitter.d.ts.map +1 -1
- package/dist/service/HealthManager.d.ts +42 -3
- package/dist/service/HealthManager.d.ts.map +1 -1
- package/package.json +9 -5
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MigrationManager - Database schema migrations with version control
|
|
3
|
+
*
|
|
4
|
+
* Manages database schema migrations with support for versioning,
|
|
5
|
+
* rollback, and migration history tracking. Automatically discovers
|
|
6
|
+
* migration files from a specified directory and applies them in order.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const migrationManager = new MigrationManager({
|
|
11
|
+
* adapter: sqlAdapter,
|
|
12
|
+
* migrationsPath: './migrations', // default
|
|
13
|
+
* tableName: 'schema_migrations' // default
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* // Run all pending migrations
|
|
17
|
+
* await migrationManager.up();
|
|
18
|
+
*
|
|
19
|
+
* // Rollback last migration
|
|
20
|
+
* await migrationManager.down();
|
|
21
|
+
*
|
|
22
|
+
* // Get migration status
|
|
23
|
+
* const status = await migrationManager.status();
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
import type { DatabaseResult, MigrationManagerConfig, MigrationStatus } from "@plyaz/types/db";
|
|
27
|
+
/**
|
|
28
|
+
* MigrationManager - Handles database schema migrations
|
|
29
|
+
*
|
|
30
|
+
* Discovers migration files, tracks migration history, and applies
|
|
31
|
+
* migrations in order with support for rollback.
|
|
32
|
+
*/
|
|
33
|
+
export declare class MigrationManager {
|
|
34
|
+
private adapter;
|
|
35
|
+
private migrationsPath;
|
|
36
|
+
private tableName;
|
|
37
|
+
private schema;
|
|
38
|
+
constructor(config: MigrationManagerConfig);
|
|
39
|
+
/**
|
|
40
|
+
* Initialize migrations table if it doesn't exist
|
|
41
|
+
*/
|
|
42
|
+
initialize(): Promise<DatabaseResult<void>>;
|
|
43
|
+
/**
|
|
44
|
+
* Discover migration files from migrations directory (including subdirectories)
|
|
45
|
+
*/
|
|
46
|
+
private discoverMigrations;
|
|
47
|
+
/**
|
|
48
|
+
* Parse SQL content to extract UP and DOWN sections
|
|
49
|
+
*/
|
|
50
|
+
private parseSqlSections;
|
|
51
|
+
/**
|
|
52
|
+
* Process dollar-quoted string delimiters ($$ or $tag$)
|
|
53
|
+
* Returns updated state for tracking if we're inside a dollar block
|
|
54
|
+
*/
|
|
55
|
+
private processDollarDelimiters;
|
|
56
|
+
/**
|
|
57
|
+
* Filter out comment-only statements
|
|
58
|
+
*/
|
|
59
|
+
private isNonCommentStatement;
|
|
60
|
+
/**
|
|
61
|
+
* Split SQL into individual statements for better error reporting
|
|
62
|
+
* Handles $$ delimited blocks (functions, triggers) correctly
|
|
63
|
+
*/
|
|
64
|
+
private splitSqlStatements;
|
|
65
|
+
/**
|
|
66
|
+
* Extract a short description from a SQL statement for logging
|
|
67
|
+
*/
|
|
68
|
+
private getStatementDescription;
|
|
69
|
+
/**
|
|
70
|
+
* Execute SQL statements individually with better error reporting
|
|
71
|
+
*/
|
|
72
|
+
private executeSqlStatements;
|
|
73
|
+
/**
|
|
74
|
+
* Load SQL migration from file
|
|
75
|
+
*/
|
|
76
|
+
private loadSqlMigration;
|
|
77
|
+
/**
|
|
78
|
+
* Load TypeScript/JavaScript migration from file
|
|
79
|
+
*/
|
|
80
|
+
private loadJsMigration;
|
|
81
|
+
/**
|
|
82
|
+
* Load migration from file
|
|
83
|
+
*/
|
|
84
|
+
private loadMigration;
|
|
85
|
+
/**
|
|
86
|
+
* Get applied migrations from database
|
|
87
|
+
*/
|
|
88
|
+
private getAppliedMigrations;
|
|
89
|
+
/**
|
|
90
|
+
* Record migration as applied
|
|
91
|
+
*/
|
|
92
|
+
private recordMigration;
|
|
93
|
+
/**
|
|
94
|
+
* Remove migration record (for rollback)
|
|
95
|
+
*/
|
|
96
|
+
private unrecordMigration;
|
|
97
|
+
/**
|
|
98
|
+
* Get migration status (applied and pending)
|
|
99
|
+
*/
|
|
100
|
+
status(): Promise<DatabaseResult<MigrationStatus>>;
|
|
101
|
+
/**
|
|
102
|
+
* Run all pending migrations
|
|
103
|
+
*/
|
|
104
|
+
up(targetVersion?: string): Promise<DatabaseResult<number>>;
|
|
105
|
+
/**
|
|
106
|
+
* Rollback last migration or rollback to specific version
|
|
107
|
+
*/
|
|
108
|
+
down(steps?: number): Promise<DatabaseResult<number>>;
|
|
109
|
+
/**
|
|
110
|
+
* Reset database (rollback all migrations)
|
|
111
|
+
*/
|
|
112
|
+
reset(): Promise<DatabaseResult<number>>;
|
|
113
|
+
/**
|
|
114
|
+
* Clear migration history (delete all records from tracking table)
|
|
115
|
+
*
|
|
116
|
+
* Use this to force fresh migrations in test/development environments
|
|
117
|
+
* without rolling back actual database changes.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* // Clear history and re-run all migrations
|
|
122
|
+
* await migrationManager.clearHistory();
|
|
123
|
+
* await migrationManager.up();
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
clearHistory(): Promise<DatabaseResult<void>>;
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=MigrationManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MigrationManager.d.ts","sourceRoot":"","sources":["../../src/migrations/MigrationManager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,KAAK,EAEV,cAAc,EAId,sBAAsB,EACtB,eAAe,EAChB,MAAM,iBAAiB,CAAC;AAiBzB;;;;;GAKG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,OAAO,CAAsB;IACrC,OAAO,CAAC,cAAc,CAAS;IAC/B,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,MAAM,CAAS;gBAEX,MAAM,EAAE,sBAAsB;IAW1C;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;IAkDjD;;OAEG;YACW,kBAAkB;IAsChC;;OAEG;IACH,OAAO,CAAC,gBAAgB;IA0BxB;;;OAGG;IACH,OAAO,CAAC,uBAAuB;IAwB/B;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAK7B;;;OAGG;IACH,OAAO,CAAC,kBAAkB;IAqC1B;;OAEG;IACH,OAAO,CAAC,uBAAuB;IA6B/B;;OAEG;YACW,oBAAoB;IA4ClC;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAgCxB;;OAEG;YACW,eAAe;IAgB7B;;OAEG;YACW,aAAa;IAoB3B;;OAEG;YACW,oBAAoB;IAkBlC;;OAEG;YACW,eAAe;IAmB7B;;OAEG;YACW,iBAAiB;IAS/B;;OAEG;IACG,MAAM,IAAI,OAAO,CAAC,cAAc,CAAC,eAAe,CAAC,CAAC;IA2BxD;;OAEG;IAEG,EAAE,CAAC,aAAa,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;IA2EjE;;OAEG;IACG,IAAI,CAAC,KAAK,GAAE,MAAU,GAAG,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;IA2E9D;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;IAK9C;;;;;;;;;;;;OAYG;IACG,YAAY,IAAI,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;CAgBpD"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility to help generate DOWN migrations from SQL files
|
|
3
|
+
*
|
|
4
|
+
* This is a helper script to analyze SQL migrations and suggest
|
|
5
|
+
* appropriate DOWN statements for rollback support.
|
|
6
|
+
*/
|
|
7
|
+
interface DownSuggestion {
|
|
8
|
+
file: string;
|
|
9
|
+
upStatements: string[];
|
|
10
|
+
downSuggestions: string[];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Parse SQL migration and suggest DOWN statements
|
|
14
|
+
*/
|
|
15
|
+
export declare function suggestDownMigration(sqlContent: string, filename: string): DownSuggestion;
|
|
16
|
+
/**
|
|
17
|
+
* Add DOWN section to a migration file
|
|
18
|
+
*/
|
|
19
|
+
export declare function addDownSection(filePath: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* Process all migrations in a directory and add DOWN sections
|
|
22
|
+
*/
|
|
23
|
+
export declare function processDirectory(migrationsPath: string, dryRun?: boolean): void;
|
|
24
|
+
export {};
|
|
25
|
+
//# sourceMappingURL=generateDownMigration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateDownMigration.d.ts","sourceRoot":"","sources":["../../src/migrations/generateDownMigration.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAYH,UAAU,cAAc;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,eAAe,EAAE,MAAM,EAAE,CAAC;CAC3B;AA4FD;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,GACf,cAAc,CAuBhB;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAevD;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,cAAc,EAAE,MAAM,EAAE,MAAM,UAAO,GAAG,IAAI,CAwC5E"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database Migrations Module
|
|
3
|
+
*
|
|
4
|
+
* Provides migration management for database schema versioning,
|
|
5
|
+
* rollback support, and migration history tracking.
|
|
6
|
+
*
|
|
7
|
+
* @module migrations
|
|
8
|
+
*/
|
|
9
|
+
export { MigrationManager } from "./MigrationManager";
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/migrations/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* CRUD operations and consistent interface patterns.
|
|
7
7
|
*
|
|
8
8
|
*/
|
|
9
|
-
import type { DatabaseResult, PaginatedResult, QueryOptions, Filter,
|
|
9
|
+
import type { DatabaseResult, PaginatedResult, QueryOptions, Filter, DatabaseServiceInterface, CreateInput, OperationConfig } from "@plyaz/types/db";
|
|
10
10
|
/**
|
|
11
11
|
* BASE REPOSITORY - Repository Layer Foundation
|
|
12
12
|
*
|
|
@@ -14,13 +14,14 @@ import type { DatabaseResult, PaginatedResult, QueryOptions, Filter, DatabaseSer
|
|
|
14
14
|
* All domain-specific repositories extend this class for type-safe database operations.
|
|
15
15
|
*
|
|
16
16
|
* **Application Flow Position:**
|
|
17
|
-
* Service Layer
|
|
17
|
+
* Service Layer → **Repository Layer** → DatabaseService → Adapter Chain
|
|
18
18
|
*
|
|
19
19
|
* **What this class provides:**
|
|
20
20
|
* - Type-safe CRUD operations for domain entities
|
|
21
21
|
* - Consistent interface across all repositories
|
|
22
22
|
* - Delegation to DatabaseService with proper table mapping
|
|
23
23
|
* - Foundation for domain-specific repository methods
|
|
24
|
+
* - Default operation config (adapter, schema, etc.) that can be overridden per-query
|
|
24
25
|
*
|
|
25
26
|
* **Called by:** Service layer (UserService, OrderService, etc.)
|
|
26
27
|
* **Calls:** DatabaseService methods (get, create, update, delete, etc.)
|
|
@@ -39,40 +40,62 @@ import type { DatabaseResult, PaginatedResult, QueryOptions, Filter, DatabaseSer
|
|
|
39
40
|
* }
|
|
40
41
|
*
|
|
41
42
|
* class UserRepository extends BaseRepository<User> {
|
|
42
|
-
* constructor(db:
|
|
43
|
+
* constructor(db: DatabaseServiceInterface) {
|
|
43
44
|
* super(db, Tables.USERS);
|
|
44
45
|
* }
|
|
45
46
|
*
|
|
46
47
|
* // Domain-specific methods
|
|
47
48
|
* async findByEmail(email: string) {
|
|
48
|
-
* return this.
|
|
49
|
-
* filter: { field: 'email', operator: 'eq', value: email }
|
|
50
|
-
* });
|
|
49
|
+
* return this.findOne({ field: 'email', operator: 'eq', value: email });
|
|
51
50
|
* }
|
|
52
51
|
* }
|
|
53
52
|
* ```
|
|
54
53
|
*
|
|
55
54
|
* @example
|
|
55
|
+
* ### Repository with Default Adapter
|
|
56
|
+
* ```typescript
|
|
57
|
+
* // Analytics repository that always uses the 'analytics' adapter by default
|
|
58
|
+
* class AnalyticsRepository extends BaseRepository<Event> {
|
|
59
|
+
* constructor(db: DatabaseServiceInterface) {
|
|
60
|
+
* super(db, 'events', { adapter: 'analytics', schema: 'analytics' });
|
|
61
|
+
* }
|
|
62
|
+
* }
|
|
63
|
+
*
|
|
64
|
+
* // All queries use analytics adapter by default
|
|
65
|
+
* await analyticsRepo.findById('event-123'); // Uses 'analytics' adapter
|
|
66
|
+
*
|
|
67
|
+
* // But can still override per-query
|
|
68
|
+
* await analyticsRepo.findById('event-123', { adapter: 'primary' });
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
56
72
|
* ### Usage in Service Layer
|
|
57
73
|
* ```typescript
|
|
58
74
|
* class UserService {
|
|
59
75
|
* constructor(private userRepo: UserRepository) {}
|
|
60
76
|
*
|
|
61
77
|
* async getUserById(id: string) {
|
|
62
|
-
* // Calls BaseRepository.findById()
|
|
78
|
+
* // Calls BaseRepository.findById() → DatabaseService.get()
|
|
63
79
|
* return this.userRepo.findById(id);
|
|
64
80
|
* }
|
|
65
81
|
* }
|
|
66
82
|
* ```
|
|
67
83
|
*/
|
|
68
|
-
export declare abstract class BaseRepository<T
|
|
69
|
-
protected readonly db:
|
|
84
|
+
export declare abstract class BaseRepository<T extends Record<string, unknown>> {
|
|
85
|
+
protected readonly db: DatabaseServiceInterface;
|
|
70
86
|
protected readonly tableName: string;
|
|
71
|
-
|
|
87
|
+
protected readonly defaultConfig?: OperationConfig;
|
|
88
|
+
constructor(db: DatabaseServiceInterface, tableName: string, defaultConfig?: OperationConfig);
|
|
89
|
+
/**
|
|
90
|
+
* Merges default repository config with per-operation config
|
|
91
|
+
* Per-operation config takes precedence over default config
|
|
92
|
+
*/
|
|
93
|
+
private mergeConfig;
|
|
72
94
|
/**
|
|
73
95
|
* Find a single entity by its primary key ID
|
|
74
96
|
*
|
|
75
97
|
* @param {string} id - The primary key ID of the entity to retrieve
|
|
98
|
+
* @param {OperationConfig} [config] - Optional per-operation configuration (adapter selection, schema override, etc.)
|
|
76
99
|
* @returns {Promise<DatabaseResult<T | null>>} Promise resolving to the entity or null if not found
|
|
77
100
|
*
|
|
78
101
|
* @example
|
|
@@ -81,13 +104,19 @@ export declare abstract class BaseRepository<T> {
|
|
|
81
104
|
* if (result.success && result.value) {
|
|
82
105
|
* console.log('Found user:', result.value.name);
|
|
83
106
|
* }
|
|
107
|
+
*
|
|
108
|
+
* // Use specific adapter for this query
|
|
109
|
+
* const analyticsResult = await userRepository.findById('user-123', {
|
|
110
|
+
* adapter: 'analytics'
|
|
111
|
+
* });
|
|
84
112
|
* ```
|
|
85
113
|
*/
|
|
86
|
-
findById(id: string): Promise<DatabaseResult<T | null>>;
|
|
114
|
+
findById(id: string, config?: OperationConfig): Promise<DatabaseResult<T | null>>;
|
|
87
115
|
/**
|
|
88
116
|
* Find multiple entities with optional filtering, sorting, and pagination
|
|
89
117
|
*
|
|
90
|
-
* @param {QueryOptions} [options] - Optional query configuration
|
|
118
|
+
* @param {QueryOptions<T>} [options] - Optional query configuration with type-safe fields
|
|
119
|
+
* @param {OperationConfig} [config] - Optional per-operation configuration (adapter selection, schema override, etc.)
|
|
91
120
|
* @returns {Promise<DatabaseResult<PaginatedResult<T>>>} Promise resolving to paginated results
|
|
92
121
|
*
|
|
93
122
|
* @example
|
|
@@ -97,13 +126,19 @@ export declare abstract class BaseRepository<T> {
|
|
|
97
126
|
* sort: [{ field: 'createdAt', direction: 'desc' }],
|
|
98
127
|
* pagination: { limit: 20, offset: 0 }
|
|
99
128
|
* });
|
|
129
|
+
*
|
|
130
|
+
* // Query from analytics database
|
|
131
|
+
* const analyticsResult = await userRepository.findMany({}, {
|
|
132
|
+
* adapter: 'analytics'
|
|
133
|
+
* });
|
|
100
134
|
* ```
|
|
101
135
|
*/
|
|
102
|
-
findMany(options?: QueryOptions): Promise<DatabaseResult<PaginatedResult<T>>>;
|
|
136
|
+
findMany(options?: QueryOptions<T>, config?: OperationConfig): Promise<DatabaseResult<PaginatedResult<T>>>;
|
|
103
137
|
/**
|
|
104
138
|
* Create a new entity in the database
|
|
105
139
|
*
|
|
106
|
-
* @param {T} data - The entity data to create
|
|
140
|
+
* @param {CreateInput<T>} data - The entity data to create (id is auto-generated)
|
|
141
|
+
* @param {OperationConfig} [config] - Optional per-operation configuration (adapter selection, schema override, etc.)
|
|
107
142
|
* @returns {Promise<DatabaseResult<T>>} Promise resolving to the created entity
|
|
108
143
|
*
|
|
109
144
|
* @example
|
|
@@ -112,14 +147,23 @@ export declare abstract class BaseRepository<T> {
|
|
|
112
147
|
* name: 'John Doe',
|
|
113
148
|
* email: 'john@example.com'
|
|
114
149
|
* });
|
|
150
|
+
*
|
|
151
|
+
* // Create in specific database/schema
|
|
152
|
+
* const result = await userRepository.create({
|
|
153
|
+
* name: 'Jane Doe'
|
|
154
|
+
* }, {
|
|
155
|
+
* adapter: 'secondary',
|
|
156
|
+
* schema: 'backoffice'
|
|
157
|
+
* });
|
|
115
158
|
* ```
|
|
116
159
|
*/
|
|
117
|
-
create(data: T): Promise<DatabaseResult<T>>;
|
|
160
|
+
create(data: CreateInput<T>, config?: OperationConfig): Promise<DatabaseResult<T>>;
|
|
118
161
|
/**
|
|
119
162
|
* Update an existing entity by ID
|
|
120
163
|
*
|
|
121
164
|
* @param {string} id - The primary key ID of the entity to update
|
|
122
165
|
* @param {Partial<T>} data - Partial entity data containing fields to update
|
|
166
|
+
* @param {OperationConfig} [config] - Optional per-operation configuration (adapter selection, schema override, etc.)
|
|
123
167
|
* @returns {Promise<DatabaseResult<T>>} Promise resolving to the updated entity
|
|
124
168
|
*
|
|
125
169
|
* @example
|
|
@@ -128,13 +172,22 @@ export declare abstract class BaseRepository<T> {
|
|
|
128
172
|
* email: 'newemail@example.com',
|
|
129
173
|
* updatedAt: new Date()
|
|
130
174
|
* });
|
|
175
|
+
*
|
|
176
|
+
* // Update in specific adapter with custom ID column
|
|
177
|
+
* const result = await userRepository.update('flag-key', {
|
|
178
|
+
* value: true
|
|
179
|
+
* }, {
|
|
180
|
+
* adapter: 'config',
|
|
181
|
+
* idColumn: 'key'
|
|
182
|
+
* });
|
|
131
183
|
* ```
|
|
132
184
|
*/
|
|
133
|
-
update(id: string, data: Partial<T
|
|
185
|
+
update(id: string, data: Partial<T>, config?: OperationConfig): Promise<DatabaseResult<T>>;
|
|
134
186
|
/**
|
|
135
187
|
* Delete an entity by ID (hard delete)
|
|
136
188
|
*
|
|
137
189
|
* @param {string} id - The primary key ID of the entity to delete
|
|
190
|
+
* @param {OperationConfig} [config] - Optional per-operation configuration (adapter selection, schema override, etc.)
|
|
138
191
|
* @returns {Promise<DatabaseResult<void>>} Promise resolving when deletion is complete
|
|
139
192
|
*
|
|
140
193
|
* @warning This is a permanent operation that cannot be undone
|
|
@@ -143,13 +196,19 @@ export declare abstract class BaseRepository<T> {
|
|
|
143
196
|
* @example
|
|
144
197
|
* ```typescript
|
|
145
198
|
* const result = await userRepository.delete('user-123');
|
|
199
|
+
*
|
|
200
|
+
* // Delete from specific adapter
|
|
201
|
+
* const result = await userRepository.delete('user-123', {
|
|
202
|
+
* adapter: 'archive'
|
|
203
|
+
* });
|
|
146
204
|
* ```
|
|
147
205
|
*/
|
|
148
|
-
delete(id: string): Promise<DatabaseResult<void>>;
|
|
206
|
+
delete(id: string, config?: OperationConfig): Promise<DatabaseResult<void>>;
|
|
149
207
|
/**
|
|
150
208
|
* Count entities matching optional filter criteria
|
|
151
209
|
*
|
|
152
|
-
* @param {Filter} [filter] - Optional filter conditions
|
|
210
|
+
* @param {Filter<T>} [filter] - Optional filter conditions with type-safe fields
|
|
211
|
+
* @param {OperationConfig} [config] - Optional per-operation configuration (adapter selection, schema override, etc.)
|
|
153
212
|
* @returns {Promise<DatabaseResult<number>>} Promise resolving to the count
|
|
154
213
|
*
|
|
155
214
|
* @example
|
|
@@ -158,13 +217,19 @@ export declare abstract class BaseRepository<T> {
|
|
|
158
217
|
* const activeResult = await userRepository.count({
|
|
159
218
|
* field: 'status', operator: 'eq', value: 'active'
|
|
160
219
|
* });
|
|
220
|
+
*
|
|
221
|
+
* // Count in specific adapter
|
|
222
|
+
* const archiveCount = await userRepository.count(undefined, {
|
|
223
|
+
* adapter: 'archive'
|
|
224
|
+
* });
|
|
161
225
|
* ```
|
|
162
226
|
*/
|
|
163
|
-
count(filter?: Filter): Promise<DatabaseResult<number>>;
|
|
227
|
+
count(filter?: Filter<T>, config?: OperationConfig): Promise<DatabaseResult<number>>;
|
|
164
228
|
/**
|
|
165
229
|
* Check if an entity exists by ID
|
|
166
230
|
*
|
|
167
231
|
* @param {string} id - The primary key ID to check
|
|
232
|
+
* @param {OperationConfig} [config] - Optional per-operation configuration (adapter selection, schema override, etc.)
|
|
168
233
|
* @returns {Promise<DatabaseResult<boolean>>} Promise resolving to existence status
|
|
169
234
|
*
|
|
170
235
|
* @example
|
|
@@ -173,13 +238,19 @@ export declare abstract class BaseRepository<T> {
|
|
|
173
238
|
* if (existsResult.success && existsResult.value) {
|
|
174
239
|
* console.log('User exists');
|
|
175
240
|
* }
|
|
241
|
+
*
|
|
242
|
+
* // Check existence in specific adapter
|
|
243
|
+
* const existsInArchive = await userRepository.exists('user-123', {
|
|
244
|
+
* adapter: 'archive'
|
|
245
|
+
* });
|
|
176
246
|
* ```
|
|
177
247
|
*/
|
|
178
|
-
exists(id: string): Promise<DatabaseResult<boolean>>;
|
|
248
|
+
exists(id: string, config?: OperationConfig): Promise<DatabaseResult<boolean>>;
|
|
179
249
|
/**
|
|
180
250
|
* Find the first entity matching filter criteria
|
|
181
251
|
*
|
|
182
|
-
* @param {Filter} filter - Filter conditions
|
|
252
|
+
* @param {Filter<T>} filter - Filter conditions with type-safe fields
|
|
253
|
+
* @param {OperationConfig} [config] - Optional per-operation configuration (adapter selection, schema override, etc.)
|
|
183
254
|
* @returns {Promise<DatabaseResult<T | null>>} Promise resolving to first match or null
|
|
184
255
|
*
|
|
185
256
|
* @example
|
|
@@ -187,13 +258,22 @@ export declare abstract class BaseRepository<T> {
|
|
|
187
258
|
* const result = await userRepository.findOne({
|
|
188
259
|
* field: 'email', operator: 'eq', value: 'john@example.com'
|
|
189
260
|
* });
|
|
261
|
+
*
|
|
262
|
+
* // Find in specific adapter
|
|
263
|
+
* const archivedUser = await userRepository.findOne({
|
|
264
|
+
* field: 'email', operator: 'eq', value: 'john@example.com'
|
|
265
|
+
* }, {
|
|
266
|
+
* adapter: 'archive',
|
|
267
|
+
* includeSoftDeleted: true
|
|
268
|
+
* });
|
|
190
269
|
* ```
|
|
191
270
|
*/
|
|
192
|
-
findOne(filter: Filter): Promise<DatabaseResult<T | null>>;
|
|
271
|
+
findOne(filter: Filter<T>, config?: OperationConfig): Promise<DatabaseResult<T | null>>;
|
|
193
272
|
/**
|
|
194
273
|
* Soft delete an entity by ID (recoverable deletion)
|
|
195
274
|
*
|
|
196
275
|
* @param {string} id - The primary key ID of the entity to soft delete
|
|
276
|
+
* @param {OperationConfig} [config] - Optional per-operation configuration (adapter selection, schema override, etc.)
|
|
197
277
|
* @returns {Promise<DatabaseResult<void>>} Promise resolving when soft deletion is complete
|
|
198
278
|
*
|
|
199
279
|
* @see {@link delete} For permanent deletion
|
|
@@ -202,8 +282,14 @@ export declare abstract class BaseRepository<T> {
|
|
|
202
282
|
* ```typescript
|
|
203
283
|
* const result = await userRepository.softDelete('user-123');
|
|
204
284
|
* // User is hidden but can be recovered
|
|
285
|
+
*
|
|
286
|
+
* // Soft delete in specific adapter
|
|
287
|
+
* const result = await userRepository.softDelete('user-123', {
|
|
288
|
+
* adapter: 'primary',
|
|
289
|
+
* skipAudit: false
|
|
290
|
+
* });
|
|
205
291
|
* ```
|
|
206
292
|
*/
|
|
207
|
-
softDelete(id: string): Promise<DatabaseResult<void>>;
|
|
293
|
+
softDelete(id: string, config?: OperationConfig): Promise<DatabaseResult<void>>;
|
|
208
294
|
}
|
|
209
295
|
//# sourceMappingURL=BaseRepository.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseRepository.d.ts","sourceRoot":"","sources":["../../src/repository/BaseRepository.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,cAAc,EACd,eAAe,EACf,YAAY,EACZ,MAAM,EACN,
|
|
1
|
+
{"version":3,"file":"BaseRepository.d.ts","sourceRoot":"","sources":["../../src/repository/BaseRepository.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,cAAc,EACd,eAAe,EACf,YAAY,EACZ,MAAM,EACN,wBAAwB,EACxB,WAAW,EACX,eAAe,EAChB,MAAM,iBAAiB,CAAC;AAEzB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyEG;AACH,8BAAsB,cAAc,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAIlE,SAAS,CAAC,QAAQ,CAAC,EAAE,EAAE,wBAAwB;IAC/C,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,MAAM;IAJtC,SAAS,CAAC,QAAQ,CAAC,aAAa,CAAC,EAAE,eAAe,CAAC;gBAG9B,EAAE,EAAE,wBAAwB,EAC5B,SAAS,EAAE,MAAM,EACpC,aAAa,CAAC,EAAE,eAAe;IAKjC;;;OAGG;IACH,OAAO,CAAC,WAAW;IAYnB;;;;;;;;;;;;;;;;;;;OAmBG;IACG,QAAQ,CACZ,EAAE,EAAE,MAAM,EACV,MAAM,CAAC,EAAE,eAAe,GACvB,OAAO,CAAC,cAAc,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;IAIpC;;;;;;;;;;;;;;;;;;;;OAoBG;IACG,QAAQ,CACZ,OAAO,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC,EACzB,MAAM,CAAC,EAAE,eAAe,GACvB,OAAO,CAAC,cAAc,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC;IAI9C;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACG,MAAM,CACV,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC,EACpB,MAAM,CAAC,EAAE,eAAe,GACvB,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;IAI7B;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACG,MAAM,CACV,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,EAChB,MAAM,CAAC,EAAE,eAAe,GACvB,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;IAS7B;;;;;;;;;;;;;;;;;;;OAmBG;IACG,MAAM,CACV,EAAE,EAAE,MAAM,EACV,MAAM,CAAC,EAAE,eAAe,GACvB,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;IAIhC;;;;;;;;;;;;;;;;;;;OAmBG;IACG,KAAK,CACT,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,EAClB,MAAM,CAAC,EAAE,eAAe,GACvB,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;IAIlC;;;;;;;;;;;;;;;;;;;OAmBG;IACG,MAAM,CACV,EAAE,EAAE,MAAM,EACV,MAAM,CAAC,EAAE,eAAe,GACvB,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;IAanC;;;;;;;;;;;;;;;;;;;;;OAqBG;IACG,OAAO,CACX,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EACjB,MAAM,CAAC,EAAE,eAAe,GACvB,OAAO,CAAC,cAAc,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;IAIpC;;;;;;;;;;;;;;;;;;;;OAoBG;IACG,UAAU,CACd,EAAE,EAAE,MAAM,EACV,MAAM,CAAC,EAAE,eAAe,GACvB,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;CAGjC"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SeedManager - Database seeding for development and testing
|
|
3
|
+
*
|
|
4
|
+
* Manages database seeding with support for ordered execution,
|
|
5
|
+
* seed history tracking, and idempotent seed operations.
|
|
6
|
+
* Automatically discovers seed files from a specified directory.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const seedManager = new SeedManager({
|
|
11
|
+
* adapter: sqlAdapter,
|
|
12
|
+
* seedsPath: './seeds', // default
|
|
13
|
+
* tableName: 'seed_history' // default
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* // Run all seeds
|
|
17
|
+
* await seedManager.run();
|
|
18
|
+
*
|
|
19
|
+
* // Run specific seed
|
|
20
|
+
* await seedManager.run('users');
|
|
21
|
+
*
|
|
22
|
+
* // Clear all data (for testing)
|
|
23
|
+
* await seedManager.reset();
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
import type { DatabaseResult, SeedRecord, SeedManagerConfig } from "@plyaz/types/db";
|
|
27
|
+
/**
|
|
28
|
+
* SeedManager - Handles database seeding operations
|
|
29
|
+
*
|
|
30
|
+
* Discovers seed files, tracks seed history, and executes
|
|
31
|
+
* seeds in order with support for cleanup and reset.
|
|
32
|
+
*/
|
|
33
|
+
export declare class SeedManager {
|
|
34
|
+
private adapter;
|
|
35
|
+
private seedsPath;
|
|
36
|
+
private tableName;
|
|
37
|
+
private schema;
|
|
38
|
+
private skipExisting;
|
|
39
|
+
constructor(config: SeedManagerConfig);
|
|
40
|
+
/**
|
|
41
|
+
* Initialize seeds tracking table if it doesn't exist
|
|
42
|
+
*/
|
|
43
|
+
initialize(): Promise<DatabaseResult<void>>;
|
|
44
|
+
/**
|
|
45
|
+
* Discover seed files from seeds directory
|
|
46
|
+
*/
|
|
47
|
+
private discoverSeeds;
|
|
48
|
+
/**
|
|
49
|
+
* Process dollar-quoted string delimiters ($$ or $tag$)
|
|
50
|
+
*/
|
|
51
|
+
private processDollarDelimiters;
|
|
52
|
+
/**
|
|
53
|
+
* Filter out comment-only statements
|
|
54
|
+
*/
|
|
55
|
+
private isNonCommentStatement;
|
|
56
|
+
/**
|
|
57
|
+
* Split SQL into individual statements for better error reporting
|
|
58
|
+
*/
|
|
59
|
+
private splitSqlStatements;
|
|
60
|
+
/**
|
|
61
|
+
* Extract a short description from a SQL statement for logging
|
|
62
|
+
*/
|
|
63
|
+
private getStatementDescription;
|
|
64
|
+
/**
|
|
65
|
+
* Execute SQL statements individually with better error reporting
|
|
66
|
+
*/
|
|
67
|
+
private executeSqlStatements;
|
|
68
|
+
/**
|
|
69
|
+
* Load SQL seed from file
|
|
70
|
+
*/
|
|
71
|
+
private loadSqlSeed;
|
|
72
|
+
/**
|
|
73
|
+
* Load seed from file (supports .ts, .js, and .sql)
|
|
74
|
+
*/
|
|
75
|
+
private loadSeed;
|
|
76
|
+
/**
|
|
77
|
+
* Get executed seeds from database
|
|
78
|
+
*/
|
|
79
|
+
private getExecutedSeeds;
|
|
80
|
+
/**
|
|
81
|
+
* Record seed as executed
|
|
82
|
+
*/
|
|
83
|
+
private recordSeed;
|
|
84
|
+
/**
|
|
85
|
+
* Remove seed record (for reset)
|
|
86
|
+
*/
|
|
87
|
+
private unrecordSeed;
|
|
88
|
+
/**
|
|
89
|
+
* Execute a seed function with optional transaction support
|
|
90
|
+
*/
|
|
91
|
+
private executeSeed;
|
|
92
|
+
/**
|
|
93
|
+
* Check if a seed should be skipped and log if applicable
|
|
94
|
+
*/
|
|
95
|
+
private shouldSkipSeed;
|
|
96
|
+
/**
|
|
97
|
+
* Run all seeds or a specific seed
|
|
98
|
+
*/
|
|
99
|
+
run(seedName?: string): Promise<DatabaseResult<number>>;
|
|
100
|
+
/**
|
|
101
|
+
* Execute cleanup function with optional transaction support
|
|
102
|
+
*/
|
|
103
|
+
private executeCleanup;
|
|
104
|
+
/**
|
|
105
|
+
* Reset all seeds (run cleanup functions)
|
|
106
|
+
*/
|
|
107
|
+
reset(): Promise<DatabaseResult<number>>;
|
|
108
|
+
/**
|
|
109
|
+
* Get seed execution status
|
|
110
|
+
*/
|
|
111
|
+
status(): Promise<DatabaseResult<{
|
|
112
|
+
executed: SeedRecord[];
|
|
113
|
+
pending: string[];
|
|
114
|
+
}>>;
|
|
115
|
+
/**
|
|
116
|
+
* Clear seed history (doesn't clean data, just removes tracking records)
|
|
117
|
+
*/
|
|
118
|
+
clearHistory(): Promise<DatabaseResult<void>>;
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=SeedManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SeedManager.d.ts","sourceRoot":"","sources":["../../src/seeds/SeedManager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,KAAK,EAEV,cAAc,EAGd,UAAU,EACV,iBAAiB,EAClB,MAAM,iBAAiB,CAAC;AAiBzB;;;;;GAKG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,OAAO,CAAsB;IACrC,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,YAAY,CAAU;gBAElB,MAAM,EAAE,iBAAiB;IAYrC;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;IAiDjD;;OAEG;YACW,aAAa;IA0B3B;;OAEG;IACH,OAAO,CAAC,uBAAuB;IAwB/B;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAK7B;;OAEG;IACH,OAAO,CAAC,kBAAkB;IA8B1B;;OAEG;IACH,OAAO,CAAC,uBAAuB;IA4B/B;;OAEG;YACW,oBAAoB;IAyClC;;OAEG;IACH,OAAO,CAAC,WAAW;IAenB;;OAEG;YAEW,QAAQ;IA0BtB;;OAEG;YACW,gBAAgB;IAkB9B;;OAEG;YACW,UAAU;IAmBxB;;OAEG;YACW,YAAY;IAS1B;;OAEG;YACW,WAAW;IAoBzB;;OAEG;IACH,OAAO,CAAC,cAAc;IAetB;;OAEG;IACG,GAAG,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;IA2C7D;;OAEG;YACW,cAAc;IAsB5B;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;IAsC9C;;OAEG;IACG,MAAM,IAAI,OAAO,CACrB,cAAc,CAAC;QAAE,QAAQ,EAAE,UAAU,EAAE,CAAC;QAAC,OAAO,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAC9D;IA2BD;;OAEG;IACG,YAAY,IAAI,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;CAgBpD"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database Seeds Module
|
|
3
|
+
*
|
|
4
|
+
* Provides seeding management for populating database with
|
|
5
|
+
* initial or test data, with history tracking and cleanup support.
|
|
6
|
+
*
|
|
7
|
+
* @module seeds
|
|
8
|
+
*/
|
|
9
|
+
export { SeedManager } from "./SeedManager";
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/seeds/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC"}
|