@geekmidas/testkit 0.0.17 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +445 -193
- package/dist/{Factory-BZ8uMoXl.d.cts → Factory-DE3hE0WO.d.mts} +2 -2
- package/dist/{Factory-CRquB4vz.d.mts → Factory-pNV7ZQ7-.d.cts} +2 -2
- package/dist/Factory.d.cts +2 -2
- package/dist/Factory.d.mts +2 -2
- package/dist/{KyselyFactory-BDS_QqRT.d.mts → KyselyFactory-CPZTUuMB.d.cts} +23 -10
- package/dist/{KyselyFactory-BcYkC0t2.mjs → KyselyFactory-CXY5gJk2.mjs} +25 -12
- package/dist/KyselyFactory-CXY5gJk2.mjs.map +1 -0
- package/dist/{KyselyFactory-Cf0o2YxO.cjs → KyselyFactory-DaaCykWP.cjs} +25 -12
- package/dist/KyselyFactory-DaaCykWP.cjs.map +1 -0
- package/dist/{KyselyFactory-DRQ83r0o.d.cts → KyselyFactory-RAyvZ8Cj.d.mts} +23 -10
- package/dist/KyselyFactory.cjs +1 -1
- package/dist/KyselyFactory.d.cts +3 -3
- package/dist/KyselyFactory.d.mts +3 -3
- package/dist/KyselyFactory.mjs +1 -1
- package/dist/{ObjectionFactory-C3tHvX1d.d.mts → ObjectionFactory-BlBicEia.d.mts} +26 -13
- package/dist/{ObjectionFactory-C4X78k0B.d.cts → ObjectionFactory-BwpN4gMX.d.cts} +26 -13
- package/dist/{ObjectionFactory-CDriunkS.cjs → ObjectionFactory-Eb04AOnv.cjs} +28 -15
- package/dist/ObjectionFactory-Eb04AOnv.cjs.map +1 -0
- package/dist/{ObjectionFactory-8hebmnai.mjs → ObjectionFactory-zf2fLKrL.mjs} +28 -15
- package/dist/ObjectionFactory-zf2fLKrL.mjs.map +1 -0
- package/dist/ObjectionFactory.cjs +1 -1
- package/dist/ObjectionFactory.d.cts +3 -3
- package/dist/ObjectionFactory.d.mts +3 -3
- package/dist/ObjectionFactory.mjs +1 -1
- package/dist/{VitestKyselyTransactionIsolator-COCVfvfr.d.mts → VitestKyselyTransactionIsolator-BqrZDeaT.d.cts} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-Cst3vFjb.cjs → VitestKyselyTransactionIsolator-DX_VPKS-.cjs} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-Cst3vFjb.cjs.map → VitestKyselyTransactionIsolator-DX_VPKS-.cjs.map} +1 -1
- package/dist/{VitestKyselyTransactionIsolator-DYUYVEh9.d.cts → VitestKyselyTransactionIsolator-DiskaURs.d.mts} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-BxjlD1YM.mjs → VitestKyselyTransactionIsolator-XDL3ngs_.mjs} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-BxjlD1YM.mjs.map → VitestKyselyTransactionIsolator-XDL3ngs_.mjs.map} +1 -1
- package/dist/VitestKyselyTransactionIsolator.cjs +2 -2
- package/dist/VitestKyselyTransactionIsolator.d.cts +2 -2
- package/dist/VitestKyselyTransactionIsolator.d.mts +2 -2
- package/dist/VitestKyselyTransactionIsolator.mjs +2 -2
- package/dist/{VitestObjectionTransactionIsolator-b973r9O1.d.mts → VitestObjectionTransactionIsolator-CD2ucJpH.d.cts} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-DzeF4UAq.cjs → VitestObjectionTransactionIsolator-D_tlOtq8.cjs} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-DzeF4UAq.cjs.map → VitestObjectionTransactionIsolator-D_tlOtq8.cjs.map} +1 -1
- package/dist/{VitestObjectionTransactionIsolator-CJ4ds5Qv.d.cts → VitestObjectionTransactionIsolator-DhQ8XGva.d.mts} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-BU-jXEhz.mjs → VitestObjectionTransactionIsolator-_EhJKu_O.mjs} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-BU-jXEhz.mjs.map → VitestObjectionTransactionIsolator-_EhJKu_O.mjs.map} +1 -1
- package/dist/VitestObjectionTransactionIsolator.cjs +2 -2
- package/dist/VitestObjectionTransactionIsolator.d.cts +2 -2
- package/dist/VitestObjectionTransactionIsolator.d.mts +2 -2
- package/dist/VitestObjectionTransactionIsolator.mjs +2 -2
- package/dist/{VitestTransactionIsolator-CskiiJbW.mjs → VitestTransactionIsolator-BIaMs4c2.mjs} +40 -2
- package/dist/VitestTransactionIsolator-BIaMs4c2.mjs.map +1 -0
- package/dist/{VitestTransactionIsolator-BQ5FpLtC.cjs → VitestTransactionIsolator-BKIrj3Uy.cjs} +45 -1
- package/dist/VitestTransactionIsolator-BKIrj3Uy.cjs.map +1 -0
- package/dist/{VitestTransactionIsolator-CsfJBxcb.d.mts → VitestTransactionIsolator-DLdQlfZ5.d.mts} +70 -3
- package/dist/{VitestTransactionIsolator-DdLNODZg.d.cts → VitestTransactionIsolator-DfA80g2M.d.cts} +70 -3
- package/dist/VitestTransactionIsolator.cjs +3 -2
- package/dist/VitestTransactionIsolator.d.cts +2 -2
- package/dist/VitestTransactionIsolator.d.mts +2 -2
- package/dist/VitestTransactionIsolator.mjs +2 -2
- package/dist/better-auth.cjs +8 -11
- package/dist/better-auth.cjs.map +1 -1
- package/dist/better-auth.d.cts +2 -2
- package/dist/better-auth.d.mts +2 -2
- package/dist/better-auth.mjs +8 -11
- package/dist/better-auth.mjs.map +1 -1
- package/dist/{directory-B4oYx02C.d.mts → directory-Mi7tdOuD.d.cts} +3 -3
- package/dist/{directory-BUcnztHI.d.cts → directory-Q178x53k.d.mts} +3 -3
- package/dist/{faker-Cg76aFNO.d.cts → faker-BSH1EMtg.d.cts} +3 -3
- package/dist/{faker-Br8MzXil.d.mts → faker-D9gz7KjY.d.mts} +3 -3
- package/dist/faker.d.cts +1 -1
- package/dist/faker.d.mts +1 -1
- package/dist/kysely.cjs +58 -4
- package/dist/kysely.cjs.map +1 -1
- package/dist/kysely.d.cts +56 -6
- package/dist/kysely.d.mts +56 -6
- package/dist/kysely.mjs +57 -5
- package/dist/kysely.mjs.map +1 -1
- package/dist/objection.cjs +54 -4
- package/dist/objection.cjs.map +1 -1
- package/dist/objection.d.cts +52 -6
- package/dist/objection.d.mts +52 -6
- package/dist/objection.mjs +53 -5
- package/dist/objection.mjs.map +1 -1
- package/dist/os/directory.d.cts +1 -1
- package/dist/os/directory.d.mts +1 -1
- package/dist/os/index.d.cts +1 -1
- package/dist/os/index.d.mts +1 -1
- package/package.json +7 -3
- package/src/KyselyFactory.ts +29 -16
- package/src/ObjectionFactory.ts +34 -19
- package/src/VitestTransactionIsolator.ts +126 -2
- package/src/__tests__/KyselyFactory.spec.ts +10 -10
- package/src/__tests__/ObjectionFactory.spec.ts +9 -12
- package/src/__tests__/integration.spec.ts +171 -14
- package/src/better-auth.ts +13 -15
- package/src/kysely.ts +71 -0
- package/src/objection.ts +66 -0
- package/dist/KyselyFactory-BcYkC0t2.mjs.map +0 -1
- package/dist/KyselyFactory-Cf0o2YxO.cjs.map +0 -1
- package/dist/ObjectionFactory-8hebmnai.mjs.map +0 -1
- package/dist/ObjectionFactory-CDriunkS.cjs.map +0 -1
- package/dist/VitestTransactionIsolator-BQ5FpLtC.cjs.map +0 -1
- package/dist/VitestTransactionIsolator-CskiiJbW.mjs.map +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { FakerFactory } from "./faker-
|
|
2
|
-
import { Factory, FactorySeed } from "./Factory-
|
|
1
|
+
import { FakerFactory } from "./faker-BSH1EMtg.cjs";
|
|
2
|
+
import { Factory, FactorySeed } from "./Factory-pNV7ZQ7-.cjs";
|
|
3
3
|
import { Knex } from "knex";
|
|
4
4
|
import { Model } from "objection";
|
|
5
5
|
|
|
@@ -21,17 +21,17 @@ import { Model } from "objection";
|
|
|
21
21
|
*
|
|
22
22
|
* // Create builders
|
|
23
23
|
* const builders = {
|
|
24
|
-
* user: (attrs) =>
|
|
24
|
+
* user: ObjectionFactory.createBuilder(User, ({ attrs, faker }) => ({
|
|
25
25
|
* id: faker.string.uuid(),
|
|
26
26
|
* name: faker.person.fullName(),
|
|
27
27
|
* email: faker.internet.email(),
|
|
28
28
|
* ...attrs
|
|
29
|
-
* }),
|
|
30
|
-
* post: (attrs) =>
|
|
29
|
+
* })),
|
|
30
|
+
* post: ObjectionFactory.createBuilder(Post, ({ attrs }) => ({
|
|
31
31
|
* title: 'Test Post',
|
|
32
32
|
* content: 'Test content',
|
|
33
33
|
* ...attrs
|
|
34
|
-
* })
|
|
34
|
+
* })),
|
|
35
35
|
* };
|
|
36
36
|
*
|
|
37
37
|
* // Create factory instance
|
|
@@ -64,15 +64,15 @@ declare class ObjectionFactory<Builders extends Record<string, any>, Seeds exten
|
|
|
64
64
|
* @template Result - The result type (defaults to the model instance)
|
|
65
65
|
*
|
|
66
66
|
* @param ModelClass - The Objection.js Model class
|
|
67
|
-
* @param
|
|
67
|
+
* @param defaults - Optional function to provide default values (receives destructured context)
|
|
68
68
|
* @param autoInsert - Whether to automatically insert the record (default: true)
|
|
69
69
|
* @returns A builder function that creates and optionally inserts records
|
|
70
70
|
*
|
|
71
71
|
* @example
|
|
72
72
|
* ```typescript
|
|
73
|
-
* // Create a simple builder with defaults
|
|
73
|
+
* // Create a simple builder with defaults - destructure only what you need
|
|
74
74
|
* const userBuilder = ObjectionFactory.createBuilder(User,
|
|
75
|
-
* (attrs,
|
|
75
|
+
* ({ attrs, faker }) => ({
|
|
76
76
|
* id: faker.string.uuid(),
|
|
77
77
|
* name: faker.person.fullName(),
|
|
78
78
|
* email: faker.internet.email(),
|
|
@@ -81,9 +81,17 @@ declare class ObjectionFactory<Builders extends Record<string, any>, Seeds exten
|
|
|
81
81
|
* })
|
|
82
82
|
* );
|
|
83
83
|
*
|
|
84
|
+
* // Only need faker? Just destructure that
|
|
85
|
+
* const leaveTypeBuilder = ObjectionFactory.createBuilder(LeaveType,
|
|
86
|
+
* ({ faker }) => ({
|
|
87
|
+
* name: faker.helpers.arrayElement(['Annual', 'Sick', 'Maternity']),
|
|
88
|
+
* code: faker.string.alpha({ length: 3, casing: 'upper' }),
|
|
89
|
+
* })
|
|
90
|
+
* );
|
|
91
|
+
*
|
|
84
92
|
* // Create a builder that doesn't auto-insert (useful for nested inserts)
|
|
85
93
|
* const addressBuilder = ObjectionFactory.createBuilder(Address,
|
|
86
|
-
* (attrs) => ({
|
|
94
|
+
* ({ attrs }) => ({
|
|
87
95
|
* street: '123 Main St',
|
|
88
96
|
* city: 'Anytown',
|
|
89
97
|
* ...attrs
|
|
@@ -93,7 +101,7 @@ declare class ObjectionFactory<Builders extends Record<string, any>, Seeds exten
|
|
|
93
101
|
*
|
|
94
102
|
* // Use with relations
|
|
95
103
|
* const postBuilder = ObjectionFactory.createBuilder(Post,
|
|
96
|
-
* async (attrs, factory) => ({
|
|
104
|
+
* async ({ attrs, factory, faker }) => ({
|
|
97
105
|
* title: faker.lorem.sentence(),
|
|
98
106
|
* content: faker.lorem.paragraphs(),
|
|
99
107
|
* authorId: attrs.authorId || (await factory.insert('user')).id,
|
|
@@ -102,7 +110,12 @@ declare class ObjectionFactory<Builders extends Record<string, any>, Seeds exten
|
|
|
102
110
|
* );
|
|
103
111
|
* ```
|
|
104
112
|
*/
|
|
105
|
-
static createBuilder<TModel extends typeof Model, Attrs extends Partial<InstanceType<TModel>> = Partial<InstanceType<TModel>>, Factory = any, Result = InstanceType<TModel>>(ModelClass: TModel,
|
|
113
|
+
static createBuilder<TModel extends typeof Model, Attrs extends Partial<InstanceType<TModel>> = Partial<InstanceType<TModel>>, Factory = any, Result = InstanceType<TModel>>(ModelClass: TModel, defaults?: (context: {
|
|
114
|
+
attrs: Attrs;
|
|
115
|
+
factory: Factory;
|
|
116
|
+
db: Knex;
|
|
117
|
+
faker: FakerFactory;
|
|
118
|
+
}) => Partial<InstanceType<TModel>> | Promise<Partial<InstanceType<TModel>>>, autoInsert?: boolean): (attrs: Attrs, factory: Factory, db: Knex, faker: FakerFactory) => Promise<Result>;
|
|
106
119
|
/**
|
|
107
120
|
* Creates a new ObjectionFactory instance.
|
|
108
121
|
*
|
|
@@ -211,4 +224,4 @@ declare class ObjectionFactory<Builders extends Record<string, any>, Seeds exten
|
|
|
211
224
|
}
|
|
212
225
|
//#endregion
|
|
213
226
|
export { ObjectionFactory };
|
|
214
|
-
//# sourceMappingURL=ObjectionFactory-
|
|
227
|
+
//# sourceMappingURL=ObjectionFactory-BwpN4gMX.d.cts.map
|
|
@@ -18,17 +18,17 @@ const require_faker = require('./faker-B14IEMIN.cjs');
|
|
|
18
18
|
*
|
|
19
19
|
* // Create builders
|
|
20
20
|
* const builders = {
|
|
21
|
-
* user: (attrs) =>
|
|
21
|
+
* user: ObjectionFactory.createBuilder(User, ({ attrs, faker }) => ({
|
|
22
22
|
* id: faker.string.uuid(),
|
|
23
23
|
* name: faker.person.fullName(),
|
|
24
24
|
* email: faker.internet.email(),
|
|
25
25
|
* ...attrs
|
|
26
|
-
* }),
|
|
27
|
-
* post: (attrs) =>
|
|
26
|
+
* })),
|
|
27
|
+
* post: ObjectionFactory.createBuilder(Post, ({ attrs }) => ({
|
|
28
28
|
* title: 'Test Post',
|
|
29
29
|
* content: 'Test content',
|
|
30
30
|
* ...attrs
|
|
31
|
-
* })
|
|
31
|
+
* })),
|
|
32
32
|
* };
|
|
33
33
|
*
|
|
34
34
|
* // Create factory instance
|
|
@@ -60,15 +60,15 @@ var ObjectionFactory = class extends require_Factory.Factory {
|
|
|
60
60
|
* @template Result - The result type (defaults to the model instance)
|
|
61
61
|
*
|
|
62
62
|
* @param ModelClass - The Objection.js Model class
|
|
63
|
-
* @param
|
|
63
|
+
* @param defaults - Optional function to provide default values (receives destructured context)
|
|
64
64
|
* @param autoInsert - Whether to automatically insert the record (default: true)
|
|
65
65
|
* @returns A builder function that creates and optionally inserts records
|
|
66
66
|
*
|
|
67
67
|
* @example
|
|
68
68
|
* ```typescript
|
|
69
|
-
* // Create a simple builder with defaults
|
|
69
|
+
* // Create a simple builder with defaults - destructure only what you need
|
|
70
70
|
* const userBuilder = ObjectionFactory.createBuilder(User,
|
|
71
|
-
* (attrs,
|
|
71
|
+
* ({ attrs, faker }) => ({
|
|
72
72
|
* id: faker.string.uuid(),
|
|
73
73
|
* name: faker.person.fullName(),
|
|
74
74
|
* email: faker.internet.email(),
|
|
@@ -77,9 +77,17 @@ var ObjectionFactory = class extends require_Factory.Factory {
|
|
|
77
77
|
* })
|
|
78
78
|
* );
|
|
79
79
|
*
|
|
80
|
+
* // Only need faker? Just destructure that
|
|
81
|
+
* const leaveTypeBuilder = ObjectionFactory.createBuilder(LeaveType,
|
|
82
|
+
* ({ faker }) => ({
|
|
83
|
+
* name: faker.helpers.arrayElement(['Annual', 'Sick', 'Maternity']),
|
|
84
|
+
* code: faker.string.alpha({ length: 3, casing: 'upper' }),
|
|
85
|
+
* })
|
|
86
|
+
* );
|
|
87
|
+
*
|
|
80
88
|
* // Create a builder that doesn't auto-insert (useful for nested inserts)
|
|
81
89
|
* const addressBuilder = ObjectionFactory.createBuilder(Address,
|
|
82
|
-
* (attrs) => ({
|
|
90
|
+
* ({ attrs }) => ({
|
|
83
91
|
* street: '123 Main St',
|
|
84
92
|
* city: 'Anytown',
|
|
85
93
|
* ...attrs
|
|
@@ -89,7 +97,7 @@ var ObjectionFactory = class extends require_Factory.Factory {
|
|
|
89
97
|
*
|
|
90
98
|
* // Use with relations
|
|
91
99
|
* const postBuilder = ObjectionFactory.createBuilder(Post,
|
|
92
|
-
* async (attrs, factory) => ({
|
|
100
|
+
* async ({ attrs, factory, faker }) => ({
|
|
93
101
|
* title: faker.lorem.sentence(),
|
|
94
102
|
* content: faker.lorem.paragraphs(),
|
|
95
103
|
* authorId: attrs.authorId || (await factory.insert('user')).id,
|
|
@@ -98,13 +106,18 @@ var ObjectionFactory = class extends require_Factory.Factory {
|
|
|
98
106
|
* );
|
|
99
107
|
* ```
|
|
100
108
|
*/
|
|
101
|
-
static createBuilder(ModelClass,
|
|
102
|
-
return async (attrs, factory, db,
|
|
109
|
+
static createBuilder(ModelClass, defaults, autoInsert) {
|
|
110
|
+
return async (attrs, factory, db, fakerInstance) => {
|
|
103
111
|
let data = { ...attrs };
|
|
104
|
-
if (
|
|
105
|
-
const
|
|
112
|
+
if (defaults) {
|
|
113
|
+
const defaultValues = await defaults({
|
|
114
|
+
attrs,
|
|
115
|
+
factory,
|
|
116
|
+
db,
|
|
117
|
+
faker: fakerInstance
|
|
118
|
+
});
|
|
106
119
|
data = {
|
|
107
|
-
...
|
|
120
|
+
...defaultValues,
|
|
108
121
|
...data
|
|
109
122
|
};
|
|
110
123
|
}
|
|
@@ -239,4 +252,4 @@ Object.defineProperty(exports, 'ObjectionFactory', {
|
|
|
239
252
|
return ObjectionFactory;
|
|
240
253
|
}
|
|
241
254
|
});
|
|
242
|
-
//# sourceMappingURL=ObjectionFactory-
|
|
255
|
+
//# sourceMappingURL=ObjectionFactory-Eb04AOnv.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ObjectionFactory-Eb04AOnv.cjs","names":["Factory","seedFn: Seed","ModelClass: TModel","defaults?: (context: {\n attrs: Attrs;\n factory: Factory;\n db: Knex;\n faker: FakerFactory;\n }) =>\n | Partial<InstanceType<TModel>>\n | Promise<Partial<InstanceType<TModel>>>","autoInsert?: boolean","attrs: Attrs","factory: Factory","db: Knex","fakerInstance: FakerFactory","data: Partial<InstanceType<TModel>>","builders: Builders","seeds: Seeds","builderName: K","attrs?: Parameters<Builders[K]>[0]","faker","count: number","attrs?: any","records: any[]","record: any","seedName: K","attrs?: Parameters<Seeds[K]>[0]"],"sources":["../src/ObjectionFactory.ts"],"sourcesContent":["import type { Knex } from 'knex';\nimport type { Model } from 'objection';\nimport { Factory, type FactorySeed } from './Factory.ts';\nimport { type FakerFactory, faker } from './faker.ts';\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 Builders extends Record<string, any>,\n Seeds extends Record<string, any>,\n> extends Factory<Builders, Seeds> {\n /**\n * Creates a typed seed function with proper type inference.\n * Inherits from the base Factory class implementation.\n *\n * @template Seed - The seed function type\n * @param seedFn - The seed function to wrap\n * @returns The same seed function with proper typing\n */\n static createSeed<Seed extends FactorySeed>(seedFn: Seed): Seed {\n return Factory.createSeed(seedFn);\n }\n\n /**\n * Creates a typed builder function for Objection.js models.\n * This is a utility method that helps create builders with proper type inference.\n *\n * @template TModel - The Objection.js Model class type\n * @template Attrs - The attributes type for the builder (defaults to Partial of model)\n * @template Factory - The factory instance type\n * @template Result - The result type (defaults to the model instance)\n *\n * @param ModelClass - The Objection.js Model class\n * @param defaults - Optional function to provide default values (receives destructured context)\n * @param autoInsert - Whether to automatically insert the record (default: true)\n * @returns A builder function that creates and optionally inserts records\n *\n * @example\n * ```typescript\n * // Create a simple builder with defaults - destructure only what you need\n * const userBuilder = ObjectionFactory.createBuilder(User,\n * ({ attrs, faker }) => ({\n * id: faker.string.uuid(),\n * name: faker.person.fullName(),\n * email: faker.internet.email(),\n * createdAt: new Date(),\n * ...attrs\n * })\n * );\n *\n * // Only need faker? Just destructure that\n * const leaveTypeBuilder = ObjectionFactory.createBuilder(LeaveType,\n * ({ faker }) => ({\n * name: faker.helpers.arrayElement(['Annual', 'Sick', 'Maternity']),\n * code: faker.string.alpha({ length: 3, casing: 'upper' }),\n * })\n * );\n *\n * // Create a builder that doesn't auto-insert (useful for nested inserts)\n * const addressBuilder = ObjectionFactory.createBuilder(Address,\n * ({ attrs }) => ({\n * street: '123 Main St',\n * city: 'Anytown',\n * ...attrs\n * }),\n * false // Don't auto-insert\n * );\n *\n * // Use with relations\n * const postBuilder = ObjectionFactory.createBuilder(Post,\n * async ({ attrs, factory, faker }) => ({\n * title: faker.lorem.sentence(),\n * content: faker.lorem.paragraphs(),\n * authorId: attrs.authorId || (await factory.insert('user')).id,\n * ...attrs\n * })\n * );\n * ```\n */\n static createBuilder<\n TModel extends typeof Model,\n Attrs extends Partial<InstanceType<TModel>> = Partial<InstanceType<TModel>>,\n Factory = any,\n Result = InstanceType<TModel>,\n >(\n ModelClass: TModel,\n defaults?: (context: {\n attrs: Attrs;\n factory: Factory;\n db: Knex;\n faker: FakerFactory;\n }) =>\n | Partial<InstanceType<TModel>>\n | Promise<Partial<InstanceType<TModel>>>,\n autoInsert?: boolean,\n ): (\n attrs: Attrs,\n factory: Factory,\n db: Knex,\n faker: FakerFactory,\n ) => Promise<Result> {\n return async (\n attrs: Attrs,\n factory: Factory,\n db: Knex,\n fakerInstance: FakerFactory,\n ) => {\n // Start with attributes\n let data: Partial<InstanceType<TModel>> = { ...attrs };\n\n // Apply defaults\n if (defaults) {\n const defaultValues = await defaults({\n attrs,\n factory,\n db,\n faker: fakerInstance,\n });\n data = { ...defaultValues, ...data };\n }\n\n // Create model instance\n const model = ModelClass.fromJson(data) as InstanceType<TModel>;\n\n // Handle insertion based on autoInsert flag\n if (autoInsert !== false) {\n // Auto insert is enabled by default\n // Extract only defined values for insertion\n const insertData = Object.entries(model).reduce((acc, [key, value]) => {\n if (value !== undefined && key !== 'id') {\n acc[key] = value;\n }\n return acc;\n }, {} as any);\n\n // Use static query method to insert data directly\n // @ts-ignore\n const result = await ModelClass.query(db).insert(insertData);\n return result as Result;\n } else {\n // Return model for factory to handle insertion\n return model as Result;\n }\n };\n }\n\n /**\n * Creates a new ObjectionFactory instance.\n *\n * @param builders - Record of builder functions for creating individual entities\n * @param seeds - Record of seed functions for creating complex test scenarios\n * @param db - Knex database connection instance\n */\n constructor(\n private builders: Builders,\n private seeds: Seeds,\n private db: Knex,\n ) {\n super();\n }\n\n /**\n * Inserts a single record into the database using the specified builder.\n * Uses Objection.js's insertGraph method to handle nested relations.\n *\n * @template K - The builder name (must be a key of Builders)\n * @param builderName - The name of the builder to use\n * @param attrs - Optional attributes to override builder defaults\n * @returns A promise resolving to the inserted record with all relations\n * @throws Error if the specified builder doesn't exist\n *\n * @example\n * ```typescript\n * // Insert with defaults\n * const user = await factory.insert('user');\n *\n * // Insert with overrides\n * const adminUser = await factory.insert('user', {\n * email: 'admin@example.com',\n * role: 'admin'\n * });\n *\n * // Insert with nested relations\n * const userWithProfile = await factory.insert('user', {\n * name: 'John Doe',\n * profile: {\n * bio: 'Software Developer',\n * avatar: 'avatar.jpg'\n * }\n * });\n * ```\n */\n async insert<K extends keyof Builders>(\n builderName: K,\n attrs?: Parameters<Builders[K]>[0],\n ): Promise<Awaited<ReturnType<Builders[K]>>> {\n if (!(builderName in this.builders)) {\n throw new Error(\n `Factory \"${\n builderName as string\n }\" does not exist. Make sure it is correct and registered in src/test/setup.ts`,\n );\n }\n\n const result = await this.builders[builderName](\n attrs || {},\n this,\n this.db,\n faker,\n );\n\n // If the builder returns a model instance, insert it\n if (result && typeof result.$query === 'function') {\n // Extract data from model, excluding undefined values and id\n const insertData = Object.entries(result).reduce((acc, [key, value]) => {\n if (value !== undefined && key !== 'id') {\n acc[key] = value;\n }\n return acc;\n }, {} as any);\n\n // Use the model's constructor to get the query builder\n return await result.constructor.query(this.db).insert(insertData);\n }\n\n // Otherwise, assume the builder handled insertion itself\n return result;\n }\n /**\n * Inserts multiple records into the database using the specified builder.\n * Supports both static attributes and dynamic attribute generation via a function.\n *\n * @param count - The number of records to insert\n * @param builderName - The name of the builder to use\n * @param attrs - Static attributes or a function that generates attributes for each record\n * @returns A promise resolving to an array of inserted records\n * @throws Error if the specified builder doesn't exist\n *\n * @example\n * ```typescript\n * // Insert multiple with same attributes\n * const users = await factory.insertMany(5, 'user', { role: 'member' });\n *\n * // Insert multiple with dynamic attributes\n * const posts = await factory.insertMany(10, 'post', (idx) => ({\n * title: `Post ${idx + 1}`,\n * content: `Content for post ${idx + 1}`,\n * publishedAt: new Date()\n * }));\n *\n * // Create users with sequential emails\n * const admins = await factory.insertMany(3, 'user', (idx) => ({\n * email: `admin${idx + 1}@example.com`,\n * role: 'admin'\n * }));\n * ```\n */\n // Method overloads for better type inference\n async insertMany<K extends keyof Builders>(\n count: number,\n builderName: K,\n attrs?: Parameters<Builders[K]>[0],\n ): Promise<Awaited<ReturnType<Builders[K]>>[]>;\n async insertMany<K extends keyof Builders>(\n count: number,\n builderName: K,\n attrs: (idx: number, faker: FakerFactory) => Parameters<Builders[K]>[0],\n ): Promise<Awaited<ReturnType<Builders[K]>>[]>;\n async insertMany<K extends keyof Builders>(\n count: number,\n builderName: K,\n attrs?: any,\n ): Promise<Awaited<ReturnType<Builders[K]>>[]> {\n if (!(builderName in this.builders)) {\n throw new Error(\n `Builder \"${\n builderName as string\n }\" is not registered in this factory. Make sure it is correct and registered in src/test/setup.ts`,\n );\n }\n\n const records: any[] = [];\n for (let i = 0; i < count; i++) {\n const newAttrs =\n typeof attrs === 'function' ? await (attrs as any)(i, faker) : attrs;\n\n records.push(\n this.builders[builderName](newAttrs, this, this.db, faker).then(\n (record: any) => {\n // If the builder returns a model instance, insert it\n if (record && typeof record.$query === 'function') {\n // Extract data from model, excluding undefined values and id\n const insertData = Object.entries(record).reduce(\n (acc, [key, value]) => {\n if (value !== undefined && key !== 'id') {\n acc[key] = value;\n }\n return acc;\n },\n {} as any,\n );\n\n // Use the model's constructor to get the query builder\n return record.constructor.query(this.db).insert(insertData);\n }\n // Otherwise, assume the builder handled insertion itself\n return record;\n },\n ),\n );\n }\n\n return Promise.all(records);\n }\n /**\n * Executes a seed function to create complex test scenarios with multiple related records.\n * Seeds are useful for setting up complete test environments with realistic data relationships.\n *\n * @template K - The seed name (must be a key of Seeds)\n * @param seedName - The name of the seed to execute\n * @param attrs - Optional configuration attributes for the seed\n * @returns The result of the seed function (typically the primary record created)\n * @throws Error if the specified seed doesn't exist\n *\n * @example\n * ```typescript\n * // Execute a simple seed\n * const user = await factory.seed('userWithProfile');\n *\n * // Execute a seed with configuration\n * const author = await factory.seed('authorWithBooks', {\n * bookCount: 5,\n * includeReviews: true\n * });\n *\n * // Use seed result in tests with Objection.js relations\n * const company = await factory.seed('companyWithDepartments', {\n * departmentCount: 3,\n * employeesPerDepartment: 10\n * });\n *\n * // Access eager loaded relations\n * const companyWithRelations = await Company.query()\n * .findById(company.id)\n * .withGraphFetched('[departments.employees]');\n * ```\n */\n seed<K extends keyof Seeds>(\n seedName: K,\n attrs?: Parameters<Seeds[K]>[0],\n ): ReturnType<Seeds[K]> {\n if (!(seedName in this.seeds)) {\n throw new Error(\n `Seed \"${\n seedName as string\n }\" is not registered in this factory. Make sure it is correct and registered in src/test/setup.ts`,\n );\n }\n\n return this.seeds[seedName](attrs || {}, this, this.db);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCA,IAAa,mBAAb,cAGUA,wBAAyB;;;;;;;;;CASjC,OAAO,WAAqCC,QAAoB;AAC9D,SAAO,wBAAQ,WAAW,OAAO;CAClC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA0DD,OAAO,cAMLC,YACAC,UAQAC,YAMmB;AACnB,SAAO,OACLC,OACAC,SACAC,IACAC,kBACG;GAEH,IAAIC,OAAsC,EAAE,GAAG,MAAO;AAGtD,OAAI,UAAU;IACZ,MAAM,gBAAgB,MAAM,SAAS;KACnC;KACA;KACA;KACA,OAAO;IACR,EAAC;AACF,WAAO;KAAE,GAAG;KAAe,GAAG;IAAM;GACrC;GAGD,MAAM,QAAQ,WAAW,SAAS,KAAK;AAGvC,OAAI,eAAe,OAAO;IAGxB,MAAM,aAAa,OAAO,QAAQ,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,MAAM,KAAK;AACrE,SAAI,oBAAuB,QAAQ,KACjC,KAAI,OAAO;AAEb,YAAO;IACR,GAAE,CAAE,EAAQ;IAIb,MAAM,SAAS,MAAM,WAAW,MAAM,GAAG,CAAC,OAAO,WAAW;AAC5D,WAAO;GACR,MAEC,QAAO;EAEV;CACF;;;;;;;;CASD,YACUC,UACAC,OACAJ,IACR;AACA,SAAO;EAJC;EACA;EACA;CAGT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiCD,MAAM,OACJK,aACAC,OAC2C;AAC3C,QAAM,eAAe,KAAK,UACxB,OAAM,IAAI,OACP,WACC,YACD;EAIL,MAAM,SAAS,MAAM,KAAK,SAAS,aACjC,SAAS,CAAE,GACX,MACA,KAAK,IACLC,oBACD;AAGD,MAAI,iBAAiB,OAAO,WAAW,YAAY;GAEjD,MAAM,aAAa,OAAO,QAAQ,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,MAAM,KAAK;AACtE,QAAI,oBAAuB,QAAQ,KACjC,KAAI,OAAO;AAEb,WAAO;GACR,GAAE,CAAE,EAAQ;AAGb,UAAO,MAAM,OAAO,YAAY,MAAM,KAAK,GAAG,CAAC,OAAO,WAAW;EAClE;AAGD,SAAO;CACR;CAyCD,MAAM,WACJC,OACAH,aACAI,OAC6C;AAC7C,QAAM,eAAe,KAAK,UACxB,OAAM,IAAI,OACP,WACC,YACD;EAIL,MAAMC,UAAiB,CAAE;AACzB,OAAK,IAAI,IAAI,GAAG,IAAI,OAAO,KAAK;GAC9B,MAAM,kBACG,UAAU,aAAa,MAAM,AAAC,MAAc,GAAGH,oBAAM,GAAG;AAEjE,WAAQ,KACN,KAAK,SAAS,aAAa,UAAU,MAAM,KAAK,IAAIA,oBAAM,CAAC,KACzD,CAACI,WAAgB;AAEf,QAAI,iBAAiB,OAAO,WAAW,YAAY;KAEjD,MAAM,aAAa,OAAO,QAAQ,OAAO,CAAC,OACxC,CAAC,KAAK,CAAC,KAAK,MAAM,KAAK;AACrB,UAAI,oBAAuB,QAAQ,KACjC,KAAI,OAAO;AAEb,aAAO;KACR,GACD,CAAE,EACH;AAGD,YAAO,OAAO,YAAY,MAAM,KAAK,GAAG,CAAC,OAAO,WAAW;IAC5D;AAED,WAAO;GACR,EACF,CACF;EACF;AAED,SAAO,QAAQ,IAAI,QAAQ;CAC5B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkCD,KACEC,UACAC,OACsB;AACtB,QAAM,YAAY,KAAK,OACrB,OAAM,IAAI,OACP,QACC,SACD;AAIL,SAAO,KAAK,MAAM,UAAU,SAAS,CAAE,GAAE,MAAM,KAAK,GAAG;CACxD;AACF"}
|
|
@@ -18,17 +18,17 @@ import { faker } from "./faker-BGKYFoCT.mjs";
|
|
|
18
18
|
*
|
|
19
19
|
* // Create builders
|
|
20
20
|
* const builders = {
|
|
21
|
-
* user: (attrs) =>
|
|
21
|
+
* user: ObjectionFactory.createBuilder(User, ({ attrs, faker }) => ({
|
|
22
22
|
* id: faker.string.uuid(),
|
|
23
23
|
* name: faker.person.fullName(),
|
|
24
24
|
* email: faker.internet.email(),
|
|
25
25
|
* ...attrs
|
|
26
|
-
* }),
|
|
27
|
-
* post: (attrs) =>
|
|
26
|
+
* })),
|
|
27
|
+
* post: ObjectionFactory.createBuilder(Post, ({ attrs }) => ({
|
|
28
28
|
* title: 'Test Post',
|
|
29
29
|
* content: 'Test content',
|
|
30
30
|
* ...attrs
|
|
31
|
-
* })
|
|
31
|
+
* })),
|
|
32
32
|
* };
|
|
33
33
|
*
|
|
34
34
|
* // Create factory instance
|
|
@@ -60,15 +60,15 @@ var ObjectionFactory = class extends Factory {
|
|
|
60
60
|
* @template Result - The result type (defaults to the model instance)
|
|
61
61
|
*
|
|
62
62
|
* @param ModelClass - The Objection.js Model class
|
|
63
|
-
* @param
|
|
63
|
+
* @param defaults - Optional function to provide default values (receives destructured context)
|
|
64
64
|
* @param autoInsert - Whether to automatically insert the record (default: true)
|
|
65
65
|
* @returns A builder function that creates and optionally inserts records
|
|
66
66
|
*
|
|
67
67
|
* @example
|
|
68
68
|
* ```typescript
|
|
69
|
-
* // Create a simple builder with defaults
|
|
69
|
+
* // Create a simple builder with defaults - destructure only what you need
|
|
70
70
|
* const userBuilder = ObjectionFactory.createBuilder(User,
|
|
71
|
-
* (attrs,
|
|
71
|
+
* ({ attrs, faker }) => ({
|
|
72
72
|
* id: faker.string.uuid(),
|
|
73
73
|
* name: faker.person.fullName(),
|
|
74
74
|
* email: faker.internet.email(),
|
|
@@ -77,9 +77,17 @@ var ObjectionFactory = class extends Factory {
|
|
|
77
77
|
* })
|
|
78
78
|
* );
|
|
79
79
|
*
|
|
80
|
+
* // Only need faker? Just destructure that
|
|
81
|
+
* const leaveTypeBuilder = ObjectionFactory.createBuilder(LeaveType,
|
|
82
|
+
* ({ faker }) => ({
|
|
83
|
+
* name: faker.helpers.arrayElement(['Annual', 'Sick', 'Maternity']),
|
|
84
|
+
* code: faker.string.alpha({ length: 3, casing: 'upper' }),
|
|
85
|
+
* })
|
|
86
|
+
* );
|
|
87
|
+
*
|
|
80
88
|
* // Create a builder that doesn't auto-insert (useful for nested inserts)
|
|
81
89
|
* const addressBuilder = ObjectionFactory.createBuilder(Address,
|
|
82
|
-
* (attrs) => ({
|
|
90
|
+
* ({ attrs }) => ({
|
|
83
91
|
* street: '123 Main St',
|
|
84
92
|
* city: 'Anytown',
|
|
85
93
|
* ...attrs
|
|
@@ -89,7 +97,7 @@ var ObjectionFactory = class extends Factory {
|
|
|
89
97
|
*
|
|
90
98
|
* // Use with relations
|
|
91
99
|
* const postBuilder = ObjectionFactory.createBuilder(Post,
|
|
92
|
-
* async (attrs, factory) => ({
|
|
100
|
+
* async ({ attrs, factory, faker }) => ({
|
|
93
101
|
* title: faker.lorem.sentence(),
|
|
94
102
|
* content: faker.lorem.paragraphs(),
|
|
95
103
|
* authorId: attrs.authorId || (await factory.insert('user')).id,
|
|
@@ -98,13 +106,18 @@ var ObjectionFactory = class extends Factory {
|
|
|
98
106
|
* );
|
|
99
107
|
* ```
|
|
100
108
|
*/
|
|
101
|
-
static createBuilder(ModelClass,
|
|
102
|
-
return async (attrs, factory, db,
|
|
109
|
+
static createBuilder(ModelClass, defaults, autoInsert) {
|
|
110
|
+
return async (attrs, factory, db, fakerInstance) => {
|
|
103
111
|
let data = { ...attrs };
|
|
104
|
-
if (
|
|
105
|
-
const
|
|
112
|
+
if (defaults) {
|
|
113
|
+
const defaultValues = await defaults({
|
|
114
|
+
attrs,
|
|
115
|
+
factory,
|
|
116
|
+
db,
|
|
117
|
+
faker: fakerInstance
|
|
118
|
+
});
|
|
106
119
|
data = {
|
|
107
|
-
...
|
|
120
|
+
...defaultValues,
|
|
108
121
|
...data
|
|
109
122
|
};
|
|
110
123
|
}
|
|
@@ -234,4 +247,4 @@ var ObjectionFactory = class extends Factory {
|
|
|
234
247
|
|
|
235
248
|
//#endregion
|
|
236
249
|
export { ObjectionFactory };
|
|
237
|
-
//# sourceMappingURL=ObjectionFactory-
|
|
250
|
+
//# sourceMappingURL=ObjectionFactory-zf2fLKrL.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ObjectionFactory-zf2fLKrL.mjs","names":["seedFn: Seed","ModelClass: TModel","defaults?: (context: {\n attrs: Attrs;\n factory: Factory;\n db: Knex;\n faker: FakerFactory;\n }) =>\n | Partial<InstanceType<TModel>>\n | Promise<Partial<InstanceType<TModel>>>","autoInsert?: boolean","attrs: Attrs","factory: Factory","db: Knex","fakerInstance: FakerFactory","data: Partial<InstanceType<TModel>>","builders: Builders","seeds: Seeds","builderName: K","attrs?: Parameters<Builders[K]>[0]","count: number","attrs?: any","records: any[]","record: any","seedName: K","attrs?: Parameters<Seeds[K]>[0]"],"sources":["../src/ObjectionFactory.ts"],"sourcesContent":["import type { Knex } from 'knex';\nimport type { Model } from 'objection';\nimport { Factory, type FactorySeed } from './Factory.ts';\nimport { type FakerFactory, faker } from './faker.ts';\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 Builders extends Record<string, any>,\n Seeds extends Record<string, any>,\n> extends Factory<Builders, Seeds> {\n /**\n * Creates a typed seed function with proper type inference.\n * Inherits from the base Factory class implementation.\n *\n * @template Seed - The seed function type\n * @param seedFn - The seed function to wrap\n * @returns The same seed function with proper typing\n */\n static createSeed<Seed extends FactorySeed>(seedFn: Seed): Seed {\n return Factory.createSeed(seedFn);\n }\n\n /**\n * Creates a typed builder function for Objection.js models.\n * This is a utility method that helps create builders with proper type inference.\n *\n * @template TModel - The Objection.js Model class type\n * @template Attrs - The attributes type for the builder (defaults to Partial of model)\n * @template Factory - The factory instance type\n * @template Result - The result type (defaults to the model instance)\n *\n * @param ModelClass - The Objection.js Model class\n * @param defaults - Optional function to provide default values (receives destructured context)\n * @param autoInsert - Whether to automatically insert the record (default: true)\n * @returns A builder function that creates and optionally inserts records\n *\n * @example\n * ```typescript\n * // Create a simple builder with defaults - destructure only what you need\n * const userBuilder = ObjectionFactory.createBuilder(User,\n * ({ attrs, faker }) => ({\n * id: faker.string.uuid(),\n * name: faker.person.fullName(),\n * email: faker.internet.email(),\n * createdAt: new Date(),\n * ...attrs\n * })\n * );\n *\n * // Only need faker? Just destructure that\n * const leaveTypeBuilder = ObjectionFactory.createBuilder(LeaveType,\n * ({ faker }) => ({\n * name: faker.helpers.arrayElement(['Annual', 'Sick', 'Maternity']),\n * code: faker.string.alpha({ length: 3, casing: 'upper' }),\n * })\n * );\n *\n * // Create a builder that doesn't auto-insert (useful for nested inserts)\n * const addressBuilder = ObjectionFactory.createBuilder(Address,\n * ({ attrs }) => ({\n * street: '123 Main St',\n * city: 'Anytown',\n * ...attrs\n * }),\n * false // Don't auto-insert\n * );\n *\n * // Use with relations\n * const postBuilder = ObjectionFactory.createBuilder(Post,\n * async ({ attrs, factory, faker }) => ({\n * title: faker.lorem.sentence(),\n * content: faker.lorem.paragraphs(),\n * authorId: attrs.authorId || (await factory.insert('user')).id,\n * ...attrs\n * })\n * );\n * ```\n */\n static createBuilder<\n TModel extends typeof Model,\n Attrs extends Partial<InstanceType<TModel>> = Partial<InstanceType<TModel>>,\n Factory = any,\n Result = InstanceType<TModel>,\n >(\n ModelClass: TModel,\n defaults?: (context: {\n attrs: Attrs;\n factory: Factory;\n db: Knex;\n faker: FakerFactory;\n }) =>\n | Partial<InstanceType<TModel>>\n | Promise<Partial<InstanceType<TModel>>>,\n autoInsert?: boolean,\n ): (\n attrs: Attrs,\n factory: Factory,\n db: Knex,\n faker: FakerFactory,\n ) => Promise<Result> {\n return async (\n attrs: Attrs,\n factory: Factory,\n db: Knex,\n fakerInstance: FakerFactory,\n ) => {\n // Start with attributes\n let data: Partial<InstanceType<TModel>> = { ...attrs };\n\n // Apply defaults\n if (defaults) {\n const defaultValues = await defaults({\n attrs,\n factory,\n db,\n faker: fakerInstance,\n });\n data = { ...defaultValues, ...data };\n }\n\n // Create model instance\n const model = ModelClass.fromJson(data) as InstanceType<TModel>;\n\n // Handle insertion based on autoInsert flag\n if (autoInsert !== false) {\n // Auto insert is enabled by default\n // Extract only defined values for insertion\n const insertData = Object.entries(model).reduce((acc, [key, value]) => {\n if (value !== undefined && key !== 'id') {\n acc[key] = value;\n }\n return acc;\n }, {} as any);\n\n // Use static query method to insert data directly\n // @ts-ignore\n const result = await ModelClass.query(db).insert(insertData);\n return result as Result;\n } else {\n // Return model for factory to handle insertion\n return model as Result;\n }\n };\n }\n\n /**\n * Creates a new ObjectionFactory instance.\n *\n * @param builders - Record of builder functions for creating individual entities\n * @param seeds - Record of seed functions for creating complex test scenarios\n * @param db - Knex database connection instance\n */\n constructor(\n private builders: Builders,\n private seeds: Seeds,\n private db: Knex,\n ) {\n super();\n }\n\n /**\n * Inserts a single record into the database using the specified builder.\n * Uses Objection.js's insertGraph method to handle nested relations.\n *\n * @template K - The builder name (must be a key of Builders)\n * @param builderName - The name of the builder to use\n * @param attrs - Optional attributes to override builder defaults\n * @returns A promise resolving to the inserted record with all relations\n * @throws Error if the specified builder doesn't exist\n *\n * @example\n * ```typescript\n * // Insert with defaults\n * const user = await factory.insert('user');\n *\n * // Insert with overrides\n * const adminUser = await factory.insert('user', {\n * email: 'admin@example.com',\n * role: 'admin'\n * });\n *\n * // Insert with nested relations\n * const userWithProfile = await factory.insert('user', {\n * name: 'John Doe',\n * profile: {\n * bio: 'Software Developer',\n * avatar: 'avatar.jpg'\n * }\n * });\n * ```\n */\n async insert<K extends keyof Builders>(\n builderName: K,\n attrs?: Parameters<Builders[K]>[0],\n ): Promise<Awaited<ReturnType<Builders[K]>>> {\n if (!(builderName in this.builders)) {\n throw new Error(\n `Factory \"${\n builderName as string\n }\" does not exist. Make sure it is correct and registered in src/test/setup.ts`,\n );\n }\n\n const result = await this.builders[builderName](\n attrs || {},\n this,\n this.db,\n faker,\n );\n\n // If the builder returns a model instance, insert it\n if (result && typeof result.$query === 'function') {\n // Extract data from model, excluding undefined values and id\n const insertData = Object.entries(result).reduce((acc, [key, value]) => {\n if (value !== undefined && key !== 'id') {\n acc[key] = value;\n }\n return acc;\n }, {} as any);\n\n // Use the model's constructor to get the query builder\n return await result.constructor.query(this.db).insert(insertData);\n }\n\n // Otherwise, assume the builder handled insertion itself\n return result;\n }\n /**\n * Inserts multiple records into the database using the specified builder.\n * Supports both static attributes and dynamic attribute generation via a function.\n *\n * @param count - The number of records to insert\n * @param builderName - The name of the builder to use\n * @param attrs - Static attributes or a function that generates attributes for each record\n * @returns A promise resolving to an array of inserted records\n * @throws Error if the specified builder doesn't exist\n *\n * @example\n * ```typescript\n * // Insert multiple with same attributes\n * const users = await factory.insertMany(5, 'user', { role: 'member' });\n *\n * // Insert multiple with dynamic attributes\n * const posts = await factory.insertMany(10, 'post', (idx) => ({\n * title: `Post ${idx + 1}`,\n * content: `Content for post ${idx + 1}`,\n * publishedAt: new Date()\n * }));\n *\n * // Create users with sequential emails\n * const admins = await factory.insertMany(3, 'user', (idx) => ({\n * email: `admin${idx + 1}@example.com`,\n * role: 'admin'\n * }));\n * ```\n */\n // Method overloads for better type inference\n async insertMany<K extends keyof Builders>(\n count: number,\n builderName: K,\n attrs?: Parameters<Builders[K]>[0],\n ): Promise<Awaited<ReturnType<Builders[K]>>[]>;\n async insertMany<K extends keyof Builders>(\n count: number,\n builderName: K,\n attrs: (idx: number, faker: FakerFactory) => Parameters<Builders[K]>[0],\n ): Promise<Awaited<ReturnType<Builders[K]>>[]>;\n async insertMany<K extends keyof Builders>(\n count: number,\n builderName: K,\n attrs?: any,\n ): Promise<Awaited<ReturnType<Builders[K]>>[]> {\n if (!(builderName in this.builders)) {\n throw new Error(\n `Builder \"${\n builderName as string\n }\" is not registered in this factory. Make sure it is correct and registered in src/test/setup.ts`,\n );\n }\n\n const records: any[] = [];\n for (let i = 0; i < count; i++) {\n const newAttrs =\n typeof attrs === 'function' ? await (attrs as any)(i, faker) : attrs;\n\n records.push(\n this.builders[builderName](newAttrs, this, this.db, faker).then(\n (record: any) => {\n // If the builder returns a model instance, insert it\n if (record && typeof record.$query === 'function') {\n // Extract data from model, excluding undefined values and id\n const insertData = Object.entries(record).reduce(\n (acc, [key, value]) => {\n if (value !== undefined && key !== 'id') {\n acc[key] = value;\n }\n return acc;\n },\n {} as any,\n );\n\n // Use the model's constructor to get the query builder\n return record.constructor.query(this.db).insert(insertData);\n }\n // Otherwise, assume the builder handled insertion itself\n return record;\n },\n ),\n );\n }\n\n return Promise.all(records);\n }\n /**\n * Executes a seed function to create complex test scenarios with multiple related records.\n * Seeds are useful for setting up complete test environments with realistic data relationships.\n *\n * @template K - The seed name (must be a key of Seeds)\n * @param seedName - The name of the seed to execute\n * @param attrs - Optional configuration attributes for the seed\n * @returns The result of the seed function (typically the primary record created)\n * @throws Error if the specified seed doesn't exist\n *\n * @example\n * ```typescript\n * // Execute a simple seed\n * const user = await factory.seed('userWithProfile');\n *\n * // Execute a seed with configuration\n * const author = await factory.seed('authorWithBooks', {\n * bookCount: 5,\n * includeReviews: true\n * });\n *\n * // Use seed result in tests with Objection.js relations\n * const company = await factory.seed('companyWithDepartments', {\n * departmentCount: 3,\n * employeesPerDepartment: 10\n * });\n *\n * // Access eager loaded relations\n * const companyWithRelations = await Company.query()\n * .findById(company.id)\n * .withGraphFetched('[departments.employees]');\n * ```\n */\n seed<K extends keyof Seeds>(\n seedName: K,\n attrs?: Parameters<Seeds[K]>[0],\n ): ReturnType<Seeds[K]> {\n if (!(seedName in this.seeds)) {\n throw new Error(\n `Seed \"${\n seedName as string\n }\" is not registered in this factory. Make sure it is correct and registered in src/test/setup.ts`,\n );\n }\n\n return this.seeds[seedName](attrs || {}, this, this.db);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCA,IAAa,mBAAb,cAGU,QAAyB;;;;;;;;;CASjC,OAAO,WAAqCA,QAAoB;AAC9D,SAAO,QAAQ,WAAW,OAAO;CAClC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA0DD,OAAO,cAMLC,YACAC,UAQAC,YAMmB;AACnB,SAAO,OACLC,OACAC,SACAC,IACAC,kBACG;GAEH,IAAIC,OAAsC,EAAE,GAAG,MAAO;AAGtD,OAAI,UAAU;IACZ,MAAM,gBAAgB,MAAM,SAAS;KACnC;KACA;KACA;KACA,OAAO;IACR,EAAC;AACF,WAAO;KAAE,GAAG;KAAe,GAAG;IAAM;GACrC;GAGD,MAAM,QAAQ,WAAW,SAAS,KAAK;AAGvC,OAAI,eAAe,OAAO;IAGxB,MAAM,aAAa,OAAO,QAAQ,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,MAAM,KAAK;AACrE,SAAI,oBAAuB,QAAQ,KACjC,KAAI,OAAO;AAEb,YAAO;IACR,GAAE,CAAE,EAAQ;IAIb,MAAM,SAAS,MAAM,WAAW,MAAM,GAAG,CAAC,OAAO,WAAW;AAC5D,WAAO;GACR,MAEC,QAAO;EAEV;CACF;;;;;;;;CASD,YACUC,UACAC,OACAJ,IACR;AACA,SAAO;EAJC;EACA;EACA;CAGT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiCD,MAAM,OACJK,aACAC,OAC2C;AAC3C,QAAM,eAAe,KAAK,UACxB,OAAM,IAAI,OACP,WACC,YACD;EAIL,MAAM,SAAS,MAAM,KAAK,SAAS,aACjC,SAAS,CAAE,GACX,MACA,KAAK,IACL,MACD;AAGD,MAAI,iBAAiB,OAAO,WAAW,YAAY;GAEjD,MAAM,aAAa,OAAO,QAAQ,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,MAAM,KAAK;AACtE,QAAI,oBAAuB,QAAQ,KACjC,KAAI,OAAO;AAEb,WAAO;GACR,GAAE,CAAE,EAAQ;AAGb,UAAO,MAAM,OAAO,YAAY,MAAM,KAAK,GAAG,CAAC,OAAO,WAAW;EAClE;AAGD,SAAO;CACR;CAyCD,MAAM,WACJC,OACAF,aACAG,OAC6C;AAC7C,QAAM,eAAe,KAAK,UACxB,OAAM,IAAI,OACP,WACC,YACD;EAIL,MAAMC,UAAiB,CAAE;AACzB,OAAK,IAAI,IAAI,GAAG,IAAI,OAAO,KAAK;GAC9B,MAAM,kBACG,UAAU,aAAa,MAAM,AAAC,MAAc,GAAG,MAAM,GAAG;AAEjE,WAAQ,KACN,KAAK,SAAS,aAAa,UAAU,MAAM,KAAK,IAAI,MAAM,CAAC,KACzD,CAACC,WAAgB;AAEf,QAAI,iBAAiB,OAAO,WAAW,YAAY;KAEjD,MAAM,aAAa,OAAO,QAAQ,OAAO,CAAC,OACxC,CAAC,KAAK,CAAC,KAAK,MAAM,KAAK;AACrB,UAAI,oBAAuB,QAAQ,KACjC,KAAI,OAAO;AAEb,aAAO;KACR,GACD,CAAE,EACH;AAGD,YAAO,OAAO,YAAY,MAAM,KAAK,GAAG,CAAC,OAAO,WAAW;IAC5D;AAED,WAAO;GACR,EACF,CACF;EACF;AAED,SAAO,QAAQ,IAAI,QAAQ;CAC5B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkCD,KACEC,UACAC,OACsB;AACtB,QAAM,YAAY,KAAK,OACrB,OAAM,IAAI,OACP,QACC,SACD;AAIL,SAAO,KAAK,MAAM,UAAU,SAAS,CAAE,GAAE,MAAM,KAAK,GAAG;CACxD;AACF"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
require('./Factory-WMhTNZ9S.cjs');
|
|
2
2
|
require('./faker-B14IEMIN.cjs');
|
|
3
|
-
const require_ObjectionFactory = require('./ObjectionFactory-
|
|
3
|
+
const require_ObjectionFactory = require('./ObjectionFactory-Eb04AOnv.cjs');
|
|
4
4
|
|
|
5
5
|
exports.ObjectionFactory = require_ObjectionFactory.ObjectionFactory;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import "./faker-
|
|
2
|
-
import "./Factory-
|
|
3
|
-
import { ObjectionFactory } from "./ObjectionFactory-
|
|
1
|
+
import "./faker-BSH1EMtg.cjs";
|
|
2
|
+
import "./Factory-pNV7ZQ7-.cjs";
|
|
3
|
+
import { ObjectionFactory } from "./ObjectionFactory-BwpN4gMX.cjs";
|
|
4
4
|
export { ObjectionFactory };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import "./faker-
|
|
2
|
-
import "./Factory-
|
|
3
|
-
import { ObjectionFactory } from "./ObjectionFactory-
|
|
1
|
+
import "./faker-D9gz7KjY.mjs";
|
|
2
|
+
import "./Factory-DE3hE0WO.mjs";
|
|
3
|
+
import { ObjectionFactory } from "./ObjectionFactory-BlBicEia.mjs";
|
|
4
4
|
export { ObjectionFactory };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IsolationLevel, VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-
|
|
1
|
+
import { IsolationLevel, VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-DfA80g2M.cjs";
|
|
2
2
|
import { Kysely, Transaction } from "kysely";
|
|
3
3
|
|
|
4
4
|
//#region src/VitestKyselyTransactionIsolator.d.ts
|
|
@@ -55,4 +55,4 @@ declare class VitestKyselyTransactionIsolator<Database> extends VitestPostgresTr
|
|
|
55
55
|
}
|
|
56
56
|
//#endregion
|
|
57
57
|
export { VitestKyselyTransactionIsolator };
|
|
58
|
-
//# sourceMappingURL=VitestKyselyTransactionIsolator-
|
|
58
|
+
//# sourceMappingURL=VitestKyselyTransactionIsolator-BqrZDeaT.d.cts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-
|
|
1
|
+
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-BKIrj3Uy.cjs');
|
|
2
2
|
|
|
3
3
|
//#region src/VitestKyselyTransactionIsolator.ts
|
|
4
4
|
/**
|
|
@@ -62,4 +62,4 @@ Object.defineProperty(exports, 'VitestKyselyTransactionIsolator', {
|
|
|
62
62
|
return VitestKyselyTransactionIsolator;
|
|
63
63
|
}
|
|
64
64
|
});
|
|
65
|
-
//# sourceMappingURL=VitestKyselyTransactionIsolator-
|
|
65
|
+
//# sourceMappingURL=VitestKyselyTransactionIsolator-DX_VPKS-.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"VitestKyselyTransactionIsolator-
|
|
1
|
+
{"version":3,"file":"VitestKyselyTransactionIsolator-DX_VPKS-.cjs","names":["VitestPostgresTransactionIsolator","conn: Kysely<Database>","level: IsolationLevel","fn: (trx: Transaction<Database>) => Promise<void>"],"sources":["../src/VitestKyselyTransactionIsolator.ts"],"sourcesContent":["import type { Kysely, Transaction } from 'kysely';\nimport {\n type IsolationLevel,\n VitestPostgresTransactionIsolator,\n} from './VitestTransactionIsolator';\n\n/**\n * Kysely-specific implementation of the Vitest transaction isolator.\n * Provides automatic transaction rollback for test isolation using Kysely's transaction API.\n * Each test runs within a database transaction that is rolled back after completion,\n * ensuring a clean state between tests without the overhead of recreating data.\n *\n * @template Database - The database schema type\n *\n * @example\n * ```typescript\n * import { VitestKyselyTransactionIsolator } from '@geekmidas/testkit';\n * import { db } from './database';\n *\n * // Create isolator instance\n * const isolator = new VitestKyselyTransactionIsolator<Database>();\n *\n * // In your test setup\n * beforeEach(async () => {\n * await isolator.start(db);\n * });\n *\n * afterEach(async () => {\n * await isolator.rollback();\n * });\n *\n * // Tests run in isolated transactions\n * it('should create user', async () => {\n * const user = await db.insertInto('users')\n * .values({ name: 'Test User' })\n * .returningAll()\n * .executeTakeFirst();\n *\n * expect(user).toBeDefined();\n * // This data will be rolled back after the test\n * });\n * ```\n */\nexport class VitestKyselyTransactionIsolator<\n Database,\n> extends VitestPostgresTransactionIsolator<\n Kysely<Database>,\n Transaction<Database>\n> {\n async destroy(conn: Kysely<Database>): Promise<void> {\n // return conn.destroy();\n }\n /**\n * Creates a Kysely transaction with the specified isolation level.\n * Implements the abstract transact method from VitestPostgresTransactionIsolator.\n *\n * @param conn - The Kysely database connection\n * @param level - The transaction isolation level\n * @param fn - The function to execute within the transaction\n * @returns Promise that resolves when the transaction completes\n */\n async transact(\n conn: Kysely<Database>,\n level: IsolationLevel,\n fn: (trx: Transaction<Database>) => Promise<void>,\n ): Promise<void> {\n const isolationLevel =\n level.toLocaleLowerCase() as Lowercase<IsolationLevel>;\n await conn.transaction().setIsolationLevel(isolationLevel).execute(fn);\n }\n // Implement any Kysely-specific transaction logic here\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,IAAa,kCAAb,cAEUA,oEAGR;CACA,MAAM,QAAQC,MAAuC,CAEpD;;;;;;;;;;CAUD,MAAM,SACJA,MACAC,OACAC,IACe;EACf,MAAM,iBACJ,MAAM,mBAAmB;AAC3B,QAAM,KAAK,aAAa,CAAC,kBAAkB,eAAe,CAAC,QAAQ,GAAG;CACvE;AAEF"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IsolationLevel, VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-
|
|
1
|
+
import { IsolationLevel, VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-DLdQlfZ5.mjs";
|
|
2
2
|
import { Kysely, Transaction } from "kysely";
|
|
3
3
|
|
|
4
4
|
//#region src/VitestKyselyTransactionIsolator.d.ts
|
|
@@ -55,4 +55,4 @@ declare class VitestKyselyTransactionIsolator<Database> extends VitestPostgresTr
|
|
|
55
55
|
}
|
|
56
56
|
//#endregion
|
|
57
57
|
export { VitestKyselyTransactionIsolator };
|
|
58
|
-
//# sourceMappingURL=VitestKyselyTransactionIsolator-
|
|
58
|
+
//# sourceMappingURL=VitestKyselyTransactionIsolator-DiskaURs.d.mts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-
|
|
1
|
+
import { VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-BIaMs4c2.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/VitestKyselyTransactionIsolator.ts
|
|
4
4
|
/**
|
|
@@ -57,4 +57,4 @@ var VitestKyselyTransactionIsolator = class extends VitestPostgresTransactionIso
|
|
|
57
57
|
|
|
58
58
|
//#endregion
|
|
59
59
|
export { VitestKyselyTransactionIsolator };
|
|
60
|
-
//# sourceMappingURL=VitestKyselyTransactionIsolator-
|
|
60
|
+
//# sourceMappingURL=VitestKyselyTransactionIsolator-XDL3ngs_.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"VitestKyselyTransactionIsolator-
|
|
1
|
+
{"version":3,"file":"VitestKyselyTransactionIsolator-XDL3ngs_.mjs","names":["conn: Kysely<Database>","level: IsolationLevel","fn: (trx: Transaction<Database>) => Promise<void>"],"sources":["../src/VitestKyselyTransactionIsolator.ts"],"sourcesContent":["import type { Kysely, Transaction } from 'kysely';\nimport {\n type IsolationLevel,\n VitestPostgresTransactionIsolator,\n} from './VitestTransactionIsolator';\n\n/**\n * Kysely-specific implementation of the Vitest transaction isolator.\n * Provides automatic transaction rollback for test isolation using Kysely's transaction API.\n * Each test runs within a database transaction that is rolled back after completion,\n * ensuring a clean state between tests without the overhead of recreating data.\n *\n * @template Database - The database schema type\n *\n * @example\n * ```typescript\n * import { VitestKyselyTransactionIsolator } from '@geekmidas/testkit';\n * import { db } from './database';\n *\n * // Create isolator instance\n * const isolator = new VitestKyselyTransactionIsolator<Database>();\n *\n * // In your test setup\n * beforeEach(async () => {\n * await isolator.start(db);\n * });\n *\n * afterEach(async () => {\n * await isolator.rollback();\n * });\n *\n * // Tests run in isolated transactions\n * it('should create user', async () => {\n * const user = await db.insertInto('users')\n * .values({ name: 'Test User' })\n * .returningAll()\n * .executeTakeFirst();\n *\n * expect(user).toBeDefined();\n * // This data will be rolled back after the test\n * });\n * ```\n */\nexport class VitestKyselyTransactionIsolator<\n Database,\n> extends VitestPostgresTransactionIsolator<\n Kysely<Database>,\n Transaction<Database>\n> {\n async destroy(conn: Kysely<Database>): Promise<void> {\n // return conn.destroy();\n }\n /**\n * Creates a Kysely transaction with the specified isolation level.\n * Implements the abstract transact method from VitestPostgresTransactionIsolator.\n *\n * @param conn - The Kysely database connection\n * @param level - The transaction isolation level\n * @param fn - The function to execute within the transaction\n * @returns Promise that resolves when the transaction completes\n */\n async transact(\n conn: Kysely<Database>,\n level: IsolationLevel,\n fn: (trx: Transaction<Database>) => Promise<void>,\n ): Promise<void> {\n const isolationLevel =\n level.toLocaleLowerCase() as Lowercase<IsolationLevel>;\n await conn.transaction().setIsolationLevel(isolationLevel).execute(fn);\n }\n // Implement any Kysely-specific transaction logic here\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,IAAa,kCAAb,cAEU,kCAGR;CACA,MAAM,QAAQA,MAAuC,CAEpD;;;;;;;;;;CAUD,MAAM,SACJA,MACAC,OACAC,IACe;EACf,MAAM,iBACJ,MAAM,mBAAmB;AAC3B,QAAM,KAAK,aAAa,CAAC,kBAAkB,eAAe,CAAC,QAAQ,GAAG;CACvE;AAEF"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require('./VitestTransactionIsolator-
|
|
2
|
-
const require_VitestKyselyTransactionIsolator = require('./VitestKyselyTransactionIsolator-
|
|
1
|
+
require('./VitestTransactionIsolator-BKIrj3Uy.cjs');
|
|
2
|
+
const require_VitestKyselyTransactionIsolator = require('./VitestKyselyTransactionIsolator-DX_VPKS-.cjs');
|
|
3
3
|
|
|
4
4
|
exports.VitestKyselyTransactionIsolator = require_VitestKyselyTransactionIsolator.VitestKyselyTransactionIsolator;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import "./VitestTransactionIsolator-
|
|
2
|
-
import { VitestKyselyTransactionIsolator } from "./VitestKyselyTransactionIsolator-
|
|
1
|
+
import "./VitestTransactionIsolator-DfA80g2M.cjs";
|
|
2
|
+
import { VitestKyselyTransactionIsolator } from "./VitestKyselyTransactionIsolator-BqrZDeaT.cjs";
|
|
3
3
|
export { VitestKyselyTransactionIsolator };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import "./VitestTransactionIsolator-
|
|
2
|
-
import { VitestKyselyTransactionIsolator } from "./VitestKyselyTransactionIsolator-
|
|
1
|
+
import "./VitestTransactionIsolator-DLdQlfZ5.mjs";
|
|
2
|
+
import { VitestKyselyTransactionIsolator } from "./VitestKyselyTransactionIsolator-DiskaURs.mjs";
|
|
3
3
|
export { VitestKyselyTransactionIsolator };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import "./VitestTransactionIsolator-
|
|
2
|
-
import { VitestKyselyTransactionIsolator } from "./VitestKyselyTransactionIsolator-
|
|
1
|
+
import "./VitestTransactionIsolator-BIaMs4c2.mjs";
|
|
2
|
+
import { VitestKyselyTransactionIsolator } from "./VitestKyselyTransactionIsolator-XDL3ngs_.mjs";
|
|
3
3
|
|
|
4
4
|
export { VitestKyselyTransactionIsolator };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IsolationLevel, VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-
|
|
1
|
+
import { IsolationLevel, VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-DfA80g2M.cjs";
|
|
2
2
|
import { Knex } from "knex";
|
|
3
3
|
|
|
4
4
|
//#region src/VitestObjectionTransactionIsolator.d.ts
|
|
@@ -56,4 +56,4 @@ declare class VitestObjectionTransactionIsolator extends VitestPostgresTransacti
|
|
|
56
56
|
}
|
|
57
57
|
//#endregion
|
|
58
58
|
export { VitestObjectionTransactionIsolator };
|
|
59
|
-
//# sourceMappingURL=VitestObjectionTransactionIsolator-
|
|
59
|
+
//# sourceMappingURL=VitestObjectionTransactionIsolator-CD2ucJpH.d.cts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-
|
|
1
|
+
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-BKIrj3Uy.cjs');
|
|
2
2
|
|
|
3
3
|
//#region src/VitestObjectionTransactionIsolator.ts
|
|
4
4
|
/**
|
|
@@ -65,4 +65,4 @@ Object.defineProperty(exports, 'VitestObjectionTransactionIsolator', {
|
|
|
65
65
|
return VitestObjectionTransactionIsolator;
|
|
66
66
|
}
|
|
67
67
|
});
|
|
68
|
-
//# sourceMappingURL=VitestObjectionTransactionIsolator-
|
|
68
|
+
//# sourceMappingURL=VitestObjectionTransactionIsolator-D_tlOtq8.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"VitestObjectionTransactionIsolator-
|
|
1
|
+
{"version":3,"file":"VitestObjectionTransactionIsolator-D_tlOtq8.cjs","names":["VitestPostgresTransactionIsolator","conn: Knex<any, any[]>","connection: Knex","level: IsolationLevel","fn: (trx: Knex.Transaction) => Promise<void>"],"sources":["../src/VitestObjectionTransactionIsolator.ts"],"sourcesContent":["import type { Knex } from 'knex';\nimport {\n type IsolationLevel,\n VitestPostgresTransactionIsolator,\n} from './VitestTransactionIsolator';\n\n/**\n * Objection.js-specific implementation of the Vitest transaction isolator.\n * Provides automatic transaction rollback for test isolation using Objection.js and Knex transaction API.\n * Each test runs within a database transaction that is rolled back after completion,\n * ensuring a clean state between tests without the overhead of recreating data.\n *\n * @example\n * ```typescript\n * import { VitestObjectionTransactionIsolator } from '@geekmidas/testkit';\n * import { knex } from './database';\n * import { User } from './models';\n * import { test } from 'vitest';\n *\n * // Create isolator instance\n * const isolator = new VitestObjectionTransactionIsolator(test);\n *\n * // Use with wrapped test API\n * const isolatedTest = isolator.wrapVitestWithTransaction(knex);\n *\n * isolatedTest('should create user', async ({ trx }) => {\n * const user = await User.query(trx)\n * .insert({ name: 'Test User' });\n *\n * expect(user).toBeDefined();\n * // This data will be rolled back after the test\n * });\n * ```\n */\nexport class VitestObjectionTransactionIsolator extends VitestPostgresTransactionIsolator<\n Knex,\n Knex.Transaction\n> {\n async destroy(conn: Knex<any, any[]>): Promise<void> {}\n /**\n * Creates a Knex transaction with the specified isolation level.\n * Implements the abstract transact method from VitestPostgresTransactionIsolator.\n * This transaction can be used with Objection.js models via Model.query(trx).\n *\n * @param conn - The Knex database connection\n * @param level - The transaction isolation level\n * @param fn - The function to execute within the transaction\n * @returns Promise that resolves when the transaction completes\n *\n * @example\n * ```typescript\n * await isolator.transact(knex, IsolationLevel.REPEATABLE_READ, async (trx) => {\n * // Use transaction with Objection models\n * await User.query(trx).insert({ name: 'Test' });\n * await Post.query(trx).where('userId', user.id).delete();\n * });\n * ```\n */\n async transact(\n connection: Knex,\n level: IsolationLevel,\n fn: (trx: Knex.Transaction) => Promise<void>,\n ): Promise<void> {\n const isolationLevel = level.toLowerCase() as Lowercase<IsolationLevel>;\n\n await connection.transaction(\n async (trx) => {\n await fn(trx);\n },\n {\n isolationLevel,\n },\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,IAAa,qCAAb,cAAwDA,oEAGtD;CACA,MAAM,QAAQC,MAAuC,CAAE;;;;;;;;;;;;;;;;;;;;CAoBvD,MAAM,SACJC,YACAC,OACAC,IACe;EACf,MAAM,iBAAiB,MAAM,aAAa;AAE1C,QAAM,WAAW,YACf,OAAO,QAAQ;AACb,SAAM,GAAG,IAAI;EACd,GACD,EACE,eACD,EACF;CACF;AACF"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IsolationLevel, VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-
|
|
1
|
+
import { IsolationLevel, VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-DLdQlfZ5.mjs";
|
|
2
2
|
import { Knex } from "knex";
|
|
3
3
|
|
|
4
4
|
//#region src/VitestObjectionTransactionIsolator.d.ts
|
|
@@ -56,4 +56,4 @@ declare class VitestObjectionTransactionIsolator extends VitestPostgresTransacti
|
|
|
56
56
|
}
|
|
57
57
|
//#endregion
|
|
58
58
|
export { VitestObjectionTransactionIsolator };
|
|
59
|
-
//# sourceMappingURL=VitestObjectionTransactionIsolator-
|
|
59
|
+
//# sourceMappingURL=VitestObjectionTransactionIsolator-DhQ8XGva.d.mts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-
|
|
1
|
+
import { VitestPostgresTransactionIsolator } from "./VitestTransactionIsolator-BIaMs4c2.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/VitestObjectionTransactionIsolator.ts
|
|
4
4
|
/**
|
|
@@ -60,4 +60,4 @@ var VitestObjectionTransactionIsolator = class extends VitestPostgresTransaction
|
|
|
60
60
|
|
|
61
61
|
//#endregion
|
|
62
62
|
export { VitestObjectionTransactionIsolator };
|
|
63
|
-
//# sourceMappingURL=VitestObjectionTransactionIsolator-
|
|
63
|
+
//# sourceMappingURL=VitestObjectionTransactionIsolator-_EhJKu_O.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"VitestObjectionTransactionIsolator-
|
|
1
|
+
{"version":3,"file":"VitestObjectionTransactionIsolator-_EhJKu_O.mjs","names":["conn: Knex<any, any[]>","connection: Knex","level: IsolationLevel","fn: (trx: Knex.Transaction) => Promise<void>"],"sources":["../src/VitestObjectionTransactionIsolator.ts"],"sourcesContent":["import type { Knex } from 'knex';\nimport {\n type IsolationLevel,\n VitestPostgresTransactionIsolator,\n} from './VitestTransactionIsolator';\n\n/**\n * Objection.js-specific implementation of the Vitest transaction isolator.\n * Provides automatic transaction rollback for test isolation using Objection.js and Knex transaction API.\n * Each test runs within a database transaction that is rolled back after completion,\n * ensuring a clean state between tests without the overhead of recreating data.\n *\n * @example\n * ```typescript\n * import { VitestObjectionTransactionIsolator } from '@geekmidas/testkit';\n * import { knex } from './database';\n * import { User } from './models';\n * import { test } from 'vitest';\n *\n * // Create isolator instance\n * const isolator = new VitestObjectionTransactionIsolator(test);\n *\n * // Use with wrapped test API\n * const isolatedTest = isolator.wrapVitestWithTransaction(knex);\n *\n * isolatedTest('should create user', async ({ trx }) => {\n * const user = await User.query(trx)\n * .insert({ name: 'Test User' });\n *\n * expect(user).toBeDefined();\n * // This data will be rolled back after the test\n * });\n * ```\n */\nexport class VitestObjectionTransactionIsolator extends VitestPostgresTransactionIsolator<\n Knex,\n Knex.Transaction\n> {\n async destroy(conn: Knex<any, any[]>): Promise<void> {}\n /**\n * Creates a Knex transaction with the specified isolation level.\n * Implements the abstract transact method from VitestPostgresTransactionIsolator.\n * This transaction can be used with Objection.js models via Model.query(trx).\n *\n * @param conn - The Knex database connection\n * @param level - The transaction isolation level\n * @param fn - The function to execute within the transaction\n * @returns Promise that resolves when the transaction completes\n *\n * @example\n * ```typescript\n * await isolator.transact(knex, IsolationLevel.REPEATABLE_READ, async (trx) => {\n * // Use transaction with Objection models\n * await User.query(trx).insert({ name: 'Test' });\n * await Post.query(trx).where('userId', user.id).delete();\n * });\n * ```\n */\n async transact(\n connection: Knex,\n level: IsolationLevel,\n fn: (trx: Knex.Transaction) => Promise<void>,\n ): Promise<void> {\n const isolationLevel = level.toLowerCase() as Lowercase<IsolationLevel>;\n\n await connection.transaction(\n async (trx) => {\n await fn(trx);\n },\n {\n isolationLevel,\n },\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,IAAa,qCAAb,cAAwD,kCAGtD;CACA,MAAM,QAAQA,MAAuC,CAAE;;;;;;;;;;;;;;;;;;;;CAoBvD,MAAM,SACJC,YACAC,OACAC,IACe;EACf,MAAM,iBAAiB,MAAM,aAAa;AAE1C,QAAM,WAAW,YACf,OAAO,QAAQ;AACb,SAAM,GAAG,IAAI;EACd,GACD,EACE,eACD,EACF;CACF;AACF"}
|