@geekmidas/testkit 0.0.8 → 0.0.9

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.
Files changed (46) hide show
  1. package/dist/PostgresObjectionMigrator-B88aTT0m.cjs +122 -0
  2. package/dist/PostgresObjectionMigrator-DydSgYFv.mjs +117 -0
  3. package/dist/PostgresObjectionMigrator.cjs +4 -0
  4. package/dist/PostgresObjectionMigrator.mjs +4 -0
  5. package/dist/{VitestKyselyTransactionIsolator-AfxPJEwR.mjs → VitestKyselyTransactionIsolator-BKGT9nEG.mjs} +1 -1
  6. package/dist/{VitestKyselyTransactionIsolator-YWnSJiIH.cjs → VitestKyselyTransactionIsolator-CIlpIO78.cjs} +1 -1
  7. package/dist/VitestKyselyTransactionIsolator.cjs +2 -2
  8. package/dist/VitestKyselyTransactionIsolator.mjs +2 -2
  9. package/dist/{VitestObjectionTransactionIsolator-BZRYy8iW.mjs → VitestObjectionTransactionIsolator-BPoLUFop.mjs} +2 -2
  10. package/dist/{VitestObjectionTransactionIsolator-0uX6DW5G.cjs → VitestObjectionTransactionIsolator-DyqLp_in.cjs} +2 -2
  11. package/dist/VitestObjectionTransactionIsolator.cjs +2 -2
  12. package/dist/VitestObjectionTransactionIsolator.mjs +2 -2
  13. package/dist/VitestTransactionIsolator.cjs +1 -1
  14. package/dist/VitestTransactionIsolator.mjs +1 -1
  15. package/dist/__tests__/KyselyFactory.spec.cjs +5 -5
  16. package/dist/__tests__/KyselyFactory.spec.mjs +5 -5
  17. package/dist/__tests__/ObjectionFactory.spec.cjs +1 -1
  18. package/dist/__tests__/ObjectionFactory.spec.mjs +1 -1
  19. package/dist/__tests__/PostgresMigrator.spec.cjs +1 -1
  20. package/dist/__tests__/PostgresMigrator.spec.mjs +1 -1
  21. package/dist/__tests__/PostgresObjectionMigrator.spec.cjs +432 -0
  22. package/dist/__tests__/PostgresObjectionMigrator.spec.mjs +431 -0
  23. package/dist/__tests__/VitestObjectionTransactionIsolator.spec.cjs +130 -0
  24. package/dist/__tests__/VitestObjectionTransactionIsolator.spec.mjs +129 -0
  25. package/dist/__tests__/integration.spec.cjs +5 -5
  26. package/dist/__tests__/integration.spec.mjs +5 -5
  27. package/dist/{helpers-CukcFAU9.mjs → helpers-BEmjyUVE.mjs} +1 -1
  28. package/dist/{helpers-Bnm3Jy9X.cjs → helpers-CNMBePuj.cjs} +12 -0
  29. package/dist/helpers.cjs +1 -1
  30. package/dist/helpers.mjs +1 -1
  31. package/dist/{kysely-B-GOhABm.cjs → kysely-CBfCXxUn.cjs} +2 -2
  32. package/dist/{kysely-CqfoKVXs.mjs → kysely-Cx_1pZYc.mjs} +2 -2
  33. package/dist/kysely.cjs +3 -3
  34. package/dist/kysely.mjs +3 -3
  35. package/dist/objection-CCD8fMLj.cjs +87 -0
  36. package/dist/objection-lsMgM5gP.mjs +82 -0
  37. package/dist/objection.cjs +7 -81
  38. package/dist/objection.mjs +6 -81
  39. package/package.json +1 -1
  40. package/src/PostgresObjectionMigrator.ts +138 -0
  41. package/src/__tests__/PostgresObjectionMigrator.spec.ts +634 -0
  42. package/src/objection.ts +1 -0
  43. /package/dist/{VitestTransactionIsolator-kFL36T8x.mjs → VitestTransactionIsolator-BWwK-ca6.mjs} +0 -0
  44. /package/dist/{VitestTransactionIsolator-DcOz0LZF.cjs → VitestTransactionIsolator-CruLTRRi.cjs} +0 -0
  45. /package/dist/{helpers-CKMlwSYT.mjs → helpers-BuPmgzyQ.mjs} +0 -0
  46. /package/dist/{helpers-H4hO5SZR.cjs → helpers-nEUtQ7eo.cjs} +0 -0
