@geekmidas/testkit 10.0.0-alpha.3 → 10.0.0-alpha.31
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
|
@@ -1,5 +1,266 @@
|
|
|
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/ObjectionFactory.ts
|
|
5
|
+
/**
|
|
6
|
+
* Factory implementation for Objection.js ORM, providing test data creation utilities.
|
|
7
|
+
* Extends the base Factory class with Objection.js-specific database operations.
|
|
8
|
+
*
|
|
9
|
+
* @template Builders - Record of builder functions for creating entities
|
|
10
|
+
* @template Seeds - Record of seed functions for complex test scenarios
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Define your models with Objection.js
|
|
15
|
+
* class User extends Model {
|
|
16
|
+
* static tableName = 'users';
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* // Create builders
|
|
20
|
+
* const builders = {
|
|
21
|
+
* user: ObjectionFactory.createBuilder(User, ({ attrs, faker }) => ({
|
|
22
|
+
* id: faker.string.uuid(),
|
|
23
|
+
* name: faker.person.fullName(),
|
|
24
|
+
* email: faker.internet.email(),
|
|
25
|
+
* ...attrs
|
|
26
|
+
* })),
|
|
27
|
+
* post: ObjectionFactory.createBuilder(Post, ({ attrs }) => ({
|
|
28
|
+
* title: 'Test Post',
|
|
29
|
+
* content: 'Test content',
|
|
30
|
+
* ...attrs
|
|
31
|
+
* })),
|
|
32
|
+
* };
|
|
33
|
+
*
|
|
34
|
+
* // Create factory instance
|
|
35
|
+
* const factory = new ObjectionFactory(builders, seeds, knex);
|
|
36
|
+
*
|
|
37
|
+
* // Use in tests
|
|
38
|
+
* const user = await factory.insert('user', { name: 'John Doe' });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
var ObjectionFactory = class extends require_Factory.Factory {
|
|
42
|
+
builders;
|
|
43
|
+
seeds;
|
|
44
|
+
db;
|
|
45
|
+
/**
|
|
46
|
+
* Creates a typed seed function with proper type inference.
|
|
47
|
+
* Inherits from the base Factory class implementation.
|
|
48
|
+
*
|
|
49
|
+
* @template Seed - The seed function type
|
|
50
|
+
* @param seedFn - The seed function to wrap (receives { attrs, factory, db } object)
|
|
51
|
+
* @returns The same seed function with proper typing
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* const seeds = {
|
|
56
|
+
* userWithPosts: ObjectionFactory.createSeed(
|
|
57
|
+
* async ({ attrs, factory }) => {
|
|
58
|
+
* const user = await factory.insert('user', attrs);
|
|
59
|
+
* await factory.insertMany(3, 'post', { userId: user.id });
|
|
60
|
+
* return user;
|
|
61
|
+
* },
|
|
62
|
+
* ),
|
|
63
|
+
* };
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
static createSeed(seedFn) {
|
|
67
|
+
return require_Factory.Factory.createSeed(seedFn);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Creates a typed builder function for Objection.js models.
|
|
71
|
+
* This is a utility method that helps create builders with proper type inference.
|
|
72
|
+
*
|
|
73
|
+
* @template TModel - The Objection.js Model class type
|
|
74
|
+
* @template Attrs - The attributes type for the builder (defaults to Partial of model)
|
|
75
|
+
* @template Factory - The factory instance type
|
|
76
|
+
* @template Result - The result type (defaults to the model instance)
|
|
77
|
+
*
|
|
78
|
+
* @param ModelClass - The Objection.js Model class
|
|
79
|
+
* @param defaults - Optional function to provide default values (receives destructured context)
|
|
80
|
+
* @param autoInsert - Whether to automatically insert the record (default: true)
|
|
81
|
+
* @returns A builder function that creates and optionally inserts records
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* // Create a simple builder with defaults - destructure only what you need
|
|
86
|
+
* const userBuilder = ObjectionFactory.createBuilder(User,
|
|
87
|
+
* ({ attrs, faker }) => ({
|
|
88
|
+
* id: faker.string.uuid(),
|
|
89
|
+
* name: faker.person.fullName(),
|
|
90
|
+
* email: faker.internet.email(),
|
|
91
|
+
* createdAt: new Date(),
|
|
92
|
+
* ...attrs
|
|
93
|
+
* })
|
|
94
|
+
* );
|
|
95
|
+
*
|
|
96
|
+
* // Only need faker? Just destructure that
|
|
97
|
+
* const leaveTypeBuilder = ObjectionFactory.createBuilder(LeaveType,
|
|
98
|
+
* ({ faker }) => ({
|
|
99
|
+
* name: faker.helpers.arrayElement(['Annual', 'Sick', 'Maternity']),
|
|
100
|
+
* code: faker.string.alpha({ length: 3, casing: 'upper' }),
|
|
101
|
+
* })
|
|
102
|
+
* );
|
|
103
|
+
*
|
|
104
|
+
* // Create a builder that doesn't auto-insert (useful for nested inserts)
|
|
105
|
+
* const addressBuilder = ObjectionFactory.createBuilder(Address,
|
|
106
|
+
* ({ attrs }) => ({
|
|
107
|
+
* street: '123 Main St',
|
|
108
|
+
* city: 'Anytown',
|
|
109
|
+
* ...attrs
|
|
110
|
+
* }),
|
|
111
|
+
* false // Don't auto-insert
|
|
112
|
+
* );
|
|
113
|
+
*
|
|
114
|
+
* // Use with relations
|
|
115
|
+
* const postBuilder = ObjectionFactory.createBuilder(Post,
|
|
116
|
+
* async ({ attrs, factory, faker }) => ({
|
|
117
|
+
* title: faker.lorem.sentence(),
|
|
118
|
+
* content: faker.lorem.paragraphs(),
|
|
119
|
+
* authorId: attrs.authorId || (await factory.insert('user')).id,
|
|
120
|
+
* ...attrs
|
|
121
|
+
* })
|
|
122
|
+
* );
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
static createBuilder(ModelClass, defaults, autoInsert) {
|
|
126
|
+
return async (attrs, factory, db, fakerInstance) => {
|
|
127
|
+
let data = { ...attrs };
|
|
128
|
+
if (defaults) data = {
|
|
129
|
+
...await defaults({
|
|
130
|
+
attrs,
|
|
131
|
+
factory,
|
|
132
|
+
db,
|
|
133
|
+
faker: fakerInstance
|
|
134
|
+
}),
|
|
135
|
+
...data
|
|
136
|
+
};
|
|
137
|
+
const model = ModelClass.fromJson(data);
|
|
138
|
+
if (autoInsert !== false) {
|
|
139
|
+
const insertData = Object.entries(model).reduce((acc, [key, value]) => {
|
|
140
|
+
if (value !== void 0 && key !== "id") acc[key] = value;
|
|
141
|
+
return acc;
|
|
142
|
+
}, {});
|
|
143
|
+
return await ModelClass.query(db).insert(insertData);
|
|
144
|
+
} else return model;
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Creates a new ObjectionFactory instance.
|
|
149
|
+
*
|
|
150
|
+
* @param builders - Record of builder functions for creating individual entities
|
|
151
|
+
* @param seeds - Record of seed functions for creating complex test scenarios
|
|
152
|
+
* @param db - Knex database connection instance
|
|
153
|
+
*/
|
|
154
|
+
constructor(builders, seeds, db) {
|
|
155
|
+
super();
|
|
156
|
+
this.builders = builders;
|
|
157
|
+
this.seeds = seeds;
|
|
158
|
+
this.db = db;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Inserts a single record into the database using the specified builder.
|
|
162
|
+
* Uses Objection.js's insertGraph method to handle nested relations.
|
|
163
|
+
*
|
|
164
|
+
* @template K - The builder name (must be a key of Builders)
|
|
165
|
+
* @param builderName - The name of the builder to use
|
|
166
|
+
* @param attrs - Optional attributes to override builder defaults
|
|
167
|
+
* @returns A promise resolving to the inserted record with all relations
|
|
168
|
+
* @throws Error if the specified builder doesn't exist
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```typescript
|
|
172
|
+
* // Insert with defaults
|
|
173
|
+
* const user = await factory.insert('user');
|
|
174
|
+
*
|
|
175
|
+
* // Insert with overrides
|
|
176
|
+
* const adminUser = await factory.insert('user', {
|
|
177
|
+
* email: 'admin@example.com',
|
|
178
|
+
* role: 'admin'
|
|
179
|
+
* });
|
|
180
|
+
*
|
|
181
|
+
* // Insert with nested relations
|
|
182
|
+
* const userWithProfile = await factory.insert('user', {
|
|
183
|
+
* name: 'John Doe',
|
|
184
|
+
* profile: {
|
|
185
|
+
* bio: 'Software Developer',
|
|
186
|
+
* avatar: 'avatar.jpg'
|
|
187
|
+
* }
|
|
188
|
+
* });
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
async insert(builderName, attrs) {
|
|
192
|
+
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`);
|
|
193
|
+
const result = await this.builders[builderName](attrs || {}, this, this.db, require_faker.faker);
|
|
194
|
+
if (result && typeof result.$query === "function") {
|
|
195
|
+
const insertData = Object.entries(result).reduce((acc, [key, value]) => {
|
|
196
|
+
if (value !== void 0 && key !== "id") acc[key] = value;
|
|
197
|
+
return acc;
|
|
198
|
+
}, {});
|
|
199
|
+
return await result.constructor.query(this.db).insert(insertData);
|
|
200
|
+
}
|
|
201
|
+
return result;
|
|
202
|
+
}
|
|
203
|
+
async insertMany(count, builderName, attrs) {
|
|
204
|
+
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`);
|
|
205
|
+
const records = [];
|
|
206
|
+
for (let i = 0; i < count; i++) {
|
|
207
|
+
const newAttrs = typeof attrs === "function" ? await attrs(i, require_faker.faker) : attrs;
|
|
208
|
+
records.push(this.builders[builderName](newAttrs, this, this.db, require_faker.faker).then((record) => {
|
|
209
|
+
if (record && typeof record.$query === "function") {
|
|
210
|
+
const insertData = Object.entries(record).reduce((acc, [key, value]) => {
|
|
211
|
+
if (value !== void 0 && key !== "id") acc[key] = value;
|
|
212
|
+
return acc;
|
|
213
|
+
}, {});
|
|
214
|
+
return record.constructor.query(this.db).insert(insertData);
|
|
215
|
+
}
|
|
216
|
+
return record;
|
|
217
|
+
}));
|
|
218
|
+
}
|
|
219
|
+
return Promise.all(records);
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Executes a seed function to create complex test scenarios with multiple related records.
|
|
223
|
+
* Seeds are useful for setting up complete test environments with realistic data relationships.
|
|
224
|
+
*
|
|
225
|
+
* @template K - The seed name (must be a key of Seeds)
|
|
226
|
+
* @param seedName - The name of the seed to execute
|
|
227
|
+
* @param attrs - Optional configuration attributes for the seed
|
|
228
|
+
* @returns The result of the seed function (typically the primary record created)
|
|
229
|
+
* @throws Error if the specified seed doesn't exist
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* ```typescript
|
|
233
|
+
* // Execute a simple seed
|
|
234
|
+
* const user = await factory.seed('userWithProfile');
|
|
235
|
+
*
|
|
236
|
+
* // Execute a seed with configuration
|
|
237
|
+
* const author = await factory.seed('authorWithBooks', {
|
|
238
|
+
* bookCount: 5,
|
|
239
|
+
* includeReviews: true
|
|
240
|
+
* });
|
|
241
|
+
*
|
|
242
|
+
* // Use seed result in tests with Objection.js relations
|
|
243
|
+
* const company = await factory.seed('companyWithDepartments', {
|
|
244
|
+
* departmentCount: 3,
|
|
245
|
+
* employeesPerDepartment: 10
|
|
246
|
+
* });
|
|
247
|
+
*
|
|
248
|
+
* // Access eager loaded relations
|
|
249
|
+
* const companyWithRelations = await Company.query()
|
|
250
|
+
* .findById(company.id)
|
|
251
|
+
* .withGraphFetched('[departments.employees]');
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
254
|
+
seed(seedName, attrs) {
|
|
255
|
+
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`);
|
|
256
|
+
return this.seeds[seedName]({
|
|
257
|
+
attrs: attrs || {},
|
|
258
|
+
factory: this,
|
|
259
|
+
db: this.db
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
//#endregion
|
|
264
|
+
exports.ObjectionFactory = ObjectionFactory;
|
|
4
265
|
|
|
5
|
-
|
|
266
|
+
//# sourceMappingURL=ObjectionFactory.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ObjectionFactory.cjs","names":["Factory","faker"],"sources":["../src/ObjectionFactory.ts"],"sourcesContent":["import type { Knex } from 'knex';\nimport type { Model } from 'objection';\nimport { type ExtractSeedAttrs, Factory, type FactorySeed } from './Factory';\nimport { type FakerFactory, faker } from './faker';\n\n/**\n * Factory implementation for Objection.js ORM, providing test data creation utilities.\n * Extends the base Factory class with Objection.js-specific database operations.\n *\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 models with Objection.js\n * class User extends Model {\n * static tableName = 'users';\n * }\n *\n * // Create builders\n * const builders = {\n * user: ObjectionFactory.createBuilder(User, ({ attrs, faker }) => ({\n * id: faker.string.uuid(),\n * name: faker.person.fullName(),\n * email: faker.internet.email(),\n * ...attrs\n * })),\n * post: ObjectionFactory.createBuilder(Post, ({ attrs }) => ({\n * title: 'Test Post',\n * content: 'Test content',\n * ...attrs\n * })),\n * };\n *\n * // Create factory instance\n * const factory = new ObjectionFactory(builders, seeds, knex);\n *\n * // Use in tests\n * const user = await factory.insert('user', { name: 'John Doe' });\n * ```\n */\nexport class ObjectionFactory<\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: ObjectionFactory.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 typed builder function for Objection.js models.\n\t * This is a utility method that helps create builders with proper type inference.\n\t *\n\t * @template TModel - The Objection.js Model class type\n\t * @template Attrs - The attributes type for the builder (defaults to Partial of model)\n\t * @template Factory - The factory instance type\n\t * @template Result - The result type (defaults to the model instance)\n\t *\n\t * @param ModelClass - The Objection.js Model class\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 = ObjectionFactory.createBuilder(User,\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 = ObjectionFactory.createBuilder(LeaveType,\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 = ObjectionFactory.createBuilder(Address,\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 * // Use with relations\n\t * const postBuilder = ObjectionFactory.createBuilder(Post,\n\t * async ({ attrs, factory, faker }) => ({\n\t * title: faker.lorem.sentence(),\n\t * content: faker.lorem.paragraphs(),\n\t * authorId: attrs.authorId || (await factory.insert('user')).id,\n\t * ...attrs\n\t * })\n\t * );\n\t * ```\n\t */\n\tstatic createBuilder<\n\t\tTModel extends typeof Model,\n\t\tAttrs extends Partial<InstanceType<TModel>> = Partial<InstanceType<TModel>>,\n\t\tFactory = any,\n\t\tResult = InstanceType<TModel>,\n\t>(\n\t\tModelClass: TModel,\n\t\tdefaults?: (context: {\n\t\t\tattrs: Attrs;\n\t\t\tfactory: Factory;\n\t\t\tdb: Knex;\n\t\t\tfaker: FakerFactory;\n\t\t}) =>\n\t\t\t| Partial<InstanceType<TModel>>\n\t\t\t| Promise<Partial<InstanceType<TModel>>>,\n\t\tautoInsert?: boolean,\n\t): (\n\t\tattrs: Attrs,\n\t\tfactory: Factory,\n\t\tdb: Knex,\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: Knex,\n\t\t\tfakerInstance: FakerFactory,\n\t\t) => {\n\t\t\t// Start with attributes\n\t\t\tlet data: Partial<InstanceType<TModel>> = { ...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// Create model instance\n\t\t\tconst model = ModelClass.fromJson(data) as InstanceType<TModel>;\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\t// Extract only defined values for insertion\n\t\t\t\tconst insertData = Object.entries(model).reduce((acc, [key, value]) => {\n\t\t\t\t\tif (value !== undefined && key !== 'id') {\n\t\t\t\t\t\tacc[key] = value;\n\t\t\t\t\t}\n\t\t\t\t\treturn acc;\n\t\t\t\t}, {} as any);\n\n\t\t\t\t// Use static query method to insert data directly\n\t\t\t\tconst result = await ModelClass.query(db).insert(insertData);\n\t\t\t\treturn result as Result;\n\t\t\t} else {\n\t\t\t\t// Return model for factory to handle insertion\n\t\t\t\treturn model as Result;\n\t\t\t}\n\t\t};\n\t}\n\n\t/**\n\t * Creates a new ObjectionFactory 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 - Knex database connection instance\n\t */\n\tconstructor(\n\t\tprivate builders: Builders,\n\t\tprivate seeds: Seeds,\n\t\tprivate db: Knex,\n\t) {\n\t\tsuper();\n\t}\n\n\t/**\n\t * Inserts a single record into the database using the specified builder.\n\t * Uses Objection.js's insertGraph method to handle nested relations.\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 with all relations\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 * // Insert with nested relations\n\t * const userWithProfile = await factory.insert('user', {\n\t * name: 'John Doe',\n\t * profile: {\n\t * bio: 'Software Developer',\n\t * avatar: 'avatar.jpg'\n\t * }\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// If the builder returns a model instance, insert it\n\t\tif (result && typeof result.$query === 'function') {\n\t\t\t// Extract data from model, excluding undefined values and id\n\t\t\tconst insertData = Object.entries(result).reduce((acc, [key, value]) => {\n\t\t\t\tif (value !== undefined && key !== 'id') {\n\t\t\t\t\tacc[key] = value;\n\t\t\t\t}\n\t\t\t\treturn acc;\n\t\t\t}, {} as any);\n\n\t\t\t// Use the model's constructor to get the query builder\n\t\t\treturn await result.constructor.query(this.db).insert(insertData);\n\t\t}\n\n\t\t// Otherwise, assume the builder handled insertion itself\n\t\treturn result;\n\t}\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 * @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) => ({\n\t * title: `Post ${idx + 1}`,\n\t * content: `Content for post ${idx + 1}`,\n\t * publishedAt: new Date()\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: (idx: number, faker: FakerFactory) => 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 records: any[] = [];\n\t\tfor (let i = 0; i < count; i++) {\n\t\t\tconst newAttrs =\n\t\t\t\ttypeof attrs === 'function' ? await (attrs as any)(i, faker) : attrs;\n\n\t\t\trecords.push(\n\t\t\t\tthis.builders[builderName](newAttrs, this, this.db, faker).then(\n\t\t\t\t\t(record: any) => {\n\t\t\t\t\t\t// If the builder returns a model instance, insert it\n\t\t\t\t\t\tif (record && typeof record.$query === 'function') {\n\t\t\t\t\t\t\t// Extract data from model, excluding undefined values and id\n\t\t\t\t\t\t\tconst insertData = Object.entries(record).reduce(\n\t\t\t\t\t\t\t\t(acc, [key, value]) => {\n\t\t\t\t\t\t\t\t\tif (value !== undefined && key !== 'id') {\n\t\t\t\t\t\t\t\t\t\tacc[key] = value;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\treturn acc;\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t{} as any,\n\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\t// Use the model's constructor to get the query builder\n\t\t\t\t\t\t\treturn record.constructor.query(this.db).insert(insertData);\n\t\t\t\t\t\t}\n\t\t\t\t\t\t// Otherwise, assume the builder handled insertion itself\n\t\t\t\t\t\treturn record;\n\t\t\t\t\t},\n\t\t\t\t),\n\t\t\t);\n\t\t}\n\n\t\treturn Promise.all(records);\n\t}\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 with Objection.js relations\n\t * const company = await factory.seed('companyWithDepartments', {\n\t * departmentCount: 3,\n\t * employeesPerDepartment: 10\n\t * });\n\t *\n\t * // Access eager loaded relations\n\t * const companyWithRelations = await Company.query()\n\t * .findById(company.id)\n\t * .withGraphFetched('[departments.employees]');\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCA,IAAa,mBAAb,cAGUA,gBAAAA,QAAyB;CA4JzB;CACA;CACA;;;;;;;;;;;;;;;;;;;;;;CAxIT,OAAgB,WAAqC,QAAoB;EACxE,OAAOA,gBAAAA,QAAQ,WAAW,MAAM;CACjC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA0DA,OAAO,cAMN,YACA,UAQA,YAMoB;EACpB,OAAO,OACN,OACA,SACA,IACA,kBACI;GAEJ,IAAI,OAAsC,EAAE,GAAG,MAAM;GAGrD,IAAI,UAOH,OAAO;IAAE,GAAG,MANgB,SAAS;KACpC;KACA;KACA;KACA,OAAO;IACR,CAAC;IAC0B,GAAG;GAAK;GAIpC,MAAM,QAAQ,WAAW,SAAS,IAAI;GAGtC,IAAI,eAAe,OAAO;IAGzB,MAAM,aAAa,OAAO,QAAQ,KAAK,CAAC,CAAC,QAAQ,KAAK,CAAC,KAAK,WAAW;KACtE,IAAI,UAAU,KAAA,KAAa,QAAQ,MAClC,IAAI,OAAO;KAEZ,OAAO;IACR,GAAG,CAAC,CAAQ;IAIZ,OAAO,MADc,WAAW,MAAM,EAAE,CAAC,CAAC,OAAO,UAAU;GAE5D,OAEC,OAAO;EAET;CACD;;;;;;;;CASA,YACC,UACA,OACA,IACC;EACD,MAAM;EAJE,KAAA,WAAA;EACA,KAAA,QAAA;EACA,KAAA,KAAA;CAGT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiCA,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;EAGA,IAAI,UAAU,OAAO,OAAO,WAAW,YAAY;GAElD,MAAM,aAAa,OAAO,QAAQ,MAAM,CAAC,CAAC,QAAQ,KAAK,CAAC,KAAK,WAAW;IACvE,IAAI,UAAU,KAAA,KAAa,QAAQ,MAClC,IAAI,OAAO;IAEZ,OAAO;GACR,GAAG,CAAC,CAAQ;GAGZ,OAAO,MAAM,OAAO,YAAY,MAAM,KAAK,EAAE,CAAC,CAAC,OAAO,UAAU;EACjE;EAGA,OAAO;CACR;CAyCA,MAAM,WACL,OACA,aACA,OAC8C;EAC9C,IAAI,EAAE,eAAe,KAAK,WACzB,MAAM,IAAI,MACT,YACC,YACA,iGACF;EAGD,MAAM,UAAiB,CAAC;EACxB,KAAK,IAAI,IAAI,GAAG,IAAI,OAAO,KAAK;GAC/B,MAAM,WACL,OAAO,UAAU,aAAa,MAAO,MAAc,GAAGA,cAAAA,KAAK,IAAI;GAEhE,QAAQ,KACP,KAAK,SAAS,YAAY,CAAC,UAAU,MAAM,KAAK,IAAIA,cAAAA,KAAK,CAAC,CAAC,MACzD,WAAgB;IAEhB,IAAI,UAAU,OAAO,OAAO,WAAW,YAAY;KAElD,MAAM,aAAa,OAAO,QAAQ,MAAM,CAAC,CAAC,QACxC,KAAK,CAAC,KAAK,WAAW;MACtB,IAAI,UAAU,KAAA,KAAa,QAAQ,MAClC,IAAI,OAAO;MAEZ,OAAO;KACR,GACA,CAAC,CACF;KAGA,OAAO,OAAO,YAAY,MAAM,KAAK,EAAE,CAAC,CAAC,OAAO,UAAU;IAC3D;IAEA,OAAO;GACR,CACD,CACD;EACD;EAEA,OAAO,QAAQ,IAAI,OAAO;CAC3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkCA,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"}
|
|
@@ -1,4 +1,237 @@
|
|
|
1
|
-
import "./faker
|
|
2
|
-
import "./Factory
|
|
3
|
-
import {
|
|
4
|
-
|
|
1
|
+
import { FakerFactory } from "./faker.cjs";
|
|
2
|
+
import { ExtractSeedAttrs, FactorySeed } from "./Factory.cjs";
|
|
3
|
+
import { Knex } from "knex";
|
|
4
|
+
import { Model } from "objection";
|
|
5
|
+
//#region src/ObjectionFactory.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Factory implementation for Objection.js ORM, providing test data creation utilities.
|
|
8
|
+
* Extends the base Factory class with Objection.js-specific database operations.
|
|
9
|
+
*
|
|
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 models with Objection.js
|
|
16
|
+
* class User extends Model {
|
|
17
|
+
* static tableName = 'users';
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* // Create builders
|
|
21
|
+
* const builders = {
|
|
22
|
+
* user: ObjectionFactory.createBuilder(User, ({ attrs, faker }) => ({
|
|
23
|
+
* id: faker.string.uuid(),
|
|
24
|
+
* name: faker.person.fullName(),
|
|
25
|
+
* email: faker.internet.email(),
|
|
26
|
+
* ...attrs
|
|
27
|
+
* })),
|
|
28
|
+
* post: ObjectionFactory.createBuilder(Post, ({ attrs }) => ({
|
|
29
|
+
* title: 'Test Post',
|
|
30
|
+
* content: 'Test content',
|
|
31
|
+
* ...attrs
|
|
32
|
+
* })),
|
|
33
|
+
* };
|
|
34
|
+
*
|
|
35
|
+
* // Create factory instance
|
|
36
|
+
* const factory = new ObjectionFactory(builders, seeds, knex);
|
|
37
|
+
*
|
|
38
|
+
* // Use in tests
|
|
39
|
+
* const user = await factory.insert('user', { name: 'John Doe' });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare class ObjectionFactory<Builders extends Record<string, any>, Seeds extends Record<string, any>> extends Factory<Builders, Seeds> {
|
|
43
|
+
private builders;
|
|
44
|
+
private seeds;
|
|
45
|
+
private db;
|
|
46
|
+
/**
|
|
47
|
+
* Creates a typed seed function with proper type inference.
|
|
48
|
+
* Inherits from the base Factory class implementation.
|
|
49
|
+
*
|
|
50
|
+
* @template Seed - The seed function type
|
|
51
|
+
* @param seedFn - The seed function to wrap (receives { attrs, factory, db } object)
|
|
52
|
+
* @returns The same seed function with proper typing
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* const seeds = {
|
|
57
|
+
* userWithPosts: ObjectionFactory.createSeed(
|
|
58
|
+
* async ({ attrs, factory }) => {
|
|
59
|
+
* const user = await factory.insert('user', attrs);
|
|
60
|
+
* await factory.insertMany(3, 'post', { userId: user.id });
|
|
61
|
+
* return user;
|
|
62
|
+
* },
|
|
63
|
+
* ),
|
|
64
|
+
* };
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
static createSeed<Seed extends FactorySeed>(seedFn: Seed): Seed;
|
|
68
|
+
/**
|
|
69
|
+
* Creates a typed builder function for Objection.js models.
|
|
70
|
+
* This is a utility method that helps create builders with proper type inference.
|
|
71
|
+
*
|
|
72
|
+
* @template TModel - The Objection.js Model class type
|
|
73
|
+
* @template Attrs - The attributes type for the builder (defaults to Partial of model)
|
|
74
|
+
* @template Factory - The factory instance type
|
|
75
|
+
* @template Result - The result type (defaults to the model instance)
|
|
76
|
+
*
|
|
77
|
+
* @param ModelClass - The Objection.js Model class
|
|
78
|
+
* @param defaults - Optional function to provide default values (receives destructured context)
|
|
79
|
+
* @param autoInsert - Whether to automatically insert the record (default: true)
|
|
80
|
+
* @returns A builder function that creates and optionally inserts records
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* // Create a simple builder with defaults - destructure only what you need
|
|
85
|
+
* const userBuilder = ObjectionFactory.createBuilder(User,
|
|
86
|
+
* ({ attrs, faker }) => ({
|
|
87
|
+
* id: faker.string.uuid(),
|
|
88
|
+
* name: faker.person.fullName(),
|
|
89
|
+
* email: faker.internet.email(),
|
|
90
|
+
* createdAt: new Date(),
|
|
91
|
+
* ...attrs
|
|
92
|
+
* })
|
|
93
|
+
* );
|
|
94
|
+
*
|
|
95
|
+
* // Only need faker? Just destructure that
|
|
96
|
+
* const leaveTypeBuilder = ObjectionFactory.createBuilder(LeaveType,
|
|
97
|
+
* ({ faker }) => ({
|
|
98
|
+
* name: faker.helpers.arrayElement(['Annual', 'Sick', 'Maternity']),
|
|
99
|
+
* code: faker.string.alpha({ length: 3, casing: 'upper' }),
|
|
100
|
+
* })
|
|
101
|
+
* );
|
|
102
|
+
*
|
|
103
|
+
* // Create a builder that doesn't auto-insert (useful for nested inserts)
|
|
104
|
+
* const addressBuilder = ObjectionFactory.createBuilder(Address,
|
|
105
|
+
* ({ attrs }) => ({
|
|
106
|
+
* street: '123 Main St',
|
|
107
|
+
* city: 'Anytown',
|
|
108
|
+
* ...attrs
|
|
109
|
+
* }),
|
|
110
|
+
* false // Don't auto-insert
|
|
111
|
+
* );
|
|
112
|
+
*
|
|
113
|
+
* // Use with relations
|
|
114
|
+
* const postBuilder = ObjectionFactory.createBuilder(Post,
|
|
115
|
+
* async ({ attrs, factory, faker }) => ({
|
|
116
|
+
* title: faker.lorem.sentence(),
|
|
117
|
+
* content: faker.lorem.paragraphs(),
|
|
118
|
+
* authorId: attrs.authorId || (await factory.insert('user')).id,
|
|
119
|
+
* ...attrs
|
|
120
|
+
* })
|
|
121
|
+
* );
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
static createBuilder<TModel extends typeof Model, Attrs extends Partial<InstanceType<TModel>> = Partial<InstanceType<TModel>>, Factory = any, Result = InstanceType<TModel>>(ModelClass: TModel, defaults?: (context: {
|
|
125
|
+
attrs: Attrs;
|
|
126
|
+
factory: Factory;
|
|
127
|
+
db: Knex;
|
|
128
|
+
faker: FakerFactory;
|
|
129
|
+
}) => Partial<InstanceType<TModel>> | Promise<Partial<InstanceType<TModel>>>, autoInsert?: boolean): (attrs: Attrs, factory: Factory, db: Knex, faker: FakerFactory) => Promise<Result>;
|
|
130
|
+
/**
|
|
131
|
+
* Creates a new ObjectionFactory instance.
|
|
132
|
+
*
|
|
133
|
+
* @param builders - Record of builder functions for creating individual entities
|
|
134
|
+
* @param seeds - Record of seed functions for creating complex test scenarios
|
|
135
|
+
* @param db - Knex database connection instance
|
|
136
|
+
*/
|
|
137
|
+
constructor(builders: Builders, seeds: Seeds, db: Knex);
|
|
138
|
+
/**
|
|
139
|
+
* Inserts a single record into the database using the specified builder.
|
|
140
|
+
* Uses Objection.js's insertGraph method to handle nested relations.
|
|
141
|
+
*
|
|
142
|
+
* @template K - The builder name (must be a key of Builders)
|
|
143
|
+
* @param builderName - The name of the builder to use
|
|
144
|
+
* @param attrs - Optional attributes to override builder defaults
|
|
145
|
+
* @returns A promise resolving to the inserted record with all relations
|
|
146
|
+
* @throws Error if the specified builder doesn't exist
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```typescript
|
|
150
|
+
* // Insert with defaults
|
|
151
|
+
* const user = await factory.insert('user');
|
|
152
|
+
*
|
|
153
|
+
* // Insert with overrides
|
|
154
|
+
* const adminUser = await factory.insert('user', {
|
|
155
|
+
* email: 'admin@example.com',
|
|
156
|
+
* role: 'admin'
|
|
157
|
+
* });
|
|
158
|
+
*
|
|
159
|
+
* // Insert with nested relations
|
|
160
|
+
* const userWithProfile = await factory.insert('user', {
|
|
161
|
+
* name: 'John Doe',
|
|
162
|
+
* profile: {
|
|
163
|
+
* bio: 'Software Developer',
|
|
164
|
+
* avatar: 'avatar.jpg'
|
|
165
|
+
* }
|
|
166
|
+
* });
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
insert<K extends keyof Builders>(builderName: K, attrs?: Parameters<Builders[K]>[0]): Promise<Awaited<ReturnType<Builders[K]>>>;
|
|
170
|
+
/**
|
|
171
|
+
* Inserts multiple records into the database using the specified builder.
|
|
172
|
+
* Supports both static attributes and dynamic attribute generation via a function.
|
|
173
|
+
*
|
|
174
|
+
* @param count - The number of records to insert
|
|
175
|
+
* @param builderName - The name of the builder to use
|
|
176
|
+
* @param attrs - Static attributes or a function that generates attributes for each record
|
|
177
|
+
* @returns A promise resolving to an array of inserted records
|
|
178
|
+
* @throws Error if the specified builder doesn't exist
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```typescript
|
|
182
|
+
* // Insert multiple with same attributes
|
|
183
|
+
* const users = await factory.insertMany(5, 'user', { role: 'member' });
|
|
184
|
+
*
|
|
185
|
+
* // Insert multiple with dynamic attributes
|
|
186
|
+
* const posts = await factory.insertMany(10, 'post', (idx) => ({
|
|
187
|
+
* title: `Post ${idx + 1}`,
|
|
188
|
+
* content: `Content for post ${idx + 1}`,
|
|
189
|
+
* publishedAt: new Date()
|
|
190
|
+
* }));
|
|
191
|
+
*
|
|
192
|
+
* // Create users with sequential emails
|
|
193
|
+
* const admins = await factory.insertMany(3, 'user', (idx) => ({
|
|
194
|
+
* email: `admin${idx + 1}@example.com`,
|
|
195
|
+
* role: 'admin'
|
|
196
|
+
* }));
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
insertMany<K extends keyof Builders>(count: number, builderName: K, attrs?: Parameters<Builders[K]>[0]): Promise<Awaited<ReturnType<Builders[K]>>[]>;
|
|
200
|
+
insertMany<K extends keyof Builders>(count: number, builderName: K, attrs: (idx: number, faker: FakerFactory) => Parameters<Builders[K]>[0]): Promise<Awaited<ReturnType<Builders[K]>>[]>;
|
|
201
|
+
/**
|
|
202
|
+
* Executes a seed function to create complex test scenarios with multiple related records.
|
|
203
|
+
* Seeds are useful for setting up complete test environments with realistic data relationships.
|
|
204
|
+
*
|
|
205
|
+
* @template K - The seed name (must be a key of Seeds)
|
|
206
|
+
* @param seedName - The name of the seed to execute
|
|
207
|
+
* @param attrs - Optional configuration attributes for the seed
|
|
208
|
+
* @returns The result of the seed function (typically the primary record created)
|
|
209
|
+
* @throws Error if the specified seed doesn't exist
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```typescript
|
|
213
|
+
* // Execute a simple seed
|
|
214
|
+
* const user = await factory.seed('userWithProfile');
|
|
215
|
+
*
|
|
216
|
+
* // Execute a seed with configuration
|
|
217
|
+
* const author = await factory.seed('authorWithBooks', {
|
|
218
|
+
* bookCount: 5,
|
|
219
|
+
* includeReviews: true
|
|
220
|
+
* });
|
|
221
|
+
*
|
|
222
|
+
* // Use seed result in tests with Objection.js relations
|
|
223
|
+
* const company = await factory.seed('companyWithDepartments', {
|
|
224
|
+
* departmentCount: 3,
|
|
225
|
+
* employeesPerDepartment: 10
|
|
226
|
+
* });
|
|
227
|
+
*
|
|
228
|
+
* // Access eager loaded relations
|
|
229
|
+
* const companyWithRelations = await Company.query()
|
|
230
|
+
* .findById(company.id)
|
|
231
|
+
* .withGraphFetched('[departments.employees]');
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
234
|
+
seed<K extends keyof Seeds>(seedName: K, attrs?: ExtractSeedAttrs<Seeds[K]>): ReturnType<Seeds[K]>;
|
|
235
|
+
}
|
|
236
|
+
//#endregion
|
|
237
|
+
//# sourceMappingURL=ObjectionFactory.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ObjectionFactory.d.cts","names":[],"sources":["../src/ObjectionFactory.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAyCa,iBACZ,iBAAiB,qBACjB,cAAc,6BACL,QAAQ,UAAU;UA4JlB;UACA;UACA;;;;;;;;;;;;;;;;;;;;;;SAxIO,WAAW,aAAa,aAAa,QAAQ,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SA4D7D,cACN,sBAAsB,OACtB,cAAc,QAAQ,aAAa,WAAW,QAAQ,aAAa,UACnE,eACA,SAAS,aAAa,SAEtB,YAAY,QACZ,YAAY;IACX,OAAO;IACP,SAAS;IACT,IAAI;IACJ,OAAO;QAEL,QAAQ,aAAa,WACrB,QAAQ,QAAQ,aAAa,WAChC,wBAEA,OAAO,OACP,SAAS,SACT,IAAI,MACJ,OAAO,iBACH,QAAQ;;;;;;;;EAoDb,YACS,UAAU,UACV,OAAO,OACP,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAoCP,OAAO,gBAAgB,UAC5B,aAAa,GACb,QAAQ,WAAW,SAAS,SAC1B,QAAQ,QAAQ,WAAW,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA+DjC,WAAW,gBAAgB,UAChC,eACA,aAAa,GACb,QAAQ,WAAW,SAAS,SAC1B,QAAQ,QAAQ,WAAW,SAAS;EACjC,WAAW,gBAAgB,UAChC,eACA,aAAa,GACb,QAAQ,aAAa,OAAO,iBAAiB,WAAW,SAAS,SAC/D,QAAQ,QAAQ,WAAW,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAgFvC,KAAK,gBAAgB,OACpB,UAAU,GACV,QAAQ,iBAAiB,MAAM,MAC7B,WAAW,MAAM"}
|