@geekmidas/testkit 0.0.10 → 0.0.12
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-Bm44VKa-.d.cts +131 -0
- package/dist/Factory-tjCDNgUK.d.mts +131 -0
- package/dist/Factory.d.cts +3 -0
- package/dist/Factory.d.mts +3 -0
- package/dist/KyselyFactory-BoPDDitt.d.cts +200 -0
- package/dist/{KyselyFactory-ELiHgHVv.mjs → KyselyFactory-C3Bc3p4L.mjs} +1 -1
- package/dist/{KyselyFactory-Bdq1s1Go.cjs → KyselyFactory-CXtfmMfK.cjs} +1 -1
- package/dist/KyselyFactory-D82j74t9.d.mts +200 -0
- package/dist/KyselyFactory.cjs +2 -2
- package/dist/KyselyFactory.d.cts +4 -0
- package/dist/KyselyFactory.d.mts +4 -0
- package/dist/KyselyFactory.mjs +2 -2
- package/dist/ObjectionFactory-BWMTXsxH.d.cts +213 -0
- package/dist/ObjectionFactory-CEG5qUrm.d.mts +213 -0
- package/dist/{ObjectionFactory-C47B03Ot.cjs → ObjectionFactory-DxIxJagq.cjs} +82 -40
- package/dist/{ObjectionFactory-89p-FFEw.mjs → ObjectionFactory-qIICOph3.mjs} +82 -40
- 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-CQ3aUoy_.d.cts +67 -0
- package/dist/PostgresKyselyMigrator-_6yHZigp.d.mts +67 -0
- package/dist/PostgresKyselyMigrator.d.cts +3 -0
- package/dist/PostgresKyselyMigrator.d.mts +3 -0
- package/dist/PostgresMigrator-BlvuQl7d.d.mts +84 -0
- package/dist/PostgresMigrator-D5UkK1_K.d.cts +84 -0
- package/dist/PostgresMigrator.d.cts +2 -0
- package/dist/PostgresMigrator.d.mts +2 -0
- package/dist/PostgresObjectionMigrator-C69n7vzr.d.mts +77 -0
- package/dist/PostgresObjectionMigrator-CZHHcCOv.d.cts +77 -0
- package/dist/PostgresObjectionMigrator.d.cts +3 -0
- package/dist/PostgresObjectionMigrator.d.mts +3 -0
- 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.d.cts +1 -0
- package/dist/__tests__/PostgresMigrator.spec.d.mts +1 -0
- package/dist/__tests__/PostgresObjectionMigrator.spec.d.cts +1 -0
- package/dist/__tests__/PostgresObjectionMigrator.spec.d.mts +1 -0
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.cjs +2 -1
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.d.cts +1 -0
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.d.mts +1 -0
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.mjs +2 -1
- package/dist/__tests__/faker.spec.cjs +1 -1
- package/dist/__tests__/faker.spec.d.cts +1 -0
- package/dist/__tests__/faker.spec.d.mts +1 -0
- package/dist/__tests__/faker.spec.mjs +1 -1
- 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.cjs +2 -2
- package/dist/example.d.cts +26 -0
- package/dist/example.d.mts +26 -0
- package/dist/example.mjs +2 -2
- package/dist/{faker-SMN4ira4.cjs → faker-B14IEMIN.cjs} +41 -1
- package/dist/{faker-CxKkEeYi.mjs → faker-BGKYFoCT.mjs} +36 -2
- package/dist/faker-ChuHaYMR.d.mts +165 -0
- package/dist/faker-km9UhOS6.d.cts +165 -0
- package/dist/faker.cjs +2 -1
- package/dist/faker.d.cts +2 -0
- package/dist/faker.d.mts +2 -0
- package/dist/faker.mjs +2 -2
- 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 +2 -1
- package/dist/objection.d.cts +88 -0
- package/dist/objection.d.mts +88 -0
- package/dist/objection.mjs +2 -1
- package/package.json +5 -5
- package/src/ObjectionFactory.ts +156 -13
- package/src/__tests__/ObjectionFactory.spec.ts +162 -0
- package/src/faker.ts +86 -0
- package/PostgresKyselyMigrator.spec +0 -471
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Factory } from "./Factory-z2m01hMj.mjs";
|
|
2
|
+
import { faker } from "./faker-BGKYFoCT.mjs";
|
|
2
3
|
|
|
3
4
|
//#region src/ObjectionFactory.ts
|
|
4
5
|
/**
|
|
@@ -50,6 +51,71 @@ var ObjectionFactory = class extends Factory {
|
|
|
50
51
|
return Factory.createSeed(seedFn);
|
|
51
52
|
}
|
|
52
53
|
/**
|
|
54
|
+
* Creates a typed builder function for Objection.js models.
|
|
55
|
+
* This is a utility method that helps create builders with proper type inference.
|
|
56
|
+
*
|
|
57
|
+
* @template TModel - The Objection.js Model class type
|
|
58
|
+
* @template Attrs - The attributes type for the builder (defaults to Partial of model)
|
|
59
|
+
* @template Factory - The factory instance type
|
|
60
|
+
* @template Result - The result type (defaults to the model instance)
|
|
61
|
+
*
|
|
62
|
+
* @param ModelClass - The Objection.js Model class
|
|
63
|
+
* @param item - Optional function to provide default values and transformations
|
|
64
|
+
* @param autoInsert - Whether to automatically insert the record (default: true)
|
|
65
|
+
* @returns A builder function that creates and optionally inserts records
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* // Create a simple builder with defaults
|
|
70
|
+
* const userBuilder = ObjectionFactory.createBuilder(User,
|
|
71
|
+
* (attrs, factory, db, faker) => ({
|
|
72
|
+
* id: faker.string.uuid(),
|
|
73
|
+
* name: faker.person.fullName(),
|
|
74
|
+
* email: faker.internet.email(),
|
|
75
|
+
* createdAt: new Date(),
|
|
76
|
+
* ...attrs
|
|
77
|
+
* })
|
|
78
|
+
* );
|
|
79
|
+
*
|
|
80
|
+
* // Create a builder that doesn't auto-insert (useful for nested inserts)
|
|
81
|
+
* const addressBuilder = ObjectionFactory.createBuilder(Address,
|
|
82
|
+
* (attrs) => ({
|
|
83
|
+
* street: '123 Main St',
|
|
84
|
+
* city: 'Anytown',
|
|
85
|
+
* ...attrs
|
|
86
|
+
* }),
|
|
87
|
+
* false // Don't auto-insert
|
|
88
|
+
* );
|
|
89
|
+
*
|
|
90
|
+
* // Use with relations
|
|
91
|
+
* const postBuilder = ObjectionFactory.createBuilder(Post,
|
|
92
|
+
* async (attrs, factory) => ({
|
|
93
|
+
* title: faker.lorem.sentence(),
|
|
94
|
+
* content: faker.lorem.paragraphs(),
|
|
95
|
+
* authorId: attrs.authorId || (await factory.insert('user')).id,
|
|
96
|
+
* ...attrs
|
|
97
|
+
* })
|
|
98
|
+
* );
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
static createBuilder(ModelClass, item, autoInsert) {
|
|
102
|
+
return async (attrs, factory, db, faker$1) => {
|
|
103
|
+
let data = { ...attrs };
|
|
104
|
+
if (item) {
|
|
105
|
+
const defaults = await item(attrs, factory, db, faker$1);
|
|
106
|
+
data = {
|
|
107
|
+
...defaults,
|
|
108
|
+
...data
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
const model = ModelClass.fromJson(data);
|
|
112
|
+
if (autoInsert !== false) {
|
|
113
|
+
const result = await model.$query(db).insertGraph(model).execute();
|
|
114
|
+
return result;
|
|
115
|
+
} else return model;
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
53
119
|
* Creates a new ObjectionFactory instance.
|
|
54
120
|
*
|
|
55
121
|
* @param builders - Record of builder functions for creating individual entities
|
|
@@ -66,7 +132,8 @@ var ObjectionFactory = class extends Factory {
|
|
|
66
132
|
* Inserts a single record into the database using the specified builder.
|
|
67
133
|
* Uses Objection.js's insertGraph method to handle nested relations.
|
|
68
134
|
*
|
|
69
|
-
* @
|
|
135
|
+
* @template K - The builder name (must be a key of Builders)
|
|
136
|
+
* @param builderName - The name of the builder to use
|
|
70
137
|
* @param attrs - Optional attributes to override builder defaults
|
|
71
138
|
* @returns A promise resolving to the inserted record with all relations
|
|
72
139
|
* @throws Error if the specified builder doesn't exist
|
|
@@ -92,47 +159,21 @@ var ObjectionFactory = class extends Factory {
|
|
|
92
159
|
* });
|
|
93
160
|
* ```
|
|
94
161
|
*/
|
|
95
|
-
insert(
|
|
96
|
-
if (!(
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
162
|
+
async insert(builderName, attrs) {
|
|
163
|
+
if (!(builderName in this.builders)) throw new Error(`Factory "${builderName}" does not exist. Make sure it is correct and registered in src/test/setup.ts`);
|
|
164
|
+
const result = await this.builders[builderName](attrs || {}, this, this.db, faker);
|
|
165
|
+
if (result && typeof result.$query === "function") return await result.$query(this.db).insertGraph(result).execute();
|
|
166
|
+
return result;
|
|
100
167
|
}
|
|
101
|
-
|
|
102
|
-
* Inserts multiple records into the database using the specified builder.
|
|
103
|
-
* Supports both static attributes and dynamic attribute generation via a function.
|
|
104
|
-
*
|
|
105
|
-
* @param count - The number of records to insert
|
|
106
|
-
* @param builderName - The name of the builder to use
|
|
107
|
-
* @param attrs - Static attributes or a function that generates attributes for each record
|
|
108
|
-
* @returns A promise resolving to an array of inserted records
|
|
109
|
-
* @throws Error if the specified builder doesn't exist
|
|
110
|
-
*
|
|
111
|
-
* @example
|
|
112
|
-
* ```typescript
|
|
113
|
-
* // Insert multiple with same attributes
|
|
114
|
-
* const users = await factory.insertMany(5, 'user', { role: 'member' });
|
|
115
|
-
*
|
|
116
|
-
* // Insert multiple with dynamic attributes
|
|
117
|
-
* const posts = await factory.insertMany(10, 'post', (idx) => ({
|
|
118
|
-
* title: `Post ${idx + 1}`,
|
|
119
|
-
* content: `Content for post ${idx + 1}`,
|
|
120
|
-
* publishedAt: new Date()
|
|
121
|
-
* }));
|
|
122
|
-
*
|
|
123
|
-
* // Create users with sequential emails
|
|
124
|
-
* const admins = await factory.insertMany(3, 'user', (idx) => ({
|
|
125
|
-
* email: `admin${idx + 1}@example.com`,
|
|
126
|
-
* role: 'admin'
|
|
127
|
-
* }));
|
|
128
|
-
* ```
|
|
129
|
-
*/
|
|
130
|
-
insertMany(count, builderName, attrs = {}) {
|
|
168
|
+
async insertMany(count, builderName, attrs) {
|
|
131
169
|
if (!(builderName in this.builders)) throw new Error(`Builder "${builderName}" is not registered in this factory. Make sure it is correct and registered in src/test/setup.ts`);
|
|
132
170
|
const records = [];
|
|
133
171
|
for (let i = 0; i < count; i++) {
|
|
134
|
-
const newAttrs = typeof attrs === "function" ? attrs(i) : attrs;
|
|
135
|
-
records.push(this.builders[builderName](newAttrs,
|
|
172
|
+
const newAttrs = typeof attrs === "function" ? attrs(i, faker) : attrs;
|
|
173
|
+
records.push(this.builders[builderName](newAttrs, this, this.db, faker).then((record) => {
|
|
174
|
+
if (record && typeof record.$query === "function") return record.$query(this.db).insertGraph(record).execute();
|
|
175
|
+
return record;
|
|
176
|
+
}));
|
|
136
177
|
}
|
|
137
178
|
return Promise.all(records);
|
|
138
179
|
}
|
|
@@ -140,6 +181,7 @@ var ObjectionFactory = class extends Factory {
|
|
|
140
181
|
* Executes a seed function to create complex test scenarios with multiple related records.
|
|
141
182
|
* Seeds are useful for setting up complete test environments with realistic data relationships.
|
|
142
183
|
*
|
|
184
|
+
* @template K - The seed name (must be a key of Seeds)
|
|
143
185
|
* @param seedName - The name of the seed to execute
|
|
144
186
|
* @param attrs - Optional configuration attributes for the seed
|
|
145
187
|
* @returns The result of the seed function (typically the primary record created)
|
|
@@ -168,9 +210,9 @@ var ObjectionFactory = class extends Factory {
|
|
|
168
210
|
* .withGraphFetched('[departments.employees]');
|
|
169
211
|
* ```
|
|
170
212
|
*/
|
|
171
|
-
seed(seedName, attrs
|
|
213
|
+
seed(seedName, attrs) {
|
|
172
214
|
if (!(seedName in this.seeds)) throw new Error(`Seed "${seedName}" is not registered in this factory. Make sure it is correct and registered in src/test/setup.ts`);
|
|
173
|
-
return this.seeds[seedName](attrs, this, this.db);
|
|
215
|
+
return this.seeds[seedName](attrs || {}, this, this.db);
|
|
174
216
|
}
|
|
175
217
|
};
|
|
176
218
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require('./Factory-WMhTNZ9S.cjs');
|
|
2
|
-
|
|
2
|
+
require('./faker-B14IEMIN.cjs');
|
|
3
|
+
const require_ObjectionFactory = require('./ObjectionFactory-DxIxJagq.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 };
|
|
@@ -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 };
|
|
@@ -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 };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { PostgresMigrator } from "./PostgresMigrator-BlvuQl7d.mjs";
|
|
2
|
+
import { Knex } from "knex";
|
|
3
|
+
|
|
4
|
+
//#region src/PostgresObjectionMigrator.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* PostgreSQL migrator implementation for Objection.js ORM with Knex.
|
|
8
|
+
* Extends PostgresMigrator to provide Knex-specific migration functionality.
|
|
9
|
+
* Automatically creates test databases and applies migrations for testing environments.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import knex from 'knex';
|
|
14
|
+
* import { PostgresObjectionMigrator } from '@geekmidas/testkit';
|
|
15
|
+
*
|
|
16
|
+
* // Create Knex instance
|
|
17
|
+
* const db = knex({
|
|
18
|
+
* client: 'pg',
|
|
19
|
+
* connection: uri,
|
|
20
|
+
* migrations: {
|
|
21
|
+
* directory: path.join(__dirname, 'migrations'),
|
|
22
|
+
* extension: 'ts'
|
|
23
|
+
* }
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* // Create and use migrator
|
|
27
|
+
* const migrator = new PostgresObjectionMigrator({
|
|
28
|
+
* uri: 'postgresql://localhost:5432/test_db',
|
|
29
|
+
* knex: db
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* const cleanup = await migrator.start();
|
|
33
|
+
* // Run tests...
|
|
34
|
+
* await cleanup();
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
declare class PostgresObjectionMigrator extends PostgresMigrator {
|
|
38
|
+
private options;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a new PostgresObjectionMigrator instance.
|
|
41
|
+
*
|
|
42
|
+
* @param options - Configuration options
|
|
43
|
+
* @param options.uri - PostgreSQL connection URI
|
|
44
|
+
* @param options.knex - Knex database instance configured with migrations
|
|
45
|
+
*/
|
|
46
|
+
constructor(options: {
|
|
47
|
+
uri: string;
|
|
48
|
+
knex: Knex;
|
|
49
|
+
});
|
|
50
|
+
/**
|
|
51
|
+
* Executes Knex migrations to the latest version.
|
|
52
|
+
* Implements the abstract migrate() method from PostgresMigrator.
|
|
53
|
+
*
|
|
54
|
+
* @throws Error if migrations fail to apply
|
|
55
|
+
* @returns Promise that resolves when all migrations are applied
|
|
56
|
+
*/
|
|
57
|
+
migrate(): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Rolls back the last batch of migrations.
|
|
60
|
+
* Useful for testing migration rollback scenarios.
|
|
61
|
+
*
|
|
62
|
+
* @returns Promise that resolves when rollback is complete
|
|
63
|
+
*/
|
|
64
|
+
rollback(): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Gets the current migration status.
|
|
67
|
+
* Returns information about completed and pending migrations.
|
|
68
|
+
*
|
|
69
|
+
* @returns Promise with migration status information
|
|
70
|
+
*/
|
|
71
|
+
status(): Promise<{
|
|
72
|
+
completed: string[];
|
|
73
|
+
pending: string[];
|
|
74
|
+
}>;
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
export { PostgresObjectionMigrator };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { PostgresMigrator } from "./PostgresMigrator-D5UkK1_K.cjs";
|
|
2
|
+
import { Knex } from "knex";
|
|
3
|
+
|
|
4
|
+
//#region src/PostgresObjectionMigrator.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* PostgreSQL migrator implementation for Objection.js ORM with Knex.
|
|
8
|
+
* Extends PostgresMigrator to provide Knex-specific migration functionality.
|
|
9
|
+
* Automatically creates test databases and applies migrations for testing environments.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import knex from 'knex';
|
|
14
|
+
* import { PostgresObjectionMigrator } from '@geekmidas/testkit';
|
|
15
|
+
*
|
|
16
|
+
* // Create Knex instance
|
|
17
|
+
* const db = knex({
|
|
18
|
+
* client: 'pg',
|
|
19
|
+
* connection: uri,
|
|
20
|
+
* migrations: {
|
|
21
|
+
* directory: path.join(__dirname, 'migrations'),
|
|
22
|
+
* extension: 'ts'
|
|
23
|
+
* }
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* // Create and use migrator
|
|
27
|
+
* const migrator = new PostgresObjectionMigrator({
|
|
28
|
+
* uri: 'postgresql://localhost:5432/test_db',
|
|
29
|
+
* knex: db
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* const cleanup = await migrator.start();
|
|
33
|
+
* // Run tests...
|
|
34
|
+
* await cleanup();
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
declare class PostgresObjectionMigrator extends PostgresMigrator {
|
|
38
|
+
private options;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a new PostgresObjectionMigrator instance.
|
|
41
|
+
*
|
|
42
|
+
* @param options - Configuration options
|
|
43
|
+
* @param options.uri - PostgreSQL connection URI
|
|
44
|
+
* @param options.knex - Knex database instance configured with migrations
|
|
45
|
+
*/
|
|
46
|
+
constructor(options: {
|
|
47
|
+
uri: string;
|
|
48
|
+
knex: Knex;
|
|
49
|
+
});
|
|
50
|
+
/**
|
|
51
|
+
* Executes Knex migrations to the latest version.
|
|
52
|
+
* Implements the abstract migrate() method from PostgresMigrator.
|
|
53
|
+
*
|
|
54
|
+
* @throws Error if migrations fail to apply
|
|
55
|
+
* @returns Promise that resolves when all migrations are applied
|
|
56
|
+
*/
|
|
57
|
+
migrate(): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Rolls back the last batch of migrations.
|
|
60
|
+
* Useful for testing migration rollback scenarios.
|
|
61
|
+
*
|
|
62
|
+
* @returns Promise that resolves when rollback is complete
|
|
63
|
+
*/
|
|
64
|
+
rollback(): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Gets the current migration status.
|
|
67
|
+
* Returns information about completed and pending migrations.
|
|
68
|
+
*
|
|
69
|
+
* @returns Promise with migration status information
|
|
70
|
+
*/
|
|
71
|
+
status(): Promise<{
|
|
72
|
+
completed: string[];
|
|
73
|
+
pending: string[];
|
|
74
|
+
}>;
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
export { PostgresObjectionMigrator };
|