@geekmidas/testkit 0.4.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (209) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/Factory-BFVnMMCC.mjs.map +1 -1
  3. package/dist/{Factory-c16c27Y6.d.cts → Factory-BOX312yd.d.cts} +3 -2
  4. package/dist/Factory-BOX312yd.d.cts.map +1 -0
  5. package/dist/Factory-BhjUOBWN.cjs.map +1 -1
  6. package/dist/{Factory-BcGJjLc8.d.mts → Factory-SFupxRC2.d.mts} +2 -1
  7. package/dist/Factory-SFupxRC2.d.mts.map +1 -0
  8. package/dist/Factory.d.cts +2 -2
  9. package/dist/Factory.d.mts +1 -1
  10. package/dist/KyselyFactory-BFqVIn_0.cjs.map +1 -1
  11. package/dist/KyselyFactory-DMswpwji.mjs.map +1 -1
  12. package/dist/{KyselyFactory-uZ45h7YU.d.cts → KyselyFactory-Dy5zzV4B.d.cts} +4 -3
  13. package/dist/KyselyFactory-Dy5zzV4B.d.cts.map +1 -0
  14. package/dist/{KyselyFactory-Cj-EultY.d.mts → KyselyFactory-vAxYodck.d.mts} +3 -2
  15. package/dist/KyselyFactory-vAxYodck.d.mts.map +1 -0
  16. package/dist/KyselyFactory.d.cts +3 -3
  17. package/dist/KyselyFactory.d.mts +2 -2
  18. package/dist/{ObjectionFactory-DL4qkuF1.d.mts → ObjectionFactory-BWjB49-i.d.mts} +3 -2
  19. package/dist/ObjectionFactory-BWjB49-i.d.mts.map +1 -0
  20. package/dist/ObjectionFactory-BeFBYcan.cjs.map +1 -1
  21. package/dist/{ObjectionFactory-CdhzKs4f.d.cts → ObjectionFactory-CD-WFuMJ.d.cts} +4 -3
  22. package/dist/ObjectionFactory-CD-WFuMJ.d.cts.map +1 -0
  23. package/dist/ObjectionFactory-QCJ7u0Ql.mjs.map +1 -1
  24. package/dist/ObjectionFactory.d.cts +3 -3
  25. package/dist/ObjectionFactory.d.mts +2 -2
  26. package/dist/{PostgresKyselyMigrator-upT-hmrz.mjs → PostgresKyselyMigrator-6sE1KOni.mjs} +2 -2
  27. package/dist/PostgresKyselyMigrator-6sE1KOni.mjs.map +1 -0
  28. package/dist/{PostgresKyselyMigrator-CIx3AFSR.d.mts → PostgresKyselyMigrator-CBltSOq5.d.cts} +3 -2
  29. package/dist/PostgresKyselyMigrator-CBltSOq5.d.cts.map +1 -0
  30. package/dist/{PostgresKyselyMigrator-CfytARcA.cjs → PostgresKyselyMigrator-D6IbPq8t.cjs} +2 -2
  31. package/dist/PostgresKyselyMigrator-D6IbPq8t.cjs.map +1 -0
  32. package/dist/{PostgresKyselyMigrator-CQ3aUoy_.d.cts → PostgresKyselyMigrator-DrVWncqd.d.mts} +3 -2
  33. package/dist/PostgresKyselyMigrator-DrVWncqd.d.mts.map +1 -0
  34. package/dist/PostgresKyselyMigrator.cjs +2 -2
  35. package/dist/PostgresKyselyMigrator.d.cts +2 -2
  36. package/dist/PostgresKyselyMigrator.d.mts +2 -2
  37. package/dist/PostgresKyselyMigrator.mjs +2 -2
  38. package/dist/{PostgresMigrator-DbuJGAVy.mjs → PostgresMigrator-BjjenqSd.mjs} +2 -2
  39. package/dist/PostgresMigrator-BjjenqSd.mjs.map +1 -0
  40. package/dist/{PostgresMigrator-D5UkK1_K.d.cts → PostgresMigrator-Bres0U6E.d.cts} +2 -1
  41. package/dist/PostgresMigrator-Bres0U6E.d.cts.map +1 -0
  42. package/dist/{PostgresMigrator-DFcNdCvD.cjs → PostgresMigrator-D6dQn0x2.cjs} +2 -2
  43. package/dist/PostgresMigrator-D6dQn0x2.cjs.map +1 -0
  44. package/dist/{PostgresMigrator-DQaRxoaY.d.mts → PostgresMigrator-S-YYosAC.d.mts} +2 -1
  45. package/dist/PostgresMigrator-S-YYosAC.d.mts.map +1 -0
  46. package/dist/PostgresMigrator.cjs +1 -1
  47. package/dist/PostgresMigrator.d.cts +1 -1
  48. package/dist/PostgresMigrator.d.mts +1 -1
  49. package/dist/PostgresMigrator.mjs +1 -1
  50. package/dist/{PostgresObjectionMigrator-CZHHcCOv.d.cts → PostgresObjectionMigrator-CPfBAP7r.d.cts} +3 -2
  51. package/dist/PostgresObjectionMigrator-CPfBAP7r.d.cts.map +1 -0
  52. package/dist/{PostgresObjectionMigrator-BG6ymgnt.cjs → PostgresObjectionMigrator-DK8ODIHQ.cjs} +2 -2
  53. package/dist/PostgresObjectionMigrator-DK8ODIHQ.cjs.map +1 -0
  54. package/dist/{PostgresObjectionMigrator-D_hCcrQu.d.mts → PostgresObjectionMigrator-DVEqB5tp.d.mts} +3 -2
  55. package/dist/PostgresObjectionMigrator-DVEqB5tp.d.mts.map +1 -0
  56. package/dist/{PostgresObjectionMigrator-DPj2pOpX.mjs → PostgresObjectionMigrator-D_QxXbIN.mjs} +2 -2
  57. package/dist/PostgresObjectionMigrator-D_QxXbIN.mjs.map +1 -0
  58. package/dist/PostgresObjectionMigrator.cjs +2 -2
  59. package/dist/PostgresObjectionMigrator.d.cts +2 -2
  60. package/dist/PostgresObjectionMigrator.d.mts +2 -2
  61. package/dist/PostgresObjectionMigrator.mjs +2 -2
  62. package/dist/{VitestKyselyTransactionIsolator-D3EZZhjZ.d.cts → VitestKyselyTransactionIsolator-CduJlHoT.d.cts} +4 -3
  63. package/dist/VitestKyselyTransactionIsolator-CduJlHoT.d.cts.map +1 -0
  64. package/dist/{VitestKyselyTransactionIsolator-Dxlp1u0f.d.mts → VitestKyselyTransactionIsolator-Cswnnj0k.d.mts} +4 -3
  65. package/dist/VitestKyselyTransactionIsolator-Cswnnj0k.d.mts.map +1 -0
  66. package/dist/{VitestKyselyTransactionIsolator-EvDLk5zg.cjs → VitestKyselyTransactionIsolator-D7RRXOBa.cjs} +2 -2
  67. package/dist/VitestKyselyTransactionIsolator-D7RRXOBa.cjs.map +1 -0
  68. package/dist/{VitestKyselyTransactionIsolator-CNURW8y6.mjs → VitestKyselyTransactionIsolator-DceyIqr4.mjs} +2 -2
  69. package/dist/VitestKyselyTransactionIsolator-DceyIqr4.mjs.map +1 -0
  70. package/dist/VitestKyselyTransactionIsolator.cjs +1 -1
  71. package/dist/VitestKyselyTransactionIsolator.d.cts +2 -2
  72. package/dist/VitestKyselyTransactionIsolator.d.mts +2 -2
  73. package/dist/VitestKyselyTransactionIsolator.mjs +1 -1
  74. package/dist/{VitestObjectionTransactionIsolator-1TpsPqfG.d.cts → VitestObjectionTransactionIsolator-BXoR6xdG.d.cts} +4 -3
  75. package/dist/VitestObjectionTransactionIsolator-BXoR6xdG.d.cts.map +1 -0
  76. package/dist/{VitestObjectionTransactionIsolator-CM5KTAFA.cjs → VitestObjectionTransactionIsolator-CdLRrzNf.cjs} +2 -2
  77. package/dist/VitestObjectionTransactionIsolator-CdLRrzNf.cjs.map +1 -0
  78. package/dist/{VitestObjectionTransactionIsolator-jQFaCz0u.mjs → VitestObjectionTransactionIsolator-OF2osYY5.mjs} +2 -2
  79. package/dist/VitestObjectionTransactionIsolator-OF2osYY5.mjs.map +1 -0
  80. package/dist/{VitestObjectionTransactionIsolator-i9jIgU8Q.d.mts → VitestObjectionTransactionIsolator-x6hY5j4u.d.mts} +4 -3
  81. package/dist/VitestObjectionTransactionIsolator-x6hY5j4u.d.mts.map +1 -0
  82. package/dist/VitestObjectionTransactionIsolator.cjs +1 -1
  83. package/dist/VitestObjectionTransactionIsolator.d.cts +2 -2
  84. package/dist/VitestObjectionTransactionIsolator.d.mts +2 -2
  85. package/dist/VitestObjectionTransactionIsolator.mjs +1 -1
  86. package/dist/{VitestTransactionIsolator-BvR19bYn.d.mts → VitestTransactionIsolator-BNWJqh9f.d.mts} +3 -2
  87. package/dist/VitestTransactionIsolator-BNWJqh9f.d.mts.map +1 -0
  88. package/dist/VitestTransactionIsolator-CMfJXZP8.cjs.map +1 -1
  89. package/dist/{VitestTransactionIsolator-CwQaxZLP.d.cts → VitestTransactionIsolator-CSroc7Df.d.cts} +3 -2
  90. package/dist/VitestTransactionIsolator-CSroc7Df.d.cts.map +1 -0
  91. package/dist/VitestTransactionIsolator-DQ7tLqgV.mjs.map +1 -1
  92. package/dist/VitestTransactionIsolator.d.cts +1 -1
  93. package/dist/VitestTransactionIsolator.d.mts +1 -1
  94. package/dist/aws.cjs.map +1 -1
  95. package/dist/aws.d.cts +2 -0
  96. package/dist/aws.d.cts.map +1 -0
  97. package/dist/aws.d.mts +2 -0
  98. package/dist/aws.d.mts.map +1 -0
  99. package/dist/aws.mjs.map +1 -1
  100. package/dist/benchmark.cjs.map +1 -1
  101. package/dist/benchmark.d.cts +1 -0
  102. package/dist/benchmark.d.cts.map +1 -0
  103. package/dist/benchmark.d.mts +1 -0
  104. package/dist/benchmark.d.mts.map +1 -0
  105. package/dist/benchmark.mjs.map +1 -1
  106. package/dist/better-auth.cjs +29 -30
  107. package/dist/better-auth.cjs.map +1 -1
  108. package/dist/better-auth.d.cts +2 -2
  109. package/dist/better-auth.d.cts.map +1 -0
  110. package/dist/better-auth.d.mts.map +1 -0
  111. package/dist/better-auth.mjs +29 -30
  112. package/dist/better-auth.mjs.map +1 -1
  113. package/dist/directory-B-Ozljzk.mjs.map +1 -1
  114. package/dist/directory-BVC8g7cX.cjs.map +1 -1
  115. package/dist/{directory-BXavAeJZ.d.mts → directory-CVrfTq1I.d.mts} +2 -1
  116. package/dist/directory-CVrfTq1I.d.mts.map +1 -0
  117. package/dist/{directory-Mi7tdOuD.d.cts → directory-DAnMWi50.d.cts} +2 -1
  118. package/dist/directory-DAnMWi50.d.cts.map +1 -0
  119. package/dist/faker-B14IEMIN.cjs.map +1 -1
  120. package/dist/faker-BGKYFoCT.mjs.map +1 -1
  121. package/dist/{faker-DvxiCtxc.d.cts → faker-Cg76aFNO.d.cts} +3 -3
  122. package/dist/faker-Cg76aFNO.d.cts.map +1 -0
  123. package/dist/faker-DHh7xs4u.d.mts.map +1 -0
  124. package/dist/faker.d.cts +1 -1
  125. package/dist/helpers.cjs.map +1 -1
  126. package/dist/helpers.d.cts +1 -0
  127. package/dist/helpers.d.cts.map +1 -0
  128. package/dist/helpers.d.mts +1 -0
  129. package/dist/helpers.d.mts.map +1 -0
  130. package/dist/helpers.mjs.map +1 -1
  131. package/dist/kysely.cjs +3 -3
  132. package/dist/kysely.cjs.map +1 -1
  133. package/dist/kysely.d.cts +8 -7
  134. package/dist/kysely.d.cts.map +1 -0
  135. package/dist/kysely.d.mts +7 -6
  136. package/dist/kysely.d.mts.map +1 -0
  137. package/dist/kysely.mjs +3 -3
  138. package/dist/kysely.mjs.map +1 -1
  139. package/dist/logger.cjs.map +1 -1
  140. package/dist/logger.d.cts +1 -0
  141. package/dist/logger.d.cts.map +1 -0
  142. package/dist/logger.d.mts +1 -0
  143. package/dist/logger.d.mts.map +1 -0
  144. package/dist/logger.mjs.map +1 -1
  145. package/dist/objection.cjs +3 -3
  146. package/dist/objection.cjs.map +1 -1
  147. package/dist/objection.d.cts +8 -7
  148. package/dist/objection.d.cts.map +1 -0
  149. package/dist/objection.d.mts +7 -6
  150. package/dist/objection.d.mts.map +1 -0
  151. package/dist/objection.mjs +3 -3
  152. package/dist/objection.mjs.map +1 -1
  153. package/dist/os/directory.d.cts +1 -1
  154. package/dist/os/directory.d.mts +1 -1
  155. package/dist/os/index.d.cts +1 -1
  156. package/dist/os/index.d.mts +1 -1
  157. package/dist/timer.cjs.map +1 -1
  158. package/dist/timer.d.cts +2 -0
  159. package/dist/timer.d.cts.map +1 -0
  160. package/dist/timer.d.mts +2 -0
  161. package/dist/timer.d.mts.map +1 -0
  162. package/dist/timer.mjs.map +1 -1
  163. package/package.json +5 -5
  164. package/src/Factory.ts +72 -72
  165. package/src/KyselyFactory.ts +330 -330
  166. package/src/ObjectionFactory.ts +354 -355
  167. package/src/PostgresKyselyMigrator.ts +37 -37
  168. package/src/PostgresMigrator.ts +107 -107
  169. package/src/PostgresObjectionMigrator.ts +91 -91
  170. package/src/VitestKyselyTransactionIsolator.ts +27 -27
  171. package/src/VitestObjectionTransactionIsolator.ts +39 -39
  172. package/src/VitestTransactionIsolator.ts +196 -195
  173. package/src/__tests__/Factory.spec.ts +163 -155
  174. package/src/__tests__/KyselyFactory.spec.ts +443 -439
  175. package/src/__tests__/ObjectionFactory.spec.ts +563 -557
  176. package/src/__tests__/PostgresKyselyMigrator.spec.ts +641 -641
  177. package/src/__tests__/PostgresMigrator.spec.ts +341 -341
  178. package/src/__tests__/PostgresObjectionMigrator.spec.ts +578 -578
  179. package/src/__tests__/VitestObjectionTransactionIsolator.spec.ts +114 -114
  180. package/src/__tests__/benchmark.spec.ts +140 -0
  181. package/src/__tests__/better-auth.spec.ts +15 -15
  182. package/src/__tests__/faker.spec.ts +226 -137
  183. package/src/__tests__/integration.spec.ts +597 -597
  184. package/src/__tests__/utilities.spec.ts +211 -0
  185. package/src/aws.ts +104 -104
  186. package/src/benchmark.ts +12 -12
  187. package/src/better-auth.ts +286 -301
  188. package/src/faker.ts +153 -153
  189. package/src/helpers.ts +6 -6
  190. package/src/kysely.ts +33 -33
  191. package/src/logger.ts +10 -10
  192. package/src/objection.ts +31 -31
  193. package/src/os/directory.ts +11 -10
  194. package/src/timer.ts +1 -1
  195. package/test/globalSetup.ts +45 -45
  196. package/test/helpers.ts +189 -189
  197. package/test/migrations/1749664623372_user.ts +13 -13
  198. package/tsconfig.json +9 -0
  199. package/vitest.config.ts +4 -4
  200. package/dist/PostgresKyselyMigrator-CfytARcA.cjs.map +0 -1
  201. package/dist/PostgresKyselyMigrator-upT-hmrz.mjs.map +0 -1
  202. package/dist/PostgresMigrator-DFcNdCvD.cjs.map +0 -1
  203. package/dist/PostgresMigrator-DbuJGAVy.mjs.map +0 -1
  204. package/dist/PostgresObjectionMigrator-BG6ymgnt.cjs.map +0 -1
  205. package/dist/PostgresObjectionMigrator-DPj2pOpX.mjs.map +0 -1
  206. package/dist/VitestKyselyTransactionIsolator-CNURW8y6.mjs.map +0 -1
  207. package/dist/VitestKyselyTransactionIsolator-EvDLk5zg.cjs.map +0 -1
  208. package/dist/VitestObjectionTransactionIsolator-CM5KTAFA.cjs.map +0 -1
  209. package/dist/VitestObjectionTransactionIsolator-jQFaCz0u.mjs.map +0 -1
