@geekmidas/testkit 0.0.3 → 0.0.5

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 (72) hide show
  1. package/PostgresKyselyMigrator.spec +471 -0
  2. package/dist/{KyselyFactory-DiiWtMYe.cjs → KyselyFactory-BX7Kv2uP.cjs} +11 -12
  3. package/dist/{KyselyFactory-DZewtWtJ.mjs → KyselyFactory-pOMOFQWE.mjs} +11 -12
  4. package/dist/KyselyFactory.cjs +2 -1
  5. package/dist/KyselyFactory.mjs +2 -1
  6. package/dist/ObjectionFactory.cjs +1 -1
  7. package/dist/ObjectionFactory.mjs +1 -1
  8. package/dist/{PostgresKyselyMigrator-ChMJpPrQ.mjs → PostgresKyselyMigrator-D8fm35-s.mjs} +1 -1
  9. package/dist/{PostgresKyselyMigrator-rY3hO_-1.cjs → PostgresKyselyMigrator-JTY2LfwD.cjs} +3 -2
  10. package/dist/PostgresKyselyMigrator.cjs +2 -2
  11. package/dist/PostgresKyselyMigrator.mjs +2 -2
  12. package/dist/{PostgresMigrator-BJ2-5A_b.cjs → PostgresMigrator-Bz-tnjB6.cjs} +2 -39
  13. package/dist/PostgresMigrator.cjs +1 -1
  14. package/dist/PostgresMigrator.mjs +1 -1
  15. package/dist/VitestKyselyTransactionIsolator-BS3R-V0I.mjs +12 -0
  16. package/dist/VitestKyselyTransactionIsolator-DWSTKIe3.cjs +17 -0
  17. package/dist/VitestKyselyTransactionIsolator.cjs +4 -0
  18. package/dist/VitestKyselyTransactionIsolator.mjs +4 -0
  19. package/dist/VitestTransactionIsolator-BjVXqFs6.mjs +40 -0
  20. package/dist/VitestTransactionIsolator-Bx2c4OzK.cjs +52 -0
  21. package/dist/VitestTransactionIsolator.cjs +4 -0
  22. package/dist/VitestTransactionIsolator.mjs +3 -0
  23. package/dist/__tests__/Factory.spec.cjs +139 -0
  24. package/dist/__tests__/Factory.spec.mjs +138 -0
  25. package/dist/__tests__/KyselyFactory.spec.cjs +220 -15008
  26. package/dist/__tests__/KyselyFactory.spec.mjs +218 -15033
  27. package/dist/__tests__/ObjectionFactory.spec.cjs +386 -0
  28. package/dist/__tests__/ObjectionFactory.spec.mjs +385 -0
  29. package/dist/__tests__/PostgresMigrator.spec.cjs +256 -0
  30. package/dist/__tests__/PostgresMigrator.spec.mjs +255 -0
  31. package/dist/__tests__/faker.spec.cjs +115 -0
  32. package/dist/__tests__/faker.spec.mjs +114 -0
  33. package/dist/__tests__/integration.spec.cjs +279 -0
  34. package/dist/__tests__/integration.spec.mjs +278 -0
  35. package/dist/chunk-CUT6urMc.cjs +30 -0
  36. package/dist/example.cjs +2 -1
  37. package/dist/example.mjs +2 -1
  38. package/dist/faker-BwaXA_RF.mjs +85 -0
  39. package/dist/faker-caz-8zt8.cjs +121 -0
  40. package/dist/faker.cjs +8 -0
  41. package/dist/faker.mjs +3 -0
  42. package/dist/helpers-B9Jdk_C7.cjs +31 -0
  43. package/dist/helpers-BfuX-cjN.mjs +111 -0
  44. package/dist/helpers-DKEBHABj.cjs +135 -0
  45. package/dist/helpers-DOiGIkaU.mjs +19 -0
  46. package/dist/helpers.cjs +6 -0
  47. package/dist/helpers.mjs +5 -0
  48. package/dist/kysely.cjs +15 -4
  49. package/dist/kysely.mjs +14 -4
  50. package/dist/objection.cjs +1 -1
  51. package/dist/objection.mjs +1 -1
  52. package/package.json +8 -2
  53. package/src/Factory.ts +3 -1
  54. package/src/KyselyFactory.ts +30 -36
  55. package/src/VitestKyselyTransactionIsolator.ts +23 -0
  56. package/src/VitestTransactionIsolator.ts +70 -0
  57. package/src/__tests__/Factory.spec.ts +164 -0
  58. package/src/__tests__/KyselyFactory.spec.ts +432 -64
  59. package/src/__tests__/ObjectionFactory.spec.ts +532 -0
  60. package/src/__tests__/PostgresMigrator.spec.ts +366 -0
  61. package/src/__tests__/faker.spec.ts +142 -0
  62. package/src/__tests__/integration.spec.ts +442 -0
  63. package/src/faker.ts +112 -0
  64. package/src/helpers.ts +28 -0
  65. package/src/kysely.ts +14 -0
  66. package/test/globalSetup.ts +41 -40
  67. package/test/helpers.ts +273 -0
  68. package/dist/magic-string.es-CxbtJGk_.mjs +0 -1014
  69. package/dist/magic-string.es-KiPEzMtt.cjs +0 -1015
  70. /package/dist/{ObjectionFactory-DeFYWbzt.cjs → ObjectionFactory-BlkzSEqo.cjs} +0 -0
  71. /package/dist/{ObjectionFactory-MAf2m8LI.mjs → ObjectionFactory-ChuX8sZN.mjs} +0 -0
  72. /package/dist/{PostgresMigrator-BKaNTth5.mjs → PostgresMigrator-CEoRKTdq.mjs} +0 -0
