@geekmidas/testkit 10.0.0-alpha.3 → 10.0.0-alpha.30
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/README.md +74 -0
- package/dist/Factory.cjs +51 -2
- package/dist/Factory.cjs.map +1 -0
- package/dist/Factory.d.cts +143 -3
- package/dist/Factory.d.cts.map +1 -0
- package/dist/Factory.d.mts +143 -3
- package/dist/Factory.d.mts.map +1 -0
- package/dist/Factory.mjs +50 -2
- package/dist/Factory.mjs.map +1 -0
- package/dist/KyselyFactory.cjs +237 -4
- package/dist/KyselyFactory.cjs.map +1 -0
- package/dist/KyselyFactory.d.cts +224 -4
- package/dist/KyselyFactory.d.cts.map +1 -0
- package/dist/KyselyFactory.d.mts +224 -4
- package/dist/KyselyFactory.d.mts.map +1 -0
- package/dist/KyselyFactory.mjs +236 -4
- package/dist/KyselyFactory.mjs.map +1 -0
- package/dist/ObjectionFactory.cjs +265 -4
- package/dist/ObjectionFactory.cjs.map +1 -0
- package/dist/ObjectionFactory.d.cts +237 -4
- package/dist/ObjectionFactory.d.cts.map +1 -0
- package/dist/ObjectionFactory.d.mts +237 -4
- package/dist/ObjectionFactory.d.mts.map +1 -0
- package/dist/ObjectionFactory.mjs +264 -4
- package/dist/ObjectionFactory.mjs.map +1 -0
- package/dist/PostgresKyselyMigrator.cjs +94 -3
- package/dist/PostgresKyselyMigrator.cjs.map +1 -0
- package/dist/PostgresKyselyMigrator.d.cts +80 -3
- package/dist/PostgresKyselyMigrator.d.cts.map +1 -0
- package/dist/PostgresKyselyMigrator.d.mts +80 -3
- package/dist/PostgresKyselyMigrator.d.mts.map +1 -0
- package/dist/PostgresKyselyMigrator.mjs +93 -3
- package/dist/PostgresKyselyMigrator.mjs.map +1 -0
- package/dist/{PostgresMigrator-C_QQ6q35.d.mts → PostgresMigrator-CDPnnwMJ.d.cts} +11 -5
- package/dist/PostgresMigrator-CDPnnwMJ.d.cts.map +1 -0
- package/dist/{PostgresMigrator-CeYy-eHF.d.cts → PostgresMigrator-CDPnnwMJ.d.mts} +11 -5
- package/dist/PostgresMigrator-CDPnnwMJ.d.mts.map +1 -0
- package/dist/PostgresMigrator.cjs +178 -2
- package/dist/PostgresMigrator.cjs.map +1 -0
- package/dist/PostgresMigrator.d.cts +1 -1
- package/dist/PostgresMigrator.d.mts +1 -1
- package/dist/PostgresMigrator.mjs +175 -2
- package/dist/PostgresMigrator.mjs.map +1 -0
- package/dist/PostgresObjectionMigrator.cjs +118 -3
- package/dist/PostgresObjectionMigrator.cjs.map +1 -0
- package/dist/PostgresObjectionMigrator.d.cts +75 -3
- package/dist/PostgresObjectionMigrator.d.cts.map +1 -0
- package/dist/PostgresObjectionMigrator.d.mts +75 -3
- package/dist/PostgresObjectionMigrator.d.mts.map +1 -0
- package/dist/PostgresObjectionMigrator.mjs +117 -3
- package/dist/PostgresObjectionMigrator.mjs.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.cjs +66 -3
- package/dist/VitestKyselyTransactionIsolator.cjs.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.d.cts +60 -3
- package/dist/VitestKyselyTransactionIsolator.d.cts.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.d.mts +60 -3
- package/dist/VitestKyselyTransactionIsolator.d.mts.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.mjs +65 -3
- package/dist/VitestKyselyTransactionIsolator.mjs.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.cjs +62 -3
- package/dist/VitestObjectionTransactionIsolator.cjs.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.d.cts +56 -3
- package/dist/VitestObjectionTransactionIsolator.d.cts.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.d.mts +56 -3
- package/dist/VitestObjectionTransactionIsolator.d.mts.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.mjs +61 -3
- package/dist/VitestObjectionTransactionIsolator.mjs.map +1 -0
- package/dist/VitestTransactionIsolator.cjs +187 -4
- package/dist/VitestTransactionIsolator.cjs.map +1 -0
- package/dist/VitestTransactionIsolator.d.cts +219 -2
- package/dist/VitestTransactionIsolator.d.cts.map +1 -0
- package/dist/VitestTransactionIsolator.d.mts +219 -2
- package/dist/VitestTransactionIsolator.d.mts.map +1 -0
- package/dist/VitestTransactionIsolator.mjs +184 -2
- package/dist/VitestTransactionIsolator.mjs.map +1 -0
- package/dist/aws.cjs +3 -4
- package/dist/aws.cjs.map +1 -1
- package/dist/aws.d.cts +3 -8
- package/dist/aws.d.cts.map +1 -1
- package/dist/aws.d.mts +3 -8
- package/dist/aws.d.mts.map +1 -1
- package/dist/aws.mjs +1 -2
- package/dist/aws.mjs.map +1 -1
- package/dist/benchmark.cjs +2 -2
- package/dist/benchmark.cjs.map +1 -1
- package/dist/benchmark.d.cts +4 -6
- package/dist/benchmark.d.cts.map +1 -1
- package/dist/benchmark.d.mts +4 -6
- package/dist/benchmark.d.mts.map +1 -1
- package/dist/benchmark.mjs +1 -1
- package/dist/benchmark.mjs.map +1 -1
- package/dist/better-auth.cjs +31 -15
- package/dist/better-auth.cjs.map +1 -1
- package/dist/better-auth.d.cts +13142 -4
- package/dist/better-auth.d.cts.map +1 -1
- package/dist/better-auth.d.mts +13142 -4
- package/dist/better-auth.d.mts.map +1 -1
- package/dist/better-auth.mjs +29 -13
- package/dist/better-auth.mjs.map +1 -1
- package/dist/browser.cjs +127 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.d.cts +47 -0
- package/dist/browser.d.cts.map +1 -0
- package/dist/browser.d.mts +47 -0
- package/dist/browser.d.mts.map +1 -0
- package/dist/browser.mjs +125 -0
- package/dist/browser.mjs.map +1 -0
- package/dist/context.cjs +80 -0
- package/dist/context.cjs.map +1 -0
- package/dist/context.d.cts +55 -0
- package/dist/context.d.cts.map +1 -0
- package/dist/context.d.mts +55 -0
- package/dist/context.d.mts.map +1 -0
- package/dist/context.mjs +73 -0
- package/dist/context.mjs.map +1 -0
- package/dist/faker.cjs +262 -8
- package/dist/faker.cjs.map +1 -0
- package/dist/faker.d.cts +162 -2
- package/dist/faker.d.cts.map +1 -0
- package/dist/faker.d.mts +162 -2
- package/dist/faker.d.mts.map +1 -0
- package/dist/faker.mjs +255 -2
- package/dist/faker.mjs.map +1 -0
- package/dist/helpers.cjs +6 -5
- package/dist/helpers.cjs.map +1 -1
- package/dist/helpers.d.cts +1 -5
- package/dist/helpers.d.cts.map +1 -1
- package/dist/helpers.d.mts +1 -5
- package/dist/helpers.d.mts.map +1 -1
- package/dist/helpers.mjs +1 -2
- package/dist/helpers.mjs.map +1 -1
- package/dist/initScript.cjs +15 -15
- package/dist/initScript.cjs.map +1 -1
- package/dist/initScript.d.cts +2 -4
- package/dist/initScript.d.cts.map +1 -1
- package/dist/initScript.d.mts +2 -4
- package/dist/initScript.d.mts.map +1 -1
- package/dist/initScript.mjs +10 -12
- package/dist/initScript.mjs.map +1 -1
- package/dist/kysely.cjs +10 -13
- package/dist/kysely.cjs.map +1 -1
- package/dist/kysely.d.cts +10 -14
- package/dist/kysely.d.cts.map +1 -1
- package/dist/kysely.d.mts +10 -14
- package/dist/kysely.d.mts.map +1 -1
- package/dist/kysely.mjs +10 -14
- package/dist/kysely.mjs.map +1 -1
- package/dist/logger.cjs +3 -4
- package/dist/logger.cjs.map +1 -1
- package/dist/logger.d.cts +1 -5
- package/dist/logger.d.cts.map +1 -1
- package/dist/logger.d.mts +1 -5
- package/dist/logger.d.mts.map +1 -1
- package/dist/logger.mjs +1 -2
- package/dist/logger.mjs.map +1 -1
- package/dist/mailbox.cjs +95 -0
- package/dist/mailbox.cjs.map +1 -0
- package/dist/mailbox.d.cts +58 -0
- package/dist/mailbox.d.cts.map +1 -0
- package/dist/mailbox.d.mts +58 -0
- package/dist/mailbox.d.mts.map +1 -0
- package/dist/mailbox.mjs +92 -0
- package/dist/mailbox.mjs.map +1 -0
- package/dist/objection.cjs +10 -13
- package/dist/objection.cjs.map +1 -1
- package/dist/objection.d.cts +10 -14
- package/dist/objection.d.cts.map +1 -1
- package/dist/objection.d.mts +10 -14
- package/dist/objection.d.mts.map +1 -1
- package/dist/objection.mjs +10 -14
- package/dist/objection.mjs.map +1 -1
- package/dist/os/directory.cjs +31 -2
- package/dist/os/directory.cjs.map +1 -0
- package/dist/os/directory.d.cts +9 -2
- package/dist/os/directory.d.cts.map +1 -0
- package/dist/os/directory.d.mts +9 -2
- package/dist/os/directory.d.mts.map +1 -0
- package/dist/os/directory.mjs +25 -2
- package/dist/os/directory.mjs.map +1 -0
- package/dist/os/index.cjs +3 -3
- package/dist/os/index.d.cts +1 -1
- package/dist/os/index.d.mts +1 -1
- package/dist/os/index.mjs +2 -3
- package/dist/requestContext.cjs +10 -11
- package/dist/requestContext.cjs.map +1 -1
- package/dist/requestContext.d.cts +4 -8
- package/dist/requestContext.d.cts.map +1 -1
- package/dist/requestContext.d.mts +4 -8
- package/dist/requestContext.d.mts.map +1 -1
- package/dist/requestContext.mjs +1 -2
- package/dist/requestContext.mjs.map +1 -1
- package/dist/{chunk-CUT6urMc.cjs → rolldown-runtime-VH7oDXx4.cjs} +8 -10
- package/dist/timer.cjs +2 -2
- package/dist/timer.cjs.map +1 -1
- package/dist/timer.d.cts +1 -4
- package/dist/timer.d.cts.map +1 -1
- package/dist/timer.d.mts +1 -4
- package/dist/timer.d.mts.map +1 -1
- package/dist/timer.mjs +1 -1
- package/dist/timer.mjs.map +1 -1
- package/dist/transactions.cjs +123 -0
- package/dist/transactions.cjs.map +1 -0
- package/dist/transactions.d.cts +44 -0
- package/dist/transactions.d.cts.map +1 -0
- package/dist/transactions.d.mts +44 -0
- package/dist/transactions.d.mts.map +1 -0
- package/dist/transactions.mjs +118 -0
- package/dist/transactions.mjs.map +1 -0
- package/package.json +59 -16
- package/dist/Factory-BFVnMMCC.mjs +0 -51
- package/dist/Factory-BFVnMMCC.mjs.map +0 -1
- package/dist/Factory-BOX312yd.d.cts +0 -147
- package/dist/Factory-BOX312yd.d.cts.map +0 -1
- package/dist/Factory-BhjUOBWN.cjs +0 -57
- package/dist/Factory-BhjUOBWN.cjs.map +0 -1
- package/dist/Factory-C6W78ulZ.d.mts +0 -147
- package/dist/Factory-C6W78ulZ.d.mts.map +0 -1
- package/dist/KyselyFactory-BFqVIn_0.cjs +0 -246
- package/dist/KyselyFactory-BFqVIn_0.cjs.map +0 -1
- package/dist/KyselyFactory-Cc2UmOJk.d.mts +0 -228
- package/dist/KyselyFactory-Cc2UmOJk.d.mts.map +0 -1
- package/dist/KyselyFactory-DMswpwji.mjs +0 -241
- package/dist/KyselyFactory-DMswpwji.mjs.map +0 -1
- package/dist/KyselyFactory-Dy5zzV4B.d.cts +0 -228
- package/dist/KyselyFactory-Dy5zzV4B.d.cts.map +0 -1
- package/dist/ObjectionFactory-BYnPr9ZP.d.mts +0 -241
- package/dist/ObjectionFactory-BYnPr9ZP.d.mts.map +0 -1
- package/dist/ObjectionFactory-BeFBYcan.cjs +0 -272
- package/dist/ObjectionFactory-BeFBYcan.cjs.map +0 -1
- package/dist/ObjectionFactory-CD-WFuMJ.d.cts +0 -241
- package/dist/ObjectionFactory-CD-WFuMJ.d.cts.map +0 -1
- package/dist/ObjectionFactory-QCJ7u0Ql.mjs +0 -267
- package/dist/ObjectionFactory-QCJ7u0Ql.mjs.map +0 -1
- package/dist/PostgresKyselyMigrator-B2M6Gjvv.d.cts +0 -84
- package/dist/PostgresKyselyMigrator-B2M6Gjvv.d.cts.map +0 -1
- package/dist/PostgresKyselyMigrator-BGBxjnFv.cjs +0 -101
- package/dist/PostgresKyselyMigrator-BGBxjnFv.cjs.map +0 -1
- package/dist/PostgresKyselyMigrator-CvwXycJE.d.mts +0 -84
- package/dist/PostgresKyselyMigrator-CvwXycJE.d.mts.map +0 -1
- package/dist/PostgresKyselyMigrator-PI0QhvB1.mjs +0 -95
- package/dist/PostgresKyselyMigrator-PI0QhvB1.mjs.map +0 -1
- package/dist/PostgresMigrator-C_QQ6q35.d.mts.map +0 -1
- package/dist/PostgresMigrator-CeYy-eHF.d.cts.map +0 -1
- package/dist/PostgresMigrator-DVAY04qN.mjs +0 -166
- package/dist/PostgresMigrator-DVAY04qN.mjs.map +0 -1
- package/dist/PostgresMigrator-M9jpzOvN.cjs +0 -172
- package/dist/PostgresMigrator-M9jpzOvN.cjs.map +0 -1
- package/dist/PostgresObjectionMigrator-1j6YIB1c.d.mts +0 -79
- package/dist/PostgresObjectionMigrator-1j6YIB1c.d.mts.map +0 -1
- package/dist/PostgresObjectionMigrator-DHVC9h_P.d.cts +0 -79
- package/dist/PostgresObjectionMigrator-DHVC9h_P.d.cts.map +0 -1
- package/dist/PostgresObjectionMigrator-DSaPhwjY.cjs +0 -123
- package/dist/PostgresObjectionMigrator-DSaPhwjY.cjs.map +0 -1
- package/dist/PostgresObjectionMigrator-DjPKdUbm.mjs +0 -118
- package/dist/PostgresObjectionMigrator-DjPKdUbm.mjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-1Saieke7.mjs +0 -67
- package/dist/VitestKyselyTransactionIsolator-1Saieke7.mjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-BjJSXryR.cjs +0 -72
- package/dist/VitestKyselyTransactionIsolator-BjJSXryR.cjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-CwlTo6yf.d.mts +0 -64
- package/dist/VitestKyselyTransactionIsolator-CwlTo6yf.d.mts.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-DJasVuIZ.d.cts +0 -64
- package/dist/VitestKyselyTransactionIsolator-DJasVuIZ.d.cts.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-B_VSWFum.mjs +0 -63
- package/dist/VitestObjectionTransactionIsolator-B_VSWFum.mjs.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-CTTjvTPO.d.cts +0 -60
- package/dist/VitestObjectionTransactionIsolator-CTTjvTPO.d.cts.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-CyZG6nq4.d.mts +0 -60
- package/dist/VitestObjectionTransactionIsolator-CyZG6nq4.d.mts.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-DDoJTu7e.cjs +0 -68
- package/dist/VitestObjectionTransactionIsolator-DDoJTu7e.cjs.map +0 -1
- package/dist/VitestTransactionIsolator-BLaw80cx.d.mts +0 -223
- package/dist/VitestTransactionIsolator-BLaw80cx.d.mts.map +0 -1
- package/dist/VitestTransactionIsolator-CvwPecpl.mjs +0 -188
- package/dist/VitestTransactionIsolator-CvwPecpl.mjs.map +0 -1
- package/dist/VitestTransactionIsolator-DyiX-QtK.cjs +0 -206
- package/dist/VitestTransactionIsolator-DyiX-QtK.cjs.map +0 -1
- package/dist/VitestTransactionIsolator-glcxyS_R.d.cts +0 -223
- package/dist/VitestTransactionIsolator-glcxyS_R.d.cts.map +0 -1
- package/dist/directory-BxjqmtCT.mjs +0 -27
- package/dist/directory-BxjqmtCT.mjs.map +0 -1
- package/dist/directory-DD_xQoSh.cjs +0 -33
- package/dist/directory-DD_xQoSh.cjs.map +0 -1
- package/dist/directory-DGOcVlKD.d.cts +0 -13
- package/dist/directory-DGOcVlKD.d.cts.map +0 -1
- package/dist/directory-DiQ_DkaF.d.mts +0 -13
- package/dist/directory-DiQ_DkaF.d.mts.map +0 -1
- package/dist/faker-B14IEMIN.cjs +0 -304
- package/dist/faker-B14IEMIN.cjs.map +0 -1
- package/dist/faker-BGKYFoCT.mjs +0 -262
- package/dist/faker-BGKYFoCT.mjs.map +0 -1
- package/dist/faker-Cg76aFNO.d.cts +0 -166
- package/dist/faker-Cg76aFNO.d.cts.map +0 -1
- package/dist/faker-D9gz7KjY.d.mts +0 -166
- package/dist/faker-D9gz7KjY.d.mts.map +0 -1
package/dist/KyselyFactory.cjs
CHANGED
|
@@ -1,5 +1,238 @@
|
|
|
1
|
-
|
|
2
|
-
require(
|
|
3
|
-
const
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_Factory = require("./Factory.cjs");
|
|
3
|
+
const require_faker = require("./faker.cjs");
|
|
4
|
+
//#region src/KyselyFactory.ts
|
|
5
|
+
/**
|
|
6
|
+
* Factory implementation for Kysely ORM, providing test data creation utilities.
|
|
7
|
+
* Extends the base Factory class with Kysely-specific database operations.
|
|
8
|
+
*
|
|
9
|
+
* @template DB - The database schema type
|
|
10
|
+
* @template Builders - Record of builder functions for creating entities
|
|
11
|
+
* @template Seeds - Record of seed functions for complex test scenarios
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // Define your database schema
|
|
16
|
+
* interface Database {
|
|
17
|
+
* users: UsersTable;
|
|
18
|
+
* posts: PostsTable;
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* // Create builders
|
|
22
|
+
* const builders = {
|
|
23
|
+
* user: KyselyFactory.createBuilder<Database, 'users'>('users', ({ attrs, faker }) => ({
|
|
24
|
+
* id: faker.string.uuid(),
|
|
25
|
+
* name: faker.person.fullName(),
|
|
26
|
+
* email: faker.internet.email(),
|
|
27
|
+
* ...attrs
|
|
28
|
+
* })),
|
|
29
|
+
* post: KyselyFactory.createBuilder<Database, 'posts'>('posts', ({ attrs }) => ({
|
|
30
|
+
* title: 'Test Post',
|
|
31
|
+
* content: 'Test content',
|
|
32
|
+
* ...attrs
|
|
33
|
+
* }))
|
|
34
|
+
* };
|
|
35
|
+
*
|
|
36
|
+
* // Create factory instance
|
|
37
|
+
* const factory = new KyselyFactory(builders, seeds, db);
|
|
38
|
+
*
|
|
39
|
+
* // Use in tests
|
|
40
|
+
* const user = await factory.insert('user', { name: 'John Doe' });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
var KyselyFactory = class extends require_Factory.Factory {
|
|
44
|
+
builders;
|
|
45
|
+
seeds;
|
|
46
|
+
db;
|
|
47
|
+
/**
|
|
48
|
+
* Creates a typed seed function with proper type inference.
|
|
49
|
+
* Inherits from the base Factory class implementation.
|
|
50
|
+
*
|
|
51
|
+
* @template Seed - The seed function type
|
|
52
|
+
* @param seedFn - The seed function to wrap (receives { attrs, factory, db } object)
|
|
53
|
+
* @returns The same seed function with proper typing
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const seeds = {
|
|
58
|
+
* userWithPosts: KyselyFactory.createSeed(
|
|
59
|
+
* async ({ attrs, factory }) => {
|
|
60
|
+
* const user = await factory.insert('user', attrs);
|
|
61
|
+
* await factory.insertMany(3, 'post', { userId: user.id });
|
|
62
|
+
* return user;
|
|
63
|
+
* },
|
|
64
|
+
* ),
|
|
65
|
+
* };
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
static createSeed(seedFn) {
|
|
69
|
+
return require_Factory.Factory.createSeed(seedFn);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Creates a new KyselyFactory instance.
|
|
73
|
+
*
|
|
74
|
+
* @param builders - Record of builder functions for creating individual entities
|
|
75
|
+
* @param seeds - Record of seed functions for creating complex test scenarios
|
|
76
|
+
* @param db - Kysely database instance or controlled transaction
|
|
77
|
+
*/
|
|
78
|
+
constructor(builders, seeds, db) {
|
|
79
|
+
super();
|
|
80
|
+
this.builders = builders;
|
|
81
|
+
this.seeds = seeds;
|
|
82
|
+
this.db = db;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Creates a typed builder function for a specific database table.
|
|
86
|
+
* This is a utility method that helps create builders with proper type inference for Kysely.
|
|
87
|
+
*
|
|
88
|
+
* @template DB - The database schema type
|
|
89
|
+
* @template TableName - The name of the table (must be a key of DB)
|
|
90
|
+
* @template Attrs - The attributes type for the builder (defaults to Partial<Insertable>)
|
|
91
|
+
* @template Factory - The factory instance type
|
|
92
|
+
* @template Result - The result type (defaults to Selectable of the table)
|
|
93
|
+
*
|
|
94
|
+
* @param table - The name of the database table
|
|
95
|
+
* @param defaults - Optional function to provide default values (receives destructured context)
|
|
96
|
+
* @param autoInsert - Whether to automatically insert the record (default: true)
|
|
97
|
+
* @returns A builder function that creates and optionally inserts records
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* // Create a simple builder with defaults - destructure only what you need
|
|
102
|
+
* const userBuilder = KyselyFactory.createBuilder<DB, 'users'>('users',
|
|
103
|
+
* ({ attrs, faker }) => ({
|
|
104
|
+
* id: faker.string.uuid(),
|
|
105
|
+
* name: faker.person.fullName(),
|
|
106
|
+
* email: faker.internet.email(),
|
|
107
|
+
* createdAt: new Date(),
|
|
108
|
+
* ...attrs
|
|
109
|
+
* })
|
|
110
|
+
* );
|
|
111
|
+
*
|
|
112
|
+
* // Only need faker? Just destructure that
|
|
113
|
+
* const leaveTypeBuilder = KyselyFactory.createBuilder<DB, 'leaveTypes'>('leaveTypes',
|
|
114
|
+
* ({ faker }) => ({
|
|
115
|
+
* name: faker.helpers.arrayElement(['Annual', 'Sick', 'Maternity']),
|
|
116
|
+
* code: faker.string.alpha({ length: 3, casing: 'upper' }),
|
|
117
|
+
* })
|
|
118
|
+
* );
|
|
119
|
+
*
|
|
120
|
+
* // Create a builder that doesn't auto-insert (useful for nested inserts)
|
|
121
|
+
* const addressBuilder = KyselyFactory.createBuilder<DB, 'addresses'>('addresses',
|
|
122
|
+
* ({ attrs }) => ({
|
|
123
|
+
* street: '123 Main St',
|
|
124
|
+
* city: 'Anytown',
|
|
125
|
+
* ...attrs
|
|
126
|
+
* }),
|
|
127
|
+
* false // Don't auto-insert
|
|
128
|
+
* );
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
static createBuilder(table, defaults, autoInsert) {
|
|
132
|
+
return async (attrs, factory, db, fakerInstance) => {
|
|
133
|
+
let data = { ...attrs };
|
|
134
|
+
if (defaults) data = {
|
|
135
|
+
...await defaults({
|
|
136
|
+
attrs,
|
|
137
|
+
factory,
|
|
138
|
+
db,
|
|
139
|
+
faker: fakerInstance
|
|
140
|
+
}),
|
|
141
|
+
...data
|
|
142
|
+
};
|
|
143
|
+
if (autoInsert !== false) {
|
|
144
|
+
const result = await db.insertInto(table).values(data).returningAll().executeTakeFirst();
|
|
145
|
+
if (!result) throw new Error(`Failed to insert into ${table}`);
|
|
146
|
+
return result;
|
|
147
|
+
} else return {
|
|
148
|
+
table,
|
|
149
|
+
data
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Inserts a single record into the database using the specified builder.
|
|
155
|
+
* The builder function is responsible for generating the record data with defaults
|
|
156
|
+
* and the factory handles the actual database insertion.
|
|
157
|
+
*
|
|
158
|
+
* @template K - The builder name (must be a key of Builders)
|
|
159
|
+
* @param builderName - The name of the builder to use
|
|
160
|
+
* @param attrs - Optional attributes to override builder defaults
|
|
161
|
+
* @returns A promise resolving to the inserted record
|
|
162
|
+
* @throws Error if the specified builder doesn't exist
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```typescript
|
|
166
|
+
* // Insert with defaults
|
|
167
|
+
* const user = await factory.insert('user');
|
|
168
|
+
*
|
|
169
|
+
* // Insert with overrides
|
|
170
|
+
* const adminUser = await factory.insert('user', {
|
|
171
|
+
* email: 'admin@example.com',
|
|
172
|
+
* role: 'admin'
|
|
173
|
+
* });
|
|
174
|
+
*
|
|
175
|
+
* // Use the inserted record
|
|
176
|
+
* const post = await factory.insert('post', {
|
|
177
|
+
* userId: user.id,
|
|
178
|
+
* title: 'My First Post'
|
|
179
|
+
* });
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
async insert(builderName, attrs) {
|
|
183
|
+
if (!(builderName in this.builders)) throw new Error(`Factory "${builderName}" does not exist. Make sure it is correct and registered in src/test/setup.ts`);
|
|
184
|
+
const result = await this.builders[builderName](attrs || {}, this, this.db, require_faker.faker);
|
|
185
|
+
if (result && typeof result === "object" && "table" in result && "data" in result) return await this.db.insertInto(result.table).values(result.data).returningAll().executeTakeFirst();
|
|
186
|
+
return result;
|
|
187
|
+
}
|
|
188
|
+
async insertMany(count, builderName, attrs) {
|
|
189
|
+
if (!(builderName in this.builders)) throw new Error(`Builder "${builderName}" is not registered in this factory. Make sure it is correct and registered in src/test/setup.ts`);
|
|
190
|
+
const promises = [];
|
|
191
|
+
for (let i = 0; i < count; i++) {
|
|
192
|
+
const newAttrs = typeof attrs === "function" ? await attrs(i, require_faker.faker) : attrs;
|
|
193
|
+
promises.push(this.insert(builderName, newAttrs));
|
|
194
|
+
}
|
|
195
|
+
return Promise.all(promises);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Executes a seed function to create complex test scenarios with multiple related records.
|
|
199
|
+
* Seeds are useful for setting up complete test environments with realistic data relationships.
|
|
200
|
+
*
|
|
201
|
+
* @template K - The seed name (must be a key of Seeds)
|
|
202
|
+
* @param seedName - The name of the seed to execute
|
|
203
|
+
* @param attrs - Optional configuration attributes for the seed
|
|
204
|
+
* @returns The result of the seed function (typically the primary record created)
|
|
205
|
+
* @throws Error if the specified seed doesn't exist
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* ```typescript
|
|
209
|
+
* // Execute a simple seed
|
|
210
|
+
* const user = await factory.seed('userWithProfile');
|
|
211
|
+
*
|
|
212
|
+
* // Execute a seed with configuration
|
|
213
|
+
* const author = await factory.seed('authorWithBooks', {
|
|
214
|
+
* bookCount: 5,
|
|
215
|
+
* includeReviews: true
|
|
216
|
+
* });
|
|
217
|
+
*
|
|
218
|
+
* // Use seed result in tests
|
|
219
|
+
* const company = await factory.seed('companyWithDepartments', {
|
|
220
|
+
* departmentCount: 3,
|
|
221
|
+
* employeesPerDepartment: 10
|
|
222
|
+
* });
|
|
223
|
+
* expect(company.departments).toHaveLength(3);
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
seed(seedName, attrs) {
|
|
227
|
+
if (!(seedName in this.seeds)) throw new Error(`Seed "${seedName}" is not registered in this factory. Make sure it is correct and registered in src/test/setup.ts`);
|
|
228
|
+
return this.seeds[seedName]({
|
|
229
|
+
attrs: attrs || {},
|
|
230
|
+
factory: this,
|
|
231
|
+
db: this.db
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
//#endregion
|
|
236
|
+
exports.KyselyFactory = KyselyFactory;
|
|
4
237
|
|
|
5
|
-
|
|
238
|
+
//# sourceMappingURL=KyselyFactory.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"KyselyFactory.cjs","names":["Factory","faker"],"sources":["../src/KyselyFactory.ts"],"sourcesContent":["import type {\n\tControlledTransaction,\n\tInsertable,\n\tKysely,\n\tSelectable,\n} from 'kysely';\nimport { type ExtractSeedAttrs, Factory, type FactorySeed } from './Factory';\nimport { type FakerFactory, faker } from './faker';\n\n/**\n * Factory implementation for Kysely ORM, providing test data creation utilities.\n * Extends the base Factory class with Kysely-specific database operations.\n *\n * @template DB - The database schema type\n * @template Builders - Record of builder functions for creating entities\n * @template Seeds - Record of seed functions for complex test scenarios\n *\n * @example\n * ```typescript\n * // Define your database schema\n * interface Database {\n * users: UsersTable;\n * posts: PostsTable;\n * }\n *\n * // Create builders\n * const builders = {\n * user: KyselyFactory.createBuilder<Database, 'users'>('users', ({ attrs, faker }) => ({\n * id: faker.string.uuid(),\n * name: faker.person.fullName(),\n * email: faker.internet.email(),\n * ...attrs\n * })),\n * post: KyselyFactory.createBuilder<Database, 'posts'>('posts', ({ attrs }) => ({\n * title: 'Test Post',\n * content: 'Test content',\n * ...attrs\n * }))\n * };\n *\n * // Create factory instance\n * const factory = new KyselyFactory(builders, seeds, db);\n *\n * // Use in tests\n * const user = await factory.insert('user', { name: 'John Doe' });\n * ```\n */\nexport class KyselyFactory<\n\tDB,\n\tBuilders extends Record<string, any>,\n\tSeeds extends Record<string, any>,\n> extends Factory<Builders, Seeds> {\n\t/**\n\t * Creates a typed seed function with proper type inference.\n\t * Inherits from the base Factory class implementation.\n\t *\n\t * @template Seed - The seed function type\n\t * @param seedFn - The seed function to wrap (receives { attrs, factory, db } object)\n\t * @returns The same seed function with proper typing\n\t *\n\t * @example\n\t * ```typescript\n\t * const seeds = {\n\t * userWithPosts: KyselyFactory.createSeed(\n\t * async ({ attrs, factory }) => {\n\t * const user = await factory.insert('user', attrs);\n\t * await factory.insertMany(3, 'post', { userId: user.id });\n\t * return user;\n\t * },\n\t * ),\n\t * };\n\t * ```\n\t */\n\tstatic override createSeed<Seed extends FactorySeed>(seedFn: Seed): Seed {\n\t\treturn Factory.createSeed(seedFn);\n\t}\n\n\t/**\n\t * Creates a new KyselyFactory instance.\n\t *\n\t * @param builders - Record of builder functions for creating individual entities\n\t * @param seeds - Record of seed functions for creating complex test scenarios\n\t * @param db - Kysely database instance or controlled transaction\n\t */\n\tconstructor(\n\t\tprivate builders: Builders,\n\t\tprivate seeds: Seeds,\n\t\tprivate db: Kysely<DB> | ControlledTransaction<DB, []>,\n\t) {\n\t\tsuper();\n\t}\n\n\t/**\n\t * Creates a typed builder function for a specific database table.\n\t * This is a utility method that helps create builders with proper type inference for Kysely.\n\t *\n\t * @template DB - The database schema type\n\t * @template TableName - The name of the table (must be a key of DB)\n\t * @template Attrs - The attributes type for the builder (defaults to Partial<Insertable>)\n\t * @template Factory - The factory instance type\n\t * @template Result - The result type (defaults to Selectable of the table)\n\t *\n\t * @param table - The name of the database table\n\t * @param defaults - Optional function to provide default values (receives destructured context)\n\t * @param autoInsert - Whether to automatically insert the record (default: true)\n\t * @returns A builder function that creates and optionally inserts records\n\t *\n\t * @example\n\t * ```typescript\n\t * // Create a simple builder with defaults - destructure only what you need\n\t * const userBuilder = KyselyFactory.createBuilder<DB, 'users'>('users',\n\t * ({ attrs, faker }) => ({\n\t * id: faker.string.uuid(),\n\t * name: faker.person.fullName(),\n\t * email: faker.internet.email(),\n\t * createdAt: new Date(),\n\t * ...attrs\n\t * })\n\t * );\n\t *\n\t * // Only need faker? Just destructure that\n\t * const leaveTypeBuilder = KyselyFactory.createBuilder<DB, 'leaveTypes'>('leaveTypes',\n\t * ({ faker }) => ({\n\t * name: faker.helpers.arrayElement(['Annual', 'Sick', 'Maternity']),\n\t * code: faker.string.alpha({ length: 3, casing: 'upper' }),\n\t * })\n\t * );\n\t *\n\t * // Create a builder that doesn't auto-insert (useful for nested inserts)\n\t * const addressBuilder = KyselyFactory.createBuilder<DB, 'addresses'>('addresses',\n\t * ({ attrs }) => ({\n\t * street: '123 Main St',\n\t * city: 'Anytown',\n\t * ...attrs\n\t * }),\n\t * false // Don't auto-insert\n\t * );\n\t * ```\n\t */\n\tstatic createBuilder<\n\t\tDB,\n\t\tTableName extends keyof DB & string,\n\t\tAttrs extends Partial<Insertable<DB[TableName]>> = Partial<\n\t\t\tInsertable<DB[TableName]>\n\t\t>,\n\t\tFactory = any,\n\t\tResult = Selectable<DB[TableName]>,\n\t>(\n\t\ttable: TableName,\n\t\tdefaults?: (context: {\n\t\t\tattrs: Attrs;\n\t\t\tfactory: Factory;\n\t\t\tdb: Kysely<DB>;\n\t\t\tfaker: FakerFactory;\n\t\t}) =>\n\t\t\t| Partial<Insertable<DB[TableName]>>\n\t\t\t| Promise<Partial<Insertable<DB[TableName]>>>,\n\t\tautoInsert?: boolean,\n\t): (\n\t\tattrs: Attrs,\n\t\tfactory: Factory,\n\t\tdb: Kysely<DB>,\n\t\tfaker: FakerFactory,\n\t) => Promise<Result> {\n\t\treturn async (\n\t\t\tattrs: Attrs,\n\t\t\tfactory: Factory,\n\t\t\tdb: Kysely<DB>,\n\t\t\tfakerInstance: FakerFactory,\n\t\t) => {\n\t\t\t// Start with attributes\n\t\t\tlet data: Partial<Insertable<DB[TableName]>> = { ...attrs };\n\n\t\t\t// Apply defaults\n\t\t\tif (defaults) {\n\t\t\t\tconst defaultValues = await defaults({\n\t\t\t\t\tattrs,\n\t\t\t\t\tfactory,\n\t\t\t\t\tdb,\n\t\t\t\t\tfaker: fakerInstance,\n\t\t\t\t});\n\t\t\t\tdata = { ...defaultValues, ...data };\n\t\t\t}\n\n\t\t\t// Handle insertion based on autoInsert flag\n\t\t\tif (autoInsert !== false) {\n\t\t\t\t// Auto insert is enabled by default\n\t\t\t\tconst result = await db\n\t\t\t\t\t.insertInto(table)\n\t\t\t\t\t.values(data as Insertable<DB[TableName]>)\n\t\t\t\t\t.returningAll()\n\t\t\t\t\t.executeTakeFirst();\n\n\t\t\t\tif (!result) {\n\t\t\t\t\tthrow new Error(`Failed to insert into ${table}`);\n\t\t\t\t}\n\n\t\t\t\treturn result as Result;\n\t\t\t} else {\n\t\t\t\t// Return object for factory to handle insertion\n\t\t\t\treturn { table, data } as any;\n\t\t\t}\n\t\t};\n\t}\n\n\t/**\n\t * Inserts a single record into the database using the specified builder.\n\t * The builder function is responsible for generating the record data with defaults\n\t * and the factory handles the actual database insertion.\n\t *\n\t * @template K - The builder name (must be a key of Builders)\n\t * @param builderName - The name of the builder to use\n\t * @param attrs - Optional attributes to override builder defaults\n\t * @returns A promise resolving to the inserted record\n\t * @throws Error if the specified builder doesn't exist\n\t *\n\t * @example\n\t * ```typescript\n\t * // Insert with defaults\n\t * const user = await factory.insert('user');\n\t *\n\t * // Insert with overrides\n\t * const adminUser = await factory.insert('user', {\n\t * email: 'admin@example.com',\n\t * role: 'admin'\n\t * });\n\t *\n\t * // Use the inserted record\n\t * const post = await factory.insert('post', {\n\t * userId: user.id,\n\t * title: 'My First Post'\n\t * });\n\t * ```\n\t */\n\tasync insert<K extends keyof Builders>(\n\t\tbuilderName: K,\n\t\tattrs?: Parameters<Builders[K]>[0],\n\t): Promise<Awaited<ReturnType<Builders[K]>>> {\n\t\tif (!(builderName in this.builders)) {\n\t\t\tthrow new Error(\n\t\t\t\t`Factory \"${\n\t\t\t\t\tbuilderName as string\n\t\t\t\t}\" does not exist. Make sure it is correct and registered in src/test/setup.ts`,\n\t\t\t);\n\t\t}\n\n\t\tconst result = await this.builders[builderName](\n\t\t\tattrs || {},\n\t\t\tthis,\n\t\t\tthis.db,\n\t\t\tfaker,\n\t\t);\n\n\t\t// For Kysely, we expect the builder to return an object with table and data properties\n\t\t// or to handle the insertion itself and return the inserted record\n\t\tif (\n\t\t\tresult &&\n\t\t\ttypeof result === 'object' &&\n\t\t\t'table' in result &&\n\t\t\t'data' in result\n\t\t) {\n\t\t\t// If the builder returns {table: string, data: object}, we insert it\n\t\t\tconst inserted = await this.db\n\t\t\t\t.insertInto(result.table)\n\t\t\t\t.values(result.data)\n\t\t\t\t.returningAll()\n\t\t\t\t.executeTakeFirst();\n\n\t\t\treturn inserted as any;\n\t\t}\n\n\t\t// Otherwise, assume the builder handled the insertion itself\n\t\treturn result;\n\t}\n\n\t/**\n\t * Inserts multiple records into the database using the specified builder.\n\t * Supports both static attributes and dynamic attribute generation via a function.\n\t *\n\t * @template K - The builder name (must be a key of Builders)\n\t * @param count - The number of records to insert\n\t * @param builderName - The name of the builder to use\n\t * @param attrs - Static attributes or a function that generates attributes for each record\n\t * @returns A promise resolving to an array of inserted records\n\t * @throws Error if the specified builder doesn't exist\n\t *\n\t * @example\n\t * ```typescript\n\t * // Insert multiple with same attributes\n\t * const users = await factory.insertMany(5, 'user', { role: 'member' });\n\t *\n\t * // Insert multiple with dynamic attributes\n\t * const posts = await factory.insertMany(10, 'post', (idx, faker) => ({\n\t * title: `Post ${idx + 1}`,\n\t * content: faker.lorem.paragraph(),\n\t * publishedAt: faker.date.past()\n\t * }));\n\t *\n\t * // Create users with sequential emails\n\t * const admins = await factory.insertMany(3, 'user', (idx) => ({\n\t * email: `admin${idx + 1}@example.com`,\n\t * role: 'admin'\n\t * }));\n\t * ```\n\t */\n\t// Method overloads for better type inference\n\tasync insertMany<K extends keyof Builders>(\n\t\tcount: number,\n\t\tbuilderName: K,\n\t\tattrs?: Parameters<Builders[K]>[0],\n\t): Promise<Awaited<ReturnType<Builders[K]>>[]>;\n\tasync insertMany<K extends keyof Builders>(\n\t\tcount: number,\n\t\tbuilderName: K,\n\t\tattrs: (\n\t\t\tidx: number,\n\t\t\tfaker: FakerFactory,\n\t\t) => Parameters<Builders[K]>[0] | Promise<Parameters<Builders[K]>[0]>,\n\t): Promise<Awaited<ReturnType<Builders[K]>>[]>;\n\tasync insertMany<K extends keyof Builders>(\n\t\tcount: number,\n\t\tbuilderName: K,\n\t\tattrs?: any,\n\t): Promise<Awaited<ReturnType<Builders[K]>>[]> {\n\t\tif (!(builderName in this.builders)) {\n\t\t\tthrow new Error(\n\t\t\t\t`Builder \"${\n\t\t\t\t\tbuilderName as string\n\t\t\t\t}\" is not registered in this factory. Make sure it is correct and registered in src/test/setup.ts`,\n\t\t\t);\n\t\t}\n\n\t\tconst promises: Promise<any>[] = [];\n\n\t\tfor (let i = 0; i < count; i++) {\n\t\t\tconst newAttrs =\n\t\t\t\ttypeof attrs === 'function' ? await attrs(i, faker) : attrs;\n\t\t\tpromises.push(this.insert(builderName, newAttrs));\n\t\t}\n\n\t\treturn Promise.all(promises);\n\t}\n\n\t/**\n\t * Executes a seed function to create complex test scenarios with multiple related records.\n\t * Seeds are useful for setting up complete test environments with realistic data relationships.\n\t *\n\t * @template K - The seed name (must be a key of Seeds)\n\t * @param seedName - The name of the seed to execute\n\t * @param attrs - Optional configuration attributes for the seed\n\t * @returns The result of the seed function (typically the primary record created)\n\t * @throws Error if the specified seed doesn't exist\n\t *\n\t * @example\n\t * ```typescript\n\t * // Execute a simple seed\n\t * const user = await factory.seed('userWithProfile');\n\t *\n\t * // Execute a seed with configuration\n\t * const author = await factory.seed('authorWithBooks', {\n\t * bookCount: 5,\n\t * includeReviews: true\n\t * });\n\t *\n\t * // Use seed result in tests\n\t * const company = await factory.seed('companyWithDepartments', {\n\t * departmentCount: 3,\n\t * employeesPerDepartment: 10\n\t * });\n\t * expect(company.departments).toHaveLength(3);\n\t * ```\n\t */\n\tseed<K extends keyof Seeds>(\n\t\tseedName: K,\n\t\tattrs?: ExtractSeedAttrs<Seeds[K]>,\n\t): ReturnType<Seeds[K]> {\n\t\tif (!(seedName in this.seeds)) {\n\t\t\tthrow new Error(\n\t\t\t\t`Seed \"${\n\t\t\t\t\tseedName as string\n\t\t\t\t}\" is not registered in this factory. Make sure it is correct and registered in src/test/setup.ts`,\n\t\t\t);\n\t\t}\n\n\t\treturn this.seeds[seedName]({\n\t\t\tattrs: attrs || {},\n\t\t\tfactory: this,\n\t\t\tdb: this.db,\n\t\t});\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CA,IAAa,gBAAb,cAIUA,gBAAAA,QAAyB;CAkCzB;CACA;CACA;;;;;;;;;;;;;;;;;;;;;;CAdT,OAAgB,WAAqC,QAAoB;EACxE,OAAOA,gBAAAA,QAAQ,WAAW,MAAM;CACjC;;;;;;;;CASA,YACC,UACA,OACA,IACC;EACD,MAAM;EAJE,KAAA,WAAA;EACA,KAAA,QAAA;EACA,KAAA,KAAA;CAGT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiDA,OAAO,cASN,OACA,UAQA,YAMoB;EACpB,OAAO,OACN,OACA,SACA,IACA,kBACI;GAEJ,IAAI,OAA2C,EAAE,GAAG,MAAM;GAG1D,IAAI,UAOH,OAAO;IAAE,GAAG,MANgB,SAAS;KACpC;KACA;KACA;KACA,OAAO;IACR,CAAC;IAC0B,GAAG;GAAK;GAIpC,IAAI,eAAe,OAAO;IAEzB,MAAM,SAAS,MAAM,GACnB,WAAW,KAAK,CAAC,CACjB,OAAO,IAAiC,CAAC,CACzC,aAAa,CAAC,CACd,iBAAiB;IAEnB,IAAI,CAAC,QACJ,MAAM,IAAI,MAAM,yBAAyB,OAAO;IAGjD,OAAO;GACR,OAEC,OAAO;IAAE;IAAO;GAAK;EAEvB;CACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+BA,MAAM,OACL,aACA,OAC4C;EAC5C,IAAI,EAAE,eAAe,KAAK,WACzB,MAAM,IAAI,MACT,YACC,YACA,8EACF;EAGD,MAAM,SAAS,MAAM,KAAK,SAAS,YAAY,CAC9C,SAAS,CAAC,GACV,MACA,KAAK,IACLC,cAAAA,KACD;EAIA,IACC,UACA,OAAO,WAAW,YAClB,WAAW,UACX,UAAU,QASV,OAAO,MANgB,KAAK,GAC1B,WAAW,OAAO,KAAK,CAAC,CACxB,OAAO,OAAO,IAAI,CAAC,CACnB,aAAa,CAAC,CACd,iBAAiB;EAMpB,OAAO;CACR;CA8CA,MAAM,WACL,OACA,aACA,OAC8C;EAC9C,IAAI,EAAE,eAAe,KAAK,WACzB,MAAM,IAAI,MACT,YACC,YACA,iGACF;EAGD,MAAM,WAA2B,CAAC;EAElC,KAAK,IAAI,IAAI,GAAG,IAAI,OAAO,KAAK;GAC/B,MAAM,WACL,OAAO,UAAU,aAAa,MAAM,MAAM,GAAGA,cAAAA,KAAK,IAAI;GACvD,SAAS,KAAK,KAAK,OAAO,aAAa,QAAQ,CAAC;EACjD;EAEA,OAAO,QAAQ,IAAI,QAAQ;CAC5B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+BA,KACC,UACA,OACuB;EACvB,IAAI,EAAE,YAAY,KAAK,QACtB,MAAM,IAAI,MACT,SACC,SACA,iGACF;EAGD,OAAO,KAAK,MAAM,SAAS,CAAC;GAC3B,OAAO,SAAS,CAAC;GACjB,SAAS;GACT,IAAI,KAAK;EACV,CAAC;CACF;AACD"}
|
package/dist/KyselyFactory.d.cts
CHANGED
|
@@ -1,4 +1,224 @@
|
|
|
1
|
-
import "./faker
|
|
2
|
-
import "./Factory
|
|
3
|
-
import {
|
|
4
|
-
|
|
1
|
+
import { FakerFactory } from "./faker.cjs";
|
|
2
|
+
import { ExtractSeedAttrs, FactorySeed } from "./Factory.cjs";
|
|
3
|
+
import { ControlledTransaction, Insertable, Kysely, Selectable } from "kysely";
|
|
4
|
+
//#region src/KyselyFactory.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Factory implementation for Kysely ORM, providing test data creation utilities.
|
|
7
|
+
* Extends the base Factory class with Kysely-specific database operations.
|
|
8
|
+
*
|
|
9
|
+
* @template DB - The database schema type
|
|
10
|
+
* @template Builders - Record of builder functions for creating entities
|
|
11
|
+
* @template Seeds - Record of seed functions for complex test scenarios
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // Define your database schema
|
|
16
|
+
* interface Database {
|
|
17
|
+
* users: UsersTable;
|
|
18
|
+
* posts: PostsTable;
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* // Create builders
|
|
22
|
+
* const builders = {
|
|
23
|
+
* user: KyselyFactory.createBuilder<Database, 'users'>('users', ({ attrs, faker }) => ({
|
|
24
|
+
* id: faker.string.uuid(),
|
|
25
|
+
* name: faker.person.fullName(),
|
|
26
|
+
* email: faker.internet.email(),
|
|
27
|
+
* ...attrs
|
|
28
|
+
* })),
|
|
29
|
+
* post: KyselyFactory.createBuilder<Database, 'posts'>('posts', ({ attrs }) => ({
|
|
30
|
+
* title: 'Test Post',
|
|
31
|
+
* content: 'Test content',
|
|
32
|
+
* ...attrs
|
|
33
|
+
* }))
|
|
34
|
+
* };
|
|
35
|
+
*
|
|
36
|
+
* // Create factory instance
|
|
37
|
+
* const factory = new KyselyFactory(builders, seeds, db);
|
|
38
|
+
*
|
|
39
|
+
* // Use in tests
|
|
40
|
+
* const user = await factory.insert('user', { name: 'John Doe' });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare class KyselyFactory<DB, Builders extends Record<string, any>, Seeds extends Record<string, any>> extends Factory<Builders, Seeds> {
|
|
44
|
+
private builders;
|
|
45
|
+
private seeds;
|
|
46
|
+
private db;
|
|
47
|
+
/**
|
|
48
|
+
* Creates a typed seed function with proper type inference.
|
|
49
|
+
* Inherits from the base Factory class implementation.
|
|
50
|
+
*
|
|
51
|
+
* @template Seed - The seed function type
|
|
52
|
+
* @param seedFn - The seed function to wrap (receives { attrs, factory, db } object)
|
|
53
|
+
* @returns The same seed function with proper typing
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const seeds = {
|
|
58
|
+
* userWithPosts: KyselyFactory.createSeed(
|
|
59
|
+
* async ({ attrs, factory }) => {
|
|
60
|
+
* const user = await factory.insert('user', attrs);
|
|
61
|
+
* await factory.insertMany(3, 'post', { userId: user.id });
|
|
62
|
+
* return user;
|
|
63
|
+
* },
|
|
64
|
+
* ),
|
|
65
|
+
* };
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
static createSeed<Seed extends FactorySeed>(seedFn: Seed): Seed;
|
|
69
|
+
/**
|
|
70
|
+
* Creates a new KyselyFactory instance.
|
|
71
|
+
*
|
|
72
|
+
* @param builders - Record of builder functions for creating individual entities
|
|
73
|
+
* @param seeds - Record of seed functions for creating complex test scenarios
|
|
74
|
+
* @param db - Kysely database instance or controlled transaction
|
|
75
|
+
*/
|
|
76
|
+
constructor(builders: Builders, seeds: Seeds, db: Kysely<DB> | ControlledTransaction<DB, []>);
|
|
77
|
+
/**
|
|
78
|
+
* Creates a typed builder function for a specific database table.
|
|
79
|
+
* This is a utility method that helps create builders with proper type inference for Kysely.
|
|
80
|
+
*
|
|
81
|
+
* @template DB - The database schema type
|
|
82
|
+
* @template TableName - The name of the table (must be a key of DB)
|
|
83
|
+
* @template Attrs - The attributes type for the builder (defaults to Partial<Insertable>)
|
|
84
|
+
* @template Factory - The factory instance type
|
|
85
|
+
* @template Result - The result type (defaults to Selectable of the table)
|
|
86
|
+
*
|
|
87
|
+
* @param table - The name of the database table
|
|
88
|
+
* @param defaults - Optional function to provide default values (receives destructured context)
|
|
89
|
+
* @param autoInsert - Whether to automatically insert the record (default: true)
|
|
90
|
+
* @returns A builder function that creates and optionally inserts records
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* // Create a simple builder with defaults - destructure only what you need
|
|
95
|
+
* const userBuilder = KyselyFactory.createBuilder<DB, 'users'>('users',
|
|
96
|
+
* ({ attrs, faker }) => ({
|
|
97
|
+
* id: faker.string.uuid(),
|
|
98
|
+
* name: faker.person.fullName(),
|
|
99
|
+
* email: faker.internet.email(),
|
|
100
|
+
* createdAt: new Date(),
|
|
101
|
+
* ...attrs
|
|
102
|
+
* })
|
|
103
|
+
* );
|
|
104
|
+
*
|
|
105
|
+
* // Only need faker? Just destructure that
|
|
106
|
+
* const leaveTypeBuilder = KyselyFactory.createBuilder<DB, 'leaveTypes'>('leaveTypes',
|
|
107
|
+
* ({ faker }) => ({
|
|
108
|
+
* name: faker.helpers.arrayElement(['Annual', 'Sick', 'Maternity']),
|
|
109
|
+
* code: faker.string.alpha({ length: 3, casing: 'upper' }),
|
|
110
|
+
* })
|
|
111
|
+
* );
|
|
112
|
+
*
|
|
113
|
+
* // Create a builder that doesn't auto-insert (useful for nested inserts)
|
|
114
|
+
* const addressBuilder = KyselyFactory.createBuilder<DB, 'addresses'>('addresses',
|
|
115
|
+
* ({ attrs }) => ({
|
|
116
|
+
* street: '123 Main St',
|
|
117
|
+
* city: 'Anytown',
|
|
118
|
+
* ...attrs
|
|
119
|
+
* }),
|
|
120
|
+
* false // Don't auto-insert
|
|
121
|
+
* );
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
static createBuilder<DB, TableName extends keyof DB & string, Attrs extends Partial<Insertable<DB[TableName]>> = Partial<Insertable<DB[TableName]>>, Factory = any, Result = Selectable<DB[TableName]>>(table: TableName, defaults?: (context: {
|
|
125
|
+
attrs: Attrs;
|
|
126
|
+
factory: Factory;
|
|
127
|
+
db: Kysely<DB>;
|
|
128
|
+
faker: FakerFactory;
|
|
129
|
+
}) => Partial<Insertable<DB[TableName]>> | Promise<Partial<Insertable<DB[TableName]>>>, autoInsert?: boolean): (attrs: Attrs, factory: Factory, db: Kysely<DB>, faker: FakerFactory) => Promise<Result>;
|
|
130
|
+
/**
|
|
131
|
+
* Inserts a single record into the database using the specified builder.
|
|
132
|
+
* The builder function is responsible for generating the record data with defaults
|
|
133
|
+
* and the factory handles the actual database insertion.
|
|
134
|
+
*
|
|
135
|
+
* @template K - The builder name (must be a key of Builders)
|
|
136
|
+
* @param builderName - The name of the builder to use
|
|
137
|
+
* @param attrs - Optional attributes to override builder defaults
|
|
138
|
+
* @returns A promise resolving to the inserted record
|
|
139
|
+
* @throws Error if the specified builder doesn't exist
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```typescript
|
|
143
|
+
* // Insert with defaults
|
|
144
|
+
* const user = await factory.insert('user');
|
|
145
|
+
*
|
|
146
|
+
* // Insert with overrides
|
|
147
|
+
* const adminUser = await factory.insert('user', {
|
|
148
|
+
* email: 'admin@example.com',
|
|
149
|
+
* role: 'admin'
|
|
150
|
+
* });
|
|
151
|
+
*
|
|
152
|
+
* // Use the inserted record
|
|
153
|
+
* const post = await factory.insert('post', {
|
|
154
|
+
* userId: user.id,
|
|
155
|
+
* title: 'My First Post'
|
|
156
|
+
* });
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
insert<K extends keyof Builders>(builderName: K, attrs?: Parameters<Builders[K]>[0]): Promise<Awaited<ReturnType<Builders[K]>>>;
|
|
160
|
+
/**
|
|
161
|
+
* Inserts multiple records into the database using the specified builder.
|
|
162
|
+
* Supports both static attributes and dynamic attribute generation via a function.
|
|
163
|
+
*
|
|
164
|
+
* @template K - The builder name (must be a key of Builders)
|
|
165
|
+
* @param count - The number of records to insert
|
|
166
|
+
* @param builderName - The name of the builder to use
|
|
167
|
+
* @param attrs - Static attributes or a function that generates attributes for each record
|
|
168
|
+
* @returns A promise resolving to an array of inserted records
|
|
169
|
+
* @throws Error if the specified builder doesn't exist
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```typescript
|
|
173
|
+
* // Insert multiple with same attributes
|
|
174
|
+
* const users = await factory.insertMany(5, 'user', { role: 'member' });
|
|
175
|
+
*
|
|
176
|
+
* // Insert multiple with dynamic attributes
|
|
177
|
+
* const posts = await factory.insertMany(10, 'post', (idx, faker) => ({
|
|
178
|
+
* title: `Post ${idx + 1}`,
|
|
179
|
+
* content: faker.lorem.paragraph(),
|
|
180
|
+
* publishedAt: faker.date.past()
|
|
181
|
+
* }));
|
|
182
|
+
*
|
|
183
|
+
* // Create users with sequential emails
|
|
184
|
+
* const admins = await factory.insertMany(3, 'user', (idx) => ({
|
|
185
|
+
* email: `admin${idx + 1}@example.com`,
|
|
186
|
+
* role: 'admin'
|
|
187
|
+
* }));
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
insertMany<K extends keyof Builders>(count: number, builderName: K, attrs?: Parameters<Builders[K]>[0]): Promise<Awaited<ReturnType<Builders[K]>>[]>;
|
|
191
|
+
insertMany<K extends keyof Builders>(count: number, builderName: K, attrs: (idx: number, faker: FakerFactory) => Parameters<Builders[K]>[0] | Promise<Parameters<Builders[K]>[0]>): Promise<Awaited<ReturnType<Builders[K]>>[]>;
|
|
192
|
+
/**
|
|
193
|
+
* Executes a seed function to create complex test scenarios with multiple related records.
|
|
194
|
+
* Seeds are useful for setting up complete test environments with realistic data relationships.
|
|
195
|
+
*
|
|
196
|
+
* @template K - The seed name (must be a key of Seeds)
|
|
197
|
+
* @param seedName - The name of the seed to execute
|
|
198
|
+
* @param attrs - Optional configuration attributes for the seed
|
|
199
|
+
* @returns The result of the seed function (typically the primary record created)
|
|
200
|
+
* @throws Error if the specified seed doesn't exist
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```typescript
|
|
204
|
+
* // Execute a simple seed
|
|
205
|
+
* const user = await factory.seed('userWithProfile');
|
|
206
|
+
*
|
|
207
|
+
* // Execute a seed with configuration
|
|
208
|
+
* const author = await factory.seed('authorWithBooks', {
|
|
209
|
+
* bookCount: 5,
|
|
210
|
+
* includeReviews: true
|
|
211
|
+
* });
|
|
212
|
+
*
|
|
213
|
+
* // Use seed result in tests
|
|
214
|
+
* const company = await factory.seed('companyWithDepartments', {
|
|
215
|
+
* departmentCount: 3,
|
|
216
|
+
* employeesPerDepartment: 10
|
|
217
|
+
* });
|
|
218
|
+
* expect(company.departments).toHaveLength(3);
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
seed<K extends keyof Seeds>(seedName: K, attrs?: ExtractSeedAttrs<Seeds[K]>): ReturnType<Seeds[K]>;
|
|
222
|
+
}
|
|
223
|
+
//#endregion
|
|
224
|
+
//# sourceMappingURL=KyselyFactory.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"KyselyFactory.d.cts","names":[],"sources":["../src/KyselyFactory.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBA+Ca,cACZ,IACA,iBAAiB,qBACjB,cAAc,6BACL,QAAQ,UAAU;UAkClB;UACA;UACA;;;;;;;;;;;;;;;;;;;;;;SAdO,WAAW,aAAa,aAAa,QAAQ,OAAO;;;;;;;;EAWpE,YACS,UAAU,UACV,OAAO,OACP,IAAI,OAAO,MAAM,sBAAsB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SAoDzC,cACN,IACA,wBAAwB,aACxB,cAAc,QAAQ,WAAW,GAAG,eAAe,QAClD,WAAW,GAAG,cAEf,eACA,SAAS,WAAW,GAAG,aAEvB,OAAO,WACP,YAAY;IACX,OAAO;IACP,SAAS;IACT,IAAI,OAAO;IACX,OAAO;QAEL,QAAQ,WAAW,GAAG,eACtB,QAAQ,QAAQ,WAAW,GAAG,eACjC,wBAEA,OAAO,OACP,SAAS,SACT,IAAI,OAAO,KACX,OAAO,iBACH,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAuEP,OAAO,gBAAgB,UAC5B,aAAa,GACb,QAAQ,WAAW,SAAS,SAC1B,QAAQ,QAAQ,WAAW,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAqEjC,WAAW,gBAAgB,UAChC,eACA,aAAa,GACb,QAAQ,WAAW,SAAS,SAC1B,QAAQ,QAAQ,WAAW,SAAS;EACjC,WAAW,gBAAgB,UAChC,eACA,aAAa,GACb,QACC,aACA,OAAO,iBACH,WAAW,SAAS,SAAS,QAAQ,WAAW,SAAS,UAC5D,QAAQ,QAAQ,WAAW,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAsDvC,KAAK,gBAAgB,OACpB,UAAU,GACV,QAAQ,iBAAiB,MAAM,MAC7B,WAAW,MAAM"}
|