@@ -0,0 +1,431 @@
1
+ import "../PostgresMigrator-BzqksJcW.mjs";
2
+ import { PostgresObjectionMigrator } from "../PostgresObjectionMigrator-DydSgYFv.mjs";
3
+ import { createTestDatabase } from "../helpers-BEmjyUVE.mjs";
4
+ import { Client } from "pg";
5
+ import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
6
+ import knex from "knex";
7
+ import { promises } from "node:fs";
8
+ import path from "node:path";
9
+
10
+ //#region src/__tests__/PostgresObjectionMigrator.spec.ts
11
+ describe("PostgresObjectionMigrator", () => {
12
+ let testDbName;
13
+ let cleanupDb;
14
+ let consoleSpy;
15
+ let consoleErrorSpy;
16
+ let testMigrationsDir;
17
+ beforeAll(async () => {
18
+ testDbName = `test_postgres_objection_migrator_${Date.now()}`;
19
+ cleanupDb = await createTestDatabase(testDbName);
20
+ testMigrationsDir = path.join(process.cwd(), "test-migrations", Date.now().toString());
21
+ await promises.mkdir(testMigrationsDir, { recursive: true });
22
+ await promises.writeFile(path.join(testMigrationsDir, "001_create_users.js"), `
23
+ exports.up = function(knex) {
24
+ return knex.schema.createTable('users', function(table) {
25
+ table.increments('id').primary();
26
+ table.string('name');
27
+ table.string('email').unique();
28
+ table.timestamps(true, true);
29
+ });
30
+ };
31
+
32
+ exports.down = function(knex) {
33
+ return knex.schema.dropTable('users');
34
+ };
35
+ `);
36
+ await promises.writeFile(path.join(testMigrationsDir, "002_create_posts.js"), `
37
+ exports.up = function(knex) {
38
+ return knex.schema.createTable('posts', function(table) {
39
+ table.increments('id').primary();
40
+ table.string('title');
41
+ table.text('content');
42
+ table.integer('user_id').unsigned().references('id').inTable('users');
43
+ table.timestamps(true, true);
44
+ });
45
+ };
46
+
47
+ exports.down = function(knex) {
48
+ return knex.schema.dropTable('posts');
49
+ };
50
+ `);
51
+ });
52
+ beforeEach(() => {
53
+ consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
54
+ consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
55
+ });
56
+ afterEach(() => {
57
+ consoleSpy.mockRestore();
58
+ consoleErrorSpy.mockRestore();
59
+ });
60
+ afterAll(async () => {
61
+ await cleanupDb();
62
+ await promises.rm(testMigrationsDir, {
63
+ recursive: true,
64
+ force: true
65
+ });
66
+ });
67
+ describe("constructor", () => {
68
+ it("should create a PostgresObjectionMigrator instance", () => {
69
+ const knexInstance = knex({
70
+ client: "pg",
71
+ connection: `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`
72
+ });
73
+ const migrator = new PostgresObjectionMigrator({
74
+ uri: `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`,
75
+ knex: knexInstance
76
+ });
77
+ expect(migrator).toBeInstanceOf(PostgresObjectionMigrator);
78
+ knexInstance.destroy();
79
+ });
80
+ });
81
+ describe("migrate method", () => {
82
+ it("should run migrations to latest", async () => {
83
+ const newDbName = `test_migrate_${Date.now()}`;
84
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
85
+ const knexInstance = knex({
86
+ client: "pg",
87
+ connection: uri,
88
+ migrations: { directory: testMigrationsDir }
89
+ });
90
+ const migrator = new PostgresObjectionMigrator({
91
+ uri,
92
+ knex: knexInstance
93
+ });
94
+ const cleanup = await migrator.start();
95
+ expect(consoleSpy).toHaveBeenCalledWith(`Migrating database: ${newDbName}`);
96
+ expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Applied batch"));
97
+ const verifyClient = new Client({
98
+ host: "localhost",
99
+ port: 5432,
100
+ user: "geekmidas",
101
+ password: "geekmidas",
102
+ database: newDbName
103
+ });
104
+ await verifyClient.connect();
105
+ const tablesResult = await verifyClient.query(`
106
+ SELECT table_name FROM information_schema.tables
107
+ WHERE table_schema = 'public'
108
+ AND table_name IN ('users', 'posts')
109
+ ORDER BY table_name
110
+ `);
111
+ expect(tablesResult.rows).toEqual([{ table_name: "posts" }, { table_name: "users" }]);
112
+ await verifyClient.end();
113
+ await cleanup();
114
+ });
115
+ it("should handle no pending migrations", async () => {
116
+ const newDbName = `test_no_pending_${Date.now()}`;
117
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
118
+ const knexInstance1 = knex({
119
+ client: "pg",
120
+ connection: uri,
121
+ migrations: { directory: testMigrationsDir }
122
+ });
123
+ const migrator1 = new PostgresObjectionMigrator({
124
+ uri,
125
+ knex: knexInstance1
126
+ });
127
+ const cleanup = await migrator1.start();
128
+ consoleSpy.mockClear();
129
+ const knexInstance2 = knex({
130
+ client: "pg",
131
+ connection: uri,
132
+ migrations: { directory: testMigrationsDir }
133
+ });
134
+ const migrator2 = new PostgresObjectionMigrator({
135
+ uri,
136
+ knex: knexInstance2
137
+ });
138
+ await migrator2.migrate();
139
+ expect(consoleSpy).toHaveBeenCalledWith("No pending migrations to apply");
140
+ await cleanup();
141
+ });
142
+ it("should handle migration errors", async () => {
143
+ const newDbName = `test_migration_error_${Date.now()}`;
144
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
145
+ const badMigrationsDir = path.join(process.cwd(), "bad-migrations", Date.now().toString());
146
+ await promises.mkdir(badMigrationsDir, { recursive: true });
147
+ await promises.writeFile(path.join(badMigrationsDir, "001_bad_migration.js"), `
148
+ exports.up = function(knex) {
149
+ throw new Error('Migration failed on purpose');
150
+ };
151
+
152
+ exports.down = function(knex) {
153
+ return Promise.resolve();
154
+ };
155
+ `);
156
+ const knexInstance = knex({
157
+ client: "pg",
158
+ connection: uri,
159
+ migrations: { directory: badMigrationsDir }
160
+ });
161
+ const migrator = new PostgresObjectionMigrator({
162
+ uri,
163
+ knex: knexInstance
164
+ });
165
+ await expect(migrator.start()).rejects.toThrow("Migration failed on purpose");
166
+ expect(consoleErrorSpy).toHaveBeenCalledWith("Failed to apply migrations:", expect.any(Error));
167
+ await promises.rm(badMigrationsDir, {
168
+ recursive: true,
169
+ force: true
170
+ });
171
+ const cleanupClient = new Client({
172
+ host: "localhost",
173
+ port: 5432,
174
+ user: "geekmidas",
175
+ password: "geekmidas",
176
+ database: "postgres"
177
+ });
178
+ await cleanupClient.connect();
179
+ await cleanupClient.query(`DROP DATABASE IF EXISTS "${newDbName}"`);
180
+ await cleanupClient.end();
181
+ });
182
+ it("should destroy knex connection after migration", async () => {
183
+ const newDbName = `test_destroy_${Date.now()}`;
184
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
185
+ const knexInstance = knex({
186
+ client: "pg",
187
+ connection: uri,
188
+ migrations: { directory: testMigrationsDir }
189
+ });
190
+ const destroySpy = vi.spyOn(knexInstance, "destroy");
191
+ const migrator = new PostgresObjectionMigrator({
192
+ uri,
193
+ knex: knexInstance
194
+ });
195
+ const cleanup = await migrator.start();
196
+ expect(destroySpy).toHaveBeenCalled();
197
+ await cleanup();
198
+ });
199
+ });
200
+ describe("rollback method", () => {
201
+ it("should rollback last migration batch", async () => {
202
+ const newDbName = `test_rollback_${Date.now()}`;
203
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
204
+ const knexInstance1 = knex({
205
+ client: "pg",
206
+ connection: uri,
207
+ migrations: { directory: testMigrationsDir }
208
+ });
209
+ const migrator1 = new PostgresObjectionMigrator({
210
+ uri,
211
+ knex: knexInstance1
212
+ });
213
+ const cleanup = await migrator1.start();
214
+ consoleSpy.mockClear();
215
+ const knexInstance2 = knex({
216
+ client: "pg",
217
+ connection: uri,
218
+ migrations: { directory: testMigrationsDir }
219
+ });
220
+ const migrator2 = new PostgresObjectionMigrator({
221
+ uri,
222
+ knex: knexInstance2
223
+ });
224
+ await migrator2.rollback();
225
+ expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Rolled back batch"));
226
+ const verifyClient = new Client({
227
+ host: "localhost",
228
+ port: 5432,
229
+ user: "geekmidas",
230
+ password: "geekmidas",
231
+ database: newDbName
232
+ });
233
+ await verifyClient.connect();
234
+ const tablesResult = await verifyClient.query(`
235
+ SELECT table_name FROM information_schema.tables
236
+ WHERE table_schema = 'public'
237
+ AND table_name IN ('users', 'posts')
238
+ `);
239
+ expect(tablesResult.rows).toEqual([]);
240
+ await verifyClient.end();
241
+ await cleanup();
242
+ });
243
+ it("should handle no migrations to rollback", async () => {
244
+ const newDbName = `test_no_rollback_${Date.now()}`;
245
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
246
+ const createClient = new Client({
247
+ host: "localhost",
248
+ port: 5432,
249
+ user: "geekmidas",
250
+ password: "geekmidas",
251
+ database: "postgres"
252
+ });
253
+ await createClient.connect();
254
+ await createClient.query(`CREATE DATABASE "${newDbName}"`);
255
+ await createClient.end();
256
+ const knexInstance = knex({
257
+ client: "pg",
258
+ connection: uri,
259
+ migrations: { directory: testMigrationsDir }
260
+ });
261
+ const migrator = new PostgresObjectionMigrator({
262
+ uri,
263
+ knex: knexInstance
264
+ });
265
+ await migrator.rollback();
266
+ expect(consoleSpy).toHaveBeenCalledWith("No migrations to rollback");
267
+ const cleanupClient = new Client({
268
+ host: "localhost",
269
+ port: 5432,
270
+ user: "geekmidas",
271
+ password: "geekmidas",
272
+ database: "postgres"
273
+ });
274
+ await cleanupClient.connect();
275
+ await cleanupClient.query(`DROP DATABASE IF EXISTS "${newDbName}"`);
276
+ await cleanupClient.end();
277
+ });
278
+ it("should handle rollback errors", async () => {
279
+ const newDbName = `test_rollback_error_${Date.now()}`;
280
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
281
+ const knexInstance = knex({
282
+ client: "pg",
283
+ connection: uri,
284
+ migrations: { directory: testMigrationsDir }
285
+ });
286
+ const migrator = new PostgresObjectionMigrator({
287
+ uri,
288
+ knex: knexInstance
289
+ });
290
+ await expect(migrator.rollback()).rejects.toThrow();
291
+ expect(consoleErrorSpy).toHaveBeenCalledWith("Failed to rollback migrations:", expect.any(Error));
292
+ await knexInstance.destroy();
293
+ });
294
+ });
295
+ describe("status method", () => {
296
+ it("should return migration status", async () => {
297
+ const newDbName = `test_status_${Date.now()}`;
298
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
299
+ const knexInstance1 = knex({
300
+ client: "pg",
301
+ connection: uri,
302
+ migrations: { directory: testMigrationsDir }
303
+ });
304
+ const migrator1 = new PostgresObjectionMigrator({
305
+ uri,
306
+ knex: knexInstance1
307
+ });
308
+ const cleanup = await migrator1.start();
309
+ const knexInstance2 = knex({
310
+ client: "pg",
311
+ connection: uri,
312
+ migrations: { directory: testMigrationsDir }
313
+ });
314
+ const migrator2 = new PostgresObjectionMigrator({
315
+ uri,
316
+ knex: knexInstance2
317
+ });
318
+ const status = await migrator2.status();
319
+ expect(status).toHaveProperty("completed");
320
+ expect(status).toHaveProperty("pending");
321
+ expect(Array.isArray(status.completed)).toBe(true);
322
+ expect(Array.isArray(status.pending)).toBe(true);
323
+ await cleanup();
324
+ });
325
+ it("should destroy connection after getting status", async () => {
326
+ const newDbName = `test_status_destroy_${Date.now()}`;
327
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
328
+ const knexInstance = knex({
329
+ client: "pg",
330
+ connection: uri,
331
+ migrations: { directory: testMigrationsDir }
332
+ });
333
+ const destroySpy = vi.spyOn(knexInstance, "destroy");
334
+ const migrator = new PostgresObjectionMigrator({
335
+ uri,
336
+ knex: knexInstance
337
+ });
338
+ const createClient = new Client({
339
+ host: "localhost",
340
+ port: 5432,
341
+ user: "geekmidas",
342
+ password: "geekmidas",
343
+ database: "postgres"
344
+ });
345
+ await createClient.connect();
346
+ await createClient.query(`CREATE DATABASE "${newDbName}"`);
347
+ await createClient.end();
348
+ await migrator.status();
349
+ expect(destroySpy).toHaveBeenCalled();
350
+ const cleanupClient = new Client({
351
+ host: "localhost",
352
+ port: 5432,
353
+ user: "geekmidas",
354
+ password: "geekmidas",
355
+ database: "postgres"
356
+ });
357
+ await cleanupClient.connect();
358
+ await cleanupClient.query(`DROP DATABASE IF EXISTS "${newDbName}"`);
359
+ await cleanupClient.end();
360
+ });
361
+ });
362
+ describe("integration scenarios", () => {
363
+ it("should handle complete workflow with complex migrations", async () => {
364
+ const integrationDbName = `test_integration_${Date.now()}`;
365
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${integrationDbName}`;
366
+ const knexInstance = knex({
367
+ client: "pg",
368
+ connection: uri,
369
+ migrations: { directory: testMigrationsDir }
370
+ });
371
+ const migrator = new PostgresObjectionMigrator({
372
+ uri,
373
+ knex: knexInstance
374
+ });
375
+ const cleanup = await migrator.start();
376
+ const testClient = new Client({
377
+ host: "localhost",
378
+ port: 5432,
379
+ user: "geekmidas",
380
+ password: "geekmidas",
381
+ database: integrationDbName
382
+ });
383
+ await testClient.connect();
384
+ const userResult = await testClient.query(`INSERT INTO users (name, email) VALUES ($1, $2) RETURNING id`, ["Test User", "test@example.com"]);
385
+ const userId = userResult.rows[0].id;
386
+ await testClient.query(`INSERT INTO posts (title, content, user_id) VALUES ($1, $2, $3)`, [
387
+ "Test Post",
388
+ "This is a test post",
389
+ userId
390
+ ]);
391
+ const postResult = await testClient.query(`SELECT * FROM posts WHERE user_id = $1`, [userId]);
392
+ expect(postResult.rowCount).toBe(1);
393
+ expect(postResult.rows[0].title).toBe("Test Post");
394
+ await testClient.end();
395
+ await cleanup();
396
+ });
397
+ it("should work with transaction-based tests", async () => {
398
+ const transactionDbName = `test_transaction_${Date.now()}`;
399
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${transactionDbName}`;
400
+ const knexInstance = knex({
401
+ client: "pg",
402
+ connection: uri,
403
+ migrations: { directory: testMigrationsDir }
404
+ });
405
+ const migrator = new PostgresObjectionMigrator({
406
+ uri,
407
+ knex: knexInstance
408
+ });
409
+ const cleanup = await migrator.start();
410
+ const testKnex = knex({
411
+ client: "pg",
412
+ connection: uri
413
+ });
414
+ await testKnex.transaction(async (trx) => {
415
+ await trx("users").insert({
416
+ name: "Transaction User",
417
+ email: "trx@example.com"
418
+ });
419
+ const users$1 = await trx("users").select("*");
420
+ expect(users$1.length).toBeGreaterThan(0);
421
+ throw new Error("Rollback transaction");
422
+ }).catch(() => {});
423
+ const users = await testKnex("users").select("*");
424
+ expect(users.length).toBe(0);
425
+ await testKnex.destroy();
426
+ await cleanup();
427
+ });
428
+ });
429
+ });
430
+
431
+ //#endregion
@@ -0,0 +1,130 @@
1
+ const require_chunk = require('../chunk-CUT6urMc.cjs');
2
+ require('../Factory-WMhTNZ9S.cjs');
3
+ require('../ObjectionFactory-C47B03Ot.cjs');
4
+ require('../PostgresMigrator-BtAWdLss.cjs');
5
+ require('../PostgresObjectionMigrator-B88aTT0m.cjs');
6
+ require('../VitestTransactionIsolator-CruLTRRi.cjs');
7
+ require('../VitestObjectionTransactionIsolator-DyqLp_in.cjs');
8
+ const require_objection = require('../objection-CCD8fMLj.cjs');
9
+ const require_helpers = require('../helpers-CNMBePuj.cjs');
10
+ const vitest = require_chunk.__toESM(require("vitest"));
11
+ const objection = require_chunk.__toESM(require("objection"));
12
+
13
+ //#region src/__tests__/VitestObjectionTransactionIsolator.spec.ts
14
+ var User = class extends objection.Model {
15
+ static get tableName() {
16
+ return "users";
17
+ }
18
+ id;
19
+ name;
20
+ email;
21
+ role;
22
+ createdAt;
23
+ updatedAt;
24
+ static get relationMappings() {
25
+ return {
26
+ posts: {
27
+ relation: objection.Model.HasManyRelation,
28
+ modelClass: Post,
29
+ join: {
30
+ from: "users.id",
31
+ to: "posts.user_id"
32
+ }
33
+ },
34
+ comments: {
35
+ relation: objection.Model.HasManyRelation,
36
+ modelClass: Comment,
37
+ join: {
38
+ from: "users.id",
39
+ to: "comments.user_id"
40
+ }
41
+ }
42
+ };
43
+ }
44
+ };
45
+ var Post = class extends objection.Model {
46
+ static get tableName() {
47
+ return "posts";
48
+ }
49
+ id;
50
+ title;
51
+ content;
52
+ userId;
53
+ published;
54
+ createdAt;
55
+ updatedAt;
56
+ static get relationMappings() {
57
+ return {
58
+ user: {
59
+ relation: objection.Model.BelongsToOneRelation,
60
+ modelClass: User,
61
+ join: {
62
+ from: "posts.user_id",
63
+ to: "users.id"
64
+ }
65
+ },
66
+ comments: {
67
+ relation: objection.Model.HasManyRelation,
68
+ modelClass: Comment,
69
+ join: {
70
+ from: "posts.id",
71
+ to: "comments.post_id"
72
+ }
73
+ }
74
+ };
75
+ }
76
+ };
77
+ var Comment = class extends objection.Model {
78
+ static get tableName() {
79
+ return "comments";
80
+ }
81
+ id;
82
+ content;
83
+ postId;
84
+ userId;
85
+ createdAt;
86
+ static get relationMappings() {
87
+ return {
88
+ post: {
89
+ relation: objection.Model.BelongsToOneRelation,
90
+ modelClass: Post,
91
+ join: {
92
+ from: "comments.post_id",
93
+ to: "posts.id"
94
+ }
95
+ },
96
+ user: {
97
+ relation: objection.Model.BelongsToOneRelation,
98
+ modelClass: User,
99
+ join: {
100
+ from: "comments.user_id",
101
+ to: "users.id"
102
+ }
103
+ }
104
+ };
105
+ }
106
+ };
107
+ const knex = require_helpers.createKnexDb();
108
+ objection.Model.knex(knex);
109
+ const it = require_objection.wrapVitestObjectionTransaction(vitest.it, knex, async (trx) => {
110
+ await require_helpers.createTestTablesKnex(trx);
111
+ });
112
+ (0, vitest.describe)("VitestObjectionTransactionIsolator", () => {
113
+ (0, vitest.describe)("Transaction Isolation", () => {
114
+ it("should rollback data after test completes", async ({ trx }) => {
115
+ const user = await User.query(trx).insert({
116
+ name: "Test User",
117
+ email: "test@example.com",
118
+ role: "user"
119
+ });
120
+ (0, vitest.expect)(user).toBeDefined();
121
+ (0, vitest.expect)(user.id).toBeDefined();
122
+ (0, vitest.expect)(user.name).toBe("Test User");
123
+ const foundUser = await User.query(trx).findById(user.id);
124
+ (0, vitest.expect)(foundUser).toBeDefined();
125
+ (0, vitest.expect)(foundUser?.email).toBe(user.email);
126
+ });
127
+ });
128
+ });
129
+
130
+ //#endregion
@@ -0,0 +1,129 @@
1
+ import "../Factory-z2m01hMj.mjs";
2
+ import "../ObjectionFactory-89p-FFEw.mjs";
3
+ import "../PostgresMigrator-BzqksJcW.mjs";
4
+ import "../PostgresObjectionMigrator-DydSgYFv.mjs";
5
+ import "../VitestTransactionIsolator-BWwK-ca6.mjs";
6
+ import "../VitestObjectionTransactionIsolator-BPoLUFop.mjs";
7
+ import { wrapVitestObjectionTransaction } from "../objection-lsMgM5gP.mjs";
8
+ import { createKnexDb, createTestTablesKnex } from "../helpers-BEmjyUVE.mjs";
9
+ import { describe, expect, it } from "vitest";
10
+ import { Model } from "objection";
11
+
12
+ //#region src/__tests__/VitestObjectionTransactionIsolator.spec.ts
13
+ var User = class extends Model {
14
+ static get tableName() {
15
+ return "users";
16
+ }
17
+ id;
18
+ name;
19
+ email;
20
+ role;
21
+ createdAt;
22
+ updatedAt;
23
+ static get relationMappings() {
24
+ return {
25
+ posts: {
26
+ relation: Model.HasManyRelation,
27
+ modelClass: Post,
28
+ join: {
29
+ from: "users.id",
30
+ to: "posts.user_id"
31
+ }
32
+ },
33
+ comments: {
34
+ relation: Model.HasManyRelation,
35
+ modelClass: Comment,
36
+ join: {
37
+ from: "users.id",
38
+ to: "comments.user_id"
39
+ }
40
+ }
41
+ };
42
+ }
43
+ };
44
+ var Post = class extends Model {
45
+ static get tableName() {
46
+ return "posts";
47
+ }
48
+ id;
49
+ title;
50
+ content;
51
+ userId;
52
+ published;
53
+ createdAt;
54
+ updatedAt;
55
+ static get relationMappings() {
56
+ return {
57
+ user: {
58
+ relation: Model.BelongsToOneRelation,
59
+ modelClass: User,
60
+ join: {
61
+ from: "posts.user_id",
62
+ to: "users.id"
63
+ }
64
+ },
65
+ comments: {
66
+ relation: Model.HasManyRelation,
67
+ modelClass: Comment,
68
+ join: {
69
+ from: "posts.id",
70
+ to: "comments.post_id"
71
+ }
72
+ }
73
+ };
74
+ }
75
+ };
76
+ var Comment = class extends Model {
77
+ static get tableName() {
78
+ return "comments";
79
+ }
80
+ id;
81
+ content;
82
+ postId;
83
+ userId;
84
+ createdAt;
85
+ static get relationMappings() {
86
+ return {
87
+ post: {
88
+ relation: Model.BelongsToOneRelation,
89
+ modelClass: Post,
90
+ join: {
91
+ from: "comments.post_id",
92
+ to: "posts.id"
93
+ }
94
+ },
95
+ user: {
96
+ relation: Model.BelongsToOneRelation,
97
+ modelClass: User,
98
+ join: {
99
+ from: "comments.user_id",
100
+ to: "users.id"
101
+ }
102
+ }
103
+ };
104
+ }
105
+ };
106
+ const knex = createKnexDb();
107
+ Model.knex(knex);
108
+ const it$1 = wrapVitestObjectionTransaction(it, knex, async (trx) => {
109
+ await createTestTablesKnex(trx);
110
+ });
111
+ describe("VitestObjectionTransactionIsolator", () => {
112
+ describe("Transaction Isolation", () => {
113
+ it$1("should rollback data after test completes", async ({ trx }) => {
114
+ const user = await User.query(trx).insert({
115
+ name: "Test User",
116
+ email: "test@example.com",
117
+ role: "user"
118
+ });
119
+ expect(user).toBeDefined();
120
+ expect(user.id).toBeDefined();
121
+ expect(user.name).toBe("Test User");
122
+ const foundUser = await User.query(trx).findById(user.id);
123
+ expect(foundUser).toBeDefined();
124
+ expect(foundUser?.email).toBe(user.email);
125
+ });
126
+ });
127
+ });
128
+
129
+ //#endregion
@@ -4,11 +4,11 @@ require('../faker-SMN4ira4.cjs');
4
4
  const require_KyselyFactory = require('../KyselyFactory-Bdq1s1Go.cjs');
5
5
  require('../PostgresMigrator-BtAWdLss.cjs');
6
6
  require('../PostgresKyselyMigrator-Bs31emFd.cjs');
7
- require('../VitestTransactionIsolator-DcOz0LZF.cjs');
8
- require('../VitestKyselyTransactionIsolator-YWnSJiIH.cjs');
9
- const require_helpers = require('../helpers-H4hO5SZR.cjs');
10
- const require_kysely = require('../kysely-B-GOhABm.cjs');
11
- const require_helpers$1 = require('../helpers-Bnm3Jy9X.cjs');
7
+ require('../VitestTransactionIsolator-CruLTRRi.cjs');
8
+ require('../VitestKyselyTransactionIsolator-CIlpIO78.cjs');
9
+ const require_helpers = require('../helpers-nEUtQ7eo.cjs');
10
+ const require_kysely = require('../kysely-CBfCXxUn.cjs');
11
+ const require_helpers$1 = require('../helpers-CNMBePuj.cjs');
12
12
  const vitest = require_chunk.__toESM(require("vitest"));
13
13
 
14
14
  //#region src/__tests__/integration.spec.ts