@@ -0,0 +1,279 @@
1
+ const require_chunk = require('../chunk-CUT6urMc.cjs');
2
+ require('../Factory-DREHoms3.cjs');
3
+ require('../faker-caz-8zt8.cjs');
4
+ const require_KyselyFactory = require('../KyselyFactory-BX7Kv2uP.cjs');
5
+ require('../VitestTransactionIsolator-Bx2c4OzK.cjs');
6
+ require('../VitestKyselyTransactionIsolator-DWSTKIe3.cjs');
7
+ const require_helpers = require('../helpers-B9Jdk_C7.cjs');
8
+ const require_helpers$1 = require('../helpers-DKEBHABj.cjs');
9
+ const vitest = require_chunk.__toESM(require("vitest"));
10
+
11
+ //#region src/__tests__/integration.spec.ts
12
+ const db = require_helpers.createKyselyDb(require_helpers$1.TEST_DATABASE_CONFIG);
13
+ const it = require_helpers.wrapVitestKyselyTransaction(db, require_helpers$1.createTestTables);
14
+ (0, vitest.describe)("Testkit Integration Tests", () => {
15
+ (0, vitest.beforeAll)(async () => {});
16
+ (0, vitest.describe)("Complex Factory Scenarios", () => {
17
+ it("should handle complex multi-table data creation", async ({ trx }) => {
18
+ const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
19
+ name: "John Doe",
20
+ email: `user${Date.now()}-${Math.random()}@example.com`,
21
+ role: "user",
22
+ createdAt: /* @__PURE__ */ new Date(),
23
+ updatedAt: /* @__PURE__ */ new Date()
24
+ }));
25
+ const postBuilder = require_KyselyFactory.KyselyFactory.createBuilder("posts", async (attrs, factory$1) => {
26
+ if (!attrs.userId) {
27
+ const user = await factory$1.insert("user");
28
+ return {
29
+ title: "Default Post Title",
30
+ content: "Default post content...",
31
+ userId: user.id,
32
+ published: false,
33
+ createdAt: /* @__PURE__ */ new Date(),
34
+ updatedAt: /* @__PURE__ */ new Date()
35
+ };
36
+ }
37
+ return {
38
+ title: "Default Post Title",
39
+ content: "Default post content...",
40
+ published: false,
41
+ createdAt: /* @__PURE__ */ new Date(),
42
+ updatedAt: /* @__PURE__ */ new Date()
43
+ };
44
+ });
45
+ const commentBuilder = require_KyselyFactory.KyselyFactory.createBuilder("comments", async (attrs, factory$1) => {
46
+ let postId = attrs.postId;
47
+ let userId = attrs.userId;
48
+ if (!postId) {
49
+ const post = await factory$1.insert("post");
50
+ postId = post.id;
51
+ }
52
+ if (!userId) {
53
+ const user = await factory$1.insert("user");
54
+ userId = user.id;
55
+ }
56
+ return {
57
+ content: "Default comment content",
58
+ postId,
59
+ userId,
60
+ createdAt: /* @__PURE__ */ new Date()
61
+ };
62
+ });
63
+ const builders = {
64
+ user: userBuilder,
65
+ post: postBuilder,
66
+ comment: commentBuilder
67
+ };
68
+ const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
69
+ const author = await factory.insert("user", {
70
+ name: "Jane Author",
71
+ email: "jane@author.com",
72
+ role: "admin"
73
+ });
74
+ const posts = await factory.insertMany(3, "post", (idx) => ({
75
+ title: `Post ${idx + 1}`,
76
+ content: `Content for post ${idx + 1}`,
77
+ userId: author.id,
78
+ published: idx < 2
79
+ }));
80
+ const comments = await factory.insertMany(5, "comment", (idx) => ({
81
+ content: `Comment ${idx + 1} on first post`,
82
+ postId: posts[0].id
83
+ }));
84
+ (0, vitest.expect)(author.name).toBe("Jane Author");
85
+ (0, vitest.expect)(author.role).toBe("admin");
86
+ (0, vitest.expect)(posts).toHaveLength(3);
87
+ (0, vitest.expect)(posts[0].title).toBe("Post 1");
88
+ (0, vitest.expect)(posts[0].published).toBe(true);
89
+ (0, vitest.expect)(posts[2].published).toBe(false);
90
+ (0, vitest.expect)(comments).toHaveLength(5);
91
+ comments.forEach((comment, idx) => {
92
+ (0, vitest.expect)(comment.content).toBe(`Comment ${idx + 1} on first post`);
93
+ (0, vitest.expect)(comment.postId).toBe(posts[0].id);
94
+ });
95
+ const authorPosts = await trx.selectFrom("posts").selectAll().where("userId", "=", author.id).execute();
96
+ (0, vitest.expect)(authorPosts).toHaveLength(3);
97
+ const firstPostComments = await trx.selectFrom("comments").selectAll().where("postId", "=", posts[0].id).execute();
98
+ (0, vitest.expect)(firstPostComments).toHaveLength(5);
99
+ });
100
+ it("should handle seeds for complex scenarios", async ({ trx }) => {
101
+ const c = await trx.selectFrom("users").select(trx.fn.count("id").as("count")).executeTakeFirst();
102
+ const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs) => ({
103
+ name: "Default User",
104
+ email: `user${Date.now()}-${Math.random()}@example.com`,
105
+ role: "user",
106
+ createdAt: /* @__PURE__ */ new Date(),
107
+ updatedAt: /* @__PURE__ */ new Date()
108
+ }));
109
+ const postBuilder = require_KyselyFactory.KyselyFactory.createBuilder("posts", async (attrs, factory$1) => {
110
+ if (!attrs.userId) {
111
+ const user = await factory$1.insert("user");
112
+ return {
113
+ title: "Default Post",
114
+ content: "Default content",
115
+ userId: user.id,
116
+ published: false,
117
+ createdAt: /* @__PURE__ */ new Date(),
118
+ updatedAt: /* @__PURE__ */ new Date()
119
+ };
120
+ }
121
+ return {
122
+ title: "Default Post",
123
+ content: "Default content",
124
+ published: false,
125
+ createdAt: /* @__PURE__ */ new Date(),
126
+ updatedAt: /* @__PURE__ */ new Date()
127
+ };
128
+ });
129
+ const builders = {
130
+ user: userBuilder,
131
+ post: postBuilder
132
+ };
133
+ const seeds = {
134
+ blogWithAdminAndPosts: require_KyselyFactory.KyselyFactory.createSeed(async (attrs, factory$1, db$1) => {
135
+ const admin = await factory$1.insert("user", {
136
+ name: "Blog Admin",
137
+ email: "admin@blog.com",
138
+ role: "admin"
139
+ });
140
+ const postCount = attrs.postCount || 3;
141
+ const posts = await factory$1.insertMany(postCount, "post", (idx) => ({
142
+ title: `Admin Post ${idx + 1}`,
143
+ content: `Content for admin post ${idx + 1}`,
144
+ userId: admin.id,
145
+ published: true
146
+ }));
147
+ return {
148
+ admin,
149
+ posts,
150
+ summary: {
151
+ adminId: admin.id,
152
+ postIds: posts.map((p) => p.id),
153
+ totalPosts: posts.length
154
+ }
155
+ };
156
+ }),
157
+ usersWithPosts: require_KyselyFactory.KyselyFactory.createSeed(async (attrs, factory$1, db$1) => {
158
+ const userCount = attrs.userCount || 2;
159
+ const postsPerUser = attrs.postsPerUser || 2;
160
+ const results = [];
161
+ for (let i = 0; i < userCount; i++) {
162
+ const user = await factory$1.insert("user", {
163
+ name: `User ${i + 1}`,
164
+ email: `user${i + 1}@example.com`
165
+ });
166
+ const posts = await factory$1.insertMany(postsPerUser, "post", (postIdx) => ({
167
+ title: `User ${i + 1} Post ${postIdx + 1}`,
168
+ content: `Content from user ${i + 1}, post ${postIdx + 1}`,
169
+ userId: user.id,
170
+ published: postIdx === 0
171
+ }));
172
+ results.push({
173
+ user,
174
+ posts
175
+ });
176
+ }
177
+ return results;
178
+ })
179
+ };
180
+ const factory = new require_KyselyFactory.KyselyFactory(builders, seeds, trx);
181
+ const blogData = await factory.seed("blogWithAdminAndPosts", { postCount: 5 });
182
+ (0, vitest.expect)(blogData.admin.name).toBe("Blog Admin");
183
+ (0, vitest.expect)(blogData.admin.role).toBe("admin");
184
+ (0, vitest.expect)(blogData.posts).toHaveLength(5);
185
+ (0, vitest.expect)(blogData.summary.totalPosts).toBe(5);
186
+ const userData = await factory.seed("usersWithPosts", {
187
+ userCount: 3,
188
+ postsPerUser: 4
189
+ });
190
+ (0, vitest.expect)(userData).toHaveLength(3);
191
+ const totalUsers = await trx.selectFrom("users").select(trx.fn.count("id").as("count")).executeTakeFirst();
192
+ const totalPosts = await trx.selectFrom("posts").select(trx.fn.count("id").as("count")).executeTakeFirst();
193
+ (0, vitest.expect)(Number(totalUsers?.count)).toBe(4);
194
+ (0, vitest.expect)(Number(totalPosts?.count)).toBe(17);
195
+ });
196
+ it("should handle transaction isolation properly", async ({ trx }) => {
197
+ const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs, factory, db$1, faker) => ({
198
+ name: "Test User",
199
+ email: faker.internet.email(),
200
+ role: "user",
201
+ createdAt: /* @__PURE__ */ new Date(),
202
+ updatedAt: /* @__PURE__ */ new Date()
203
+ }));
204
+ const builders = { user: userBuilder };
205
+ const factory1 = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
206
+ const user = await factory1.insert("user", {
207
+ name: "Transaction User",
208
+ email: "transaction@test.com"
209
+ });
210
+ const userInTrx = await trx.selectFrom("users").selectAll().where("id", "=", user.id).executeTakeFirst();
211
+ (0, vitest.expect)(userInTrx).toBeDefined();
212
+ (0, vitest.expect)(userInTrx?.name).toBe("Transaction User");
213
+ });
214
+ });
215
+ (0, vitest.describe)("Performance and Edge Cases", () => {
216
+ it("should handle creating many records efficiently", async ({ trx }) => {
217
+ const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs, factory$1, db$1, faker) => ({
218
+ name: `User ${Math.random()}`,
219
+ email: faker.internet.email().toLowerCase(),
220
+ role: "user",
221
+ createdAt: /* @__PURE__ */ new Date(),
222
+ updatedAt: /* @__PURE__ */ new Date()
223
+ }));
224
+ const builders = { user: userBuilder };
225
+ const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
226
+ const startTime = Date.now();
227
+ const users = await factory.insertMany(100, "user");
228
+ const endTime = Date.now();
229
+ const duration = endTime - startTime;
230
+ (0, vitest.expect)(users).toHaveLength(100);
231
+ (0, vitest.expect)(duration).toBeLessThan(5e3);
232
+ const emails = users.map((u) => u.email);
233
+ const uniqueEmails = new Set(emails);
234
+ (0, vitest.expect)(uniqueEmails.size).toBe(100);
235
+ });
236
+ it("should handle complex attribute generation", async ({ trx }) => {
237
+ const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder("users", async (attrs, factory$1, db$1, faker) => {
238
+ return {
239
+ name: `Generated User ${attrs.id}`,
240
+ email: faker.internet.email().toLowerCase(),
241
+ role: "user"
242
+ };
243
+ });
244
+ const postBuilder = require_KyselyFactory.KyselyFactory.createBuilder("posts", async (attrs, factory$1) => {
245
+ let userId = attrs.userId;
246
+ if (!userId) {
247
+ const user = await factory$1.insert("user");
248
+ userId = user.id;
249
+ }
250
+ return {
251
+ title: `Auto-generated Post`,
252
+ content: `This is auto-generated content for post. Lorem ipsum dolor sit amet.`,
253
+ published: true,
254
+ userId,
255
+ createdAt: /* @__PURE__ */ new Date(),
256
+ updatedAt: /* @__PURE__ */ new Date()
257
+ };
258
+ });
259
+ const builders = {
260
+ user: userBuilder,
261
+ post: postBuilder
262
+ };
263
+ const factory = new require_KyselyFactory.KyselyFactory(builders, {}, trx);
264
+ const posts = await factory.insertMany(10, "post", (i) => ({ published: i % 2 === 0 }));
265
+ (0, vitest.expect)(posts).toHaveLength(10);
266
+ const users = await trx.selectFrom("users").selectAll().execute();
267
+ users.forEach((user) => {
268
+ (0, vitest.expect)(user.email).toBe(user.email.toLowerCase());
269
+ (0, vitest.expect)(user.name).not.toMatch(/^\s|\s$/);
270
+ });
271
+ const publishedPosts = posts.filter((p) => p.published);
272
+ const unpublishedPosts = posts.filter((p) => !p.published);
273
+ (0, vitest.expect)(publishedPosts).toHaveLength(5);
274
+ (0, vitest.expect)(unpublishedPosts).toHaveLength(5);
275
+ });
276
+ });
277
+ });
278
+
279
+ //#endregion
@@ -0,0 +1,278 @@
1
+ import "../Factory-DlzMkMzb.mjs";
2
+ import "../faker-BwaXA_RF.mjs";
3
+ import { KyselyFactory } from "../KyselyFactory-pOMOFQWE.mjs";
4
+ import "../VitestTransactionIsolator-BjVXqFs6.mjs";
5
+ import "../VitestKyselyTransactionIsolator-BS3R-V0I.mjs";
6
+ import { createKyselyDb, wrapVitestKyselyTransaction } from "../helpers-DOiGIkaU.mjs";
7
+ import { TEST_DATABASE_CONFIG, createTestTables } from "../helpers-BfuX-cjN.mjs";
8
+ import { beforeAll, describe, expect } from "vitest";
9
+
10
+ //#region src/__tests__/integration.spec.ts
11
+ const db = createKyselyDb(TEST_DATABASE_CONFIG);
12
+ const it$1 = wrapVitestKyselyTransaction(db, createTestTables);
13
+ describe("Testkit Integration Tests", () => {
14
+ beforeAll(async () => {});
15
+ describe("Complex Factory Scenarios", () => {
16
+ it$1("should handle complex multi-table data creation", async ({ trx }) => {
17
+ const userBuilder = KyselyFactory.createBuilder("users", async (attrs) => ({
18
+ name: "John Doe",
19
+ email: `user${Date.now()}-${Math.random()}@example.com`,
20
+ role: "user",
21
+ createdAt: /* @__PURE__ */ new Date(),
22
+ updatedAt: /* @__PURE__ */ new Date()
23
+ }));
24
+ const postBuilder = KyselyFactory.createBuilder("posts", async (attrs, factory$1) => {
25
+ if (!attrs.userId) {
26
+ const user = await factory$1.insert("user");
27
+ return {
28
+ title: "Default Post Title",
29
+ content: "Default post content...",
30
+ userId: user.id,
31
+ published: false,
32
+ createdAt: /* @__PURE__ */ new Date(),
33
+ updatedAt: /* @__PURE__ */ new Date()
34
+ };
35
+ }
36
+ return {
37
+ title: "Default Post Title",
38
+ content: "Default post content...",
39
+ published: false,
40
+ createdAt: /* @__PURE__ */ new Date(),
41
+ updatedAt: /* @__PURE__ */ new Date()
42
+ };
43
+ });
44
+ const commentBuilder = KyselyFactory.createBuilder("comments", async (attrs, factory$1) => {
45
+ let postId = attrs.postId;
46
+ let userId = attrs.userId;
47
+ if (!postId) {
48
+ const post = await factory$1.insert("post");
49
+ postId = post.id;
50
+ }
51
+ if (!userId) {
52
+ const user = await factory$1.insert("user");
53
+ userId = user.id;
54
+ }
55
+ return {
56
+ content: "Default comment content",
57
+ postId,
58
+ userId,
59
+ createdAt: /* @__PURE__ */ new Date()
60
+ };
61
+ });
62
+ const builders = {
63
+ user: userBuilder,
64
+ post: postBuilder,
65
+ comment: commentBuilder
66
+ };
67
+ const factory = new KyselyFactory(builders, {}, trx);
68
+ const author = await factory.insert("user", {
69
+ name: "Jane Author",
70
+ email: "jane@author.com",
71
+ role: "admin"
72
+ });
73
+ const posts = await factory.insertMany(3, "post", (idx) => ({
74
+ title: `Post ${idx + 1}`,
75
+ content: `Content for post ${idx + 1}`,
76
+ userId: author.id,
77
+ published: idx < 2
78
+ }));
79
+ const comments = await factory.insertMany(5, "comment", (idx) => ({
80
+ content: `Comment ${idx + 1} on first post`,
81
+ postId: posts[0].id
82
+ }));
83
+ expect(author.name).toBe("Jane Author");
84
+ expect(author.role).toBe("admin");
85
+ expect(posts).toHaveLength(3);
86
+ expect(posts[0].title).toBe("Post 1");
87
+ expect(posts[0].published).toBe(true);
88
+ expect(posts[2].published).toBe(false);
89
+ expect(comments).toHaveLength(5);
90
+ comments.forEach((comment, idx) => {
91
+ expect(comment.content).toBe(`Comment ${idx + 1} on first post`);
92
+ expect(comment.postId).toBe(posts[0].id);
93
+ });
94
+ const authorPosts = await trx.selectFrom("posts").selectAll().where("userId", "=", author.id).execute();
95
+ expect(authorPosts).toHaveLength(3);
96
+ const firstPostComments = await trx.selectFrom("comments").selectAll().where("postId", "=", posts[0].id).execute();
97
+ expect(firstPostComments).toHaveLength(5);
98
+ });
99
+ it$1("should handle seeds for complex scenarios", async ({ trx }) => {
100
+ const c = await trx.selectFrom("users").select(trx.fn.count("id").as("count")).executeTakeFirst();
101
+ const userBuilder = KyselyFactory.createBuilder("users", async (attrs) => ({
102
+ name: "Default User",
103
+ email: `user${Date.now()}-${Math.random()}@example.com`,
104
+ role: "user",
105
+ createdAt: /* @__PURE__ */ new Date(),
106
+ updatedAt: /* @__PURE__ */ new Date()
107
+ }));
108
+ const postBuilder = KyselyFactory.createBuilder("posts", async (attrs, factory$1) => {
109
+ if (!attrs.userId) {
110
+ const user = await factory$1.insert("user");
111
+ return {
112
+ title: "Default Post",
113
+ content: "Default content",
114
+ userId: user.id,
115
+ published: false,
116
+ createdAt: /* @__PURE__ */ new Date(),
117
+ updatedAt: /* @__PURE__ */ new Date()
118
+ };
119
+ }
120
+ return {
121
+ title: "Default Post",
122
+ content: "Default content",
123
+ published: false,
124
+ createdAt: /* @__PURE__ */ new Date(),
125
+ updatedAt: /* @__PURE__ */ new Date()
126
+ };
127
+ });
128
+ const builders = {
129
+ user: userBuilder,
130
+ post: postBuilder
131
+ };
132
+ const seeds = {
133
+ blogWithAdminAndPosts: KyselyFactory.createSeed(async (attrs, factory$1, db$1) => {
134
+ const admin = await factory$1.insert("user", {
135
+ name: "Blog Admin",
136
+ email: "admin@blog.com",
137
+ role: "admin"
138
+ });
139
+ const postCount = attrs.postCount || 3;
140
+ const posts = await factory$1.insertMany(postCount, "post", (idx) => ({
141
+ title: `Admin Post ${idx + 1}`,
142
+ content: `Content for admin post ${idx + 1}`,
143
+ userId: admin.id,
144
+ published: true
145
+ }));
146
+ return {
147
+ admin,
148
+ posts,
149
+ summary: {
150
+ adminId: admin.id,
151
+ postIds: posts.map((p) => p.id),
152
+ totalPosts: posts.length
153
+ }
154
+ };
155
+ }),
156
+ usersWithPosts: KyselyFactory.createSeed(async (attrs, factory$1, db$1) => {
157
+ const userCount = attrs.userCount || 2;
158
+ const postsPerUser = attrs.postsPerUser || 2;
159
+ const results = [];
160
+ for (let i = 0; i < userCount; i++) {
161
+ const user = await factory$1.insert("user", {
162
+ name: `User ${i + 1}`,
163
+ email: `user${i + 1}@example.com`
164
+ });
165
+ const posts = await factory$1.insertMany(postsPerUser, "post", (postIdx) => ({
166
+ title: `User ${i + 1} Post ${postIdx + 1}`,
167
+ content: `Content from user ${i + 1}, post ${postIdx + 1}`,
168
+ userId: user.id,
169
+ published: postIdx === 0
170
+ }));
171
+ results.push({
172
+ user,
173
+ posts
174
+ });
175
+ }
176
+ return results;
177
+ })
178
+ };
179
+ const factory = new KyselyFactory(builders, seeds, trx);
180
+ const blogData = await factory.seed("blogWithAdminAndPosts", { postCount: 5 });
181
+ expect(blogData.admin.name).toBe("Blog Admin");
182
+ expect(blogData.admin.role).toBe("admin");
183
+ expect(blogData.posts).toHaveLength(5);
184
+ expect(blogData.summary.totalPosts).toBe(5);
185
+ const userData = await factory.seed("usersWithPosts", {
186
+ userCount: 3,
187
+ postsPerUser: 4
188
+ });
189
+ expect(userData).toHaveLength(3);
190
+ const totalUsers = await trx.selectFrom("users").select(trx.fn.count("id").as("count")).executeTakeFirst();
191
+ const totalPosts = await trx.selectFrom("posts").select(trx.fn.count("id").as("count")).executeTakeFirst();
192
+ expect(Number(totalUsers?.count)).toBe(4);
193
+ expect(Number(totalPosts?.count)).toBe(17);
194
+ });
195
+ it$1("should handle transaction isolation properly", async ({ trx }) => {
196
+ const userBuilder = KyselyFactory.createBuilder("users", async (attrs, factory, db$1, faker) => ({
197
+ name: "Test User",
198
+ email: faker.internet.email(),
199
+ role: "user",
200
+ createdAt: /* @__PURE__ */ new Date(),
201
+ updatedAt: /* @__PURE__ */ new Date()
202
+ }));
203
+ const builders = { user: userBuilder };
204
+ const factory1 = new KyselyFactory(builders, {}, trx);
205
+ const user = await factory1.insert("user", {
206
+ name: "Transaction User",
207
+ email: "transaction@test.com"
208
+ });
209
+ const userInTrx = await trx.selectFrom("users").selectAll().where("id", "=", user.id).executeTakeFirst();
210
+ expect(userInTrx).toBeDefined();
211
+ expect(userInTrx?.name).toBe("Transaction User");
212
+ });
213
+ });
214
+ describe("Performance and Edge Cases", () => {
215
+ it$1("should handle creating many records efficiently", async ({ trx }) => {
216
+ const userBuilder = KyselyFactory.createBuilder("users", async (attrs, factory$1, db$1, faker) => ({
217
+ name: `User ${Math.random()}`,
218
+ email: faker.internet.email().toLowerCase(),
219
+ role: "user",
220
+ createdAt: /* @__PURE__ */ new Date(),
221
+ updatedAt: /* @__PURE__ */ new Date()
222
+ }));
223
+ const builders = { user: userBuilder };
224
+ const factory = new KyselyFactory(builders, {}, trx);
225
+ const startTime = Date.now();
226
+ const users = await factory.insertMany(100, "user");
227
+ const endTime = Date.now();
228
+ const duration = endTime - startTime;
229
+ expect(users).toHaveLength(100);
230
+ expect(duration).toBeLessThan(5e3);
231
+ const emails = users.map((u) => u.email);
232
+ const uniqueEmails = new Set(emails);
233
+ expect(uniqueEmails.size).toBe(100);
234
+ });
235
+ it$1("should handle complex attribute generation", async ({ trx }) => {
236
+ const userBuilder = KyselyFactory.createBuilder("users", async (attrs, factory$1, db$1, faker) => {
237
+ return {
238
+ name: `Generated User ${attrs.id}`,
239
+ email: faker.internet.email().toLowerCase(),
240
+ role: "user"
241
+ };
242
+ });
243
+ const postBuilder = KyselyFactory.createBuilder("posts", async (attrs, factory$1) => {
244
+ let userId = attrs.userId;
245
+ if (!userId) {
246
+ const user = await factory$1.insert("user");
247
+ userId = user.id;
248
+ }
249
+ return {
250
+ title: `Auto-generated Post`,
251
+ content: `This is auto-generated content for post. Lorem ipsum dolor sit amet.`,
252
+ published: true,
253
+ userId,
254
+ createdAt: /* @__PURE__ */ new Date(),
255
+ updatedAt: /* @__PURE__ */ new Date()
256
+ };
257
+ });
258
+ const builders = {
259
+ user: userBuilder,
260
+ post: postBuilder
261
+ };
262
+ const factory = new KyselyFactory(builders, {}, trx);
263
+ const posts = await factory.insertMany(10, "post", (i) => ({ published: i % 2 === 0 }));
264
+ expect(posts).toHaveLength(10);
265
+ const users = await trx.selectFrom("users").selectAll().execute();
266
+ users.forEach((user) => {
267
+ expect(user.email).toBe(user.email.toLowerCase());
268
+ expect(user.name).not.toMatch(/^\s|\s$/);
269
+ });
270
+ const publishedPosts = posts.filter((p) => p.published);
271
+ const unpublishedPosts = posts.filter((p) => !p.published);
272
+ expect(publishedPosts).toHaveLength(5);
273
+ expect(unpublishedPosts).toHaveLength(5);
274
+ });
275
+ });
276
+ });
277
+
278
+ //#endregion
@@ -0,0 +1,30 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+
23
+ //#endregion
24
+
25
+ Object.defineProperty(exports, '__toESM', {
26
+ enumerable: true,
27
+ get: function () {
28
+ return __toESM;
29
+ }
30
+ });
package/dist/example.cjs CHANGED
@@ -1,5 +1,6 @@
1
1
  require('./Factory-DREHoms3.cjs');
