@geekmidas/testkit 0.0.10 → 0.0.12
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/dist/Factory-Bm44VKa-.d.cts +131 -0
- package/dist/Factory-tjCDNgUK.d.mts +131 -0
- package/dist/Factory.d.cts +3 -0
- package/dist/Factory.d.mts +3 -0
- package/dist/KyselyFactory-BoPDDitt.d.cts +200 -0
- package/dist/{KyselyFactory-ELiHgHVv.mjs → KyselyFactory-C3Bc3p4L.mjs} +1 -1
- package/dist/{KyselyFactory-Bdq1s1Go.cjs → KyselyFactory-CXtfmMfK.cjs} +1 -1
- package/dist/KyselyFactory-D82j74t9.d.mts +200 -0
- package/dist/KyselyFactory.cjs +2 -2
- package/dist/KyselyFactory.d.cts +4 -0
- package/dist/KyselyFactory.d.mts +4 -0
- package/dist/KyselyFactory.mjs +2 -2
- package/dist/ObjectionFactory-BWMTXsxH.d.cts +213 -0
- package/dist/ObjectionFactory-CEG5qUrm.d.mts +213 -0
- package/dist/{ObjectionFactory-C47B03Ot.cjs → ObjectionFactory-DxIxJagq.cjs} +82 -40
- package/dist/{ObjectionFactory-89p-FFEw.mjs → ObjectionFactory-qIICOph3.mjs} +82 -40
- package/dist/ObjectionFactory.cjs +2 -1
- package/dist/ObjectionFactory.d.cts +4 -0
- package/dist/ObjectionFactory.d.mts +4 -0
- package/dist/ObjectionFactory.mjs +2 -1
- package/dist/PostgresKyselyMigrator-CQ3aUoy_.d.cts +67 -0
- package/dist/PostgresKyselyMigrator-_6yHZigp.d.mts +67 -0
- package/dist/PostgresKyselyMigrator.d.cts +3 -0
- package/dist/PostgresKyselyMigrator.d.mts +3 -0
- package/dist/PostgresMigrator-BlvuQl7d.d.mts +84 -0
- package/dist/PostgresMigrator-D5UkK1_K.d.cts +84 -0
- package/dist/PostgresMigrator.d.cts +2 -0
- package/dist/PostgresMigrator.d.mts +2 -0
- package/dist/PostgresObjectionMigrator-C69n7vzr.d.mts +77 -0
- package/dist/PostgresObjectionMigrator-CZHHcCOv.d.cts +77 -0
- package/dist/PostgresObjectionMigrator.d.cts +3 -0
- package/dist/PostgresObjectionMigrator.d.mts +3 -0
- package/dist/VitestKyselyTransactionIsolator-ClCazkBO.d.mts +56 -0
- package/dist/VitestKyselyTransactionIsolator-UE1J-UoP.d.cts +56 -0
- package/dist/VitestKyselyTransactionIsolator.d.cts +3 -0
- package/dist/VitestKyselyTransactionIsolator.d.mts +3 -0
- package/dist/VitestObjectionTransactionIsolator-CO2nTi9r.d.cts +57 -0
- package/dist/VitestObjectionTransactionIsolator-D264iuPy.d.mts +57 -0
- package/dist/VitestObjectionTransactionIsolator.d.cts +3 -0
- package/dist/VitestObjectionTransactionIsolator.d.mts +3 -0
- package/dist/VitestTransactionIsolator-DHf2MxmC.d.cts +118 -0
- package/dist/VitestTransactionIsolator-Xqyjlmw6.d.mts +118 -0
- package/dist/VitestTransactionIsolator.d.cts +2 -0
- package/dist/VitestTransactionIsolator.d.mts +2 -0
- package/dist/__tests__/Factory.spec.d.cts +1 -0
- package/dist/__tests__/Factory.spec.d.mts +1 -0
- package/dist/__tests__/KyselyFactory.spec.cjs +2 -2
- package/dist/__tests__/KyselyFactory.spec.d.cts +1 -0
- package/dist/__tests__/KyselyFactory.spec.d.mts +1 -0
- package/dist/__tests__/KyselyFactory.spec.mjs +2 -2
- package/dist/__tests__/ObjectionFactory.spec.cjs +117 -1
- package/dist/__tests__/ObjectionFactory.spec.d.cts +1 -0
- package/dist/__tests__/ObjectionFactory.spec.d.mts +1 -0
- package/dist/__tests__/ObjectionFactory.spec.mjs +117 -1
- package/dist/__tests__/PostgresMigrator.spec.d.cts +1 -0
- package/dist/__tests__/PostgresMigrator.spec.d.mts +1 -0
- package/dist/__tests__/PostgresObjectionMigrator.spec.d.cts +1 -0
- package/dist/__tests__/PostgresObjectionMigrator.spec.d.mts +1 -0
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.cjs +2 -1
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.d.cts +1 -0
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.d.mts +1 -0
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.mjs +2 -1
- package/dist/__tests__/faker.spec.cjs +1 -1
- package/dist/__tests__/faker.spec.d.cts +1 -0
- package/dist/__tests__/faker.spec.d.mts +1 -0
- package/dist/__tests__/faker.spec.mjs +1 -1
- package/dist/__tests__/integration.spec.cjs +2 -2
- package/dist/__tests__/integration.spec.d.cts +1 -0
- package/dist/__tests__/integration.spec.d.mts +1 -0
- package/dist/__tests__/integration.spec.mjs +2 -2
- package/dist/example.cjs +2 -2
- package/dist/example.d.cts +26 -0
- package/dist/example.d.mts +26 -0
- package/dist/example.mjs +2 -2
- package/dist/{faker-SMN4ira4.cjs → faker-B14IEMIN.cjs} +41 -1
- package/dist/{faker-CxKkEeYi.mjs → faker-BGKYFoCT.mjs} +36 -2
- package/dist/faker-ChuHaYMR.d.mts +165 -0
- package/dist/faker-km9UhOS6.d.cts +165 -0
- package/dist/faker.cjs +2 -1
- package/dist/faker.d.cts +2 -0
- package/dist/faker.d.mts +2 -0
- package/dist/faker.mjs +2 -2
- package/dist/helpers.d.cts +41 -0
- package/dist/helpers.d.mts +41 -0
- package/dist/kysely.cjs +2 -2
- package/dist/kysely.d.cts +73 -0
- package/dist/kysely.d.mts +73 -0
- package/dist/kysely.mjs +2 -2
- package/dist/objection.cjs +2 -1
- package/dist/objection.d.cts +88 -0
- package/dist/objection.d.mts +88 -0
- package/dist/objection.mjs +2 -1
- package/package.json +5 -5
- package/src/ObjectionFactory.ts +156 -13
- package/src/__tests__/ObjectionFactory.spec.ts +162 -0
- package/src/faker.ts +86 -0
- package/PostgresKyselyMigrator.spec +0 -471
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { FakerFactory } from "./faker-km9UhOS6.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/Factory.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Abstract base class for database factories used in testing.
|
|
7
|
+
* Provides a standardized interface for creating test data using builder and seed patterns.
|
|
8
|
+
*
|
|
9
|
+
* @template Builders - Record of builder functions for creating individual entities
|
|
10
|
+
* @template Seeds - Record of seed functions for creating complex test scenarios
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Define builders for creating individual records
|
|
15
|
+
* const builders = {
|
|
16
|
+
* user: (attrs) => ({ name: 'Test User', email: 'test@example.com', ...attrs }),
|
|
17
|
+
* post: (attrs) => ({ title: 'Test Post', content: 'Content', ...attrs })
|
|
18
|
+
* };
|
|
19
|
+
*
|
|
20
|
+
* // Define seeds for complex scenarios
|
|
21
|
+
* const seeds = {
|
|
22
|
+
* userWithPosts: async (attrs, factory) => {
|
|
23
|
+
* const user = await factory.insert('user', attrs);
|
|
24
|
+
* await factory.insertMany(3, 'post', { userId: user.id });
|
|
25
|
+
* return user;
|
|
26
|
+
* }
|
|
27
|
+
* };
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
declare abstract class Factory<Builders extends Record<string, any>, Seeds extends Record<string, any>> {
|
|
31
|
+
/**
|
|
32
|
+
* Creates a typed seed function with proper type inference.
|
|
33
|
+
* This is a utility method to help with TypeScript type checking when defining seeds.
|
|
34
|
+
*
|
|
35
|
+
* @template Seed - The seed function type
|
|
36
|
+
* @param seedFn - The seed function to wrap
|
|
37
|
+
* @returns The same seed function with proper typing
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const userWithPostsSeed = Factory.createSeed(async (attrs, factory, db) => {
|
|
42
|
+
* const user = await factory.insert('user', attrs);
|
|
43
|
+
* return user;
|
|
44
|
+
* });
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
static createSeed<Seed extends FactorySeed>(seedFn: Seed): Seed;
|
|
48
|
+
/**
|
|
49
|
+
* Inserts an object into the database using a builder function.
|
|
50
|
+
*
|
|
51
|
+
* @param builderName - The name of the builder to use
|
|
52
|
+
* @param attrs - The attributes to insert
|
|
53
|
+
*/
|
|
54
|
+
abstract insert<K extends keyof Builders>(builderName: K, attrs?: Parameters<Builders[K]>[0]): Promise<Awaited<ReturnType<Builders[K]>>>;
|
|
55
|
+
/**
|
|
56
|
+
* Inserts multiple objects into the database
|
|
57
|
+
*
|
|
58
|
+
* @param count - Number of objects to insert
|
|
59
|
+
* @param builderName - The name of the builder to use
|
|
60
|
+
* @param attrs - The attributes to insert
|
|
61
|
+
*/
|
|
62
|
+
abstract insertMany<K extends keyof Builders>(count: number, builderName: K, attrs?: Parameters<Builders[K]>[0] | ((idx: number, faker: FakerFactory) => Parameters<Builders[K]>[0])): Promise<Awaited<ReturnType<Builders[K]>>[]>;
|
|
63
|
+
/**
|
|
64
|
+
* Seeds the database using a seed function.
|
|
65
|
+
*
|
|
66
|
+
* @param seedName - The name of the seed to use
|
|
67
|
+
* @returns The result of the seed function
|
|
68
|
+
* @param attrs - The attributes to pass to the seed function
|
|
69
|
+
*/
|
|
70
|
+
abstract seed<K extends keyof Seeds>(seedName: K, attrs?: Parameters<Seeds[K]>[0]): ReturnType<Seeds[K]>;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Type definition for a factory builder function that can work with different database types.
|
|
74
|
+
* Builders are responsible for creating individual database records with default values and relationships.
|
|
75
|
+
*
|
|
76
|
+
* @template Attrs - The attributes/input type for the builder
|
|
77
|
+
* @template Factory - The factory instance type
|
|
78
|
+
* @template Result - The type of object returned by the builder
|
|
79
|
+
* @template DB - The database connection type (Kysely, Knex, etc.)
|
|
80
|
+
*
|
|
81
|
+
* @param attrs - Partial attributes to override defaults
|
|
82
|
+
* @param factory - The factory instance for creating related records
|
|
83
|
+
* @param db - The database connection
|
|
84
|
+
* @returns The created record or a promise resolving to it
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* const userBuilder: MixedFactoryBuilder<UserAttrs, Factory, User, Kysely<DB>> =
|
|
89
|
+
* async (attrs, factory, db) => {
|
|
90
|
+
* return {
|
|
91
|
+
* id: faker.string.uuid(),
|
|
92
|
+
* name: faker.person.fullName(),
|
|
93
|
+
* email: faker.internet.email(),
|
|
94
|
+
* ...attrs
|
|
95
|
+
* };
|
|
96
|
+
* };
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
type MixedFactoryBuilder<Attrs = any, Factory = any, Result = any, DB = any> = (attrs: Attrs, factory: Factory, db: DB) => Result | Promise<Result>;
|
|
100
|
+
/**
|
|
101
|
+
* Type definition for a factory seed function used to create complex test scenarios.
|
|
102
|
+
* Seeds typically create multiple related records to set up a complete test environment.
|
|
103
|
+
*
|
|
104
|
+
* @template Attrs - The attributes/input type for the seed
|
|
105
|
+
* @template Factory - The factory instance type
|
|
106
|
+
* @template Result - The type of object returned by the seed
|
|
107
|
+
* @template DB - The database connection type (Kysely, Knex, etc.)
|
|
108
|
+
*
|
|
109
|
+
* @param attrs - Configuration attributes for the seed
|
|
110
|
+
* @param factory - The factory instance for creating records
|
|
111
|
+
* @param db - The database connection
|
|
112
|
+
* @returns A promise resolving to the seed result
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```typescript
|
|
116
|
+
* const userWithPostsSeed: FactorySeed<{ postCount?: number }, Factory, User, DB> =
|
|
117
|
+
* async (attrs, factory, db) => {
|
|
118
|
+
* const user = await factory.insert('user', attrs);
|
|
119
|
+
* const postCount = attrs.postCount || 3;
|
|
120
|
+
*
|
|
121
|
+
* for (let i = 0; i < postCount; i++) {
|
|
122
|
+
* await factory.insert('post', { userId: user.id });
|
|
123
|
+
* }
|
|
124
|
+
*
|
|
125
|
+
* return user;
|
|
126
|
+
* };
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
type FactorySeed<Attrs = any, Factory = any, Result = any, DB = any> = (attrs: Attrs, factory: Factory, db: DB) => Promise<Result>;
|
|
130
|
+
//#endregion
|
|
131
|
+
export { Factory, FactorySeed, MixedFactoryBuilder };
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { FakerFactory } from "./faker-ChuHaYMR.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/Factory.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Abstract base class for database factories used in testing.
|
|
7
|
+
* Provides a standardized interface for creating test data using builder and seed patterns.
|
|
8
|
+
*
|
|
9
|
+
* @template Builders - Record of builder functions for creating individual entities
|
|
10
|
+
* @template Seeds - Record of seed functions for creating complex test scenarios
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Define builders for creating individual records
|
|
15
|
+
* const builders = {
|
|
16
|
+
* user: (attrs) => ({ name: 'Test User', email: 'test@example.com', ...attrs }),
|
|
17
|
+
* post: (attrs) => ({ title: 'Test Post', content: 'Content', ...attrs })
|
|
18
|
+
* };
|
|
19
|
+
*
|
|
20
|
+
* // Define seeds for complex scenarios
|
|
21
|
+
* const seeds = {
|
|
22
|
+
* userWithPosts: async (attrs, factory) => {
|
|
23
|
+
* const user = await factory.insert('user', attrs);
|
|
24
|
+
* await factory.insertMany(3, 'post', { userId: user.id });
|
|
25
|
+
* return user;
|
|
26
|
+
* }
|
|
27
|
+
* };
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
declare abstract class Factory<Builders extends Record<string, any>, Seeds extends Record<string, any>> {
|
|
31
|
+
/**
|
|
32
|
+
* Creates a typed seed function with proper type inference.
|
|
33
|
+
* This is a utility method to help with TypeScript type checking when defining seeds.
|
|
34
|
+
*
|
|
35
|
+
* @template Seed - The seed function type
|
|
36
|
+
* @param seedFn - The seed function to wrap
|
|
37
|
+
* @returns The same seed function with proper typing
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const userWithPostsSeed = Factory.createSeed(async (attrs, factory, db) => {
|
|
42
|
+
* const user = await factory.insert('user', attrs);
|
|
43
|
+
* return user;
|
|
44
|
+
* });
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
static createSeed<Seed extends FactorySeed>(seedFn: Seed): Seed;
|
|
48
|
+
/**
|
|
49
|
+
* Inserts an object into the database using a builder function.
|
|
50
|
+
*
|
|
51
|
+
* @param builderName - The name of the builder to use
|
|
52
|
+
* @param attrs - The attributes to insert
|
|
53
|
+
*/
|
|
54
|
+
abstract insert<K extends keyof Builders>(builderName: K, attrs?: Parameters<Builders[K]>[0]): Promise<Awaited<ReturnType<Builders[K]>>>;
|
|
55
|
+
/**
|
|
56
|
+
* Inserts multiple objects into the database
|
|
57
|
+
*
|
|
58
|
+
* @param count - Number of objects to insert
|
|
59
|
+
* @param builderName - The name of the builder to use
|
|
60
|
+
* @param attrs - The attributes to insert
|
|
61
|
+
*/
|
|
62
|
+
abstract insertMany<K extends keyof Builders>(count: number, builderName: K, attrs?: Parameters<Builders[K]>[0] | ((idx: number, faker: FakerFactory) => Parameters<Builders[K]>[0])): Promise<Awaited<ReturnType<Builders[K]>>[]>;
|
|
63
|
+
/**
|
|
64
|
+
* Seeds the database using a seed function.
|
|
65
|
+
*
|
|
66
|
+
* @param seedName - The name of the seed to use
|
|
67
|
+
* @returns The result of the seed function
|
|
68
|
+
* @param attrs - The attributes to pass to the seed function
|
|
69
|
+
*/
|
|
70
|
+
abstract seed<K extends keyof Seeds>(seedName: K, attrs?: Parameters<Seeds[K]>[0]): ReturnType<Seeds[K]>;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Type definition for a factory builder function that can work with different database types.
|
|
74
|
+
* Builders are responsible for creating individual database records with default values and relationships.
|
|
75
|
+
*
|
|
76
|
+
* @template Attrs - The attributes/input type for the builder
|
|
77
|
+
* @template Factory - The factory instance type
|
|
78
|
+
* @template Result - The type of object returned by the builder
|
|
79
|
+
* @template DB - The database connection type (Kysely, Knex, etc.)
|
|
80
|
+
*
|
|
81
|
+
* @param attrs - Partial attributes to override defaults
|
|
82
|
+
* @param factory - The factory instance for creating related records
|
|
83
|
+
* @param db - The database connection
|
|
84
|
+
* @returns The created record or a promise resolving to it
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* const userBuilder: MixedFactoryBuilder<UserAttrs, Factory, User, Kysely<DB>> =
|
|
89
|
+
* async (attrs, factory, db) => {
|
|
90
|
+
* return {
|
|
91
|
+
* id: faker.string.uuid(),
|
|
92
|
+
* name: faker.person.fullName(),
|
|
93
|
+
* email: faker.internet.email(),
|
|
94
|
+
* ...attrs
|
|
95
|
+
* };
|
|
96
|
+
* };
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
type MixedFactoryBuilder<Attrs = any, Factory = any, Result = any, DB = any> = (attrs: Attrs, factory: Factory, db: DB) => Result | Promise<Result>;
|
|
100
|
+
/**
|
|
101
|
+
* Type definition for a factory seed function used to create complex test scenarios.
|
|
102
|
+
* Seeds typically create multiple related records to set up a complete test environment.
|
|
103
|
+
*
|
|
104
|
+
* @template Attrs - The attributes/input type for the seed
|
|
105
|
+
* @template Factory - The factory instance type
|
|
106
|
+
* @template Result - The type of object returned by the seed
|
|
107
|
+
* @template DB - The database connection type (Kysely, Knex, etc.)
|
|
108
|
+
*
|
|
109
|
+
* @param attrs - Configuration attributes for the seed
|
|
110
|
+
* @param factory - The factory instance for creating records
|
|
111
|
+
* @param db - The database connection
|
|
112
|
+
* @returns A promise resolving to the seed result
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```typescript
|
|
116
|
+
* const userWithPostsSeed: FactorySeed<{ postCount?: number }, Factory, User, DB> =
|
|
117
|
+
* async (attrs, factory, db) => {
|
|
118
|
+
* const user = await factory.insert('user', attrs);
|
|
119
|
+
* const postCount = attrs.postCount || 3;
|
|
120
|
+
*
|
|
121
|
+
* for (let i = 0; i < postCount; i++) {
|
|
122
|
+
* await factory.insert('post', { userId: user.id });
|
|
123
|
+
* }
|
|
124
|
+
*
|
|
125
|
+
* return user;
|
|
126
|
+
* };
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
type FactorySeed<Attrs = any, Factory = any, Result = any, DB = any> = (attrs: Attrs, factory: Factory, db: DB) => Promise<Result>;
|
|
130
|
+
//#endregion
|
|
131
|
+
export { Factory, FactorySeed, MixedFactoryBuilder };
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { FakerFactory } from "./faker-km9UhOS6.cjs";
|
|
2
|
+
import { Factory, FactorySeed } from "./Factory-Bm44VKa-.cjs";
|
|
3
|
+
import { ControlledTransaction, Insertable, Kysely, Selectable } from "kysely";
|
|
4
|
+
|
|
5
|
+
//#region src/KyselyFactory.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Factory implementation for Kysely ORM, providing test data creation utilities.
|
|
9
|
+
* Extends the base Factory class with Kysely-specific database operations.
|
|
10
|
+
*
|
|
11
|
+
* @template DB - The database schema type
|
|
12
|
+
* @template Builders - Record of builder functions for creating entities
|
|
13
|
+
* @template Seeds - Record of seed functions for complex test scenarios
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // Define your database schema
|
|
18
|
+
* interface Database {
|
|
19
|
+
* users: UsersTable;
|
|
20
|
+
* posts: PostsTable;
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* // Create builders
|
|
24
|
+
* const builders = {
|
|
25
|
+
* user: KyselyFactory.createBuilder<Database, 'users'>('users', (attrs, factory, db, faker) => ({
|
|
26
|
+
* id: faker.string.uuid(),
|
|
27
|
+
* name: faker.person.fullName(),
|
|
28
|
+
* email: faker.internet.email(),
|
|
29
|
+
* ...attrs
|
|
30
|
+
* })),
|
|
31
|
+
* post: KyselyFactory.createBuilder<Database, 'posts'>('posts', (attrs) => ({
|
|
32
|
+
* title: 'Test Post',
|
|
33
|
+
* content: 'Test content',
|
|
34
|
+
* ...attrs
|
|
35
|
+
* }))
|
|
36
|
+
* };
|
|
37
|
+
*
|
|
38
|
+
* // Create factory instance
|
|
39
|
+
* const factory = new KyselyFactory(builders, seeds, db);
|
|
40
|
+
*
|
|
41
|
+
* // Use in tests
|
|
42
|
+
* const user = await factory.insert('user', { name: 'John Doe' });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
declare class KyselyFactory<DB, Builders extends Record<string, any>, Seeds extends Record<string, any>> extends Factory<Builders, Seeds> {
|
|
46
|
+
private builders;
|
|
47
|
+
private seeds;
|
|
48
|
+
private db;
|
|
49
|
+
/**
|
|
50
|
+
* Creates a typed seed function with proper type inference.
|
|
51
|
+
* Inherits from the base Factory class implementation.
|
|
52
|
+
*
|
|
53
|
+
* @template Seed - The seed function type
|
|
54
|
+
* @param seedFn - The seed function to wrap
|
|
55
|
+
* @returns The same seed function with proper typing
|
|
56
|
+
*/
|
|
57
|
+
static createSeed<Seed extends FactorySeed>(seedFn: Seed): Seed;
|
|
58
|
+
/**
|
|
59
|
+
* Creates a new KyselyFactory instance.
|
|
60
|
+
*
|
|
61
|
+
* @param builders - Record of builder functions for creating individual entities
|
|
62
|
+
* @param seeds - Record of seed functions for creating complex test scenarios
|
|
63
|
+
* @param db - Kysely database instance or controlled transaction
|
|
64
|
+
*/
|
|
65
|
+
constructor(builders: Builders, seeds: Seeds, db: Kysely<DB> | ControlledTransaction<DB, []>);
|
|
66
|
+
/**
|
|
67
|
+
* Creates a typed builder function for a specific database table.
|
|
68
|
+
* This is a utility method that helps create builders with proper type inference for Kysely.
|
|
69
|
+
*
|
|
70
|
+
* @template DB - The database schema type
|
|
71
|
+
* @template TableName - The name of the table (must be a key of DB)
|
|
72
|
+
* @template Attrs - The attributes type for the builder (defaults to Partial<Insertable>)
|
|
73
|
+
* @template Factory - The factory instance type
|
|
74
|
+
* @template Result - The result type (defaults to Selectable of the table)
|
|
75
|
+
*
|
|
76
|
+
* @param table - The name of the database table
|
|
77
|
+
* @param item - Optional function to provide default values and transformations
|
|
78
|
+
* @param autoInsert - Whether to automatically insert the record (default: true)
|
|
79
|
+
* @returns A builder function that creates and optionally inserts records
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* // Create a simple builder with defaults
|
|
84
|
+
* const userBuilder = KyselyFactory.createBuilder<DB, 'users'>('users',
|
|
85
|
+
* (attrs, factory, db, faker) => ({
|
|
86
|
+
* id: faker.string.uuid(),
|
|
87
|
+
* name: faker.person.fullName(),
|
|
88
|
+
* email: faker.internet.email(),
|
|
89
|
+
* createdAt: new Date(),
|
|
90
|
+
* ...attrs
|
|
91
|
+
* })
|
|
92
|
+
* );
|
|
93
|
+
*
|
|
94
|
+
* // Create a builder that doesn't auto-insert (useful for nested inserts)
|
|
95
|
+
* const addressBuilder = KyselyFactory.createBuilder<DB, 'addresses'>('addresses',
|
|
96
|
+
* (attrs) => ({
|
|
97
|
+
* street: '123 Main St',
|
|
98
|
+
* city: 'Anytown',
|
|
99
|
+
* ...attrs
|
|
100
|
+
* }),
|
|
101
|
+
* false // Don't auto-insert
|
|
102
|
+
* );
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
static createBuilder<DB, TableName extends keyof DB & string, Attrs extends Partial<Insertable<DB[TableName]>> = Partial<Insertable<DB[TableName]>>, Factory = any, Result = Selectable<DB[TableName]>>(table: TableName, item?: (attrs: Attrs, factory: Factory, db: Kysely<DB>, faker: FakerFactory) => Partial<Insertable<DB[TableName]>> | Promise<Partial<Insertable<DB[TableName]>>>, autoInsert?: boolean): (attrs: Attrs, factory: Factory, db: Kysely<DB>, faker: FakerFactory) => Promise<Result>;
|
|
106
|
+
/**
|
|
107
|
+
* Inserts a single record into the database using the specified builder.
|
|
108
|
+
* The builder function is responsible for generating the record data with defaults
|
|
109
|
+
* and the factory handles the actual database insertion.
|
|
110
|
+
*
|
|
111
|
+
* @template K - The builder name (must be a key of Builders)
|
|
112
|
+
* @param builderName - The name of the builder to use
|
|
113
|
+
* @param attrs - Optional attributes to override builder defaults
|
|
114
|
+
* @returns A promise resolving to the inserted record
|
|
115
|
+
* @throws Error if the specified builder doesn't exist
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* // Insert with defaults
|
|
120
|
+
* const user = await factory.insert('user');
|
|
121
|
+
*
|
|
122
|
+
* // Insert with overrides
|
|
123
|
+
* const adminUser = await factory.insert('user', {
|
|
124
|
+
* email: 'admin@example.com',
|
|
125
|
+
* role: 'admin'
|
|
126
|
+
* });
|
|
127
|
+
*
|
|
128
|
+
* // Use the inserted record
|
|
129
|
+
* const post = await factory.insert('post', {
|
|
130
|
+
* userId: user.id,
|
|
131
|
+
* title: 'My First Post'
|
|
132
|
+
* });
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
insert<K extends keyof Builders>(builderName: K, attrs?: Parameters<Builders[K]>[0]): Promise<Awaited<ReturnType<Builders[K]>>>;
|
|
136
|
+
/**
|
|
137
|
+
* Inserts multiple records into the database using the specified builder.
|
|
138
|
+
* Supports both static attributes and dynamic attribute generation via a function.
|
|
139
|
+
*
|
|
140
|
+
* @template K - The builder name (must be a key of Builders)
|
|
141
|
+
* @param count - The number of records to insert
|
|
142
|
+
* @param builderName - The name of the builder to use
|
|
143
|
+
* @param attrs - Static attributes or a function that generates attributes for each record
|
|
144
|
+
* @returns A promise resolving to an array of inserted records
|
|
145
|
+
* @throws Error if the specified builder doesn't exist
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* // Insert multiple with same attributes
|
|
150
|
+
* const users = await factory.insertMany(5, 'user', { role: 'member' });
|
|
151
|
+
*
|
|
152
|
+
* // Insert multiple with dynamic attributes
|
|
153
|
+
* const posts = await factory.insertMany(10, 'post', (idx, faker) => ({
|
|
154
|
+
* title: `Post ${idx + 1}`,
|
|
155
|
+
* content: faker.lorem.paragraph(),
|
|
156
|
+
* publishedAt: faker.date.past()
|
|
157
|
+
* }));
|
|
158
|
+
*
|
|
159
|
+
* // Create users with sequential emails
|
|
160
|
+
* const admins = await factory.insertMany(3, 'user', (idx) => ({
|
|
161
|
+
* email: `admin${idx + 1}@example.com`,
|
|
162
|
+
* role: 'admin'
|
|
163
|
+
* }));
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
insertMany<K extends keyof Builders>(count: number, builderName: K, attrs?: Parameters<Builders[K]>[0]): Promise<Awaited<ReturnType<Builders[K]>>[]>;
|
|
167
|
+
insertMany<K extends keyof Builders>(count: number, builderName: K, attrs: (idx: number, faker: FakerFactory) => Parameters<Builders[K]>[0]): Promise<Awaited<ReturnType<Builders[K]>>[]>;
|
|
168
|
+
/**
|
|
169
|
+
* Executes a seed function to create complex test scenarios with multiple related records.
|
|
170
|
+
* Seeds are useful for setting up complete test environments with realistic data relationships.
|
|
171
|
+
*
|
|
172
|
+
* @template K - The seed name (must be a key of Seeds)
|
|
173
|
+
* @param seedName - The name of the seed to execute
|
|
174
|
+
* @param attrs - Optional configuration attributes for the seed
|
|
175
|
+
* @returns The result of the seed function (typically the primary record created)
|
|
176
|
+
* @throws Error if the specified seed doesn't exist
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```typescript
|
|
180
|
+
* // Execute a simple seed
|
|
181
|
+
* const user = await factory.seed('userWithProfile');
|
|
182
|
+
*
|
|
183
|
+
* // Execute a seed with configuration
|
|
184
|
+
* const author = await factory.seed('authorWithBooks', {
|
|
185
|
+
* bookCount: 5,
|
|
186
|
+
* includeReviews: true
|
|
187
|
+
* });
|
|
188
|
+
*
|
|
189
|
+
* // Use seed result in tests
|
|
190
|
+
* const company = await factory.seed('companyWithDepartments', {
|
|
191
|
+
* departmentCount: 3,
|
|
192
|
+
* employeesPerDepartment: 10
|
|
193
|
+
* });
|
|
194
|
+
* expect(company.departments).toHaveLength(3);
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
seed<K extends keyof Seeds>(seedName: K, attrs?: Parameters<Seeds[K]>[0]): ReturnType<Seeds[K]>;
|
|
198
|
+
}
|
|
199
|
+
//#endregion
|
|
200
|
+
export { KyselyFactory };
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { FakerFactory } from "./faker-ChuHaYMR.mjs";
|
|
2
|
+
import { Factory, FactorySeed } from "./Factory-tjCDNgUK.mjs";
|
|
3
|
+
import { ControlledTransaction, Insertable, Kysely, Selectable } from "kysely";
|
|
4
|
+
|
|
5
|
+
//#region src/KyselyFactory.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Factory implementation for Kysely ORM, providing test data creation utilities.
|
|
9
|
+
* Extends the base Factory class with Kysely-specific database operations.
|
|
10
|
+
*
|
|
11
|
+
* @template DB - The database schema type
|
|
12
|
+
* @template Builders - Record of builder functions for creating entities
|
|
13
|
+
* @template Seeds - Record of seed functions for complex test scenarios
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // Define your database schema
|
|
18
|
+
* interface Database {
|
|
19
|
+
* users: UsersTable;
|
|
20
|
+
* posts: PostsTable;
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* // Create builders
|
|
24
|
+
* const builders = {
|
|
25
|
+
* user: KyselyFactory.createBuilder<Database, 'users'>('users', (attrs, factory, db, faker) => ({
|
|
26
|
+
* id: faker.string.uuid(),
|
|
27
|
+
* name: faker.person.fullName(),
|
|
28
|
+
* email: faker.internet.email(),
|
|
29
|
+
* ...attrs
|
|
30
|
+
* })),
|
|
31
|
+
* post: KyselyFactory.createBuilder<Database, 'posts'>('posts', (attrs) => ({
|
|
32
|
+
* title: 'Test Post',
|
|
33
|
+
* content: 'Test content',
|
|
34
|
+
* ...attrs
|
|
35
|
+
* }))
|
|
36
|
+
* };
|
|
37
|
+
*
|
|
38
|
+
* // Create factory instance
|
|
39
|
+
* const factory = new KyselyFactory(builders, seeds, db);
|
|
40
|
+
*
|
|
41
|
+
* // Use in tests
|
|
42
|
+
* const user = await factory.insert('user', { name: 'John Doe' });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
declare class KyselyFactory<DB, Builders extends Record<string, any>, Seeds extends Record<string, any>> extends Factory<Builders, Seeds> {
|
|
46
|
+
private builders;
|
|
47
|
+
private seeds;
|
|
48
|
+
private db;
|
|
49
|
+
/**
|
|
50
|
+
* Creates a typed seed function with proper type inference.
|
|
51
|
+
* Inherits from the base Factory class implementation.
|
|
52
|
+
*
|
|
53
|
+
* @template Seed - The seed function type
|
|
54
|
+
* @param seedFn - The seed function to wrap
|
|
55
|
+
* @returns The same seed function with proper typing
|
|
56
|
+
*/
|
|
57
|
+
static createSeed<Seed extends FactorySeed>(seedFn: Seed): Seed;
|
|
58
|
+
/**
|
|
59
|
+
* Creates a new KyselyFactory instance.
|
|
60
|
+
*
|
|
61
|
+
* @param builders - Record of builder functions for creating individual entities
|
|
62
|
+
* @param seeds - Record of seed functions for creating complex test scenarios
|
|
63
|
+
* @param db - Kysely database instance or controlled transaction
|
|
64
|
+
*/
|
|
65
|
+
constructor(builders: Builders, seeds: Seeds, db: Kysely<DB> | ControlledTransaction<DB, []>);
|
|
66
|
+
/**
|
|
67
|
+
* Creates a typed builder function for a specific database table.
|
|
68
|
+
* This is a utility method that helps create builders with proper type inference for Kysely.
|
|
69
|
+
*
|
|
70
|
+
* @template DB - The database schema type
|
|
71
|
+
* @template TableName - The name of the table (must be a key of DB)
|
|
72
|
+
* @template Attrs - The attributes type for the builder (defaults to Partial<Insertable>)
|
|
73
|
+
* @template Factory - The factory instance type
|
|
74
|
+
* @template Result - The result type (defaults to Selectable of the table)
|
|
75
|
+
*
|
|
76
|
+
* @param table - The name of the database table
|
|
77
|
+
* @param item - Optional function to provide default values and transformations
|
|
78
|
+
* @param autoInsert - Whether to automatically insert the record (default: true)
|
|
79
|
+
* @returns A builder function that creates and optionally inserts records
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* // Create a simple builder with defaults
|
|
84
|
+
* const userBuilder = KyselyFactory.createBuilder<DB, 'users'>('users',
|
|
85
|
+
* (attrs, factory, db, faker) => ({
|
|
86
|
+
* id: faker.string.uuid(),
|
|
87
|
+
* name: faker.person.fullName(),
|
|
88
|
+
* email: faker.internet.email(),
|
|
89
|
+
* createdAt: new Date(),
|
|
90
|
+
* ...attrs
|
|
91
|
+
* })
|
|
92
|
+
* );
|
|
93
|
+
*
|
|
94
|
+
* // Create a builder that doesn't auto-insert (useful for nested inserts)
|
|
95
|
+
* const addressBuilder = KyselyFactory.createBuilder<DB, 'addresses'>('addresses',
|
|
96
|
+
* (attrs) => ({
|
|
97
|
+
* street: '123 Main St',
|
|
98
|
+
* city: 'Anytown',
|
|
99
|
+
* ...attrs
|
|
100
|
+
* }),
|
|
101
|
+
* false // Don't auto-insert
|
|
102
|
+
* );
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
static createBuilder<DB, TableName extends keyof DB & string, Attrs extends Partial<Insertable<DB[TableName]>> = Partial<Insertable<DB[TableName]>>, Factory = any, Result = Selectable<DB[TableName]>>(table: TableName, item?: (attrs: Attrs, factory: Factory, db: Kysely<DB>, faker: FakerFactory) => Partial<Insertable<DB[TableName]>> | Promise<Partial<Insertable<DB[TableName]>>>, autoInsert?: boolean): (attrs: Attrs, factory: Factory, db: Kysely<DB>, faker: FakerFactory) => Promise<Result>;
|
|
106
|
+
/**
|
|
107
|
+
* Inserts a single record into the database using the specified builder.
|
|
108
|
+
* The builder function is responsible for generating the record data with defaults
|
|
109
|
+
* and the factory handles the actual database insertion.
|
|
110
|
+
*
|
|
111
|
+
* @template K - The builder name (must be a key of Builders)
|
|
112
|
+
* @param builderName - The name of the builder to use
|
|
113
|
+
* @param attrs - Optional attributes to override builder defaults
|
|
114
|
+
* @returns A promise resolving to the inserted record
|
|
115
|
+
* @throws Error if the specified builder doesn't exist
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* // Insert with defaults
|
|
120
|
+
* const user = await factory.insert('user');
|
|
121
|
+
*
|
|
122
|
+
* // Insert with overrides
|
|
123
|
+
* const adminUser = await factory.insert('user', {
|
|
124
|
+
* email: 'admin@example.com',
|
|
125
|
+
* role: 'admin'
|
|
126
|
+
* });
|
|
127
|
+
*
|
|
128
|
+
* // Use the inserted record
|
|
129
|
+
* const post = await factory.insert('post', {
|
|
130
|
+
* userId: user.id,
|
|
131
|
+
* title: 'My First Post'
|
|
132
|
+
* });
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
insert<K extends keyof Builders>(builderName: K, attrs?: Parameters<Builders[K]>[0]): Promise<Awaited<ReturnType<Builders[K]>>>;
|
|
136
|
+
/**
|
|
137
|
+
* Inserts multiple records into the database using the specified builder.
|
|
138
|
+
* Supports both static attributes and dynamic attribute generation via a function.
|
|
139
|
+
*
|
|
140
|
+
* @template K - The builder name (must be a key of Builders)
|
|
141
|
+
* @param count - The number of records to insert
|
|
142
|
+
* @param builderName - The name of the builder to use
|
|
143
|
+
* @param attrs - Static attributes or a function that generates attributes for each record
|
|
144
|
+
* @returns A promise resolving to an array of inserted records
|
|
145
|
+
* @throws Error if the specified builder doesn't exist
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* // Insert multiple with same attributes
|
|
150
|
+
* const users = await factory.insertMany(5, 'user', { role: 'member' });
|
|
151
|
+
*
|
|
152
|
+
* // Insert multiple with dynamic attributes
|
|
153
|
+
* const posts = await factory.insertMany(10, 'post', (idx, faker) => ({
|
|
154
|
+
* title: `Post ${idx + 1}`,
|
|
155
|
+
* content: faker.lorem.paragraph(),
|
|
156
|
+
* publishedAt: faker.date.past()
|
|
157
|
+
* }));
|
|
158
|
+
*
|
|
159
|
+
* // Create users with sequential emails
|
|
160
|
+
* const admins = await factory.insertMany(3, 'user', (idx) => ({
|
|
161
|
+
* email: `admin${idx + 1}@example.com`,
|
|
162
|
+
* role: 'admin'
|
|
163
|
+
* }));
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
insertMany<K extends keyof Builders>(count: number, builderName: K, attrs?: Parameters<Builders[K]>[0]): Promise<Awaited<ReturnType<Builders[K]>>[]>;
|
|
167
|
+
insertMany<K extends keyof Builders>(count: number, builderName: K, attrs: (idx: number, faker: FakerFactory) => Parameters<Builders[K]>[0]): Promise<Awaited<ReturnType<Builders[K]>>[]>;
|
|
168
|
+
/**
|
|
169
|
+
* Executes a seed function to create complex test scenarios with multiple related records.
|
|
170
|
+
* Seeds are useful for setting up complete test environments with realistic data relationships.
|
|
171
|
+
*
|
|
172
|
+
* @template K - The seed name (must be a key of Seeds)
|
|
173
|
+
* @param seedName - The name of the seed to execute
|
|
174
|
+
* @param attrs - Optional configuration attributes for the seed
|
|
175
|
+
* @returns The result of the seed function (typically the primary record created)
|
|
176
|
+
* @throws Error if the specified seed doesn't exist
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```typescript
|
|
180
|
+
* // Execute a simple seed
|
|
181
|
+
* const user = await factory.seed('userWithProfile');
|
|
182
|
+
*
|
|
183
|
+
* // Execute a seed with configuration
|
|
184
|
+
* const author = await factory.seed('authorWithBooks', {
|
|
185
|
+
* bookCount: 5,
|
|
186
|
+
* includeReviews: true
|
|
187
|
+
* });
|
|
188
|
+
*
|
|
189
|
+
* // Use seed result in tests
|
|
190
|
+
* const company = await factory.seed('companyWithDepartments', {
|
|
191
|
+
* departmentCount: 3,
|
|
192
|
+
* employeesPerDepartment: 10
|
|
193
|
+
* });
|
|
194
|
+
* expect(company.departments).toHaveLength(3);
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
seed<K extends keyof Seeds>(seedName: K, attrs?: Parameters<Seeds[K]>[0]): ReturnType<Seeds[K]>;
|
|
198
|
+
}
|
|
199
|
+
//#endregion
|
|
200
|
+
export { KyselyFactory };
|