@geekmidas/testkit 9.0.2 → 10.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{Factory-CUM2767q.d.cts → Factory-Cwzho3c8.d.cts} +2 -2
- package/dist/{Factory-ClyZLRsO.d.mts.map → Factory-Cwzho3c8.d.cts.map} +1 -1
- package/dist/{Factory-ClyZLRsO.d.mts → Factory-SFupxRC2.d.mts} +2 -2
- package/dist/{Factory-CUM2767q.d.cts.map → Factory-SFupxRC2.d.mts.map} +1 -1
- package/dist/Factory.d.cts +2 -2
- package/dist/Factory.d.mts +2 -2
- package/dist/{KyselyFactory-OMwcuAX_.d.cts → KyselyFactory-hMcVtvJq.d.cts} +3 -3
- package/dist/{KyselyFactory-CTZmiGI9.d.mts.map → KyselyFactory-hMcVtvJq.d.cts.map} +1 -1
- package/dist/{KyselyFactory-CTZmiGI9.d.mts → KyselyFactory-vAxYodck.d.mts} +3 -3
- package/dist/{KyselyFactory-OMwcuAX_.d.cts.map → KyselyFactory-vAxYodck.d.mts.map} +1 -1
- package/dist/KyselyFactory.d.cts +3 -3
- package/dist/KyselyFactory.d.mts +3 -3
- package/dist/{ObjectionFactory-DDrKwAoO.d.mts → ObjectionFactory-BWjB49-i.d.mts} +3 -3
- package/dist/{ObjectionFactory-DDrKwAoO.d.mts.map → ObjectionFactory-BWjB49-i.d.mts.map} +1 -1
- package/dist/{ObjectionFactory-DvmZVHhe.d.cts → ObjectionFactory-DW-qwnqO.d.cts} +3 -3
- package/dist/{ObjectionFactory-DvmZVHhe.d.cts.map → ObjectionFactory-DW-qwnqO.d.cts.map} +1 -1
- package/dist/ObjectionFactory.d.cts +3 -3
- package/dist/ObjectionFactory.d.mts +3 -3
- package/dist/{VitestKyselyTransactionIsolator-CfSMOFlN.mjs → VitestKyselyTransactionIsolator-1Saieke7.mjs} +10 -3
- package/dist/VitestKyselyTransactionIsolator-1Saieke7.mjs.map +1 -0
- package/dist/{VitestKyselyTransactionIsolator-0CqOsuc9.cjs → VitestKyselyTransactionIsolator-BjJSXryR.cjs} +10 -3
- package/dist/VitestKyselyTransactionIsolator-BjJSXryR.cjs.map +1 -0
- package/dist/{VitestKyselyTransactionIsolator-CduJlHoT.d.cts → VitestKyselyTransactionIsolator-CwlTo6yf.d.mts} +8 -3
- package/dist/VitestKyselyTransactionIsolator-CwlTo6yf.d.mts.map +1 -0
- package/dist/{VitestKyselyTransactionIsolator-Cswnnj0k.d.mts → VitestKyselyTransactionIsolator-DJasVuIZ.d.cts} +8 -3
- package/dist/VitestKyselyTransactionIsolator-DJasVuIZ.d.cts.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.cjs +2 -2
- package/dist/VitestKyselyTransactionIsolator.d.cts +2 -2
- package/dist/VitestKyselyTransactionIsolator.d.mts +2 -2
- package/dist/VitestKyselyTransactionIsolator.mjs +2 -2
- package/dist/{VitestObjectionTransactionIsolator-CNo40EdC.mjs → VitestObjectionTransactionIsolator-B_VSWFum.mjs} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-CNo40EdC.mjs.map → VitestObjectionTransactionIsolator-B_VSWFum.mjs.map} +1 -1
- package/dist/{VitestObjectionTransactionIsolator-BXoR6xdG.d.cts → VitestObjectionTransactionIsolator-CTTjvTPO.d.cts} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-BXoR6xdG.d.cts.map → VitestObjectionTransactionIsolator-CTTjvTPO.d.cts.map} +1 -1
- package/dist/{VitestObjectionTransactionIsolator-x6hY5j4u.d.mts → VitestObjectionTransactionIsolator-CyZG6nq4.d.mts} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-x6hY5j4u.d.mts.map → VitestObjectionTransactionIsolator-CyZG6nq4.d.mts.map} +1 -1
- package/dist/{VitestObjectionTransactionIsolator-B5J9jjKq.cjs → VitestObjectionTransactionIsolator-DDoJTu7e.cjs} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-B5J9jjKq.cjs.map → VitestObjectionTransactionIsolator-DDoJTu7e.cjs.map} +1 -1
- package/dist/VitestObjectionTransactionIsolator.cjs +2 -2
- package/dist/VitestObjectionTransactionIsolator.d.cts +2 -2
- package/dist/VitestObjectionTransactionIsolator.d.mts +2 -2
- package/dist/VitestObjectionTransactionIsolator.mjs +2 -2
- package/dist/{VitestTransactionIsolator-BNWJqh9f.d.mts → VitestTransactionIsolator-BLaw80cx.d.mts} +20 -4
- package/dist/VitestTransactionIsolator-BLaw80cx.d.mts.map +1 -0
- package/dist/{VitestTransactionIsolator-D6ZxLWys.mjs → VitestTransactionIsolator-CvwPecpl.mjs} +17 -4
- package/dist/VitestTransactionIsolator-CvwPecpl.mjs.map +1 -0
- package/dist/{VitestTransactionIsolator-CtZaOUjH.cjs → VitestTransactionIsolator-DyiX-QtK.cjs} +17 -4
- package/dist/VitestTransactionIsolator-DyiX-QtK.cjs.map +1 -0
- package/dist/{VitestTransactionIsolator-CSroc7Df.d.cts → VitestTransactionIsolator-glcxyS_R.d.cts} +20 -4
- package/dist/VitestTransactionIsolator-glcxyS_R.d.cts.map +1 -0
- package/dist/VitestTransactionIsolator.cjs +1 -1
- package/dist/VitestTransactionIsolator.d.cts +2 -2
- package/dist/VitestTransactionIsolator.d.mts +2 -2
- package/dist/VitestTransactionIsolator.mjs +1 -1
- package/dist/better-auth.d.cts +2 -2
- package/dist/better-auth.d.mts +2 -2
- package/dist/{directory-DGOcVlKD.d.cts → directory-Bpz5c5pj.d.cts} +3 -3
- package/dist/{directory-DGOcVlKD.d.cts.map → directory-Bpz5c5pj.d.cts.map} +1 -1
- package/dist/{faker-BcjUfHxx.d.mts → faker-DHh7xs4u.d.mts} +3 -3
- package/dist/{faker-BcjUfHxx.d.mts.map → faker-DHh7xs4u.d.mts.map} +1 -1
- package/dist/{faker-zVCm31nU.d.cts → faker-Dg3trU4a.d.cts} +3 -3
- package/dist/{faker-zVCm31nU.d.cts.map → faker-Dg3trU4a.d.cts.map} +1 -1
- package/dist/faker.d.cts +1 -1
- package/dist/faker.d.mts +1 -1
- package/dist/kysely.cjs +2 -2
- package/dist/kysely.d.cts +5 -5
- package/dist/kysely.d.mts +5 -5
- package/dist/kysely.mjs +2 -2
- package/dist/objection.cjs +2 -2
- package/dist/objection.d.cts +5 -5
- package/dist/objection.d.mts +5 -5
- package/dist/objection.mjs +2 -2
- package/dist/os/directory.d.cts +1 -1
- package/dist/os/index.d.cts +1 -1
- package/dist/requestContext.cjs +1 -1
- package/dist/requestContext.d.cts +1 -1
- package/dist/requestContext.d.mts +1 -1
- package/dist/requestContext.mjs +1 -1
- package/package.json +10 -7
- package/CHANGELOG.md +0 -151
- package/dist/VitestKyselyTransactionIsolator-0CqOsuc9.cjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-CduJlHoT.d.cts.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-CfSMOFlN.mjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-Cswnnj0k.d.mts.map +0 -1
- package/dist/VitestTransactionIsolator-BNWJqh9f.d.mts.map +0 -1
- package/dist/VitestTransactionIsolator-CSroc7Df.d.cts.map +0 -1
- package/dist/VitestTransactionIsolator-CtZaOUjH.cjs.map +0 -1
- package/dist/VitestTransactionIsolator-D6ZxLWys.mjs.map +0 -1
- package/src/Factory.ts +0 -174
- package/src/KyselyFactory.ts +0 -391
- package/src/ObjectionFactory.ts +0 -412
- package/src/PostgresKyselyMigrator.ts +0 -104
- package/src/PostgresMigrator.ts +0 -193
- package/src/PostgresObjectionMigrator.ts +0 -138
- package/src/VitestKyselyTransactionIsolator.ts +0 -72
- package/src/VitestObjectionTransactionIsolator.ts +0 -75
- package/src/VitestTransactionIsolator.ts +0 -348
- package/src/__tests__/Factory.spec.ts +0 -178
- package/src/__tests__/KyselyFactory.spec.ts +0 -458
- package/src/__tests__/ObjectionFactory.spec.ts +0 -586
- package/src/__tests__/PostgresKyselyMigrator.spec.ts +0 -794
- package/src/__tests__/PostgresMigrator.spec.ts +0 -471
- package/src/__tests__/PostgresObjectionMigrator.spec.ts +0 -634
- package/src/__tests__/VitestObjectionTransactionIsolator.spec.ts +0 -138
- package/src/__tests__/benchmark.spec.ts +0 -140
- package/src/__tests__/better-auth.spec.ts +0 -21
- package/src/__tests__/faker.spec.ts +0 -231
- package/src/__tests__/initScript.spec.ts +0 -332
- package/src/__tests__/integration.spec.ts +0 -610
- package/src/__tests__/requestContext.spec.ts +0 -113
- package/src/__tests__/utilities.spec.ts +0 -211
- package/src/aws.ts +0 -131
- package/src/benchmark.ts +0 -48
- package/src/better-auth.ts +0 -310
- package/src/faker.ts +0 -349
- package/src/helpers.ts +0 -45
- package/src/initScript.ts +0 -122
- package/src/kysely.ts +0 -166
- package/src/logger.ts +0 -18
- package/src/objection.ts +0 -157
- package/src/os/directory.ts +0 -26
- package/src/os/index.ts +0 -1
- package/src/requestContext.ts +0 -119
- package/src/timer.ts +0 -3
- package/test/globalSetup.ts +0 -54
- package/test/helpers.ts +0 -268
- package/test/migrations/1749664623372_user.ts +0 -22
- package/tsconfig.json +0 -9
- package/vitest.config.ts +0 -8
package/CHANGELOG.md
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
# @geekmidas/testkit
|
|
2
|
-
|
|
3
|
-
## 9.0.2
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- [#12](https://github.com/geekmidas/toolbox/pull/12) [`d53863a`](https://github.com/geekmidas/toolbox/commit/d53863a84db2e4ab5420e08f79128b637043fc42) Thanks [@geekmidas](https://github.com/geekmidas)! - Align every published package on a single version and keep them in step.
|
|
8
|
-
|
|
9
|
-
All packages now share one version, enforced by a changesets `fixed` group. The
|
|
10
|
-
baseline is 9.0.1 — @geekmidas/client's published version — so nothing moves
|
|
11
|
-
backwards; this release takes the whole set to 9.0.2 together.
|
|
12
|
-
|
|
13
|
-
Independent versions made "which version of the docs applies to me"
|
|
14
|
-
unanswerable: a reader on constructs@7 and cli@2 was on no version at all. One
|
|
15
|
-
number per release makes versioned documentation possible, and lets 9 freeze as
|
|
16
|
-
the current paradigm while the constructs rework is developed against it.
|
|
17
|
-
|
|
18
|
-
Every release now publishes every package, and a major anywhere is a major
|
|
19
|
-
everywhere. Peer ranges get simpler in return.
|
|
20
|
-
|
|
21
|
-
- Updated dependencies [[`d53863a`](https://github.com/geekmidas/toolbox/commit/d53863a84db2e4ab5420e08f79128b637043fc42)]:
|
|
22
|
-
- @geekmidas/envkit@9.0.2
|
|
23
|
-
- @geekmidas/logger@9.0.2
|
|
24
|
-
- @geekmidas/services@9.0.2
|
|
25
|
-
|
|
26
|
-
## 3.1.1
|
|
27
|
-
|
|
28
|
-
### Patch Changes
|
|
29
|
-
|
|
30
|
-
- 🐛 [#11](https://github.com/geekmidas/toolbox/pull/11) [`40f4dc0`](https://github.com/geekmidas/toolbox/commit/40f4dc095911b2223a255029d8f776caf7781309) Thanks [@geekmidas](https://github.com/geekmidas)! - Patch release across all packages to realign published versions with the
|
|
31
|
-
registry. The previous release only published the four packages that had
|
|
32
|
-
version bumps; the remaining packages failed with "cannot publish over the
|
|
33
|
-
previously published versions" because their versions were unchanged.
|
|
34
|
-
- Updated dependencies [[`40f4dc0`](https://github.com/geekmidas/toolbox/commit/40f4dc095911b2223a255029d8f776caf7781309)]:
|
|
35
|
-
- @geekmidas/envkit@1.1.1
|
|
36
|
-
- @geekmidas/logger@1.0.3
|
|
37
|
-
- @geekmidas/services@2.0.1
|
|
38
|
-
|
|
39
|
-
## 3.1.0
|
|
40
|
-
|
|
41
|
-
### Minor Changes
|
|
42
|
-
|
|
43
|
-
- [#9](https://github.com/geekmidas/toolbox/pull/9) [`e31a60a`](https://github.com/geekmidas/toolbox/commit/e31a60a971366180a0e7bec6e7da56d8f36aa21f) Thanks [@geekmidas](https://github.com/geekmidas)! - Support kysely 0.29.
|
|
44
|
-
|
|
45
|
-
kysely 0.29 moved `Migrator` and `FileMigrationProvider` from the root barrel
|
|
46
|
-
(`'kysely'`) to the `'kysely/migration'` subpath. `@geekmidas/testkit`'s
|
|
47
|
-
`PostgresKyselyMigrator` now imports `Migrator` from `'kysely/migration'` and
|
|
48
|
-
its kysely peer becomes `~0.29.4` — consumers must be on kysely 0.29+.
|
|
49
|
-
|
|
50
|
-
The library packages that only declare a kysely _peer_ (`db`, `audit`, `studio`,
|
|
51
|
-
`telescope`) don't touch the moved symbols, so their peer range is _widened_ to
|
|
52
|
-
`>=0.28.2 <0.30.0` — they now support both 0.28 and 0.29 (non-breaking).
|
|
53
|
-
|
|
54
|
-
`@geekmidas/cli`'s scaffolded `test/globalSetup.ts` template now imports
|
|
55
|
-
`FileMigrationProvider` from `'kysely/migration'` so generated projects work on
|
|
56
|
-
kysely 0.29.
|
|
57
|
-
|
|
58
|
-
## 3.0.0
|
|
59
|
-
|
|
60
|
-
### Patch Changes
|
|
61
|
-
|
|
62
|
-
- Updated dependencies [[`7323f34`](https://github.com/geekmidas/toolbox/commit/7323f34176d63170dd53450889ac0b5959420c3c), [`79e2929`](https://github.com/geekmidas/toolbox/commit/79e292978d3dbc8927e25814bdb051d1c380600a)]:
|
|
63
|
-
- @geekmidas/envkit@1.1.0
|
|
64
|
-
- @geekmidas/services@2.0.0
|
|
65
|
-
|
|
66
|
-
## 2.0.0
|
|
67
|
-
|
|
68
|
-
### Patch Changes
|
|
69
|
-
|
|
70
|
-
- Updated dependencies [[`811d740`](https://github.com/geekmidas/toolbox/commit/811d740ae3875d59ad1b0dc50261266963c8cb76)]:
|
|
71
|
-
- @geekmidas/services@1.1.0
|
|
72
|
-
|
|
73
|
-
## 1.0.9
|
|
74
|
-
|
|
75
|
-
### Patch Changes
|
|
76
|
-
|
|
77
|
-
- 🐛 [#3](https://github.com/geekmidas/toolbox/pull/3) [`42fda53`](https://github.com/geekmidas/toolbox/commit/42fda532bdf4489a3352f6a684f5f30beafccedd) Thanks [@geekmidas](https://github.com/geekmidas)! - Fix stale logger from service initialization
|
|
78
|
-
|
|
79
|
-
- Updated dependencies [[`42fda53`](https://github.com/geekmidas/toolbox/commit/42fda532bdf4489a3352f6a684f5f30beafccedd)]:
|
|
80
|
-
- @geekmidas/services@1.0.4
|
|
81
|
-
|
|
82
|
-
## 1.0.8
|
|
83
|
-
|
|
84
|
-
### Patch Changes
|
|
85
|
-
|
|
86
|
-
- ✨ [`351f73b`](https://github.com/geekmidas/toolbox/commit/351f73b032bc0742b7f611a9fbcdfc85bbfd69a8) Thanks [@geekmidas](https://github.com/geekmidas)! - Update request context and add support for trpc
|
|
87
|
-
|
|
88
|
-
- Updated dependencies [[`351f73b`](https://github.com/geekmidas/toolbox/commit/351f73b032bc0742b7f611a9fbcdfc85bbfd69a8)]:
|
|
89
|
-
- @geekmidas/services@1.0.3
|
|
90
|
-
|
|
91
|
-
## 1.0.7
|
|
92
|
-
|
|
93
|
-
### Patch Changes
|
|
94
|
-
|
|
95
|
-
- 🐛 [`d70c6c0`](https://github.com/geekmidas/toolbox/commit/d70c6c0aeb8a79da2473ac77dbd8255a4a2f5651) Thanks [@geekmidas](https://github.com/geekmidas)! - Fix `package.json` exports so TypeScript declarations resolve correctly under NodeNext/Bundler module resolution. Each subpath export now nests `types` inside its `import`/`require` condition, pointing at the `.d.mts` and `.d.cts` files that `tsdown` actually emits (previously the exports referenced non-existent `.d.ts` files, causing type-resolution failures for consumers). Both ESM (`.mjs`) and CJS (`.cjs`) runtime entry points are preserved. Additionally, `@geekmidas/ui` had `import` paths pointing at `.js` files that were never emitted — those are corrected to `.mjs`.
|
|
96
|
-
|
|
97
|
-
- Updated dependencies [[`d70c6c0`](https://github.com/geekmidas/toolbox/commit/d70c6c0aeb8a79da2473ac77dbd8255a4a2f5651)]:
|
|
98
|
-
- @geekmidas/envkit@1.0.7
|
|
99
|
-
- @geekmidas/logger@1.0.2
|
|
100
|
-
|
|
101
|
-
## 1.0.6
|
|
102
|
-
|
|
103
|
-
### Patch Changes
|
|
104
|
-
|
|
105
|
-
- 🐛 [`b8a17e3`](https://github.com/geekmidas/toolbox/commit/b8a17e33de415a5d749297f7840564e824609a92) Thanks [@geekmidas](https://github.com/geekmidas)! - Fix global zod registry and fix this reference on test extensions
|
|
106
|
-
|
|
107
|
-
## 1.0.5
|
|
108
|
-
|
|
109
|
-
### Patch Changes
|
|
110
|
-
|
|
111
|
-
- 🐛 [`184c254`](https://github.com/geekmidas/toolbox/commit/184c2547a674d37c48948a9922c947ca98ff1f17) Thanks [@geekmidas](https://github.com/geekmidas)! - Fix script parsing
|
|
112
|
-
|
|
113
|
-
## 1.0.4
|
|
114
|
-
|
|
115
|
-
### Patch Changes
|
|
116
|
-
|
|
117
|
-
- ✨ [`7815080`](https://github.com/geekmidas/toolbox/commit/781508030df7f67881f5e79349338f62b35ed684) Thanks [@geekmidas](https://github.com/geekmidas)! - Add after create hook for init scripts
|
|
118
|
-
|
|
119
|
-
## 1.0.3
|
|
120
|
-
|
|
121
|
-
### Patch Changes
|
|
122
|
-
|
|
123
|
-
- ✨ [`e99f8cb`](https://github.com/geekmidas/toolbox/commit/e99f8cbb95fa5b818d94ee99b308c2ac0b239e0b) Thanks [@geekmidas](https://github.com/geekmidas)! - Add initialization scripts for postgres
|
|
124
|
-
|
|
125
|
-
## 1.0.2
|
|
126
|
-
|
|
127
|
-
### Patch Changes
|
|
128
|
-
|
|
129
|
-
- ✨ [`83a24de`](https://github.com/geekmidas/toolbox/commit/83a24de902b3fadd98444cab552ecd84f32b6661) Thanks [@geekmidas](https://github.com/geekmidas)! - Add pg-boss event publisher/subscriber, CLI setup and upgrade commands, and secrets sync via AWS SSM
|
|
130
|
-
- ✨ **@geekmidas/events**: Add pg-boss backend for event publishing and subscribing with connection string support
|
|
131
|
-
- ✨ **@geekmidas/cli**: Add `gkm setup` command for dev environment initialization, `gkm upgrade` command with workspace detection, and secrets push/pull via AWS SSM Parameter Store
|
|
132
|
-
- 🐛 **@geekmidas/testkit**: Fix database creation race condition in PostgresMigrator
|
|
133
|
-
- ✨ **@geekmidas/constructs**: Add integration tests for pg-boss with HonoEndpoint
|
|
134
|
-
|
|
135
|
-
## 1.0.1
|
|
136
|
-
|
|
137
|
-
### Patch Changes
|
|
138
|
-
|
|
139
|
-
- ✨ [`3b6d7d9`](https://github.com/geekmidas/toolbox/commit/3b6d7d9ed41dc08675395d937248a8ab754af9e1) Thanks [@geekmidas](https://github.com/geekmidas)! - Add state provider configuration to workspace config
|
|
140
|
-
|
|
141
|
-
## 1.0.0
|
|
142
|
-
|
|
143
|
-
### Major Changes
|
|
144
|
-
|
|
145
|
-
- [`ff7b115`](https://github.com/geekmidas/toolbox/commit/ff7b11599f60f84ac6cdc73714c853ecf786b2e8) Thanks [@geekmidas](https://github.com/geekmidas)! - Version 1 Stable release
|
|
146
|
-
|
|
147
|
-
### Patch Changes
|
|
148
|
-
|
|
149
|
-
- Updated dependencies [[`ff7b115`](https://github.com/geekmidas/toolbox/commit/ff7b11599f60f84ac6cdc73714c853ecf786b2e8)]:
|
|
150
|
-
- @geekmidas/envkit@1.0.0
|
|
151
|
-
- @geekmidas/logger@1.0.0
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"VitestKyselyTransactionIsolator-0CqOsuc9.cjs","names":["VitestPostgresTransactionIsolator","_conn: Kysely<Database>","conn: Kysely<Database>","level: IsolationLevel","fn: (trx: Transaction<Database>) => Promise<void>"],"sources":["../src/VitestKyselyTransactionIsolator.ts"],"sourcesContent":["import type { Kysely, Transaction } from 'kysely';\nimport {\n\ttype IsolationLevel,\n\tVitestPostgresTransactionIsolator,\n} from './VitestTransactionIsolator';\n\n/**\n * Kysely-specific implementation of the Vitest transaction isolator.\n * Provides automatic transaction rollback for test isolation using Kysely's transaction API.\n * Each test runs within a database transaction that is rolled back after completion,\n * ensuring a clean state between tests without the overhead of recreating data.\n *\n * @template Database - The database schema type\n *\n * @example\n * ```typescript\n * import { VitestKyselyTransactionIsolator } from '@geekmidas/testkit';\n * import { db } from './database';\n *\n * // Create isolator instance\n * const isolator = new VitestKyselyTransactionIsolator<Database>();\n *\n * // In your test setup\n * beforeEach(async () => {\n * await isolator.start(db);\n * });\n *\n * afterEach(async () => {\n * await isolator.rollback();\n * });\n *\n * // Tests run in isolated transactions\n * it('should create user', async () => {\n * const user = await db.insertInto('users')\n * .values({ name: 'Test User' })\n * .returningAll()\n * .executeTakeFirst();\n *\n * expect(user).toBeDefined();\n * // This data will be rolled back after the test\n * });\n * ```\n */\nexport class VitestKyselyTransactionIsolator<\n\tDatabase,\n> extends VitestPostgresTransactionIsolator<\n\tKysely<Database>,\n\tTransaction<Database>\n> {\n\tasync destroy(_conn: Kysely<Database>): Promise<void> {\n\t\t// return conn.destroy();\n\t}\n\t/**\n\t * Creates a Kysely transaction with the specified isolation level.\n\t * Implements the abstract transact method from VitestPostgresTransactionIsolator.\n\t *\n\t * @param conn - The Kysely database connection\n\t * @param level - The transaction isolation level\n\t * @param fn - The function to execute within the transaction\n\t * @returns Promise that resolves when the transaction completes\n\t */\n\tasync transact(\n\t\tconn: Kysely<Database>,\n\t\tlevel: IsolationLevel,\n\t\tfn: (trx: Transaction<Database>) => Promise<void>,\n\t): Promise<void> {\n\t\tconst isolationLevel =\n\t\t\tlevel.toLocaleLowerCase() as Lowercase<IsolationLevel>;\n\t\tawait conn.transaction().setIsolationLevel(isolationLevel).execute(fn);\n\t}\n\t// Implement any Kysely-specific transaction logic here\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,IAAa,kCAAb,cAEUA,oEAGR;CACD,MAAM,QAAQC,OAAwC,CAErD;;;;;;;;;;CAUD,MAAM,SACLC,MACAC,OACAC,IACgB;EAChB,MAAM,iBACL,MAAM,mBAAmB;AAC1B,QAAM,KAAK,aAAa,CAAC,kBAAkB,eAAe,CAAC,QAAQ,GAAG;CACtE;AAED"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"VitestKyselyTransactionIsolator-CduJlHoT.d.cts","names":[],"sources":["../src/VitestKyselyTransactionIsolator.ts"],"sourcesContent":[],"mappings":";;;;;;;AA2CA;;;;;;;;;;;;;;;;;AAE2C;;;;;;;;;;;;;;;;;;cAF9B,kDAEH,kCACT,OAAO,WACP,YAAY;iBAES,OAAO,YAAY;;;;;;;;;;iBAajC,OAAO,kBACN,0BACG,YAAY,cAAc,gBAClC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"VitestKyselyTransactionIsolator-CfSMOFlN.mjs","names":["_conn: Kysely<Database>","conn: Kysely<Database>","level: IsolationLevel","fn: (trx: Transaction<Database>) => Promise<void>"],"sources":["../src/VitestKyselyTransactionIsolator.ts"],"sourcesContent":["import type { Kysely, Transaction } from 'kysely';\nimport {\n\ttype IsolationLevel,\n\tVitestPostgresTransactionIsolator,\n} from './VitestTransactionIsolator';\n\n/**\n * Kysely-specific implementation of the Vitest transaction isolator.\n * Provides automatic transaction rollback for test isolation using Kysely's transaction API.\n * Each test runs within a database transaction that is rolled back after completion,\n * ensuring a clean state between tests without the overhead of recreating data.\n *\n * @template Database - The database schema type\n *\n * @example\n * ```typescript\n * import { VitestKyselyTransactionIsolator } from '@geekmidas/testkit';\n * import { db } from './database';\n *\n * // Create isolator instance\n * const isolator = new VitestKyselyTransactionIsolator<Database>();\n *\n * // In your test setup\n * beforeEach(async () => {\n * await isolator.start(db);\n * });\n *\n * afterEach(async () => {\n * await isolator.rollback();\n * });\n *\n * // Tests run in isolated transactions\n * it('should create user', async () => {\n * const user = await db.insertInto('users')\n * .values({ name: 'Test User' })\n * .returningAll()\n * .executeTakeFirst();\n *\n * expect(user).toBeDefined();\n * // This data will be rolled back after the test\n * });\n * ```\n */\nexport class VitestKyselyTransactionIsolator<\n\tDatabase,\n> extends VitestPostgresTransactionIsolator<\n\tKysely<Database>,\n\tTransaction<Database>\n> {\n\tasync destroy(_conn: Kysely<Database>): Promise<void> {\n\t\t// return conn.destroy();\n\t}\n\t/**\n\t * Creates a Kysely transaction with the specified isolation level.\n\t * Implements the abstract transact method from VitestPostgresTransactionIsolator.\n\t *\n\t * @param conn - The Kysely database connection\n\t * @param level - The transaction isolation level\n\t * @param fn - The function to execute within the transaction\n\t * @returns Promise that resolves when the transaction completes\n\t */\n\tasync transact(\n\t\tconn: Kysely<Database>,\n\t\tlevel: IsolationLevel,\n\t\tfn: (trx: Transaction<Database>) => Promise<void>,\n\t): Promise<void> {\n\t\tconst isolationLevel =\n\t\t\tlevel.toLocaleLowerCase() as Lowercase<IsolationLevel>;\n\t\tawait conn.transaction().setIsolationLevel(isolationLevel).execute(fn);\n\t}\n\t// Implement any Kysely-specific transaction logic here\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,IAAa,kCAAb,cAEU,kCAGR;CACD,MAAM,QAAQA,OAAwC,CAErD;;;;;;;;;;CAUD,MAAM,SACLC,MACAC,OACAC,IACgB;EAChB,MAAM,iBACL,MAAM,mBAAmB;AAC1B,QAAM,KAAK,aAAa,CAAC,kBAAkB,eAAe,CAAC,QAAQ,GAAG;CACtE;AAED"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"VitestKyselyTransactionIsolator-Cswnnj0k.d.mts","names":[],"sources":["../src/VitestKyselyTransactionIsolator.ts"],"sourcesContent":[],"mappings":";;;;;;;AA2CA;;;;;;;;;;;;;;;;;AAE2C;;;;;;;;;;;;;;;;;;cAF9B,kDAEH,kCACT,OAAO,WACP,YAAY;iBAES,OAAO,YAAY;;;;;;;;;;iBAajC,OAAO,kBACN,0BACG,YAAY,cAAc,gBAClC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"VitestTransactionIsolator-BNWJqh9f.d.mts","names":[],"sources":["../src/VitestTransactionIsolator.ts"],"sourcesContent":[],"mappings":";;;;;;AASA;AAWA;;;;AAGoC,UAdnB,gBAcmB,CAAA,WAAA,EAAA,YAAA,MAAA,CAAA,CAAA;EAAQ;AAe5C;;;EACiB,GACZ,EA1BC,WA0BD;;;AAAkB;AAQvB;AAkDsB,KA9EV,wBA8EU,CAAA,WAAiC,EAAA,WAAA,MAAA,CAAA,GA3EnD,gBA2EmD,CA3ElC,WA2EkC,CAAA,GA3EnB,QA2EmB;;;;;;;;;;;;;;AAkD5C,KA9GC,eA8GD,CAAA,WAAA,EAAA,QAAA,CAAA,GAAA,CAAA,GAAA,EA7GL,WA6GK,EAAA,GA5GN,QA4GM,GA5GK,OA4GL,CA5Ga,QA4Gb,CAAA;;;;;AAAuD;AA+ElE;AAAgC,aAnLpB,cAAA;EAmLoB;;;AAA6B;EACjD,gBAAA,GAAA,kBAAkB;EAAA;;;AAA6B;EAK1C,cAAA,GAAA,gBAAyB;EAAA;;;;EAMX,eAEhB,GAAA,iBAAA;EAAW;;;;EAIuB,YAArC,GAAA,cAAA;AAAe;AAO3B;;;;;;;;;;AAM2B;AAW3B;;;;;;;;;;;;;;;;AAiBuD,uBA5LjC,iCA4LiC,CAAA,KAAA,EAAA,WAAA,CAAA,CAAA;EAAC,iBACjD,GAAA;EAAO;AAqCd;;;;;;;;EAOgD,SAArC,QAAA,CAAA,IAAA,EA9NH,KA8NG,EAAA,cAAA,EA7NO,cA6NP,EAAA,EAAA,EAAA,CAAA,GAAA,EA5NC,WA4ND,EAAA,GA5NiB,OA4NjB,CAAA,IAAA,CAAA,CAAA,EA3NP,OA2NO,CAAA,IAAA,CAAA;EAAe,SACE,OAAA,CAAA,IAAA,EA1NJ,KA0NI,CAAA,EA1NI,OA0NJ,CAAA,IAAA,CAAA;EAAW;;;AAAZ;;mBApNQ;;;;;;;;;;;;;;;;;;;;;;;;;;6CA2BS,uCACjC,0BAA0B,OAAO,aAAa,YAAS,QAAA,iBAAA,uBAAA;;KA+EtD,mCAAmC,OAAO,QAAQ;KAClD,2BAA2B,qBAAqB;;;;UAK3C,+DAGC;;cAGL,mBAAmB;;gBAEjB,gBAAgB;;mBAEb;;aAEN,gBAAgB,aAAa;;;;;;KAO7B,8CAEM,yCAEL,iBACN,gBACD,SAAS,KAAK,QAAQ,SAAS;;;;;;;;;KAWzB,uDAEM,0CACA,WAAW,qBAAqB,WAChD,sBAEE;iDAIQ,iBAAiB,eAAe,WAAW,MAChD;kEAMK,iBAAiB,eAAe,WAAW,MAChD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAqCS,iDAEE,mCAEP,WAAW,uCAER,aACH,gBAAgB,aAAa,YACrC,yBAAyB,aAAa,UAAU"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"VitestTransactionIsolator-CSroc7Df.d.cts","names":[],"sources":["../src/VitestTransactionIsolator.ts"],"sourcesContent":[],"mappings":";;;;;;AASA;AAWA;;;;AAGoC,UAdnB,gBAcmB,CAAA,WAAA,EAAA,YAAA,MAAA,CAAA,CAAA;EAAQ;AAe5C;;;EACiB,GACZ,EA1BC,WA0BD;;;AAAkB;AAQvB;AAkDsB,KA9EV,wBA8EU,CAAA,WAAiC,EAAA,WAAA,MAAA,CAAA,GA3EnD,gBA2EmD,CA3ElC,WA2EkC,CAAA,GA3EnB,QA2EmB;;;;;;;;;;;;;;AAkD5C,KA9GC,eA8GD,CAAA,WAAA,EAAA,QAAA,CAAA,GAAA,CAAA,GAAA,EA7GL,WA6GK,EAAA,GA5GN,QA4GM,GA5GK,OA4GL,CA5Ga,QA4Gb,CAAA;;;;;AAAuD;AA+ElE;AAAgC,aAnLpB,cAAA;EAmLoB;;;AAA6B;EACjD,gBAAA,GAAA,kBAAkB;EAAA;;;AAA6B;EAK1C,cAAA,GAAA,gBAAyB;EAAA;;;;EAMX,eAEhB,GAAA,iBAAA;EAAW;;;;EAIuB,YAArC,GAAA,cAAA;AAAe;AAO3B;;;;;;;;;;AAM2B;AAW3B;;;;;;;;;;;;;;;;AAiBuD,uBA5LjC,iCA4LiC,CAAA,KAAA,EAAA,WAAA,CAAA,CAAA;EAAC,iBACjD,GAAA;EAAO;AAqCd;;;;;;;;EAOgD,SAArC,QAAA,CAAA,IAAA,EA9NH,KA8NG,EAAA,cAAA,EA7NO,cA6NP,EAAA,EAAA,EAAA,CAAA,GAAA,EA5NC,WA4ND,EAAA,GA5NiB,OA4NjB,CAAA,IAAA,CAAA,CAAA,EA3NP,OA2NO,CAAA,IAAA,CAAA;EAAe,SACE,OAAA,CAAA,IAAA,EA1NJ,KA0NI,CAAA,EA1NI,OA0NJ,CAAA,IAAA,CAAA;EAAW;;;AAAZ;;mBApNQ;;;;;;;;;;;;;;;;;;;;;;;;;;6CA2BS,uCACjC,0BAA0B,OAAO,aAAa,YAAS,QAAA,iBAAA,uBAAA;;KA+EtD,mCAAmC,OAAO,QAAQ;KAClD,2BAA2B,qBAAqB;;;;UAK3C,+DAGC;;cAGL,mBAAmB;;gBAEjB,gBAAgB;;mBAEb;;aAEN,gBAAgB,aAAa;;;;;;KAO7B,8CAEM,yCAEL,iBACN,gBACD,SAAS,KAAK,QAAQ,SAAS;;;;;;;;;KAWzB,uDAEM,0CACA,WAAW,qBAAqB,WAChD,sBAEE;iDAIQ,iBAAiB,eAAe,WAAW,MAChD;kEAMK,iBAAiB,eAAe,WAAW,MAChD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAqCS,iDAEE,mCAEP,WAAW,uCAER,aACH,gBAAgB,aAAa,YACrC,yBAAyB,aAAa,UAAU"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"VitestTransactionIsolator-CtZaOUjH.cjs","names":["api: TestAPI","options: TransactionWrapperOptions<TConn, Transaction, Extended>","additionalFixtures: Record<string, unknown>","use: (value: unknown) => Promise<void>","use: (value: Transaction) => Promise<void>","testError: Error | undefined","wrappedTest: T","fixtures: FixtureCreators<Transaction, Extended>","fixtureDefinitions: Record<string, any>"],"sources":["../src/VitestTransactionIsolator.ts"],"sourcesContent":["import type { TestAPI } from 'vitest';\n\n/**\n * Type definition for test fixtures that provide transaction access.\n * Used with Vitest's test.extend() API to inject transactions into tests.\n *\n * @template Transaction - The transaction type specific to the database driver\n * @template Extended - Additional context properties provided by the extend function\n */\nexport interface DatabaseFixtures<Transaction, _Extended = object> {\n\t/**\n\t * The database transaction available to the test.\n\t * All database operations should use this transaction to ensure proper rollback.\n\t */\n\ttrx: Transaction;\n}\n\n/**\n * Combined fixtures type that merges the base transaction fixture with extended context.\n */\nexport type ExtendedDatabaseFixtures<\n\tTransaction,\n\tExtended = object,\n> = DatabaseFixtures<Transaction> & Extended;\n\n/**\n * Function type for extending test context with additional properties.\n * Receives the transaction and returns additional context to be merged with { trx }.\n *\n * @template Transaction - The transaction type\n * @template Extended - The type of additional context to provide\n *\n * @example\n * ```typescript\n * const extendContext: ExtendContextFn<Transaction<DB>, { factory: KyselyFactory }> =\n * (trx) => ({ factory: new KyselyFactory(builders, seeds, trx) });\n * ```\n */\nexport type ExtendContextFn<Transaction, Extended> = (\n\ttrx: Transaction,\n) => Extended | Promise<Extended>;\n\n/**\n * PostgreSQL transaction isolation levels.\n * Controls the visibility of concurrent transactions.\n *\n * @see https://www.postgresql.org/docs/current/transaction-iso.html\n */\nexport enum IsolationLevel {\n\t/**\n\t * Lowest isolation level. Allows dirty reads.\n\t * Not recommended for testing.\n\t */\n\tREAD_UNCOMMITTED = 'READ UNCOMMITTED',\n\t/**\n\t * Default PostgreSQL isolation level.\n\t * Prevents dirty reads but allows non-repeatable reads.\n\t */\n\tREAD_COMMITTED = 'READ COMMITTED',\n\t/**\n\t * Prevents dirty reads and non-repeatable reads.\n\t * Recommended for most test scenarios.\n\t */\n\tREPEATABLE_READ = 'REPEATABLE READ',\n\t/**\n\t * Highest isolation level. Prevents all phenomena.\n\t * May cause performance overhead in tests.\n\t */\n\tSERIALIZABLE = 'SERIALIZABLE',\n}\n\n/**\n * Abstract base class for implementing database transaction isolation in Vitest tests.\n * Provides automatic transaction rollback after each test to maintain test isolation.\n * Subclasses must implement the transact() method for their specific database driver.\n *\n * @template TConn - The database connection type\n * @template Transaction - The transaction type\n *\n * @example\n * ```typescript\n * // Implement for your database driver\n * class MyDatabaseIsolator extends VitestPostgresTransactionIsolator<MyDB, MyTx> {\n * async transact(conn: MyDB, level: IsolationLevel, fn: (tx: MyTx) => Promise<void>) {\n * await conn.transaction(level, fn);\n * }\n * }\n *\n * // Use in tests\n * const isolator = new MyDatabaseIsolator(test);\n * const isolatedTest = isolator.wrapVitestWithTransaction(db);\n *\n * isolatedTest('should create user', async ({ trx }) => {\n * await trx.insert('users', { name: 'Test' });\n * // Data is automatically rolled back after test\n * });\n * ```\n */\nexport abstract class VitestPostgresTransactionIsolator<TConn, Transaction> {\n\t/**\n\t * Abstract method to create a transaction with the specified isolation level.\n\t * Must be implemented by subclasses for specific database drivers.\n\t *\n\t * @param conn - The database connection\n\t * @param isolationLevel - The transaction isolation level\n\t * @param fn - The function to execute within the transaction\n\t * @returns Promise that resolves when the transaction completes\n\t */\n\tabstract transact(\n\t\tconn: TConn,\n\t\tisolationLevel: IsolationLevel,\n\t\tfn: (trx: Transaction) => Promise<void>,\n\t): Promise<void>;\n\n\tabstract destroy(conn: TConn): Promise<void>;\n\t/**\n\t * Creates a new VitestPostgresTransactionIsolator instance.\n\t *\n\t * @param api - The Vitest test API (usually the `test` export from vitest)\n\t */\n\tconstructor(private readonly api: TestAPI) {}\n\n\t/**\n\t * Creates a wrapped version of Vitest's test API that provides transaction isolation.\n\t * Each test will run within a database transaction that is automatically rolled back.\n\t *\n\t * @param options - Configuration options for transaction wrapping\n\t * @returns A wrapped test API with transaction support\n\t *\n\t * @example\n\t * ```typescript\n\t * const isolatedTest = isolator.wrapVitestWithTransaction({\n\t * connection: db,\n\t * setup: async (trx) => {\n\t * await trx.insert('settings', { key: 'test', value: 'true' });\n\t * },\n\t * fixtures: {\n\t * factory: (trx) => new Factory(trx),\n\t * },\n\t * });\n\t *\n\t * isolatedTest('test with transaction', async ({ trx, factory }) => {\n\t * const user = await factory.insert('user', { name: 'Test' });\n\t * expect(user).toBeDefined();\n\t * });\n\t * ```\n\t */\n\twrapVitestWithTransaction<Extended extends Record<string, unknown> = {}>(\n\t\toptions: TransactionWrapperOptions<TConn, Transaction, Extended>,\n\t) {\n\t\tconst {\n\t\t\tconnection,\n\t\t\tsetup,\n\t\t\tisolationLevel = IsolationLevel.REPEATABLE_READ,\n\t\t\tfixtures,\n\t\t} = options;\n\n\t\t// Build fixture definitions for additional fixtures that depend on trx\n\t\tconst additionalFixtures: Record<string, unknown> = {};\n\t\tif (fixtures) {\n\t\t\tfor (const [key, creator] of Object.entries(fixtures)) {\n\t\t\t\tadditionalFixtures[key] = async (\n\t\t\t\t\t{ trx }: { trx: Transaction },\n\t\t\t\t\tuse: (value: unknown) => Promise<void>,\n\t\t\t\t) => {\n\t\t\t\t\tconst value = await (creator as (trx: Transaction) => unknown)(trx);\n\t\t\t\t\tawait use(value);\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\n\t\ttype CombinedFixtures = DatabaseFixtures<Transaction> & Extended;\n\n\t\t// Cast to bypass Vitest's strict fixture typing which can't infer\n\t\t// dynamically built fixture objects\n\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\tconst api = this.api as TestAPI & {\n\t\t\textend: <T>(fixtures: any) => TestAPI<T>;\n\t\t};\n\n\t\treturn api.extend<CombinedFixtures>({\n\t\t\t// This fixture automatically provides a transaction to each test\n\t\t\t// biome-ignore lint/correctness/noEmptyPattern: this has to be like this to satisfy Biome\n\t\t\ttrx: async ({}: {}, use: (value: Transaction) => Promise<void>) => {\n\t\t\t\t// Create a custom error class for rollback\n\t\t\t\tclass TestRollback extends Error {\n\t\t\t\t\tconstructor() {\n\t\t\t\t\t\tsuper('Test rollback');\n\t\t\t\t\t\tthis.name = 'TestRollback';\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tlet testError: Error | undefined;\n\t\t\t\tconst conn = await connection();\n\t\t\t\ttry {\n\t\t\t\t\tawait this.transact(conn, isolationLevel, async (transaction) => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t// Provide the transaction to the test\n\t\t\t\t\t\t\tawait setup?.(transaction);\n\t\t\t\t\t\t\tawait use(transaction);\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\t// Capture any test errors\n\t\t\t\t\t\t\ttestError = error as Error;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Always throw to trigger rollback\n\t\t\t\t\t\tthrow new TestRollback();\n\t\t\t\t\t});\n\t\t\t\t} catch (error) {\n\t\t\t\t\t// Only rethrow if it's not our rollback error\n\t\t\t\t\tif (!(error instanceof TestRollback)) {\n\t\t\t\t\t\tthrow error;\n\t\t\t\t\t}\n\n\t\t\t\t\t// If the test had an error, throw it now\n\t\t\t\t\tif (testError) {\n\t\t\t\t\t\tthrow testError;\n\t\t\t\t\t}\n\t\t\t\t} finally {\n\t\t\t\t\tawait this.destroy(conn);\n\t\t\t\t}\n\t\t\t},\n\t\t\t...additionalFixtures,\n\t\t});\n\t}\n}\n\nexport type DatabaseConnectionFn<Conn> = () => Conn | Promise<Conn>;\nexport type DatabaseConnection<Conn> = DatabaseConnectionFn<Conn>;\n\n/**\n * Options for wrapping Vitest tests with database transaction isolation.\n */\nexport interface TransactionWrapperOptions<\n\tTConn,\n\tTransaction,\n\tExtended extends Record<string, unknown> = {},\n> {\n\t/** Function that creates or returns a database connection */\n\tconnection: DatabaseConnection<TConn>;\n\t/** Optional setup function to run within the transaction before each test */\n\tsetup?: (trx: Transaction) => Promise<void>;\n\t/** Transaction isolation level (defaults to REPEATABLE_READ) */\n\tisolationLevel?: IsolationLevel;\n\t/** Additional fixtures that depend on the transaction */\n\tfixtures?: FixtureCreators<Transaction, Extended>;\n}\n\n/**\n * Type for fixture creator functions that depend on the transaction.\n * Each function receives the transaction and returns the fixture value.\n */\nexport type FixtureCreators<\n\tTransaction,\n\tExtended extends Record<string, unknown>,\n> = {\n\t[K in keyof Extended]: (\n\t\ttrx: Transaction,\n\t) => Extended[K] | Promise<Extended[K]>;\n};\n\n/**\n * The test API returned by extendWithFixtures.\n * Provides access to both the transaction (trx) and all extended fixtures.\n *\n * @template Transaction - The transaction type\n * @template Extended - The type of additional fixtures provided\n * @template BaseTest - The base wrapped test type\n */\nexport type TestWithExtendedFixtures<\n\tTransaction,\n\tExtended extends Record<string, unknown>,\n\tBaseTest extends ReturnType<TestAPI['extend']> = ReturnType<\n\t\tTestAPI['extend']\n\t>,\n> = BaseTest & {\n\t<C extends object>(\n\t\tname: string,\n\t\tfn: (\n\t\t\tcontext: DatabaseFixtures<Transaction> & Extended & C,\n\t\t) => Promise<void>,\n\t): void;\n\t<C extends object>(\n\t\tname: string,\n\t\toptions: object,\n\t\tfn: (\n\t\t\tcontext: DatabaseFixtures<Transaction> & Extended & C,\n\t\t) => Promise<void>,\n\t): void;\n};\n\n/**\n * Extends a wrapped test API with additional fixtures that depend on the transaction.\n * This allows composing test context with factories, repositories, or other helpers.\n *\n * @template Transaction - The transaction type\n * @template Extended - The type of additional context to provide\n * @param wrappedTest - The base wrapped test from wrapVitestWithTransaction\n * @param fixtures - Object mapping fixture names to creator functions\n * @returns An extended test API with both trx and the additional fixtures\n *\n * @example\n * ```typescript\n * import { wrapVitestKyselyTransaction, extendWithFixtures } from '@geekmidas/testkit/kysely';\n *\n * // Create base wrapped test\n * const baseTest = wrapVitestKyselyTransaction(test, {\n * connection: db,\n * setup: createTestTables,\n * });\n *\n * // Extend with fixtures\n * const it = extendWithFixtures(baseTest, {\n * factory: (trx) => new KyselyFactory(builders, seeds, trx),\n * userRepo: (trx) => new UserRepository(trx),\n * });\n *\n * // Use in tests - trx and all fixtures are available\n * it('should create user with factory', async ({ trx, factory, userRepo }) => {\n * const user = await factory.insert('user', { name: 'Test' });\n * expect(user).toBeDefined();\n * });\n * ```\n */\nexport function extendWithFixtures<\n\tTransaction,\n\tExtended extends Record<string, unknown>,\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tT extends ReturnType<TestAPI['extend']> = any,\n>(\n\twrappedTest: T,\n\tfixtures: FixtureCreators<Transaction, Extended>,\n): TestWithExtendedFixtures<Transaction, Extended, T> {\n\t// Build fixture definitions for Vitest's extend API\n\tconst fixtureDefinitions: Record<string, any> = {};\n\n\tfor (const [key, creator] of Object.entries(fixtures)) {\n\t\tfixtureDefinitions[key] = async (\n\t\t\t{ trx }: { trx: Transaction },\n\t\t\tuse: (value: unknown) => Promise<void>,\n\t\t) => {\n\t\t\tconst value = await (creator as (trx: Transaction) => unknown)(trx);\n\t\t\tawait use(value);\n\t\t};\n\t}\n\n\treturn (wrappedTest as any).extend(fixtureDefinitions);\n}\n"],"mappings":";;;;;;;;AAgDA,IAAY,4DAAL;;;;;AAKN;;;;;AAKA;;;;;AAKA;;;;;AAKA;;AACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BD,IAAsB,oCAAtB,MAA4E;;;;;;CAsB3E,YAA6BA,KAAc;EAAd;CAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;CA2B7C,0BACCC,SACC;EACD,MAAM,EACL,YACA,OACA,iBAAiB,eAAe,iBAChC,UACA,GAAG;EAGJ,MAAMC,qBAA8C,CAAE;AACtD,MAAI,SACH,MAAK,MAAM,CAAC,KAAK,QAAQ,IAAI,OAAO,QAAQ,SAAS,CACpD,oBAAmB,OAAO,OACzB,EAAE,KAA2B,EAC7BC,QACI;GACJ,MAAM,QAAQ,MAAM,AAAC,QAA0C,IAAI;AACnE,SAAM,IAAI,MAAM;EAChB;EASH,MAAM,MAAM,KAAK;AAIjB,SAAO,IAAI,OAAyB;GAGnC,KAAK,OAAO,EAAM,EAAEC,QAA+C;IAElE,MAAM,qBAAqB,MAAM;KAChC,cAAc;AACb,YAAM,gBAAgB;AACtB,WAAK,OAAO;KACZ;IACD;IAED,IAAIC;IACJ,MAAM,OAAO,MAAM,YAAY;AAC/B,QAAI;AACH,WAAM,KAAK,SAAS,MAAM,gBAAgB,OAAO,gBAAgB;AAChE,UAAI;AAEH,aAAM,QAAQ,YAAY;AAC1B,aAAM,IAAI,YAAY;MACtB,SAAQ,OAAO;AAEf,mBAAY;MACZ;AAGD,YAAM,IAAI;KACV,EAAC;IACF,SAAQ,OAAO;AAEf,WAAM,iBAAiB,cACtB,OAAM;AAIP,SAAI,UACH,OAAM;IAEP,UAAS;AACT,WAAM,KAAK,QAAQ,KAAK;IACxB;GACD;GACD,GAAG;EACH,EAAC;CACF;AACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmGD,SAAgB,mBAMfC,aACAC,UACqD;CAErD,MAAMC,qBAA0C,CAAE;AAElD,MAAK,MAAM,CAAC,KAAK,QAAQ,IAAI,OAAO,QAAQ,SAAS,CACpD,oBAAmB,OAAO,OACzB,EAAE,KAA2B,EAC7BL,QACI;EACJ,MAAM,QAAQ,MAAM,AAAC,QAA0C,IAAI;AACnE,QAAM,IAAI,MAAM;CAChB;AAGF,QAAO,AAAC,YAAoB,OAAO,mBAAmB;AACtD"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"VitestTransactionIsolator-D6ZxLWys.mjs","names":["api: TestAPI","options: TransactionWrapperOptions<TConn, Transaction, Extended>","additionalFixtures: Record<string, unknown>","use: (value: unknown) => Promise<void>","use: (value: Transaction) => Promise<void>","testError: Error | undefined","wrappedTest: T","fixtures: FixtureCreators<Transaction, Extended>","fixtureDefinitions: Record<string, any>"],"sources":["../src/VitestTransactionIsolator.ts"],"sourcesContent":["import type { TestAPI } from 'vitest';\n\n/**\n * Type definition for test fixtures that provide transaction access.\n * Used with Vitest's test.extend() API to inject transactions into tests.\n *\n * @template Transaction - The transaction type specific to the database driver\n * @template Extended - Additional context properties provided by the extend function\n */\nexport interface DatabaseFixtures<Transaction, _Extended = object> {\n\t/**\n\t * The database transaction available to the test.\n\t * All database operations should use this transaction to ensure proper rollback.\n\t */\n\ttrx: Transaction;\n}\n\n/**\n * Combined fixtures type that merges the base transaction fixture with extended context.\n */\nexport type ExtendedDatabaseFixtures<\n\tTransaction,\n\tExtended = object,\n> = DatabaseFixtures<Transaction> & Extended;\n\n/**\n * Function type for extending test context with additional properties.\n * Receives the transaction and returns additional context to be merged with { trx }.\n *\n * @template Transaction - The transaction type\n * @template Extended - The type of additional context to provide\n *\n * @example\n * ```typescript\n * const extendContext: ExtendContextFn<Transaction<DB>, { factory: KyselyFactory }> =\n * (trx) => ({ factory: new KyselyFactory(builders, seeds, trx) });\n * ```\n */\nexport type ExtendContextFn<Transaction, Extended> = (\n\ttrx: Transaction,\n) => Extended | Promise<Extended>;\n\n/**\n * PostgreSQL transaction isolation levels.\n * Controls the visibility of concurrent transactions.\n *\n * @see https://www.postgresql.org/docs/current/transaction-iso.html\n */\nexport enum IsolationLevel {\n\t/**\n\t * Lowest isolation level. Allows dirty reads.\n\t * Not recommended for testing.\n\t */\n\tREAD_UNCOMMITTED = 'READ UNCOMMITTED',\n\t/**\n\t * Default PostgreSQL isolation level.\n\t * Prevents dirty reads but allows non-repeatable reads.\n\t */\n\tREAD_COMMITTED = 'READ COMMITTED',\n\t/**\n\t * Prevents dirty reads and non-repeatable reads.\n\t * Recommended for most test scenarios.\n\t */\n\tREPEATABLE_READ = 'REPEATABLE READ',\n\t/**\n\t * Highest isolation level. Prevents all phenomena.\n\t * May cause performance overhead in tests.\n\t */\n\tSERIALIZABLE = 'SERIALIZABLE',\n}\n\n/**\n * Abstract base class for implementing database transaction isolation in Vitest tests.\n * Provides automatic transaction rollback after each test to maintain test isolation.\n * Subclasses must implement the transact() method for their specific database driver.\n *\n * @template TConn - The database connection type\n * @template Transaction - The transaction type\n *\n * @example\n * ```typescript\n * // Implement for your database driver\n * class MyDatabaseIsolator extends VitestPostgresTransactionIsolator<MyDB, MyTx> {\n * async transact(conn: MyDB, level: IsolationLevel, fn: (tx: MyTx) => Promise<void>) {\n * await conn.transaction(level, fn);\n * }\n * }\n *\n * // Use in tests\n * const isolator = new MyDatabaseIsolator(test);\n * const isolatedTest = isolator.wrapVitestWithTransaction(db);\n *\n * isolatedTest('should create user', async ({ trx }) => {\n * await trx.insert('users', { name: 'Test' });\n * // Data is automatically rolled back after test\n * });\n * ```\n */\nexport abstract class VitestPostgresTransactionIsolator<TConn, Transaction> {\n\t/**\n\t * Abstract method to create a transaction with the specified isolation level.\n\t * Must be implemented by subclasses for specific database drivers.\n\t *\n\t * @param conn - The database connection\n\t * @param isolationLevel - The transaction isolation level\n\t * @param fn - The function to execute within the transaction\n\t * @returns Promise that resolves when the transaction completes\n\t */\n\tabstract transact(\n\t\tconn: TConn,\n\t\tisolationLevel: IsolationLevel,\n\t\tfn: (trx: Transaction) => Promise<void>,\n\t): Promise<void>;\n\n\tabstract destroy(conn: TConn): Promise<void>;\n\t/**\n\t * Creates a new VitestPostgresTransactionIsolator instance.\n\t *\n\t * @param api - The Vitest test API (usually the `test` export from vitest)\n\t */\n\tconstructor(private readonly api: TestAPI) {}\n\n\t/**\n\t * Creates a wrapped version of Vitest's test API that provides transaction isolation.\n\t * Each test will run within a database transaction that is automatically rolled back.\n\t *\n\t * @param options - Configuration options for transaction wrapping\n\t * @returns A wrapped test API with transaction support\n\t *\n\t * @example\n\t * ```typescript\n\t * const isolatedTest = isolator.wrapVitestWithTransaction({\n\t * connection: db,\n\t * setup: async (trx) => {\n\t * await trx.insert('settings', { key: 'test', value: 'true' });\n\t * },\n\t * fixtures: {\n\t * factory: (trx) => new Factory(trx),\n\t * },\n\t * });\n\t *\n\t * isolatedTest('test with transaction', async ({ trx, factory }) => {\n\t * const user = await factory.insert('user', { name: 'Test' });\n\t * expect(user).toBeDefined();\n\t * });\n\t * ```\n\t */\n\twrapVitestWithTransaction<Extended extends Record<string, unknown> = {}>(\n\t\toptions: TransactionWrapperOptions<TConn, Transaction, Extended>,\n\t) {\n\t\tconst {\n\t\t\tconnection,\n\t\t\tsetup,\n\t\t\tisolationLevel = IsolationLevel.REPEATABLE_READ,\n\t\t\tfixtures,\n\t\t} = options;\n\n\t\t// Build fixture definitions for additional fixtures that depend on trx\n\t\tconst additionalFixtures: Record<string, unknown> = {};\n\t\tif (fixtures) {\n\t\t\tfor (const [key, creator] of Object.entries(fixtures)) {\n\t\t\t\tadditionalFixtures[key] = async (\n\t\t\t\t\t{ trx }: { trx: Transaction },\n\t\t\t\t\tuse: (value: unknown) => Promise<void>,\n\t\t\t\t) => {\n\t\t\t\t\tconst value = await (creator as (trx: Transaction) => unknown)(trx);\n\t\t\t\t\tawait use(value);\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\n\t\ttype CombinedFixtures = DatabaseFixtures<Transaction> & Extended;\n\n\t\t// Cast to bypass Vitest's strict fixture typing which can't infer\n\t\t// dynamically built fixture objects\n\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\tconst api = this.api as TestAPI & {\n\t\t\textend: <T>(fixtures: any) => TestAPI<T>;\n\t\t};\n\n\t\treturn api.extend<CombinedFixtures>({\n\t\t\t// This fixture automatically provides a transaction to each test\n\t\t\t// biome-ignore lint/correctness/noEmptyPattern: this has to be like this to satisfy Biome\n\t\t\ttrx: async ({}: {}, use: (value: Transaction) => Promise<void>) => {\n\t\t\t\t// Create a custom error class for rollback\n\t\t\t\tclass TestRollback extends Error {\n\t\t\t\t\tconstructor() {\n\t\t\t\t\t\tsuper('Test rollback');\n\t\t\t\t\t\tthis.name = 'TestRollback';\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tlet testError: Error | undefined;\n\t\t\t\tconst conn = await connection();\n\t\t\t\ttry {\n\t\t\t\t\tawait this.transact(conn, isolationLevel, async (transaction) => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t// Provide the transaction to the test\n\t\t\t\t\t\t\tawait setup?.(transaction);\n\t\t\t\t\t\t\tawait use(transaction);\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\t// Capture any test errors\n\t\t\t\t\t\t\ttestError = error as Error;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Always throw to trigger rollback\n\t\t\t\t\t\tthrow new TestRollback();\n\t\t\t\t\t});\n\t\t\t\t} catch (error) {\n\t\t\t\t\t// Only rethrow if it's not our rollback error\n\t\t\t\t\tif (!(error instanceof TestRollback)) {\n\t\t\t\t\t\tthrow error;\n\t\t\t\t\t}\n\n\t\t\t\t\t// If the test had an error, throw it now\n\t\t\t\t\tif (testError) {\n\t\t\t\t\t\tthrow testError;\n\t\t\t\t\t}\n\t\t\t\t} finally {\n\t\t\t\t\tawait this.destroy(conn);\n\t\t\t\t}\n\t\t\t},\n\t\t\t...additionalFixtures,\n\t\t});\n\t}\n}\n\nexport type DatabaseConnectionFn<Conn> = () => Conn | Promise<Conn>;\nexport type DatabaseConnection<Conn> = DatabaseConnectionFn<Conn>;\n\n/**\n * Options for wrapping Vitest tests with database transaction isolation.\n */\nexport interface TransactionWrapperOptions<\n\tTConn,\n\tTransaction,\n\tExtended extends Record<string, unknown> = {},\n> {\n\t/** Function that creates or returns a database connection */\n\tconnection: DatabaseConnection<TConn>;\n\t/** Optional setup function to run within the transaction before each test */\n\tsetup?: (trx: Transaction) => Promise<void>;\n\t/** Transaction isolation level (defaults to REPEATABLE_READ) */\n\tisolationLevel?: IsolationLevel;\n\t/** Additional fixtures that depend on the transaction */\n\tfixtures?: FixtureCreators<Transaction, Extended>;\n}\n\n/**\n * Type for fixture creator functions that depend on the transaction.\n * Each function receives the transaction and returns the fixture value.\n */\nexport type FixtureCreators<\n\tTransaction,\n\tExtended extends Record<string, unknown>,\n> = {\n\t[K in keyof Extended]: (\n\t\ttrx: Transaction,\n\t) => Extended[K] | Promise<Extended[K]>;\n};\n\n/**\n * The test API returned by extendWithFixtures.\n * Provides access to both the transaction (trx) and all extended fixtures.\n *\n * @template Transaction - The transaction type\n * @template Extended - The type of additional fixtures provided\n * @template BaseTest - The base wrapped test type\n */\nexport type TestWithExtendedFixtures<\n\tTransaction,\n\tExtended extends Record<string, unknown>,\n\tBaseTest extends ReturnType<TestAPI['extend']> = ReturnType<\n\t\tTestAPI['extend']\n\t>,\n> = BaseTest & {\n\t<C extends object>(\n\t\tname: string,\n\t\tfn: (\n\t\t\tcontext: DatabaseFixtures<Transaction> & Extended & C,\n\t\t) => Promise<void>,\n\t): void;\n\t<C extends object>(\n\t\tname: string,\n\t\toptions: object,\n\t\tfn: (\n\t\t\tcontext: DatabaseFixtures<Transaction> & Extended & C,\n\t\t) => Promise<void>,\n\t): void;\n};\n\n/**\n * Extends a wrapped test API with additional fixtures that depend on the transaction.\n * This allows composing test context with factories, repositories, or other helpers.\n *\n * @template Transaction - The transaction type\n * @template Extended - The type of additional context to provide\n * @param wrappedTest - The base wrapped test from wrapVitestWithTransaction\n * @param fixtures - Object mapping fixture names to creator functions\n * @returns An extended test API with both trx and the additional fixtures\n *\n * @example\n * ```typescript\n * import { wrapVitestKyselyTransaction, extendWithFixtures } from '@geekmidas/testkit/kysely';\n *\n * // Create base wrapped test\n * const baseTest = wrapVitestKyselyTransaction(test, {\n * connection: db,\n * setup: createTestTables,\n * });\n *\n * // Extend with fixtures\n * const it = extendWithFixtures(baseTest, {\n * factory: (trx) => new KyselyFactory(builders, seeds, trx),\n * userRepo: (trx) => new UserRepository(trx),\n * });\n *\n * // Use in tests - trx and all fixtures are available\n * it('should create user with factory', async ({ trx, factory, userRepo }) => {\n * const user = await factory.insert('user', { name: 'Test' });\n * expect(user).toBeDefined();\n * });\n * ```\n */\nexport function extendWithFixtures<\n\tTransaction,\n\tExtended extends Record<string, unknown>,\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tT extends ReturnType<TestAPI['extend']> = any,\n>(\n\twrappedTest: T,\n\tfixtures: FixtureCreators<Transaction, Extended>,\n): TestWithExtendedFixtures<Transaction, Extended, T> {\n\t// Build fixture definitions for Vitest's extend API\n\tconst fixtureDefinitions: Record<string, any> = {};\n\n\tfor (const [key, creator] of Object.entries(fixtures)) {\n\t\tfixtureDefinitions[key] = async (\n\t\t\t{ trx }: { trx: Transaction },\n\t\t\tuse: (value: unknown) => Promise<void>,\n\t\t) => {\n\t\t\tconst value = await (creator as (trx: Transaction) => unknown)(trx);\n\t\t\tawait use(value);\n\t\t};\n\t}\n\n\treturn (wrappedTest as any).extend(fixtureDefinitions);\n}\n"],"mappings":";;;;;;;AAgDA,IAAY,4DAAL;;;;;AAKN;;;;;AAKA;;;;;AAKA;;;;;AAKA;;AACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BD,IAAsB,oCAAtB,MAA4E;;;;;;CAsB3E,YAA6BA,KAAc;EAAd;CAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;CA2B7C,0BACCC,SACC;EACD,MAAM,EACL,YACA,OACA,iBAAiB,eAAe,iBAChC,UACA,GAAG;EAGJ,MAAMC,qBAA8C,CAAE;AACtD,MAAI,SACH,MAAK,MAAM,CAAC,KAAK,QAAQ,IAAI,OAAO,QAAQ,SAAS,CACpD,oBAAmB,OAAO,OACzB,EAAE,KAA2B,EAC7BC,QACI;GACJ,MAAM,QAAQ,MAAM,AAAC,QAA0C,IAAI;AACnE,SAAM,IAAI,MAAM;EAChB;EASH,MAAM,MAAM,KAAK;AAIjB,SAAO,IAAI,OAAyB;GAGnC,KAAK,OAAO,EAAM,EAAEC,QAA+C;IAElE,MAAM,qBAAqB,MAAM;KAChC,cAAc;AACb,YAAM,gBAAgB;AACtB,WAAK,OAAO;KACZ;IACD;IAED,IAAIC;IACJ,MAAM,OAAO,MAAM,YAAY;AAC/B,QAAI;AACH,WAAM,KAAK,SAAS,MAAM,gBAAgB,OAAO,gBAAgB;AAChE,UAAI;AAEH,aAAM,QAAQ,YAAY;AAC1B,aAAM,IAAI,YAAY;MACtB,SAAQ,OAAO;AAEf,mBAAY;MACZ;AAGD,YAAM,IAAI;KACV,EAAC;IACF,SAAQ,OAAO;AAEf,WAAM,iBAAiB,cACtB,OAAM;AAIP,SAAI,UACH,OAAM;IAEP,UAAS;AACT,WAAM,KAAK,QAAQ,KAAK;IACxB;GACD;GACD,GAAG;EACH,EAAC;CACF;AACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmGD,SAAgB,mBAMfC,aACAC,UACqD;CAErD,MAAMC,qBAA0C,CAAE;AAElD,MAAK,MAAM,CAAC,KAAK,QAAQ,IAAI,OAAO,QAAQ,SAAS,CACpD,oBAAmB,OAAO,OACzB,EAAE,KAA2B,EAC7BL,QACI;EACJ,MAAM,QAAQ,MAAM,AAAC,QAA0C,IAAI;AACnE,QAAM,IAAI,MAAM;CAChB;AAGF,QAAO,AAAC,YAAoB,OAAO,mBAAmB;AACtD"}
|
package/src/Factory.ts
DELETED
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
import type { FakerFactory } from './faker';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Abstract base class for database factories used in testing.
|
|
5
|
-
* Provides a standardized interface for creating test data using builder and seed patterns.
|
|
6
|
-
*
|
|
7
|
-
* @template Builders - Record of builder functions for creating individual entities
|
|
8
|
-
* @template Seeds - Record of seed functions for creating complex test scenarios
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* ```typescript
|
|
12
|
-
* // Define builders for creating individual records
|
|
13
|
-
* const builders = {
|
|
14
|
-
* user: (attrs) => ({ name: 'Test User', email: 'test@example.com', ...attrs }),
|
|
15
|
-
* post: (attrs) => ({ title: 'Test Post', content: 'Content', ...attrs })
|
|
16
|
-
* };
|
|
17
|
-
*
|
|
18
|
-
* // Define seeds for complex scenarios
|
|
19
|
-
* const seeds = {
|
|
20
|
-
* userWithPosts: async (attrs, factory) => {
|
|
21
|
-
* const user = await factory.insert('user', attrs);
|
|
22
|
-
* await factory.insertMany(3, 'post', { userId: user.id });
|
|
23
|
-
* return user;
|
|
24
|
-
* }
|
|
25
|
-
* };
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
export abstract class Factory<
|
|
29
|
-
Builders extends Record<string, any>,
|
|
30
|
-
Seeds extends Record<string, any>,
|
|
31
|
-
> {
|
|
32
|
-
/**
|
|
33
|
-
* Creates a typed seed function with proper type inference.
|
|
34
|
-
* This is a utility method to help with TypeScript type checking when defining seeds.
|
|
35
|
-
*
|
|
36
|
-
* @template Seed - The seed function type
|
|
37
|
-
* @param seedFn - The seed function to wrap
|
|
38
|
-
* @returns The same seed function with proper typing
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* ```typescript
|
|
42
|
-
* const userWithPostsSeed = Factory.createSeed(async ({ attrs, factory, db }) => {
|
|
43
|
-
* const user = await factory.insert('user', attrs);
|
|
44
|
-
* return user;
|
|
45
|
-
* });
|
|
46
|
-
* ```
|
|
47
|
-
*/
|
|
48
|
-
static createSeed<Seed extends FactorySeed>(seedFn: Seed): Seed {
|
|
49
|
-
return seedFn;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Inserts an object into the database using a builder function.
|
|
53
|
-
*
|
|
54
|
-
* @param builderName - The name of the builder to use
|
|
55
|
-
* @param attrs - The attributes to insert
|
|
56
|
-
*/
|
|
57
|
-
abstract insert<K extends keyof Builders>(
|
|
58
|
-
builderName: K,
|
|
59
|
-
attrs?: Parameters<Builders[K]>[0],
|
|
60
|
-
): Promise<Awaited<ReturnType<Builders[K]>>>;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Inserts multiple objects into the database
|
|
64
|
-
*
|
|
65
|
-
* @param count - Number of objects to insert
|
|
66
|
-
* @param builderName - The name of the builder to use
|
|
67
|
-
* @param attrs - The attributes to insert
|
|
68
|
-
*/
|
|
69
|
-
abstract insertMany<K extends keyof Builders>(
|
|
70
|
-
count: number,
|
|
71
|
-
builderName: K,
|
|
72
|
-
attrs?:
|
|
73
|
-
| Parameters<Builders[K]>[0]
|
|
74
|
-
| ((
|
|
75
|
-
idx: number,
|
|
76
|
-
faker: FakerFactory,
|
|
77
|
-
) => Promise<Parameters<Builders[K]>[0]>),
|
|
78
|
-
): Promise<Awaited<ReturnType<Builders[K]>>[]>;
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Seeds the database using a seed function.
|
|
82
|
-
*
|
|
83
|
-
* @param seedName - The name of the seed to use
|
|
84
|
-
* @returns The result of the seed function
|
|
85
|
-
* @param attrs - The attributes to pass to the seed function
|
|
86
|
-
*/
|
|
87
|
-
abstract seed<K extends keyof Seeds>(
|
|
88
|
-
seedName: K,
|
|
89
|
-
attrs?: ExtractSeedAttrs<Seeds[K]>,
|
|
90
|
-
): ReturnType<Seeds[K]>;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Type definition for a factory builder function that can work with different database types.
|
|
95
|
-
* Builders are responsible for creating individual database records with default values and relationships.
|
|
96
|
-
*
|
|
97
|
-
* @template Attrs - The attributes/input type for the builder
|
|
98
|
-
* @template Factory - The factory instance type
|
|
99
|
-
* @template Result - The type of object returned by the builder
|
|
100
|
-
* @template DB - The database connection type (Kysely, Knex, etc.)
|
|
101
|
-
*
|
|
102
|
-
* @param attrs - Partial attributes to override defaults
|
|
103
|
-
* @param factory - The factory instance for creating related records
|
|
104
|
-
* @param db - The database connection
|
|
105
|
-
* @returns The created record or a promise resolving to it
|
|
106
|
-
*
|
|
107
|
-
* @example
|
|
108
|
-
* ```typescript
|
|
109
|
-
* const userBuilder: MixedFactoryBuilder<UserAttrs, Factory, User, Kysely<DB>> =
|
|
110
|
-
* async (attrs, factory, db) => {
|
|
111
|
-
* return {
|
|
112
|
-
* id: faker.string.uuid(),
|
|
113
|
-
* name: faker.person.fullName(),
|
|
114
|
-
* email: faker.internet.email(),
|
|
115
|
-
* ...attrs
|
|
116
|
-
* };
|
|
117
|
-
* };
|
|
118
|
-
* ```
|
|
119
|
-
*/
|
|
120
|
-
export type MixedFactoryBuilder<
|
|
121
|
-
Attrs = any,
|
|
122
|
-
Factory = any,
|
|
123
|
-
Result = any,
|
|
124
|
-
DB = any,
|
|
125
|
-
> = (attrs: Attrs, factory: Factory, db: DB) => Result | Promise<Result>;
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Type definition for a factory seed function used to create complex test scenarios.
|
|
129
|
-
* Seeds typically create multiple related records to set up a complete test environment.
|
|
130
|
-
*
|
|
131
|
-
* @template Attrs - The attributes/input type for the seed
|
|
132
|
-
* @template Factory - The factory instance type
|
|
133
|
-
* @template Result - The type of object returned by the seed
|
|
134
|
-
* @template DB - The database connection type (Kysely, Knex, etc.)
|
|
135
|
-
*
|
|
136
|
-
* @param context - Object containing attrs, factory, and db
|
|
137
|
-
* @param context.attrs - Configuration attributes for the seed
|
|
138
|
-
* @param context.factory - The factory instance for creating records
|
|
139
|
-
* @param context.db - The database connection
|
|
140
|
-
* @returns A promise resolving to the seed result
|
|
141
|
-
*
|
|
142
|
-
* @example
|
|
143
|
-
* ```typescript
|
|
144
|
-
* const userWithPostsSeed: FactorySeed<{ postCount?: number }, Factory, User, DB> =
|
|
145
|
-
* async ({ attrs, factory, db }) => {
|
|
146
|
-
* const user = await factory.insert('user', attrs);
|
|
147
|
-
* const postCount = attrs.postCount || 3;
|
|
148
|
-
*
|
|
149
|
-
* for (let i = 0; i < postCount; i++) {
|
|
150
|
-
* await factory.insert('post', { userId: user.id });
|
|
151
|
-
* }
|
|
152
|
-
*
|
|
153
|
-
* return user;
|
|
154
|
-
* };
|
|
155
|
-
* ```
|
|
156
|
-
*/
|
|
157
|
-
export type FactorySeed<
|
|
158
|
-
Attrs = any,
|
|
159
|
-
Factory = any,
|
|
160
|
-
Result = any,
|
|
161
|
-
DB = any,
|
|
162
|
-
> = (context: { attrs: Attrs; factory: Factory; db: DB }) => Promise<Result>;
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Helper type to extract the Attrs type from a FactorySeed function.
|
|
166
|
-
* Used internally by Factory implementations to correctly type the seed method parameters.
|
|
167
|
-
*/
|
|
168
|
-
export type ExtractSeedAttrs<T> = T extends (context: {
|
|
169
|
-
attrs: infer A;
|
|
170
|
-
factory: any;
|
|
171
|
-
db: any;
|
|
172
|
-
}) => any
|
|
173
|
-
? A
|
|
174
|
-
: never;
|