2
- const require_KyselyFactory = require('./KyselyFactory-DiiWtMYe.cjs');
2
+ require('./faker-caz-8zt8.cjs');
3
+ const require_KyselyFactory = require('./KyselyFactory-BX7Kv2uP.cjs');
3
4
 
4
5
  //#region src/example.ts
5
6
  const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder({
package/dist/example.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  import "./Factory-DlzMkMzb.mjs";
2
- import { KyselyFactory } from "./KyselyFactory-DZewtWtJ.mjs";
2
+ import "./faker-BwaXA_RF.mjs";
3
+ import { KyselyFactory } from "./KyselyFactory-pOMOFQWE.mjs";
3
4
 
4
5
  //#region src/example.ts
5
6
  const userBuilder = KyselyFactory.createBuilder({
@@ -0,0 +1,85 @@
1
+ import { faker } from "@faker-js/faker";
2
+
3
+ //#region src/faker.ts
4
+ /**
5
+ * Atomic counter implementation for thread-safe sequence generation
6
+ */
7
+ var AtomicCounter = class {
8
+ value;
9
+ constructor(initialValue = 0) {
10
+ this.value = initialValue;
11
+ }
12
+ increment() {
13
+ return ++this.value;
14
+ }
15
+ get() {
16
+ return this.value;
17
+ }
18
+ reset(value = 0) {
19
+ this.value = value;
20
+ }
21
+ };
22
+ /**
23
+ * Sets the `insertedAt` and `updatedAt` to a random date in the past.
24
+ */
25
+ function timestamps() {
26
+ const createdAt = faker$1.date.past();
27
+ const updatedAt = faker$1.date.between({
28
+ from: createdAt,
29
+ to: /* @__PURE__ */ new Date()
30
+ });
31
+ createdAt.setMilliseconds(0);
32
+ updatedAt.setMilliseconds(0);
33
+ return {
34
+ createdAt,
35
+ updatedAt
36
+ };
37
+ }
38
+ /**
39
+ * Returns a reverse domain name identifier.
40
+ */
41
+ function identifier(suffix) {
42
+ return [
43
+ faker$1.internet.domainSuffix(),
44
+ faker$1.internet.domainWord(),
45
+ suffix ? suffix : faker$1.internet.domainWord() + sequence("identifier")
46
+ ].join(".");
47
+ }
48
+ const sequences = /* @__PURE__ */ new Map();
49
+ function sequence(name = "default") {
50
+ if (!sequences.has(name)) sequences.set(name, new AtomicCounter());
51
+ const counter = sequences.get(name);
52
+ return counter.increment();
53
+ }
54
+ /**
55
+ * Resets a sequence counter to a specific value (default: 0)
56
+ */
57
+ function resetSequence(name = "default", value = 0) {
58
+ if (sequences.has(name)) {
59
+ const counter = sequences.get(name);
60
+ counter.reset(value);
61
+ } else sequences.set(name, new AtomicCounter(value));
62
+ }
63
+ /**
64
+ * Resets all sequence counters
65
+ */
66
+ function resetAllSequences() {
67
+ sequences.clear();
68
+ }
69
+ /**
70
+ * Returns a random price number.
71
+ */
72
+ function price() {
73
+ return +faker$1.commerce.price();
74
+ }
75
+ const faker$1 = Object.freeze(Object.assign({}, faker, {
76
+ timestamps,
77
+ identifier,
78
+ sequence,
79
+ resetSequence,
80
+ resetAllSequences,
81
+ price
82
+ }));
83
+
84
+ //#endregion
85
+ export { faker$1 as faker, identifier, resetAllSequences, resetSequence, sequence, timestamps };