@@ -1,454 +1,458 @@
1
1
  import pg from 'pg';
2
2
  import { describe, expect, it } from 'vitest';
3
3
  import { TEST_DATABASE_CONFIG } from '../../test/globalSetup';
4
- import { type TestDatabase, createTestTables } from '../../test/helpers';
5
- import { KyselyFactory } from '../KyselyFactory';
4
+ import { createTestTables, type TestDatabase } from '../../test/helpers';
6
5
  import { createKyselyDb } from '../helpers';
6
+ import { KyselyFactory } from '../KyselyFactory';
7
7
  import { wrapVitestKyselyTransaction } from '../kysely';
8
8
 
9
9
  const db = () => createKyselyDb<TestDatabase>(TEST_DATABASE_CONFIG);
10
10
  const itWithTransaction = wrapVitestKyselyTransaction<TestDatabase>(it, {
11
- connection: db,
12
- setup: createTestTables,
11
+ connection: db,
12
+ setup: createTestTables,
13
13
  });
14
14
 
15
15
  const int8TypeId = 20;
16
16
  pg.types.setTypeParser(int8TypeId, (val) => {
17
- return parseInt(val, 10);
17
+ return parseInt(val, 10);
18
18
  });
19
19
  describe('KyselyFactory', () => {
20
- describe('KyselyFactory.insert', () => {
21
- itWithTransaction(
22
- 'should insert a record with defaults',
23
- async ({ trx }) => {
24
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
25
- 'users',
26
- async ({ attrs }) => ({
27
- name: 'John Doe',
28
- email: `user${Date.now()}@example.com`,
29
- createdAt: new Date(),
30
- }),
31
- );
32
-
33
- const builders = {
34
- user: userBuilder,
35
- };
36
-
37
- const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
38
- builders,
39
- {},
40
- trx,
41
- );
42
-
43
- const user = await factory.insert('user');
44
-
45
- expect(user).toBeDefined();
46
- expect(user.id).toBeDefined();
47
- expect(user.name).toBe('John Doe');
48
- expect(user.email).toContain('user');
49
- expect(user.email).toContain('@example.com');
50
- expect(user.createdAt).toBeInstanceOf(Date);
51
- },
52
- );
53
-
54
- itWithTransaction(
55
- 'should override defaults with provided attributes',
56
- async ({ trx }) => {
57
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
58
- 'users',
59
- async ({ attrs }) => ({
60
- name: 'John Doe',
61
- email: `user${Date.now()}@example.com`,
62
- createdAt: new Date(),
63
- }),
64
- );
65
-
66
- const builders = {
67
- user: userBuilder,
68
- };
69
-
70
- const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
71
- builders,
72
- {},
73
- trx,
74
- );
75
-
76
- const customEmail = 'custom@test.com';
77
- const customName = 'Jane Smith';
78
- const user = await factory.insert('user', {
79
- email: customEmail,
80
- name: customName,
81
- });
82
-
83
- expect(user.name).toBe(customName);
84
- expect(user.email).toBe(customEmail);
85
- },
86
- );
87
-
88
- itWithTransaction('should handle relations', async ({ trx }) => {
89
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
90
- 'users',
91
- async (attrs) => ({
92
- name: 'John Doe',
93
- email: `user${Date.now()}@example.com`,
94
- createdAt: new Date(),
95
- }),
96
- );
97
-
98
- const postBuilder = KyselyFactory.createBuilder<TestDatabase, 'posts'>(
99
- 'posts',
100
- async ({ attrs, factory }) => {
101
- // Create a user if userId not provided
102
- if (!attrs.userId) {
103
- const user = await factory.insert('user');
104
- return {
105
- title: 'Default Post',
106
- content: 'Default content',
107
- userId: user.id,
108
- createdAt: new Date(),
109
- };
110
- }
111
- return {
112
- title: 'Default Post',
113
- content: 'Default content',
114
- createdAt: new Date(),
115
- };
116
- },
117
- );
118
-
119
- const builders = {
120
- user: userBuilder,
121
- post: postBuilder,
122
- };
123
-
124
- const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
125
- builders,
126
- {},
127
- trx,
128
- );
129
-
130
- const post = await factory.insert('post', {
131
- title: 'Test Post',
132
- });
133
-
134
- expect(post).toBeDefined();
135
- expect(post.title).toBe('Test Post');
136
- expect(post.userId).toBeDefined();
137
- expect(typeof post.userId).toBe('number');
138
- });
139
-
140
- itWithTransaction(
141
- 'should throw error for non-existent builder',
142
- async ({ trx }) => {
143
- const factory = new KyselyFactory<TestDatabase, any, {}>({}, {}, trx);
144
-
145
- await expect(factory.insert('nonExistent' as any)).rejects.toThrow(
146
- 'Factory "nonExistent" does not exist',
147
- );
148
- },
149
- );
150
- });
151
-
152
- describe('KyselyFactory.insertMany', () => {
153
- itWithTransaction(
154
- 'should insert multiple records with same attributes',
155
- async ({ trx }) => {
156
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
157
- 'users',
158
- async () => ({
159
- name: 'John Doe',
160
- email: `user${Date.now()}-${Math.random()}@example.com`,
161
- createdAt: new Date(),
162
- }),
163
- );
164
-
165
- const builders = {
166
- user: userBuilder,
167
- };
168
-
169
- const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
170
- builders,
171
- {},
172
- trx,
173
- );
174
-
175
- const users = await factory.insertMany(3, 'user');
176
-
177
- expect(users).toHaveLength(3);
178
- users.forEach((user, index) => {
179
- expect(user.id).toBeDefined();
180
- expect(user.name).toBe('John Doe');
181
- expect(user.email).toContain('@example.com');
182
- });
183
- },
184
- );
185
-
186
- itWithTransaction(
187
- 'should insert multiple records with dynamic attributes',
188
- async ({ trx }) => {
189
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
190
- 'users',
191
- async () => ({
192
- email: `user${Date.now()}-${Math.random()}@example.com`,
193
- createdAt: new Date(),
194
- }),
195
- );
196
-
197
- const builders = {
198
- user: userBuilder,
199
- };
200
-
201
- const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
202
- builders,
203
- {},
204
- trx,
205
- );
206
-
207
- const users = await factory.insertMany(3, 'user', (idx) => ({
208
- name: `User ${idx}`,
209
- }));
210
-
211
- expect(users).toHaveLength(3);
212
- users.forEach((user, index) => {
213
- expect(user.name).toBe(`User ${index}`);
214
- });
215
- },
216
- );
217
-
218
- itWithTransaction(
219
- 'should throw error for non-existent builder',
220
- async ({ trx }) => {
221
- const factory = new KyselyFactory<TestDatabase, any, {}>({}, {}, trx);
222
-
223
- await expect(
224
- factory.insertMany(2, 'nonExistent' as any),
225
- ).rejects.toThrow('Builder "nonExistent" is not registered');
226
- },
227
- );
228
- });
229
-
230
- describe('KyselyFactory.createBuilder', () => {
231
- itWithTransaction('should work with async defaults', async ({ trx }) => {
232
- let counter = 0;
233
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
234
- 'users',
235
- async () => {
236
- // Simulate async operation
237
- await new Promise((resolve) => setTimeout(resolve, 10));
238
- counter++;
239
- return {
240
- name: `Async User ${counter}`,
241
- email: `user${counter}@example.com`,
242
- createdAt: new Date(),
243
- };
244
- },
245
- );
246
-
247
- const builders = {
248
- user: userBuilder,
249
- };
250
-
251
- const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
252
- builders,
253
- {},
254
- trx,
255
- );
256
-
257
- const user1 = await factory.insert('user');
258
- const user2 = await factory.insert('user');
259
-
260
- expect(user1.name).toBe('Async User 1');
261
- expect(user2.name).toBe('Async User 2');
262
- });
263
- });
264
-
265
- describe('KyselyFactory.seed', () => {
266
- itWithTransaction('should execute seed functions', async ({ trx }) => {
267
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
268
- 'users',
269
- async (attrs) => ({
270
- name: 'John Doe',
271
- email: `user${Date.now()}@example.com`,
272
- createdAt: new Date(),
273
- }),
274
- );
275
-
276
- const builders = {
277
- user: userBuilder,
278
- };
279
-
280
- const seeds = {
281
- createAdminUser: KyselyFactory.createSeed(
282
- async ({
283
- attrs,
284
- factory,
285
- }: { attrs: { name?: string }; factory: any; db: any }) => {
286
- return await factory.insert('user', {
287
- name: attrs.name || 'Admin User',
288
- email: 'admin@example.com',
289
- });
290
- },
291
- ),
292
- };
293
-
294
- const factory = new KyselyFactory<
295
- TestDatabase,
296
- typeof builders,
297
- typeof seeds
298
- >(builders, seeds, trx);
299
-
300
- const adminUser = await factory.seed('createAdminUser');
301
-
302
- expect(adminUser).toBeDefined();
303
- expect(adminUser.name).toBe('Admin User');
304
- expect(adminUser.email).toBe('admin@example.com');
305
- });
306
-
307
- itWithTransaction(
308
- 'should pass attributes to seed functions',
309
- async ({ trx }) => {
310
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
311
- 'users',
312
- async ({ attrs }) => ({
313
- name: 'John Doe',
314
- email: `user${Date.now()}@example.com`,
315
- createdAt: new Date(),
316
- }),
317
- );
318
-
319
- const builders = {
320
- user: userBuilder,
321
- };
322
-
323
- const seeds = {
324
- createCustomUser: KyselyFactory.createSeed(
325
- async ({
326
- attrs,
327
- factory,
328
- }: {
329
- attrs: { name: string; email: string };
330
- factory: any;
331
- db: any;
332
- }) => {
333
- return await factory.insert('user', attrs);
334
- },
335
- ),
336
- };
337
-
338
- const factory = new KyselyFactory<
339
- TestDatabase,
340
- typeof builders,
341
- typeof seeds
342
- >(builders, seeds, trx);
343
-
344
- const customUser = await factory.seed('createCustomUser', {
345
- name: 'Custom User',
346
- email: 'custom@test.com',
347
- });
348
-
349
- expect(customUser.name).toBe('Custom User');
350
- expect(customUser.email).toBe('custom@test.com');
351
- },
352
- );
353
-
354
- itWithTransaction(
355
- 'should throw error for non-existent seed',
356
- async ({ trx }) => {
357
- const factory = new KyselyFactory<TestDatabase, any, any>({}, {}, trx);
358
-
359
- expect(() => factory.seed('nonExistent' as any)).toThrow(
360
- 'Seed "nonExistent" is not registered',
361
- );
362
- },
363
- );
364
- });
365
-
366
- describe('Factory integration', () => {
367
- itWithTransaction(
368
- 'should work with controlled transactions',
369
- async ({ trx }) => {
370
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
371
- 'users',
372
- async ({ attrs }) => ({
373
- name: 'John Doe',
374
- email: `user${Date.now()}@example.com`,
375
- createdAt: new Date(),
376
- }),
377
- );
378
-
379
- const builders = {
380
- user: userBuilder,
381
- };
382
-
383
- const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
384
- builders,
385
- {},
386
- trx,
387
- );
388
-
389
- const user = await factory.insert('user');
390
-
391
- // Verify the user exists in the transaction
392
- const foundUser = await trx
393
- .selectFrom('users')
394
- .selectAll()
395
- .where('id', '=', user.id)
396
- .executeTakeFirst();
397
-
398
- expect(foundUser).toBeDefined();
399
- expect(foundUser?.id).toBe(user.id);
400
- },
401
- );
402
-
403
- itWithTransaction(
404
- 'should work with factory passed to defaults',
405
- async ({ trx }) => {
406
- const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
407
- 'users',
408
- async ({ attrs }) => ({
409
- name: 'John Doe',
410
- email: `user${Date.now()}@example.com`,
411
- createdAt: new Date(),
412
- }),
413
- );
414
-
415
- const postBuilder = KyselyFactory.createBuilder<TestDatabase, 'posts'>(
416
- 'posts',
417
- async ({ factory }) => {
418
- const user = await factory.insert('user');
419
- return {
420
- title: 'Default Post',
421
- content: 'Default content',
422
- userId: user.id,
423
- createdAt: new Date(),
424
- };
425
- },
426
- );
427
-
428
- const builders = {
429
- user: userBuilder,
430
- post: postBuilder,
431
- };
432
-
433
- const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
434
- builders,
435
- {},
436
- trx,
437
- );
438
-
439
- const post = await factory.insert('post');
440
-
441
- expect(post.userId).toBeDefined();
442
-
443
- // Verify the related user exists
444
- const relatedUser = await trx
445
- .selectFrom('users')
446
- .selectAll()
447
- .where('id', '=', post.userId)
448
- .executeTakeFirst();
449
-
450
- expect(relatedUser).toBeDefined();
451
- },
452
- );
453
- });
20
+ describe('KyselyFactory.insert', () => {
21
+ itWithTransaction(
22
+ 'should insert a record with defaults',
23
+ async ({ trx }) => {
24
+ const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
25
+ 'users',
26
+ async ({ attrs }) => ({
27
+ name: 'John Doe',
28
+ email: `user${Date.now()}@example.com`,
29
+ createdAt: new Date(),
30
+ }),
31
+ );
32
+
33
+ const builders = {
34
+ user: userBuilder,
35
+ };
36
+
37
+ const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
38
+ builders,
39
+ {},
40
+ trx,
41
+ );
42
+
43
+ const user = await factory.insert('user');
44
+
45
+ expect(user).toBeDefined();
46
+ expect(user.id).toBeDefined();
47
+ expect(user.name).toBe('John Doe');
48
+ expect(user.email).toContain('user');
49
+ expect(user.email).toContain('@example.com');
50
+ expect(user.createdAt).toBeInstanceOf(Date);
51
+ },
52
+ );
53
+
54
+ itWithTransaction(
55
+ 'should override defaults with provided attributes',
56
+ async ({ trx }) => {
57
+ const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
58
+ 'users',
59
+ async ({ attrs }) => ({
60
+ name: 'John Doe',
61
+ email: `user${Date.now()}@example.com`,
62
+ createdAt: new Date(),
63
+ }),
64
+ );
65
+
66
+ const builders = {
67
+ user: userBuilder,
68
+ };
69
+
70
+ const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
71
+ builders,
72
+ {},
73
+ trx,
74
+ );
75
+
76
+ const customEmail = 'custom@test.com';
77
+ const customName = 'Jane Smith';
78
+ const user = await factory.insert('user', {
79
+ email: customEmail,
80
+ name: customName,
81
+ });
82
+
83
+ expect(user.name).toBe(customName);
84
+ expect(user.email).toBe(customEmail);
85
+ },
86
+ );
87
+
88
+ itWithTransaction('should handle relations', async ({ trx }) => {
89
+ const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
90
+ 'users',
91
+ async (_attrs) => ({
92
+ name: 'John Doe',
93
+ email: `user${Date.now()}@example.com`,
94
+ createdAt: new Date(),
95
+ }),
96
+ );
97
+
98
+ const postBuilder = KyselyFactory.createBuilder<TestDatabase, 'posts'>(
99
+ 'posts',
100
+ async ({ attrs, factory }) => {
101
+ // Create a user if userId not provided
102
+ if (!attrs.userId) {
103
+ const user = await factory.insert('user');
104
+ return {
105
+ title: 'Default Post',
106
+ content: 'Default content',
107
+ userId: user.id,
108
+ createdAt: new Date(),
109
+ };
110
+ }
111
+ return {
112
+ title: 'Default Post',
113
+ content: 'Default content',
114
+ createdAt: new Date(),
115
+ };
116
+ },
117
+ );
118
+
119
+ const builders = {
120
+ user: userBuilder,
121
+ post: postBuilder,
122
+ };
123
+
124
+ const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
125
+ builders,
126
+ {},
127
+ trx,
128
+ );
129
+
130
+ const post = await factory.insert('post', {
131
+ title: 'Test Post',
132
+ });
133
+
134
+ expect(post).toBeDefined();
135
+ expect(post.title).toBe('Test Post');
136
+ expect(post.userId).toBeDefined();
137
+ expect(typeof post.userId).toBe('number');
138
+ });
139
+
140
+ itWithTransaction(
141
+ 'should throw error for non-existent builder',
142
+ async ({ trx }) => {
143
+ const factory = new KyselyFactory<TestDatabase, any, {}>({}, {}, trx);
144
+
145
+ await expect(factory.insert('nonExistent' as any)).rejects.toThrow(
146
+ 'Factory "nonExistent" does not exist',
147
+ );
148
+ },
149
+ );
150
+ });
151
+
152
+ describe('KyselyFactory.insertMany', () => {
153
+ itWithTransaction(
154
+ 'should insert multiple records with same attributes',
155
+ async ({ trx }) => {
156
+ const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
157
+ 'users',
158
+ async () => ({
159
+ name: 'John Doe',
160
+ email: `user${Date.now()}-${Math.random()}@example.com`,
161
+ createdAt: new Date(),
162
+ }),
163
+ );
164
+
165
+ const builders = {
166
+ user: userBuilder,
167
+ };
168
+
169
+ const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
170
+ builders,
171
+ {},
172
+ trx,
173
+ );
174
+
175
+ const users = await factory.insertMany(3, 'user');
176
+
177
+ expect(users).toHaveLength(3);
178
+ users.forEach((user, _index) => {
179
+ expect(user.id).toBeDefined();
180
+ expect(user.name).toBe('John Doe');
181
+ expect(user.email).toContain('@example.com');
182
+ });
183
+ },
184
+ );
185
+
186
+ itWithTransaction(
187
+ 'should insert multiple records with dynamic attributes',
188
+ async ({ trx }) => {
189
+ const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
190
+ 'users',
191
+ async () => ({
192
+ email: `user${Date.now()}-${Math.random()}@example.com`,
193
+ createdAt: new Date(),
194
+ }),
195
+ );
196
+
197
+ const builders = {
198
+ user: userBuilder,
199
+ };
200
+
201
+ const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
202
+ builders,
203
+ {},
204
+ trx,
205
+ );
206
+
207
+ const users = await factory.insertMany(3, 'user', (idx) => ({
208
+ name: `User ${idx}`,
209
+ }));
210
+
211
+ expect(users).toHaveLength(3);
212
+ users.forEach((user, index) => {
213
+ expect(user.name).toBe(`User ${index}`);
214
+ });
215
+ },
216
+ );
217
+
218
+ itWithTransaction(
219
+ 'should throw error for non-existent builder',
220
+ async ({ trx }) => {
221
+ const factory = new KyselyFactory<TestDatabase, any, {}>({}, {}, trx);
222
+
223
+ await expect(
224
+ factory.insertMany(2, 'nonExistent' as any),
225
+ ).rejects.toThrow('Builder "nonExistent" is not registered');
226
+ },
227
+ );
228
+ });
229
+
230
+ describe('KyselyFactory.createBuilder', () => {
231
+ itWithTransaction('should work with async defaults', async ({ trx }) => {
232
+ let counter = 0;
233
+ const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
234
+ 'users',
235
+ async () => {
236
+ // Simulate async operation
237
+ await new Promise((resolve) => setTimeout(resolve, 10));
238
+ counter++;
239
+ return {
240
+ name: `Async User ${counter}`,
241
+ email: `user${counter}@example.com`,
242
+ createdAt: new Date(),
243
+ };
244
+ },
245
+ );
246
+
247
+ const builders = {
248
+ user: userBuilder,
249
+ };
250
+
251
+ const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
252
+ builders,
253
+ {},
254
+ trx,
255
+ );
256
+
257
+ const user1 = await factory.insert('user');
258
+ const user2 = await factory.insert('user');
259
+
260
+ expect(user1.name).toBe('Async User 1');
261
+ expect(user2.name).toBe('Async User 2');
262
+ });
263
+ });
264
+
265
+ describe('KyselyFactory.seed', () => {
266
+ itWithTransaction('should execute seed functions', async ({ trx }) => {
267
+ const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
268
+ 'users',
269
+ async (_attrs) => ({
270
+ name: 'John Doe',
271
+ email: `user${Date.now()}@example.com`,
272
+ createdAt: new Date(),
273
+ }),
274
+ );
275
+
276
+ const builders = {
277
+ user: userBuilder,
278
+ };
279
+
280
+ const seeds = {
281
+ createAdminUser: KyselyFactory.createSeed(
282
+ async ({
283
+ attrs,
284
+ factory,
285
+ }: {
286
+ attrs: { name?: string };
287
+ factory: any;
288
+ db: any;
289
+ }) => {
290
+ return await factory.insert('user', {
291
+ name: attrs.name || 'Admin User',
292
+ email: 'admin@example.com',
293
+ });
294
+ },
295
+ ),
296
+ };
297
+
298
+ const factory = new KyselyFactory<
299
+ TestDatabase,
300
+ typeof builders,
301
+ typeof seeds
302
+ >(builders, seeds, trx);
303
+
304
+ const adminUser = await factory.seed('createAdminUser');
305
+
306
+ expect(adminUser).toBeDefined();
307
+ expect(adminUser.name).toBe('Admin User');
308
+ expect(adminUser.email).toBe('admin@example.com');
309
+ });
310
+
311
+ itWithTransaction(
312
+ 'should pass attributes to seed functions',
313
+ async ({ trx }) => {
314
+ const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
315
+ 'users',
316
+ async ({ attrs }) => ({
317
+ name: 'John Doe',
318
+ email: `user${Date.now()}@example.com`,
319
+ createdAt: new Date(),
320
+ }),
321
+ );
322
+
323
+ const builders = {
324
+ user: userBuilder,
325
+ };
326
+
327
+ const seeds = {
328
+ createCustomUser: KyselyFactory.createSeed(
329
+ async ({
330
+ attrs,
331
+ factory,
332
+ }: {
333
+ attrs: { name: string; email: string };
334
+ factory: any;
335
+ db: any;
336
+ }) => {
337
+ return await factory.insert('user', attrs);
338
+ },
339
+ ),
340
+ };
341
+
342
+ const factory = new KyselyFactory<
343
+ TestDatabase,
344
+ typeof builders,
345
+ typeof seeds
346
+ >(builders, seeds, trx);
347
+
348
+ const customUser = await factory.seed('createCustomUser', {
349
+ name: 'Custom User',
350
+ email: 'custom@test.com',
351
+ });
352
+
353
+ expect(customUser.name).toBe('Custom User');
354
+ expect(customUser.email).toBe('custom@test.com');
355
+ },
356
+ );
357
+
358
+ itWithTransaction(
359
+ 'should throw error for non-existent seed',
360
+ async ({ trx }) => {
361
+ const factory = new KyselyFactory<TestDatabase, any, any>({}, {}, trx);
362
+
363
+ expect(() => factory.seed('nonExistent' as any)).toThrow(
364
+ 'Seed "nonExistent" is not registered',
365
+ );
366
+ },
367
+ );
368
+ });
369
+
370
+ describe('Factory integration', () => {
371
+ itWithTransaction(
372
+ 'should work with controlled transactions',
373
+ async ({ trx }) => {
374
+ const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
375
+ 'users',
376
+ async ({ attrs }) => ({
377
+ name: 'John Doe',
378
+ email: `user${Date.now()}@example.com`,
379
+ createdAt: new Date(),
380
+ }),
381
+ );
382
+
383
+ const builders = {
384
+ user: userBuilder,
385
+ };
386
+
387
+ const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
388
+ builders,
389
+ {},
390
+ trx,
391
+ );
392
+
393
+ const user = await factory.insert('user');
394
+
395
+ // Verify the user exists in the transaction
396
+ const foundUser = await trx
397
+ .selectFrom('users')
398
+ .selectAll()
399
+ .where('id', '=', user.id)
400
+ .executeTakeFirst();
401
+
402
+ expect(foundUser).toBeDefined();
403
+ expect(foundUser?.id).toBe(user.id);
404
+ },
405
+ );
406
+
407
+ itWithTransaction(
408
+ 'should work with factory passed to defaults',
409
+ async ({ trx }) => {
410
+ const userBuilder = KyselyFactory.createBuilder<TestDatabase, 'users'>(
411
+ 'users',
412
+ async ({ attrs }) => ({
413
+ name: 'John Doe',
414
+ email: `user${Date.now()}@example.com`,
415
+ createdAt: new Date(),
416
+ }),
417
+ );
418
+
419
+ const postBuilder = KyselyFactory.createBuilder<TestDatabase, 'posts'>(
420
+ 'posts',
421
+ async ({ factory }) => {
422
+ const user = await factory.insert('user');
423
+ return {
424
+ title: 'Default Post',
425
+ content: 'Default content',
426
+ userId: user.id,
427
+ createdAt: new Date(),
428
+ };
429
+ },
430
+ );
431
+
432
+ const builders = {
433
+ user: userBuilder,
434
+ post: postBuilder,
435
+ };
436
+
437
+ const factory = new KyselyFactory<TestDatabase, typeof builders, {}>(
438
+ builders,
439
+ {},
440
+ trx,
441
+ );
442
+
443
+ const post = await factory.insert('post');
444
+
445
+ expect(post.userId).toBeDefined();
446
+
447
+ // Verify the related user exists
448
+ const relatedUser = await trx
449
+ .selectFrom('users')
450
+ .selectAll()
451
+ .where('id', '=', post.userId)
452
+ .executeTakeFirst();
453
+
454
+ expect(relatedUser).toBeDefined();
455
+ },
456
+ );
457
+ });
454
458
  });