@geekmidas/testkit 0.0.9 → 0.0.11
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/Factory-B9LP1L18.d.cts +131 -0
- package/dist/Factory-DiZSNxC0.d.mts +131 -0
- package/dist/Factory.d.cts +3 -0
- package/dist/Factory.d.mts +3 -0
- package/dist/KyselyFactory-B-zlLbov.d.mts +200 -0
- package/dist/KyselyFactory-CrLWxJW4.d.cts +200 -0
- package/dist/KyselyFactory.d.cts +4 -0
- package/dist/KyselyFactory.d.mts +4 -0
- package/dist/ObjectionFactory-CJCpvwts.d.mts +213 -0
- package/dist/{ObjectionFactory-C47B03Ot.cjs → ObjectionFactory-Wq80ypMM.cjs} +82 -40
- package/dist/{ObjectionFactory-89p-FFEw.mjs → ObjectionFactory-aqM0dDW7.mjs} +82 -40
- package/dist/ObjectionFactory-vME-wNlq.d.cts +213 -0
- package/dist/ObjectionFactory.cjs +2 -1
- package/dist/ObjectionFactory.d.cts +4 -0
- package/dist/ObjectionFactory.d.mts +4 -0
- package/dist/ObjectionFactory.mjs +2 -1
- package/dist/{PostgresKyselyMigrator-ChIpZFYB.mjs → PostgresKyselyMigrator-Bdhl251C.mjs} +1 -1
- package/dist/PostgresKyselyMigrator-CQ3aUoy_.d.cts +67 -0
- package/dist/{PostgresKyselyMigrator-Bs31emFd.cjs → PostgresKyselyMigrator-CfytARcA.cjs} +1 -1
- package/dist/PostgresKyselyMigrator-_6yHZigp.d.mts +67 -0
- package/dist/PostgresKyselyMigrator.cjs +2 -2
- package/dist/PostgresKyselyMigrator.d.cts +3 -0
- package/dist/PostgresKyselyMigrator.d.mts +3 -0
- package/dist/PostgresKyselyMigrator.mjs +2 -2
- package/dist/PostgresMigrator-BlvuQl7d.d.mts +84 -0
- package/dist/PostgresMigrator-D5UkK1_K.d.cts +84 -0
- package/dist/{PostgresMigrator-BtAWdLss.cjs → PostgresMigrator-DFcNdCvD.cjs} +2 -1
- package/dist/{PostgresMigrator-BzqksJcW.mjs → PostgresMigrator-DxPC_gGu.mjs} +3 -2
- package/dist/PostgresMigrator.cjs +1 -1
- package/dist/PostgresMigrator.d.cts +2 -0
- package/dist/PostgresMigrator.d.mts +2 -0
- package/dist/PostgresMigrator.mjs +1 -1
- package/dist/{PostgresObjectionMigrator-B88aTT0m.cjs → PostgresObjectionMigrator-BG6ymgnt.cjs} +1 -1
- package/dist/PostgresObjectionMigrator-C69n7vzr.d.mts +77 -0
- package/dist/PostgresObjectionMigrator-CZHHcCOv.d.cts +77 -0
- package/dist/{PostgresObjectionMigrator-DydSgYFv.mjs → PostgresObjectionMigrator-G4h5FLvU.mjs} +1 -1
- package/dist/PostgresObjectionMigrator.cjs +2 -2
- package/dist/PostgresObjectionMigrator.d.cts +3 -0
- package/dist/PostgresObjectionMigrator.d.mts +3 -0
- package/dist/PostgresObjectionMigrator.mjs +2 -2
- package/dist/VitestKyselyTransactionIsolator-ClCazkBO.d.mts +56 -0
- package/dist/VitestKyselyTransactionIsolator-UE1J-UoP.d.cts +56 -0
- package/dist/VitestKyselyTransactionIsolator.d.cts +3 -0
- package/dist/VitestKyselyTransactionIsolator.d.mts +3 -0
- package/dist/VitestObjectionTransactionIsolator-CO2nTi9r.d.cts +57 -0
- package/dist/VitestObjectionTransactionIsolator-D264iuPy.d.mts +57 -0
- package/dist/VitestObjectionTransactionIsolator.d.cts +3 -0
- package/dist/VitestObjectionTransactionIsolator.d.mts +3 -0
- package/dist/VitestTransactionIsolator-DHf2MxmC.d.cts +118 -0
- package/dist/VitestTransactionIsolator-Xqyjlmw6.d.mts +118 -0
- package/dist/VitestTransactionIsolator.d.cts +2 -0
- package/dist/VitestTransactionIsolator.d.mts +2 -0
- package/dist/__tests__/Factory.spec.d.cts +1 -0
- package/dist/__tests__/Factory.spec.d.mts +1 -0
- package/dist/__tests__/KyselyFactory.spec.cjs +2 -2
- package/dist/__tests__/KyselyFactory.spec.d.cts +1 -0
- package/dist/__tests__/KyselyFactory.spec.d.mts +1 -0
- package/dist/__tests__/KyselyFactory.spec.mjs +2 -2
- package/dist/__tests__/ObjectionFactory.spec.cjs +117 -1
- package/dist/__tests__/ObjectionFactory.spec.d.cts +1 -0
- package/dist/__tests__/ObjectionFactory.spec.d.mts +1 -0
- package/dist/__tests__/ObjectionFactory.spec.mjs +117 -1
- package/dist/__tests__/PostgresMigrator.spec.cjs +1 -1
- package/dist/__tests__/PostgresMigrator.spec.d.cts +1 -0
- package/dist/__tests__/PostgresMigrator.spec.d.mts +1 -0
- package/dist/__tests__/PostgresMigrator.spec.mjs +1 -1
- package/dist/__tests__/PostgresObjectionMigrator.spec.cjs +2 -2
- package/dist/__tests__/PostgresObjectionMigrator.spec.d.cts +1 -0
- package/dist/__tests__/PostgresObjectionMigrator.spec.d.mts +1 -0
- package/dist/__tests__/PostgresObjectionMigrator.spec.mjs +2 -2
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.cjs +4 -3
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.d.cts +1 -0
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.d.mts +1 -0
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.mjs +4 -3
- package/dist/__tests__/faker.spec.d.cts +1 -0
- package/dist/__tests__/faker.spec.d.mts +1 -0
- package/dist/__tests__/integration.spec.cjs +2 -2
- package/dist/__tests__/integration.spec.d.cts +1 -0
- package/dist/__tests__/integration.spec.d.mts +1 -0
- package/dist/__tests__/integration.spec.mjs +2 -2
- package/dist/example.d.cts +26 -0
- package/dist/example.d.mts +26 -0
- package/dist/faker-DAiFK3T3.d.cts +155 -0
- package/dist/faker-nN9Ki6fn.d.mts +155 -0
- package/dist/faker.d.cts +2 -0
- package/dist/faker.d.mts +2 -0
- package/dist/helpers.d.cts +41 -0
- package/dist/helpers.d.mts +41 -0
- package/dist/kysely.cjs +2 -2
- package/dist/kysely.d.cts +73 -0
- package/dist/kysely.d.mts +73 -0
- package/dist/kysely.mjs +2 -2
- package/dist/objection.cjs +4 -3
- package/dist/objection.d.cts +88 -0
- package/dist/objection.d.mts +88 -0
- package/dist/objection.mjs +4 -3
- package/package.json +5 -5
- package/src/ObjectionFactory.ts +156 -13
- package/src/PostgresMigrator.ts +3 -1
- package/src/__tests__/ObjectionFactory.spec.ts +162 -0
- package/PostgresKyselyMigrator.spec +0 -471
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { FakerFactory } from "./faker-DAiFK3T3.cjs";
|
|
2
|
+
import { Factory, FactorySeed } from "./Factory-B9LP1L18.cjs";
|
|
3
|
+
import { Knex } from "knex";
|
|
4
|
+
import { Model } from "objection";
|
|
5
|
+
|
|
6
|
+
//#region src/ObjectionFactory.d.ts
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Factory implementation for Objection.js ORM, providing test data creation utilities.
|
|
10
|
+
* Extends the base Factory class with Objection.js-specific database operations.
|
|
11
|
+
*
|
|
12
|
+
* @template Builders - Record of builder functions for creating entities
|
|
13
|
+
* @template Seeds - Record of seed functions for complex test scenarios
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // Define your models with Objection.js
|
|
18
|
+
* class User extends Model {
|
|
19
|
+
* static tableName = 'users';
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* // Create builders
|
|
23
|
+
* const builders = {
|
|
24
|
+
* user: (attrs) => User.fromJson({
|
|
25
|
+
* id: faker.string.uuid(),
|
|
26
|
+
* name: faker.person.fullName(),
|
|
27
|
+
* email: faker.internet.email(),
|
|
28
|
+
* ...attrs
|
|
29
|
+
* }),
|
|
30
|
+
* post: (attrs) => Post.fromJson({
|
|
31
|
+
* title: 'Test Post',
|
|
32
|
+
* content: 'Test content',
|
|
33
|
+
* ...attrs
|
|
34
|
+
* })
|
|
35
|
+
* };
|
|
36
|
+
*
|
|
37
|
+
* // Create factory instance
|
|
38
|
+
* const factory = new ObjectionFactory(builders, seeds, knex);
|
|
39
|
+
*
|
|
40
|
+
* // Use in tests
|
|
41
|
+
* const user = await factory.insert('user', { name: 'John Doe' });
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
declare class ObjectionFactory<Builders extends Record<string, any>, Seeds extends Record<string, any>> extends Factory<Builders, Seeds> {
|
|
45
|
+
private builders;
|
|
46
|
+
private seeds;
|
|
47
|
+
private db;
|
|
48
|
+
/**
|
|
49
|
+
* Creates a typed seed function with proper type inference.
|
|
50
|
+
* Inherits from the base Factory class implementation.
|
|
51
|
+
*
|
|
52
|
+
* @template Seed - The seed function type
|
|
53
|
+
* @param seedFn - The seed function to wrap
|
|
54
|
+
* @returns The same seed function with proper typing
|
|
55
|
+
*/
|
|
56
|
+
static createSeed<Seed extends FactorySeed>(seedFn: Seed): Seed;
|
|
57
|
+
/**
|
|
58
|
+
* Creates a typed builder function for Objection.js models.
|
|
59
|
+
* This is a utility method that helps create builders with proper type inference.
|
|
60
|
+
*
|
|
61
|
+
* @template TModel - The Objection.js Model class type
|
|
62
|
+
* @template Attrs - The attributes type for the builder (defaults to Partial of model)
|
|
63
|
+
* @template Factory - The factory instance type
|
|
64
|
+
* @template Result - The result type (defaults to the model instance)
|
|
65
|
+
*
|
|
66
|
+
* @param ModelClass - The Objection.js Model class
|
|
67
|
+
* @param item - Optional function to provide default values and transformations
|
|
68
|
+
* @param autoInsert - Whether to automatically insert the record (default: true)
|
|
69
|
+
* @returns A builder function that creates and optionally inserts records
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* // Create a simple builder with defaults
|
|
74
|
+
* const userBuilder = ObjectionFactory.createBuilder(User,
|
|
75
|
+
* (attrs, factory, db, faker) => ({
|
|
76
|
+
* id: faker.string.uuid(),
|
|
77
|
+
* name: faker.person.fullName(),
|
|
78
|
+
* email: faker.internet.email(),
|
|
79
|
+
* createdAt: new Date(),
|
|
80
|
+
* ...attrs
|
|
81
|
+
* })
|
|
82
|
+
* );
|
|
83
|
+
*
|
|
84
|
+
* // Create a builder that doesn't auto-insert (useful for nested inserts)
|
|
85
|
+
* const addressBuilder = ObjectionFactory.createBuilder(Address,
|
|
86
|
+
* (attrs) => ({
|
|
87
|
+
* street: '123 Main St',
|
|
88
|
+
* city: 'Anytown',
|
|
89
|
+
* ...attrs
|
|
90
|
+
* }),
|
|
91
|
+
* false // Don't auto-insert
|
|
92
|
+
* );
|
|
93
|
+
*
|
|
94
|
+
* // Use with relations
|
|
95
|
+
* const postBuilder = ObjectionFactory.createBuilder(Post,
|
|
96
|
+
* async (attrs, factory) => ({
|
|
97
|
+
* title: faker.lorem.sentence(),
|
|
98
|
+
* content: faker.lorem.paragraphs(),
|
|
99
|
+
* authorId: attrs.authorId || (await factory.insert('user')).id,
|
|
100
|
+
* ...attrs
|
|
101
|
+
* })
|
|
102
|
+
* );
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
static createBuilder<TModel extends typeof Model, Attrs extends Partial<InstanceType<TModel>> = Partial<InstanceType<TModel>>, Factory = any, Result = InstanceType<TModel>>(ModelClass: TModel, item?: (attrs: Attrs, factory: Factory, db: Knex, faker: FakerFactory) => Partial<InstanceType<TModel>> | Promise<Partial<InstanceType<TModel>>>, autoInsert?: boolean): (attrs: Attrs, factory: Factory, db: Knex, faker: FakerFactory) => Promise<Result>;
|
|
106
|
+
/**
|
|
107
|
+
* Creates a new ObjectionFactory instance.
|
|
108
|
+
*
|
|
109
|
+
* @param builders - Record of builder functions for creating individual entities
|
|
110
|
+
* @param seeds - Record of seed functions for creating complex test scenarios
|
|
111
|
+
* @param db - Knex database connection instance
|
|
112
|
+
*/
|
|
113
|
+
constructor(builders: Builders, seeds: Seeds, db: Knex);
|
|
114
|
+
/**
|
|
115
|
+
* Inserts a single record into the database using the specified builder.
|
|
116
|
+
* Uses Objection.js's insertGraph method to handle nested relations.
|
|
117
|
+
*
|
|
118
|
+
* @template K - The builder name (must be a key of Builders)
|
|
119
|
+
* @param builderName - The name of the builder to use
|
|
120
|
+
* @param attrs - Optional attributes to override builder defaults
|
|
121
|
+
* @returns A promise resolving to the inserted record with all relations
|
|
122
|
+
* @throws Error if the specified builder doesn't exist
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* // Insert with defaults
|
|
127
|
+
* const user = await factory.insert('user');
|
|
128
|
+
*
|
|
129
|
+
* // Insert with overrides
|
|
130
|
+
* const adminUser = await factory.insert('user', {
|
|
131
|
+
* email: 'admin@example.com',
|
|
132
|
+
* role: 'admin'
|
|
133
|
+
* });
|
|
134
|
+
*
|
|
135
|
+
* // Insert with nested relations
|
|
136
|
+
* const userWithProfile = await factory.insert('user', {
|
|
137
|
+
* name: 'John Doe',
|
|
138
|
+
* profile: {
|
|
139
|
+
* bio: 'Software Developer',
|
|
140
|
+
* avatar: 'avatar.jpg'
|
|
141
|
+
* }
|
|
142
|
+
* });
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
insert<K extends keyof Builders>(builderName: K, attrs?: Parameters<Builders[K]>[0]): Promise<Awaited<ReturnType<Builders[K]>>>;
|
|
146
|
+
/**
|
|
147
|
+
* Inserts multiple records into the database using the specified builder.
|
|
148
|
+
* Supports both static attributes and dynamic attribute generation via a function.
|
|
149
|
+
*
|
|
150
|
+
* @param count - The number of records to insert
|
|
151
|
+
* @param builderName - The name of the builder to use
|
|
152
|
+
* @param attrs - Static attributes or a function that generates attributes for each record
|
|
153
|
+
* @returns A promise resolving to an array of inserted records
|
|
154
|
+
* @throws Error if the specified builder doesn't exist
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* // Insert multiple with same attributes
|
|
159
|
+
* const users = await factory.insertMany(5, 'user', { role: 'member' });
|
|
160
|
+
*
|
|
161
|
+
* // Insert multiple with dynamic attributes
|
|
162
|
+
* const posts = await factory.insertMany(10, 'post', (idx) => ({
|
|
163
|
+
* title: `Post ${idx + 1}`,
|
|
164
|
+
* content: `Content for post ${idx + 1}`,
|
|
165
|
+
* publishedAt: new Date()
|
|
166
|
+
* }));
|
|
167
|
+
*
|
|
168
|
+
* // Create users with sequential emails
|
|
169
|
+
* const admins = await factory.insertMany(3, 'user', (idx) => ({
|
|
170
|
+
* email: `admin${idx + 1}@example.com`,
|
|
171
|
+
* role: 'admin'
|
|
172
|
+
* }));
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
insertMany<K extends keyof Builders>(count: number, builderName: K, attrs?: Parameters<Builders[K]>[0]): Promise<Awaited<ReturnType<Builders[K]>>[]>;
|
|
176
|
+
insertMany<K extends keyof Builders>(count: number, builderName: K, attrs: (idx: number, faker: FakerFactory) => Parameters<Builders[K]>[0]): Promise<Awaited<ReturnType<Builders[K]>>[]>;
|
|
177
|
+
/**
|
|
178
|
+
* Executes a seed function to create complex test scenarios with multiple related records.
|
|
179
|
+
* Seeds are useful for setting up complete test environments with realistic data relationships.
|
|
180
|
+
*
|
|
181
|
+
* @template K - The seed name (must be a key of Seeds)
|
|
182
|
+
* @param seedName - The name of the seed to execute
|
|
183
|
+
* @param attrs - Optional configuration attributes for the seed
|
|
184
|
+
* @returns The result of the seed function (typically the primary record created)
|
|
185
|
+
* @throws Error if the specified seed doesn't exist
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```typescript
|
|
189
|
+
* // Execute a simple seed
|
|
190
|
+
* const user = await factory.seed('userWithProfile');
|
|
191
|
+
*
|
|
192
|
+
* // Execute a seed with configuration
|
|
193
|
+
* const author = await factory.seed('authorWithBooks', {
|
|
194
|
+
* bookCount: 5,
|
|
195
|
+
* includeReviews: true
|
|
196
|
+
* });
|
|
197
|
+
*
|
|
198
|
+
* // Use seed result in tests with Objection.js relations
|
|
199
|
+
* const company = await factory.seed('companyWithDepartments', {
|
|
200
|
+
* departmentCount: 3,
|
|
201
|
+
* employeesPerDepartment: 10
|
|
202
|
+
* });
|
|
203
|
+
*
|
|
204
|
+
* // Access eager loaded relations
|
|
205
|
+
* const companyWithRelations = await Company.query()
|
|
206
|
+
* .findById(company.id)
|
|
207
|
+
* .withGraphFetched('[departments.employees]');
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
seed<K extends keyof Seeds>(seedName: K, attrs?: Parameters<Seeds[K]>[0]): ReturnType<Seeds[K]>;
|
|
211
|
+
}
|
|
212
|
+
//#endregion
|
|
213
|
+
export { ObjectionFactory };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require('./Factory-WMhTNZ9S.cjs');
|
|
2
|
-
|
|
2
|
+
require('./faker-SMN4ira4.cjs');
|
|
3
|
+
const require_ObjectionFactory = require('./ObjectionFactory-Wq80ypMM.cjs');
|
|
3
4
|
|
|
4
5
|
exports.ObjectionFactory = require_ObjectionFactory.ObjectionFactory;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { PostgresMigrator } from "./PostgresMigrator-D5UkK1_K.cjs";
|
|
2
|
+
import { Kysely, MigrationProvider } from "kysely";
|
|
3
|
+
|
|
4
|
+
//#region src/PostgresKyselyMigrator.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* PostgreSQL migrator implementation for Kysely ORM.
|
|
8
|
+
* Extends PostgresMigrator to provide Kysely-specific migration functionality.
|
|
9
|
+
* Automatically creates test databases and applies migrations for testing environments.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { FileMigrationProvider } from 'kysely';
|
|
14
|
+
* import { PostgresKyselyMigrator } from '@geekmidas/testkit';
|
|
15
|
+
*
|
|
16
|
+
* // Create migration provider
|
|
17
|
+
* const provider = new FileMigrationProvider({
|
|
18
|
+
* fs: require('fs'),
|
|
19
|
+
* path: require('path'),
|
|
20
|
+
* migrationFolder: path.join(__dirname, 'migrations')
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Create Kysely instance
|
|
24
|
+
* const db = new Kysely<Database>({
|
|
25
|
+
* dialect: new PostgresDialect({
|
|
26
|
+
* pool: new Pool({ connectionString: uri })
|
|
27
|
+
* })
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* // Create and use migrator
|
|
31
|
+
* const migrator = new PostgresKyselyMigrator({
|
|
32
|
+
* uri: 'postgresql://localhost:5432/test_db',
|
|
33
|
+
* db,
|
|
34
|
+
* provider
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* const cleanup = await migrator.start();
|
|
38
|
+
* // Run tests...
|
|
39
|
+
* await cleanup();
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
declare class PostgresKyselyMigrator extends PostgresMigrator {
|
|
43
|
+
private options;
|
|
44
|
+
/**
|
|
45
|
+
* Creates a new PostgresKyselyMigrator instance.
|
|
46
|
+
*
|
|
47
|
+
* @param options - Configuration options
|
|
48
|
+
* @param options.uri - PostgreSQL connection URI
|
|
49
|
+
* @param options.db - Kysely database instance
|
|
50
|
+
* @param options.provider - Migration provider for locating migration files
|
|
51
|
+
*/
|
|
52
|
+
constructor(options: {
|
|
53
|
+
uri: string;
|
|
54
|
+
db: Kysely<any>;
|
|
55
|
+
provider: MigrationProvider;
|
|
56
|
+
});
|
|
57
|
+
/**
|
|
58
|
+
* Executes Kysely migrations to the latest version.
|
|
59
|
+
* Implements the abstract migrate() method from PostgresMigrator.
|
|
60
|
+
*
|
|
61
|
+
* @throws Error if migrations fail to apply
|
|
62
|
+
* @returns Promise that resolves when all migrations are applied
|
|
63
|
+
*/
|
|
64
|
+
migrate(): Promise<void>;
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
export { PostgresKyselyMigrator };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
-
const require_PostgresMigrator = require('./PostgresMigrator-
|
|
2
|
+
const require_PostgresMigrator = require('./PostgresMigrator-DFcNdCvD.cjs');
|
|
3
3
|
const kysely = require_chunk.__toESM(require("kysely"));
|
|
4
4
|
|
|
5
5
|
//#region src/PostgresKyselyMigrator.ts
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { PostgresMigrator } from "./PostgresMigrator-BlvuQl7d.mjs";
|
|
2
|
+
import { Kysely, MigrationProvider } from "kysely";
|
|
3
|
+
|
|
4
|
+
//#region src/PostgresKyselyMigrator.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* PostgreSQL migrator implementation for Kysely ORM.
|
|
8
|
+
* Extends PostgresMigrator to provide Kysely-specific migration functionality.
|
|
9
|
+
* Automatically creates test databases and applies migrations for testing environments.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { FileMigrationProvider } from 'kysely';
|
|
14
|
+
* import { PostgresKyselyMigrator } from '@geekmidas/testkit';
|
|
15
|
+
*
|
|
16
|
+
* // Create migration provider
|
|
17
|
+
* const provider = new FileMigrationProvider({
|
|
18
|
+
* fs: require('fs'),
|
|
19
|
+
* path: require('path'),
|
|
20
|
+
* migrationFolder: path.join(__dirname, 'migrations')
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Create Kysely instance
|
|
24
|
+
* const db = new Kysely<Database>({
|
|
25
|
+
* dialect: new PostgresDialect({
|
|
26
|
+
* pool: new Pool({ connectionString: uri })
|
|
27
|
+
* })
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* // Create and use migrator
|
|
31
|
+
* const migrator = new PostgresKyselyMigrator({
|
|
32
|
+
* uri: 'postgresql://localhost:5432/test_db',
|
|
33
|
+
* db,
|
|
34
|
+
* provider
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* const cleanup = await migrator.start();
|
|
38
|
+
* // Run tests...
|
|
39
|
+
* await cleanup();
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
declare class PostgresKyselyMigrator extends PostgresMigrator {
|
|
43
|
+
private options;
|
|
44
|
+
/**
|
|
45
|
+
* Creates a new PostgresKyselyMigrator instance.
|
|
46
|
+
*
|
|
47
|
+
* @param options - Configuration options
|
|
48
|
+
* @param options.uri - PostgreSQL connection URI
|
|
49
|
+
* @param options.db - Kysely database instance
|
|
50
|
+
* @param options.provider - Migration provider for locating migration files
|
|
51
|
+
*/
|
|
52
|
+
constructor(options: {
|
|
53
|
+
uri: string;
|
|
54
|
+
db: Kysely<any>;
|
|
55
|
+
provider: MigrationProvider;
|
|
56
|
+
});
|
|
57
|
+
/**
|
|
58
|
+
* Executes Kysely migrations to the latest version.
|
|
59
|
+
* Implements the abstract migrate() method from PostgresMigrator.
|
|
60
|
+
*
|
|
61
|
+
* @throws Error if migrations fail to apply
|
|
62
|
+
* @returns Promise that resolves when all migrations are applied
|
|
63
|
+
*/
|
|
64
|
+
migrate(): Promise<void>;
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
export { PostgresKyselyMigrator };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require('./PostgresMigrator-
|
|
2
|
-
const require_PostgresKyselyMigrator = require('./PostgresKyselyMigrator-
|
|
1
|
+
require('./PostgresMigrator-DFcNdCvD.cjs');
|
|
2
|
+
const require_PostgresKyselyMigrator = require('./PostgresKyselyMigrator-CfytARcA.cjs');
|
|
3
3
|
|
|
4
4
|
exports.PostgresKyselyMigrator = require_PostgresKyselyMigrator.PostgresKyselyMigrator;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import "./PostgresMigrator-
|
|
2
|
-
import { PostgresKyselyMigrator } from "./PostgresKyselyMigrator-
|
|
1
|
+
import "./PostgresMigrator-DxPC_gGu.mjs";
|
|
2
|
+
import { PostgresKyselyMigrator } from "./PostgresKyselyMigrator-Bdhl251C.mjs";
|
|
3
3
|
|
|
4
4
|
export { PostgresKyselyMigrator };
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
//#region src/PostgresMigrator.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Abstract base class for PostgreSQL database migration utilities.
|
|
4
|
+
* Provides database creation, migration, and cleanup functionality for testing.
|
|
5
|
+
* Subclasses must implement the migrate() method to define migration logic.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* class MyMigrator extends PostgresMigrator {
|
|
10
|
+
* async migrate(): Promise<void> {
|
|
11
|
+
* // Run your migrations here
|
|
12
|
+
* await this.runMigrations();
|
|
13
|
+
* }
|
|
14
|
+
* }
|
|
15
|
+
*
|
|
16
|
+
* // Use in tests
|
|
17
|
+
* const migrator = new MyMigrator('postgresql://localhost:5432/test_db');
|
|
18
|
+
* const cleanup = await migrator.start();
|
|
19
|
+
*
|
|
20
|
+
* // Run tests...
|
|
21
|
+
*
|
|
22
|
+
* // Clean up
|
|
23
|
+
* await cleanup();
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
declare abstract class PostgresMigrator {
|
|
27
|
+
private uri;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a new PostgresMigrator instance.
|
|
30
|
+
*
|
|
31
|
+
* @param uri - PostgreSQL connection URI
|
|
32
|
+
*/
|
|
33
|
+
constructor(uri: string);
|
|
34
|
+
/**
|
|
35
|
+
* Abstract method to be implemented by subclasses.
|
|
36
|
+
* Should contain the migration logic for setting up database schema.
|
|
37
|
+
*
|
|
38
|
+
* @returns Promise that resolves when migrations are complete
|
|
39
|
+
*/
|
|
40
|
+
abstract migrate(): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Creates a PostgreSQL database if it doesn't already exist.
|
|
43
|
+
* Connects to the 'postgres' database to check and create the target database.
|
|
44
|
+
*
|
|
45
|
+
* @param uri - PostgreSQL connection URI
|
|
46
|
+
* @returns Object indicating whether the database already existed
|
|
47
|
+
* @private
|
|
48
|
+
*/
|
|
49
|
+
private static create;
|
|
50
|
+
/**
|
|
51
|
+
* Drops a PostgreSQL database.
|
|
52
|
+
* Used for cleanup after tests are complete.
|
|
53
|
+
*
|
|
54
|
+
* @param uri - PostgreSQL connection URI
|
|
55
|
+
* @throws Error if database cannot be dropped
|
|
56
|
+
* @private
|
|
57
|
+
*/
|
|
58
|
+
private static drop;
|
|
59
|
+
/**
|
|
60
|
+
* Starts the migration process by creating the database and running migrations.
|
|
61
|
+
* Returns a cleanup function that will drop the database when called.
|
|
62
|
+
*
|
|
63
|
+
* @returns Async cleanup function that drops the created database
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const migrator = new MyMigrator('postgresql://localhost:5432/test_db');
|
|
68
|
+
*
|
|
69
|
+
* // Start migrations and get cleanup function
|
|
70
|
+
* const cleanup = await migrator.start();
|
|
71
|
+
*
|
|
72
|
+
* try {
|
|
73
|
+
* // Run your tests here
|
|
74
|
+
* await runTests();
|
|
75
|
+
* } finally {
|
|
76
|
+
* // Always clean up
|
|
77
|
+
* await cleanup();
|
|
78
|
+
* }
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
start(): Promise<() => Promise<void>>;
|
|
82
|
+
}
|
|
83
|
+
//#endregion
|
|
84
|
+
export { PostgresMigrator };
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
//#region src/PostgresMigrator.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Abstract base class for PostgreSQL database migration utilities.
|
|
4
|
+
* Provides database creation, migration, and cleanup functionality for testing.
|
|
5
|
+
* Subclasses must implement the migrate() method to define migration logic.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* class MyMigrator extends PostgresMigrator {
|
|
10
|
+
* async migrate(): Promise<void> {
|
|
11
|
+
* // Run your migrations here
|
|
12
|
+
* await this.runMigrations();
|
|
13
|
+
* }
|
|
14
|
+
* }
|
|
15
|
+
*
|
|
16
|
+
* // Use in tests
|
|
17
|
+
* const migrator = new MyMigrator('postgresql://localhost:5432/test_db');
|
|
18
|
+
* const cleanup = await migrator.start();
|
|
19
|
+
*
|
|
20
|
+
* // Run tests...
|
|
21
|
+
*
|
|
22
|
+
* // Clean up
|
|
23
|
+
* await cleanup();
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
declare abstract class PostgresMigrator {
|
|
27
|
+
private uri;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a new PostgresMigrator instance.
|
|
30
|
+
*
|
|
31
|
+
* @param uri - PostgreSQL connection URI
|
|
32
|
+
*/
|
|
33
|
+
constructor(uri: string);
|
|
34
|
+
/**
|
|
35
|
+
* Abstract method to be implemented by subclasses.
|
|
36
|
+
* Should contain the migration logic for setting up database schema.
|
|
37
|
+
*
|
|
38
|
+
* @returns Promise that resolves when migrations are complete
|
|
39
|
+
*/
|
|
40
|
+
abstract migrate(): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Creates a PostgreSQL database if it doesn't already exist.
|
|
43
|
+
* Connects to the 'postgres' database to check and create the target database.
|
|
44
|
+
*
|
|
45
|
+
* @param uri - PostgreSQL connection URI
|
|
46
|
+
* @returns Object indicating whether the database already existed
|
|
47
|
+
* @private
|
|
48
|
+
*/
|
|
49
|
+
private static create;
|
|
50
|
+
/**
|
|
51
|
+
* Drops a PostgreSQL database.
|
|
52
|
+
* Used for cleanup after tests are complete.
|
|
53
|
+
*
|
|
54
|
+
* @param uri - PostgreSQL connection URI
|
|
55
|
+
* @throws Error if database cannot be dropped
|
|
56
|
+
* @private
|
|
57
|
+
*/
|
|
58
|
+
private static drop;
|
|
59
|
+
/**
|
|
60
|
+
* Starts the migration process by creating the database and running migrations.
|
|
61
|
+
* Returns a cleanup function that will drop the database when called.
|
|
62
|
+
*
|
|
63
|
+
* @returns Async cleanup function that drops the created database
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const migrator = new MyMigrator('postgresql://localhost:5432/test_db');
|
|
68
|
+
*
|
|
69
|
+
* // Start migrations and get cleanup function
|
|
70
|
+
* const cleanup = await migrator.start();
|
|
71
|
+
*
|
|
72
|
+
* try {
|
|
73
|
+
* // Run your tests here
|
|
74
|
+
* await runTests();
|
|
75
|
+
* } finally {
|
|
76
|
+
* // Always clean up
|
|
77
|
+
* await cleanup();
|
|
78
|
+
* }
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
start(): Promise<() => Promise<void>>;
|
|
82
|
+
}
|
|
83
|
+
//#endregion
|
|
84
|
+
export { PostgresMigrator };
|
|
@@ -2,6 +2,7 @@ const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
|
2
2
|
const pg = require_chunk.__toESM(require("pg"));
|
|
3
3
|
|
|
4
4
|
//#region src/PostgresMigrator.ts
|
|
5
|
+
const { Client } = pg.default;
|
|
5
6
|
/**
|
|
6
7
|
* Creates a PostgreSQL client connected to the 'postgres' database.
|
|
7
8
|
* Extracts connection details from the provided URI.
|
|
@@ -18,7 +19,7 @@ const pg = require_chunk.__toESM(require("pg"));
|
|
|
18
19
|
*/
|
|
19
20
|
async function setupClient(uri) {
|
|
20
21
|
const url = new URL(uri);
|
|
21
|
-
const db = new
|
|
22
|
+
const db = new Client({
|
|
22
23
|
user: url.username,
|
|
23
24
|
password: url.password,
|
|
24
25
|
host: url.hostname,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import pg from "pg";
|
|
2
2
|
|
|
3
3
|
//#region src/PostgresMigrator.ts
|
|
4
|
+
const { Client: Client$1 } = pg;
|
|
4
5
|
/**
|
|
5
6
|
* Creates a PostgreSQL client connected to the 'postgres' database.
|
|
6
7
|
* Extracts connection details from the provided URI.
|
|
@@ -17,7 +18,7 @@ import { Client } from "pg";
|
|
|
17
18
|
*/
|
|
18
19
|
async function setupClient(uri) {
|
|
19
20
|
const url = new URL(uri);
|
|
20
|
-
const db = new Client({
|
|
21
|
+
const db = new Client$1({
|
|
21
22
|
user: url.username,
|
|
22
23
|
password: url.password,
|
|
23
24
|
host: url.hostname,
|