@geekmidas/testkit 0.1.1 → 0.3.0
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-z2m01hMj.mjs → Factory-BFVnMMCC.mjs} +2 -2
- package/dist/Factory-BFVnMMCC.mjs.map +1 -0
- package/dist/{Factory-DE3hE0WO.d.mts → Factory-BcGJjLc8.d.mts} +24 -10
- package/dist/{Factory-WMhTNZ9S.cjs → Factory-BhjUOBWN.cjs} +2 -2
- package/dist/Factory-BhjUOBWN.cjs.map +1 -0
- package/dist/{Factory-pNV7ZQ7-.d.cts → Factory-IdfEkOeT.d.cts} +24 -10
- package/dist/Factory.cjs +1 -1
- package/dist/Factory.d.cts +3 -3
- package/dist/Factory.d.mts +3 -3
- package/dist/Factory.mjs +1 -1
- package/dist/{KyselyFactory-DaaCykWP.cjs → KyselyFactory-BFqVIn_0.cjs} +21 -4
- package/dist/KyselyFactory-BFqVIn_0.cjs.map +1 -0
- package/dist/{KyselyFactory-CPZTUuMB.d.cts → KyselyFactory-Cj-EultY.d.mts} +18 -5
- package/dist/{KyselyFactory-CXY5gJk2.mjs → KyselyFactory-DMswpwji.mjs} +21 -4
- package/dist/KyselyFactory-DMswpwji.mjs.map +1 -0
- package/dist/{KyselyFactory-RAyvZ8Cj.d.mts → KyselyFactory-Dxhi_ZkA.d.cts} +18 -5
- package/dist/KyselyFactory.cjs +2 -2
- package/dist/KyselyFactory.d.cts +3 -3
- package/dist/KyselyFactory.d.mts +3 -3
- package/dist/KyselyFactory.mjs +2 -2
- package/dist/{ObjectionFactory-BlBicEia.d.mts → ObjectionFactory-BRAkAByP.d.cts} +18 -5
- package/dist/{ObjectionFactory-Eb04AOnv.cjs → ObjectionFactory-BeFBYcan.cjs} +21 -4
- package/dist/ObjectionFactory-BeFBYcan.cjs.map +1 -0
- package/dist/{ObjectionFactory-BwpN4gMX.d.cts → ObjectionFactory-DL4qkuF1.d.mts} +18 -5
- package/dist/{ObjectionFactory-zf2fLKrL.mjs → ObjectionFactory-QCJ7u0Ql.mjs} +21 -4
- package/dist/ObjectionFactory-QCJ7u0Ql.mjs.map +1 -0
- package/dist/ObjectionFactory.cjs +2 -2
- package/dist/ObjectionFactory.d.cts +3 -3
- package/dist/ObjectionFactory.d.mts +3 -3
- package/dist/ObjectionFactory.mjs +2 -2
- package/dist/{VitestKyselyTransactionIsolator-XDL3ngs_.mjs → VitestKyselyTransactionIsolator-CNURW8y6.mjs} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-XDL3ngs_.mjs.map → VitestKyselyTransactionIsolator-CNURW8y6.mjs.map} +1 -1
- package/dist/{VitestKyselyTransactionIsolator-DiskaURs.d.mts → VitestKyselyTransactionIsolator-D3EZZhjZ.d.cts} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-BqrZDeaT.d.cts → VitestKyselyTransactionIsolator-Dxlp1u0f.d.mts} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-DX_VPKS-.cjs → VitestKyselyTransactionIsolator-EvDLk5zg.cjs} +2 -2
- package/dist/{VitestKyselyTransactionIsolator-DX_VPKS-.cjs.map → VitestKyselyTransactionIsolator-EvDLk5zg.cjs.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-CD2ucJpH.d.cts → VitestObjectionTransactionIsolator-1TpsPqfG.d.cts} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-D_tlOtq8.cjs → VitestObjectionTransactionIsolator-CM5KTAFA.cjs} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-D_tlOtq8.cjs.map → VitestObjectionTransactionIsolator-CM5KTAFA.cjs.map} +1 -1
- package/dist/{VitestObjectionTransactionIsolator-DhQ8XGva.d.mts → VitestObjectionTransactionIsolator-i9jIgU8Q.d.mts} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-_EhJKu_O.mjs → VitestObjectionTransactionIsolator-jQFaCz0u.mjs} +2 -2
- package/dist/{VitestObjectionTransactionIsolator-_EhJKu_O.mjs.map → VitestObjectionTransactionIsolator-jQFaCz0u.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-DLdQlfZ5.d.mts → VitestTransactionIsolator-BvR19bYn.d.mts} +31 -14
- package/dist/{VitestTransactionIsolator-BKIrj3Uy.cjs → VitestTransactionIsolator-CMfJXZP8.cjs} +52 -36
- package/dist/VitestTransactionIsolator-CMfJXZP8.cjs.map +1 -0
- package/dist/{VitestTransactionIsolator-DfA80g2M.d.cts → VitestTransactionIsolator-CwQaxZLP.d.cts} +31 -14
- package/dist/{VitestTransactionIsolator-BIaMs4c2.mjs → VitestTransactionIsolator-DQ7tLqgV.mjs} +52 -36
- package/dist/VitestTransactionIsolator-DQ7tLqgV.mjs.map +1 -0
- package/dist/VitestTransactionIsolator.cjs +1 -1
- package/dist/VitestTransactionIsolator.d.cts +2 -2
- package/dist/VitestTransactionIsolator.d.mts +2 -2
- package/dist/VitestTransactionIsolator.mjs +1 -1
- package/dist/benchmark.cjs +49 -0
- package/dist/benchmark.cjs.map +1 -0
- package/dist/benchmark.d.cts +35 -0
- package/dist/benchmark.d.mts +35 -0
- package/dist/benchmark.mjs +45 -0
- package/dist/benchmark.mjs.map +1 -0
- package/dist/better-auth.d.cts +2 -2
- package/dist/better-auth.d.mts +2 -2
- package/dist/{directory-Q178x53k.d.mts → directory-BXavAeJZ.d.mts} +3 -3
- package/dist/{faker-BSH1EMtg.d.cts → faker-CbYiF-8_.d.cts} +3 -3
- package/dist/{faker-D9gz7KjY.d.mts → faker-DHh7xs4u.d.mts} +3 -3
- package/dist/faker.d.cts +1 -1
- package/dist/faker.d.mts +1 -1
- package/dist/kysely.cjs +25 -31
- package/dist/kysely.cjs.map +1 -1
- package/dist/kysely.d.cts +39 -34
- package/dist/kysely.d.mts +39 -34
- package/dist/kysely.mjs +25 -31
- package/dist/kysely.mjs.map +1 -1
- package/dist/objection.cjs +25 -49
- package/dist/objection.cjs.map +1 -1
- package/dist/objection.d.cts +39 -52
- package/dist/objection.d.mts +39 -52
- package/dist/objection.mjs +25 -49
- package/dist/objection.mjs.map +1 -1
- package/dist/os/directory.d.mts +1 -1
- package/dist/os/index.d.mts +1 -1
- package/package.json +6 -1
- package/src/Factory.ts +25 -11
- package/src/KyselyFactory.ts +21 -4
- package/src/ObjectionFactory.ts +21 -4
- package/src/VitestTransactionIsolator.ts +67 -16
- package/src/__tests__/Factory.spec.ts +16 -10
- package/src/__tests__/KyselyFactory.spec.ts +16 -11
- package/src/__tests__/ObjectionFactory.spec.ts +61 -53
- package/src/__tests__/VitestObjectionTransactionIsolator.spec.ts +6 -3
- package/src/__tests__/integration.spec.ts +24 -18
- package/src/benchmark.ts +48 -0
- package/src/kysely.ts +44 -33
- package/src/objection.ts +42 -51
- package/dist/Factory-WMhTNZ9S.cjs.map +0 -1
- package/dist/Factory-z2m01hMj.mjs.map +0 -1
- package/dist/KyselyFactory-CXY5gJk2.mjs.map +0 -1
- package/dist/KyselyFactory-DaaCykWP.cjs.map +0 -1
- package/dist/ObjectionFactory-Eb04AOnv.cjs.map +0 -1
- package/dist/ObjectionFactory-zf2fLKrL.mjs.map +0 -1
- package/dist/VitestTransactionIsolator-BIaMs4c2.mjs.map +0 -1
- package/dist/VitestTransactionIsolator-BKIrj3Uy.cjs.map +0 -1
|
@@ -35,7 +35,7 @@ var Factory = class {
|
|
|
35
35
|
*
|
|
36
36
|
* @example
|
|
37
37
|
* ```typescript
|
|
38
|
-
* const userWithPostsSeed = Factory.createSeed(async (attrs, factory, db) => {
|
|
38
|
+
* const userWithPostsSeed = Factory.createSeed(async ({ attrs, factory, db }) => {
|
|
39
39
|
* const user = await factory.insert('user', attrs);
|
|
40
40
|
* return user;
|
|
41
41
|
* });
|
|
@@ -48,4 +48,4 @@ var Factory = class {
|
|
|
48
48
|
|
|
49
49
|
//#endregion
|
|
50
50
|
export { Factory };
|
|
51
|
-
//# sourceMappingURL=Factory-
|
|
51
|
+
//# sourceMappingURL=Factory-BFVnMMCC.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Factory-BFVnMMCC.mjs","names":["seedFn: Seed"],"sources":["../src/Factory.ts"],"sourcesContent":["import type { FakerFactory } from './faker';\n\n/**\n * Abstract base class for database factories used in testing.\n * Provides a standardized interface for creating test data using builder and seed patterns.\n *\n * @template Builders - Record of builder functions for creating individual entities\n * @template Seeds - Record of seed functions for creating complex test scenarios\n *\n * @example\n * ```typescript\n * // Define builders for creating individual records\n * const builders = {\n * user: (attrs) => ({ name: 'Test User', email: 'test@example.com', ...attrs }),\n * post: (attrs) => ({ title: 'Test Post', content: 'Content', ...attrs })\n * };\n *\n * // Define seeds for complex scenarios\n * const seeds = {\n * userWithPosts: async (attrs, factory) => {\n * const user = await factory.insert('user', attrs);\n * await factory.insertMany(3, 'post', { userId: user.id });\n * return user;\n * }\n * };\n * ```\n */\nexport abstract class Factory<\n Builders extends Record<string, any>,\n Seeds extends Record<string, any>,\n> {\n /**\n * Creates a typed seed function with proper type inference.\n * This is a utility method to help with TypeScript type checking when defining seeds.\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 * @example\n * ```typescript\n * const userWithPostsSeed = Factory.createSeed(async ({ attrs, factory, db }) => {\n * const user = await factory.insert('user', attrs);\n * return user;\n * });\n * ```\n */\n static createSeed<Seed extends FactorySeed>(seedFn: Seed): Seed {\n return seedFn;\n }\n /**\n * Inserts an object into the database using a builder function.\n *\n * @param builderName - The name of the builder to use\n * @param attrs - The attributes to insert\n */\n abstract insert<K extends keyof Builders>(\n builderName: K,\n attrs?: Parameters<Builders[K]>[0],\n ): Promise<Awaited<ReturnType<Builders[K]>>>;\n\n /**\n * Inserts multiple objects into the database\n *\n * @param count - Number of objects to insert\n * @param builderName - The name of the builder to use\n * @param attrs - The attributes to insert\n */\n abstract insertMany<K extends keyof Builders>(\n count: number,\n builderName: K,\n attrs?:\n | Parameters<Builders[K]>[0]\n | ((\n idx: number,\n faker: FakerFactory,\n ) => Promise<Parameters<Builders[K]>[0]>),\n ): Promise<Awaited<ReturnType<Builders[K]>>[]>;\n\n /**\n * Seeds the database using a seed function.\n *\n * @param seedName - The name of the seed to use\n * @returns The result of the seed function\n * @param attrs - The attributes to pass to the seed function\n */\n abstract seed<K extends keyof Seeds>(\n seedName: K,\n attrs?: ExtractSeedAttrs<Seeds[K]>,\n ): ReturnType<Seeds[K]>;\n}\n\n/**\n * Type definition for a factory builder function that can work with different database types.\n * Builders are responsible for creating individual database records with default values and relationships.\n *\n * @template Attrs - The attributes/input type for the builder\n * @template Factory - The factory instance type\n * @template Result - The type of object returned by the builder\n * @template DB - The database connection type (Kysely, Knex, etc.)\n *\n * @param attrs - Partial attributes to override defaults\n * @param factory - The factory instance for creating related records\n * @param db - The database connection\n * @returns The created record or a promise resolving to it\n *\n * @example\n * ```typescript\n * const userBuilder: MixedFactoryBuilder<UserAttrs, Factory, User, Kysely<DB>> =\n * async (attrs, factory, db) => {\n * return {\n * id: faker.string.uuid(),\n * name: faker.person.fullName(),\n * email: faker.internet.email(),\n * ...attrs\n * };\n * };\n * ```\n */\nexport type MixedFactoryBuilder<\n Attrs = any,\n Factory = any,\n Result = any,\n DB = any,\n> = (attrs: Attrs, factory: Factory, db: DB) => Result | Promise<Result>;\n\n/**\n * Type definition for a factory seed function used to create complex test scenarios.\n * Seeds typically create multiple related records to set up a complete test environment.\n *\n * @template Attrs - The attributes/input type for the seed\n * @template Factory - The factory instance type\n * @template Result - The type of object returned by the seed\n * @template DB - The database connection type (Kysely, Knex, etc.)\n *\n * @param context - Object containing attrs, factory, and db\n * @param context.attrs - Configuration attributes for the seed\n * @param context.factory - The factory instance for creating records\n * @param context.db - The database connection\n * @returns A promise resolving to the seed result\n *\n * @example\n * ```typescript\n * const userWithPostsSeed: FactorySeed<{ postCount?: number }, Factory, User, DB> =\n * async ({ attrs, factory, db }) => {\n * const user = await factory.insert('user', attrs);\n * const postCount = attrs.postCount || 3;\n *\n * for (let i = 0; i < postCount; i++) {\n * await factory.insert('post', { userId: user.id });\n * }\n *\n * return user;\n * };\n * ```\n */\nexport type FactorySeed<\n Attrs = any,\n Factory = any,\n Result = any,\n DB = any,\n> = (context: { attrs: Attrs; factory: Factory; db: DB }) => Promise<Result>;\n\n/**\n * Helper type to extract the Attrs type from a FactorySeed function.\n * Used internally by Factory implementations to correctly type the seed method parameters.\n */\nexport type ExtractSeedAttrs<T> = T extends (context: {\n attrs: infer A;\n factory: any;\n db: any;\n}) => any\n ? A\n : never;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,IAAsB,UAAtB,MAGE;;;;;;;;;;;;;;;;;CAiBA,OAAO,WAAqCA,QAAoB;AAC9D,SAAO;CACR;AAyCF"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FakerFactory } from "./faker-
|
|
1
|
+
import { FakerFactory } from "./faker-DHh7xs4u.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/Factory.d.ts
|
|
4
4
|
|
|
@@ -38,7 +38,7 @@ declare abstract class Factory<Builders extends Record<string, any>, Seeds exten
|
|
|
38
38
|
*
|
|
39
39
|
* @example
|
|
40
40
|
* ```typescript
|
|
41
|
-
* const userWithPostsSeed = Factory.createSeed(async (attrs, factory, db) => {
|
|
41
|
+
* const userWithPostsSeed = Factory.createSeed(async ({ attrs, factory, db }) => {
|
|
42
42
|
* const user = await factory.insert('user', attrs);
|
|
43
43
|
* return user;
|
|
44
44
|
* });
|
|
@@ -67,7 +67,7 @@ declare abstract class Factory<Builders extends Record<string, any>, Seeds exten
|
|
|
67
67
|
* @returns The result of the seed function
|
|
68
68
|
* @param attrs - The attributes to pass to the seed function
|
|
69
69
|
*/
|
|
70
|
-
abstract seed<K extends keyof Seeds>(seedName: K, attrs?:
|
|
70
|
+
abstract seed<K extends keyof Seeds>(seedName: K, attrs?: ExtractSeedAttrs<Seeds[K]>): ReturnType<Seeds[K]>;
|
|
71
71
|
}
|
|
72
72
|
/**
|
|
73
73
|
* Type definition for a factory builder function that can work with different database types.
|
|
@@ -106,15 +106,16 @@ type MixedFactoryBuilder<Attrs = any, Factory = any, Result = any, DB = any> = (
|
|
|
106
106
|
* @template Result - The type of object returned by the seed
|
|
107
107
|
* @template DB - The database connection type (Kysely, Knex, etc.)
|
|
108
108
|
*
|
|
109
|
-
* @param
|
|
110
|
-
* @param
|
|
111
|
-
* @param
|
|
109
|
+
* @param context - Object containing attrs, factory, and db
|
|
110
|
+
* @param context.attrs - Configuration attributes for the seed
|
|
111
|
+
* @param context.factory - The factory instance for creating records
|
|
112
|
+
* @param context.db - The database connection
|
|
112
113
|
* @returns A promise resolving to the seed result
|
|
113
114
|
*
|
|
114
115
|
* @example
|
|
115
116
|
* ```typescript
|
|
116
117
|
* const userWithPostsSeed: FactorySeed<{ postCount?: number }, Factory, User, DB> =
|
|
117
|
-
* async (attrs, factory, db) => {
|
|
118
|
+
* async ({ attrs, factory, db }) => {
|
|
118
119
|
* const user = await factory.insert('user', attrs);
|
|
119
120
|
* const postCount = attrs.postCount || 3;
|
|
120
121
|
*
|
|
@@ -126,7 +127,20 @@ type MixedFactoryBuilder<Attrs = any, Factory = any, Result = any, DB = any> = (
|
|
|
126
127
|
* };
|
|
127
128
|
* ```
|
|
128
129
|
*/
|
|
129
|
-
type FactorySeed<Attrs = any, Factory = any, Result = any, DB = any> = (
|
|
130
|
+
type FactorySeed<Attrs = any, Factory = any, Result = any, DB = any> = (context: {
|
|
131
|
+
attrs: Attrs;
|
|
132
|
+
factory: Factory;
|
|
133
|
+
db: DB;
|
|
134
|
+
}) => Promise<Result>;
|
|
135
|
+
/**
|
|
136
|
+
* Helper type to extract the Attrs type from a FactorySeed function.
|
|
137
|
+
* Used internally by Factory implementations to correctly type the seed method parameters.
|
|
138
|
+
*/
|
|
139
|
+
type ExtractSeedAttrs<T> = T extends ((context: {
|
|
140
|
+
attrs: infer A;
|
|
141
|
+
factory: any;
|
|
142
|
+
db: any;
|
|
143
|
+
}) => any) ? A : never;
|
|
130
144
|
//#endregion
|
|
131
|
-
export { Factory, FactorySeed, MixedFactoryBuilder };
|
|
132
|
-
//# sourceMappingURL=Factory-
|
|
145
|
+
export { ExtractSeedAttrs, Factory, FactorySeed, MixedFactoryBuilder };
|
|
146
|
+
//# sourceMappingURL=Factory-BcGJjLc8.d.mts.map
|
|
@@ -36,7 +36,7 @@ var Factory = class {
|
|
|
36
36
|
*
|
|
37
37
|
* @example
|
|
38
38
|
* ```typescript
|
|
39
|
-
* const userWithPostsSeed = Factory.createSeed(async (attrs, factory, db) => {
|
|
39
|
+
* const userWithPostsSeed = Factory.createSeed(async ({ attrs, factory, db }) => {
|
|
40
40
|
* const user = await factory.insert('user', attrs);
|
|
41
41
|
* return user;
|
|
42
42
|
* });
|
|
@@ -54,4 +54,4 @@ Object.defineProperty(exports, 'Factory', {
|
|
|
54
54
|
return Factory;
|
|
55
55
|
}
|
|
56
56
|
});
|
|
57
|
-
//# sourceMappingURL=Factory-
|
|
57
|
+
//# sourceMappingURL=Factory-BhjUOBWN.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Factory-BhjUOBWN.cjs","names":["seedFn: Seed"],"sources":["../src/Factory.ts"],"sourcesContent":["import type { FakerFactory } from './faker';\n\n/**\n * Abstract base class for database factories used in testing.\n * Provides a standardized interface for creating test data using builder and seed patterns.\n *\n * @template Builders - Record of builder functions for creating individual entities\n * @template Seeds - Record of seed functions for creating complex test scenarios\n *\n * @example\n * ```typescript\n * // Define builders for creating individual records\n * const builders = {\n * user: (attrs) => ({ name: 'Test User', email: 'test@example.com', ...attrs }),\n * post: (attrs) => ({ title: 'Test Post', content: 'Content', ...attrs })\n * };\n *\n * // Define seeds for complex scenarios\n * const seeds = {\n * userWithPosts: async (attrs, factory) => {\n * const user = await factory.insert('user', attrs);\n * await factory.insertMany(3, 'post', { userId: user.id });\n * return user;\n * }\n * };\n * ```\n */\nexport abstract class Factory<\n Builders extends Record<string, any>,\n Seeds extends Record<string, any>,\n> {\n /**\n * Creates a typed seed function with proper type inference.\n * This is a utility method to help with TypeScript type checking when defining seeds.\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 * @example\n * ```typescript\n * const userWithPostsSeed = Factory.createSeed(async ({ attrs, factory, db }) => {\n * const user = await factory.insert('user', attrs);\n * return user;\n * });\n * ```\n */\n static createSeed<Seed extends FactorySeed>(seedFn: Seed): Seed {\n return seedFn;\n }\n /**\n * Inserts an object into the database using a builder function.\n *\n * @param builderName - The name of the builder to use\n * @param attrs - The attributes to insert\n */\n abstract insert<K extends keyof Builders>(\n builderName: K,\n attrs?: Parameters<Builders[K]>[0],\n ): Promise<Awaited<ReturnType<Builders[K]>>>;\n\n /**\n * Inserts multiple objects into the database\n *\n * @param count - Number of objects to insert\n * @param builderName - The name of the builder to use\n * @param attrs - The attributes to insert\n */\n abstract insertMany<K extends keyof Builders>(\n count: number,\n builderName: K,\n attrs?:\n | Parameters<Builders[K]>[0]\n | ((\n idx: number,\n faker: FakerFactory,\n ) => Promise<Parameters<Builders[K]>[0]>),\n ): Promise<Awaited<ReturnType<Builders[K]>>[]>;\n\n /**\n * Seeds the database using a seed function.\n *\n * @param seedName - The name of the seed to use\n * @returns The result of the seed function\n * @param attrs - The attributes to pass to the seed function\n */\n abstract seed<K extends keyof Seeds>(\n seedName: K,\n attrs?: ExtractSeedAttrs<Seeds[K]>,\n ): ReturnType<Seeds[K]>;\n}\n\n/**\n * Type definition for a factory builder function that can work with different database types.\n * Builders are responsible for creating individual database records with default values and relationships.\n *\n * @template Attrs - The attributes/input type for the builder\n * @template Factory - The factory instance type\n * @template Result - The type of object returned by the builder\n * @template DB - The database connection type (Kysely, Knex, etc.)\n *\n * @param attrs - Partial attributes to override defaults\n * @param factory - The factory instance for creating related records\n * @param db - The database connection\n * @returns The created record or a promise resolving to it\n *\n * @example\n * ```typescript\n * const userBuilder: MixedFactoryBuilder<UserAttrs, Factory, User, Kysely<DB>> =\n * async (attrs, factory, db) => {\n * return {\n * id: faker.string.uuid(),\n * name: faker.person.fullName(),\n * email: faker.internet.email(),\n * ...attrs\n * };\n * };\n * ```\n */\nexport type MixedFactoryBuilder<\n Attrs = any,\n Factory = any,\n Result = any,\n DB = any,\n> = (attrs: Attrs, factory: Factory, db: DB) => Result | Promise<Result>;\n\n/**\n * Type definition for a factory seed function used to create complex test scenarios.\n * Seeds typically create multiple related records to set up a complete test environment.\n *\n * @template Attrs - The attributes/input type for the seed\n * @template Factory - The factory instance type\n * @template Result - The type of object returned by the seed\n * @template DB - The database connection type (Kysely, Knex, etc.)\n *\n * @param context - Object containing attrs, factory, and db\n * @param context.attrs - Configuration attributes for the seed\n * @param context.factory - The factory instance for creating records\n * @param context.db - The database connection\n * @returns A promise resolving to the seed result\n *\n * @example\n * ```typescript\n * const userWithPostsSeed: FactorySeed<{ postCount?: number }, Factory, User, DB> =\n * async ({ attrs, factory, db }) => {\n * const user = await factory.insert('user', attrs);\n * const postCount = attrs.postCount || 3;\n *\n * for (let i = 0; i < postCount; i++) {\n * await factory.insert('post', { userId: user.id });\n * }\n *\n * return user;\n * };\n * ```\n */\nexport type FactorySeed<\n Attrs = any,\n Factory = any,\n Result = any,\n DB = any,\n> = (context: { attrs: Attrs; factory: Factory; db: DB }) => Promise<Result>;\n\n/**\n * Helper type to extract the Attrs type from a FactorySeed function.\n * Used internally by Factory implementations to correctly type the seed method parameters.\n */\nexport type ExtractSeedAttrs<T> = T extends (context: {\n attrs: infer A;\n factory: any;\n db: any;\n}) => any\n ? A\n : never;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,IAAsB,UAAtB,MAGE;;;;;;;;;;;;;;;;;CAiBA,OAAO,WAAqCA,QAAoB;AAC9D,SAAO;CACR;AAyCF"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FakerFactory } from "./faker-
|
|
1
|
+
import { FakerFactory } from "./faker-CbYiF-8_.cjs";
|
|
2
2
|
|
|
3
3
|
//#region src/Factory.d.ts
|
|
4
4
|
|
|
@@ -38,7 +38,7 @@ declare abstract class Factory<Builders extends Record<string, any>, Seeds exten
|
|
|
38
38
|
*
|
|
39
39
|
* @example
|
|
40
40
|
* ```typescript
|
|
41
|
-
* const userWithPostsSeed = Factory.createSeed(async (attrs, factory, db) => {
|
|
41
|
+
* const userWithPostsSeed = Factory.createSeed(async ({ attrs, factory, db }) => {
|
|
42
42
|
* const user = await factory.insert('user', attrs);
|
|
43
43
|
* return user;
|
|
44
44
|
* });
|
|
@@ -67,7 +67,7 @@ declare abstract class Factory<Builders extends Record<string, any>, Seeds exten
|
|
|
67
67
|
* @returns The result of the seed function
|
|
68
68
|
* @param attrs - The attributes to pass to the seed function
|
|
69
69
|
*/
|
|
70
|
-
abstract seed<K extends keyof Seeds>(seedName: K, attrs?:
|
|
70
|
+
abstract seed<K extends keyof Seeds>(seedName: K, attrs?: ExtractSeedAttrs<Seeds[K]>): ReturnType<Seeds[K]>;
|
|
71
71
|
}
|
|
72
72
|
/**
|
|
73
73
|
* Type definition for a factory builder function that can work with different database types.
|
|
@@ -106,15 +106,16 @@ type MixedFactoryBuilder<Attrs = any, Factory = any, Result = any, DB = any> = (
|
|
|
106
106
|
* @template Result - The type of object returned by the seed
|
|
107
107
|
* @template DB - The database connection type (Kysely, Knex, etc.)
|
|
108
108
|
*
|
|
109
|
-
* @param
|
|
110
|
-
* @param
|
|
111
|
-
* @param
|
|
109
|
+
* @param context - Object containing attrs, factory, and db
|
|
110
|
+
* @param context.attrs - Configuration attributes for the seed
|
|
111
|
+
* @param context.factory - The factory instance for creating records
|
|
112
|
+
* @param context.db - The database connection
|
|
112
113
|
* @returns A promise resolving to the seed result
|
|
113
114
|
*
|
|
114
115
|
* @example
|
|
115
116
|
* ```typescript
|
|
116
117
|
* const userWithPostsSeed: FactorySeed<{ postCount?: number }, Factory, User, DB> =
|
|
117
|
-
* async (attrs, factory, db) => {
|
|
118
|
+
* async ({ attrs, factory, db }) => {
|
|
118
119
|
* const user = await factory.insert('user', attrs);
|
|
119
120
|
* const postCount = attrs.postCount || 3;
|
|
120
121
|
*
|
|
@@ -126,7 +127,20 @@ type MixedFactoryBuilder<Attrs = any, Factory = any, Result = any, DB = any> = (
|
|
|
126
127
|
* };
|
|
127
128
|
* ```
|
|
128
129
|
*/
|
|
129
|
-
type FactorySeed<Attrs = any, Factory = any, Result = any, DB = any> = (
|
|
130
|
+
type FactorySeed<Attrs = any, Factory = any, Result = any, DB = any> = (context: {
|
|
131
|
+
attrs: Attrs;
|
|
132
|
+
factory: Factory;
|
|
133
|
+
db: DB;
|
|
134
|
+
}) => Promise<Result>;
|
|
135
|
+
/**
|
|
136
|
+
* Helper type to extract the Attrs type from a FactorySeed function.
|
|
137
|
+
* Used internally by Factory implementations to correctly type the seed method parameters.
|
|
138
|
+
*/
|
|
139
|
+
type ExtractSeedAttrs<T> = T extends ((context: {
|
|
140
|
+
attrs: infer A;
|
|
141
|
+
factory: any;
|
|
142
|
+
db: any;
|
|
143
|
+
}) => any) ? A : never;
|
|
130
144
|
//#endregion
|
|
131
|
-
export { Factory, FactorySeed, MixedFactoryBuilder };
|
|
132
|
-
//# sourceMappingURL=Factory-
|
|
145
|
+
export { ExtractSeedAttrs, Factory, FactorySeed, MixedFactoryBuilder };
|
|
146
|
+
//# sourceMappingURL=Factory-IdfEkOeT.d.cts.map
|
package/dist/Factory.cjs
CHANGED
package/dist/Factory.d.cts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import "./faker-
|
|
2
|
-
import { Factory, FactorySeed, MixedFactoryBuilder } from "./Factory-
|
|
3
|
-
export { Factory, FactorySeed, MixedFactoryBuilder };
|
|
1
|
+
import "./faker-CbYiF-8_.cjs";
|
|
2
|
+
import { ExtractSeedAttrs, Factory, FactorySeed, MixedFactoryBuilder } from "./Factory-IdfEkOeT.cjs";
|
|
3
|
+
export { ExtractSeedAttrs, Factory, FactorySeed, MixedFactoryBuilder };
|
package/dist/Factory.d.mts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import "./faker-
|
|
2
|
-
import { Factory, FactorySeed, MixedFactoryBuilder } from "./Factory-
|
|
3
|
-
export { Factory, FactorySeed, MixedFactoryBuilder };
|
|
1
|
+
import "./faker-DHh7xs4u.mjs";
|
|
2
|
+
import { ExtractSeedAttrs, Factory, FactorySeed, MixedFactoryBuilder } from "./Factory-BcGJjLc8.mjs";
|
|
3
|
+
export { ExtractSeedAttrs, Factory, FactorySeed, MixedFactoryBuilder };
|
package/dist/Factory.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_Factory = require('./Factory-
|
|
1
|
+
const require_Factory = require('./Factory-BhjUOBWN.cjs');
|
|
2
2
|
const require_faker = require('./faker-B14IEMIN.cjs');
|
|
3
3
|
|
|
4
4
|
//#region src/KyselyFactory.ts
|
|
@@ -46,8 +46,21 @@ var KyselyFactory = class extends require_Factory.Factory {
|
|
|
46
46
|
* Inherits from the base Factory class implementation.
|
|
47
47
|
*
|
|
48
48
|
* @template Seed - The seed function type
|
|
49
|
-
* @param seedFn - The seed function to wrap
|
|
49
|
+
* @param seedFn - The seed function to wrap (receives { attrs, factory, db } object)
|
|
50
50
|
* @returns The same seed function with proper typing
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const seeds = {
|
|
55
|
+
* userWithPosts: KyselyFactory.createSeed(
|
|
56
|
+
* async ({ attrs, factory }) => {
|
|
57
|
+
* const user = await factory.insert('user', attrs);
|
|
58
|
+
* await factory.insertMany(3, 'post', { userId: user.id });
|
|
59
|
+
* return user;
|
|
60
|
+
* },
|
|
61
|
+
* ),
|
|
62
|
+
* };
|
|
63
|
+
* ```
|
|
51
64
|
*/
|
|
52
65
|
static createSeed(seedFn) {
|
|
53
66
|
return require_Factory.Factory.createSeed(seedFn);
|
|
@@ -215,7 +228,11 @@ var KyselyFactory = class extends require_Factory.Factory {
|
|
|
215
228
|
*/
|
|
216
229
|
seed(seedName, attrs) {
|
|
217
230
|
if (!(seedName in this.seeds)) throw new Error(`Seed "${seedName}" is not registered in this factory. Make sure it is correct and registered in src/test/setup.ts`);
|
|
218
|
-
return this.seeds[seedName](
|
|
231
|
+
return this.seeds[seedName]({
|
|
232
|
+
attrs: attrs || {},
|
|
233
|
+
factory: this,
|
|
234
|
+
db: this.db
|
|
235
|
+
});
|
|
219
236
|
}
|
|
220
237
|
};
|
|
221
238
|
|
|
@@ -226,4 +243,4 @@ Object.defineProperty(exports, 'KyselyFactory', {
|
|
|
226
243
|
return KyselyFactory;
|
|
227
244
|
}
|
|
228
245
|
});
|
|
229
|
-
//# sourceMappingURL=KyselyFactory-
|
|
246
|
+
//# sourceMappingURL=KyselyFactory-BFqVIn_0.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"KyselyFactory-BFqVIn_0.cjs","names":["Factory","seedFn: Seed","builders: Builders","seeds: Seeds","db: Kysely<DB> | ControlledTransaction<DB, []>","table: TableName","defaults?: (context: {\n attrs: Attrs;\n factory: Factory;\n db: Kysely<DB>;\n faker: FakerFactory;\n }) =>\n | Partial<Insertable<DB[TableName]>>\n | Promise<Partial<Insertable<DB[TableName]>>>","autoInsert?: boolean","attrs: Attrs","factory: Factory","db: Kysely<DB>","fakerInstance: FakerFactory","data: Partial<Insertable<DB[TableName]>>","builderName: K","attrs?: Parameters<Builders[K]>[0]","faker","count: number","attrs?: any","promises: Promise<any>[]","seedName: K","attrs?: ExtractSeedAttrs<Seeds[K]>"],"sources":["../src/KyselyFactory.ts"],"sourcesContent":["import type {\n ControlledTransaction,\n Insertable,\n Kysely,\n Selectable,\n} from 'kysely';\nimport { type ExtractSeedAttrs, Factory, type FactorySeed } from './Factory.ts';\nimport { type FakerFactory, faker } from './faker.ts';\n\n/**\n * Factory implementation for Kysely ORM, providing test data creation utilities.\n * Extends the base Factory class with Kysely-specific database operations.\n *\n * @template DB - The database schema type\n * @template Builders - Record of builder functions for creating entities\n * @template Seeds - Record of seed functions for complex test scenarios\n *\n * @example\n * ```typescript\n * // Define your database schema\n * interface Database {\n * users: UsersTable;\n * posts: PostsTable;\n * }\n *\n * // Create builders\n * const builders = {\n * user: KyselyFactory.createBuilder<Database, 'users'>('users', ({ attrs, faker }) => ({\n * id: faker.string.uuid(),\n * name: faker.person.fullName(),\n * email: faker.internet.email(),\n * ...attrs\n * })),\n * post: KyselyFactory.createBuilder<Database, 'posts'>('posts', ({ attrs }) => ({\n * title: 'Test Post',\n * content: 'Test content',\n * ...attrs\n * }))\n * };\n *\n * // Create factory instance\n * const factory = new KyselyFactory(builders, seeds, db);\n *\n * // Use in tests\n * const user = await factory.insert('user', { name: 'John Doe' });\n * ```\n */\nexport class KyselyFactory<\n DB,\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 (receives { attrs, factory, db } object)\n * @returns The same seed function with proper typing\n *\n * @example\n * ```typescript\n * const seeds = {\n * userWithPosts: KyselyFactory.createSeed(\n * async ({ attrs, factory }) => {\n * const user = await factory.insert('user', attrs);\n * await factory.insertMany(3, 'post', { userId: user.id });\n * return user;\n * },\n * ),\n * };\n * ```\n */\n static createSeed<Seed extends FactorySeed>(seedFn: Seed): Seed {\n return Factory.createSeed(seedFn);\n }\n\n /**\n * Creates a new KyselyFactory 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 - Kysely database instance or controlled transaction\n */\n constructor(\n private builders: Builders,\n private seeds: Seeds,\n private db: Kysely<DB> | ControlledTransaction<DB, []>,\n ) {\n super();\n }\n\n /**\n * Creates a typed builder function for a specific database table.\n * This is a utility method that helps create builders with proper type inference for Kysely.\n *\n * @template DB - The database schema type\n * @template TableName - The name of the table (must be a key of DB)\n * @template Attrs - The attributes type for the builder (defaults to Partial<Insertable>)\n * @template Factory - The factory instance type\n * @template Result - The result type (defaults to Selectable of the table)\n *\n * @param table - The name of the database table\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 = KyselyFactory.createBuilder<DB, 'users'>('users',\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 = KyselyFactory.createBuilder<DB, 'leaveTypes'>('leaveTypes',\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 = KyselyFactory.createBuilder<DB, 'addresses'>('addresses',\n * ({ attrs }) => ({\n * street: '123 Main St',\n * city: 'Anytown',\n * ...attrs\n * }),\n * false // Don't auto-insert\n * );\n * ```\n */\n static createBuilder<\n DB,\n TableName extends keyof DB & string,\n Attrs extends Partial<Insertable<DB[TableName]>> = Partial<\n Insertable<DB[TableName]>\n >,\n Factory = any,\n Result = Selectable<DB[TableName]>,\n >(\n table: TableName,\n defaults?: (context: {\n attrs: Attrs;\n factory: Factory;\n db: Kysely<DB>;\n faker: FakerFactory;\n }) =>\n | Partial<Insertable<DB[TableName]>>\n | Promise<Partial<Insertable<DB[TableName]>>>,\n autoInsert?: boolean,\n ): (\n attrs: Attrs,\n factory: Factory,\n db: Kysely<DB>,\n faker: FakerFactory,\n ) => Promise<Result> {\n return async (\n attrs: Attrs,\n factory: Factory,\n db: Kysely<DB>,\n fakerInstance: FakerFactory,\n ) => {\n // Start with attributes\n let data: Partial<Insertable<DB[TableName]>> = { ...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 // Handle insertion based on autoInsert flag\n if (autoInsert !== false) {\n // Auto insert is enabled by default\n const result = await db\n .insertInto(table)\n .values(data as Insertable<DB[TableName]>)\n .returningAll()\n .executeTakeFirst();\n\n if (!result) {\n throw new Error(`Failed to insert into ${table}`);\n }\n\n return result as Result;\n } else {\n // Return object for factory to handle insertion\n return { table, data } as any;\n }\n };\n }\n\n /**\n * Inserts a single record into the database using the specified builder.\n * The builder function is responsible for generating the record data with defaults\n * and the factory handles the actual database insertion.\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\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 * // Use the inserted record\n * const post = await factory.insert('post', {\n * userId: user.id,\n * title: 'My First Post'\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 // For Kysely, we expect the builder to return an object with table and data properties\n // or to handle the insertion itself and return the inserted record\n if (\n result &&\n typeof result === 'object' &&\n 'table' in result &&\n 'data' in result\n ) {\n // If the builder returns {table: string, data: object}, we insert it\n const inserted = await this.db\n .insertInto(result.table)\n .values(result.data)\n .returningAll()\n .executeTakeFirst();\n\n return inserted as any;\n }\n\n // Otherwise, assume the builder handled the insertion itself\n return result;\n }\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 * @template K - The builder name (must be a key of Builders)\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, faker) => ({\n * title: `Post ${idx + 1}`,\n * content: faker.lorem.paragraph(),\n * publishedAt: faker.date.past()\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: (\n idx: number,\n faker: FakerFactory,\n ) => Parameters<Builders[K]>[0] | Promise<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 promises: Promise<any>[] = [];\n\n for (let i = 0; i < count; i++) {\n const newAttrs =\n typeof attrs === 'function' ? await attrs(i, faker) : attrs;\n promises.push(this.insert(builderName, newAttrs));\n }\n\n return Promise.all(promises);\n }\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\n * const company = await factory.seed('companyWithDepartments', {\n * departmentCount: 3,\n * employeesPerDepartment: 10\n * });\n * expect(company.departments).toHaveLength(3);\n * ```\n */\n seed<K extends keyof Seeds>(\n seedName: K,\n attrs?: ExtractSeedAttrs<Seeds[K]>,\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]({\n attrs: attrs || {},\n factory: this,\n db: this.db,\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CA,IAAa,gBAAb,cAIUA,wBAAyB;;;;;;;;;;;;;;;;;;;;;;CAsBjC,OAAO,WAAqCC,QAAoB;AAC9D,SAAO,wBAAQ,WAAW,OAAO;CAClC;;;;;;;;CASD,YACUC,UACAC,OACAC,IACR;AACA,SAAO;EAJC;EACA;EACA;CAGT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiDD,OAAO,cASLC,OACAC,UAQAC,YAMmB;AACnB,SAAO,OACLC,OACAC,SACAC,IACAC,kBACG;GAEH,IAAIC,OAA2C,EAAE,GAAG,MAAO;AAG3D,OAAI,UAAU;IACZ,MAAM,gBAAgB,MAAM,SAAS;KACnC;KACA;KACA;KACA,OAAO;IACR,EAAC;AACF,WAAO;KAAE,GAAG;KAAe,GAAG;IAAM;GACrC;AAGD,OAAI,eAAe,OAAO;IAExB,MAAM,SAAS,MAAM,GAClB,WAAW,MAAM,CACjB,OAAO,KAAkC,CACzC,cAAc,CACd,kBAAkB;AAErB,SAAK,OACH,OAAM,IAAI,OAAO,wBAAwB,MAAM;AAGjD,WAAO;GACR,MAEC,QAAO;IAAE;IAAO;GAAM;EAEzB;CACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+BD,MAAM,OACJC,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;AAID,MACE,iBACO,WAAW,YAClB,WAAW,UACX,UAAU,QACV;GAEA,MAAM,WAAW,MAAM,KAAK,GACzB,WAAW,OAAO,MAAM,CACxB,OAAO,OAAO,KAAK,CACnB,cAAc,CACd,kBAAkB;AAErB,UAAO;EACR;AAGD,SAAO;CACR;CA8CD,MAAM,WACJC,OACAH,aACAI,OAC6C;AAC7C,QAAM,eAAe,KAAK,UACxB,OAAM,IAAI,OACP,WACC,YACD;EAIL,MAAMC,WAA2B,CAAE;AAEnC,OAAK,IAAI,IAAI,GAAG,IAAI,OAAO,KAAK;GAC9B,MAAM,kBACG,UAAU,aAAa,MAAM,MAAM,GAAGH,oBAAM,GAAG;AACxD,YAAS,KAAK,KAAK,OAAO,aAAa,SAAS,CAAC;EAClD;AAED,SAAO,QAAQ,IAAI,SAAS;CAC7B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+BD,KACEI,UACAC,OACsB;AACtB,QAAM,YAAY,KAAK,OACrB,OAAM,IAAI,OACP,QACC,SACD;AAIL,SAAO,KAAK,MAAM,UAAU;GAC1B,OAAO,SAAS,CAAE;GAClB,SAAS;GACT,IAAI,KAAK;EACV,EAAC;CACH;AACF"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { FakerFactory } from "./faker-
|
|
2
|
-
import { Factory, FactorySeed } from "./Factory-
|
|
1
|
+
import { FakerFactory } from "./faker-DHh7xs4u.mjs";
|
|
2
|
+
import { ExtractSeedAttrs, Factory, FactorySeed } from "./Factory-BcGJjLc8.mjs";
|
|
3
3
|
import { ControlledTransaction, Insertable, Kysely, Selectable } from "kysely";
|
|
4
4
|
|
|
5
5
|
//#region src/KyselyFactory.d.ts
|
|
@@ -51,8 +51,21 @@ declare class KyselyFactory<DB, Builders extends Record<string, any>, Seeds exte
|
|
|
51
51
|
* Inherits from the base Factory class implementation.
|
|
52
52
|
*
|
|
53
53
|
* @template Seed - The seed function type
|
|
54
|
-
* @param seedFn - The seed function to wrap
|
|
54
|
+
* @param seedFn - The seed function to wrap (receives { attrs, factory, db } object)
|
|
55
55
|
* @returns The same seed function with proper typing
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const seeds = {
|
|
60
|
+
* userWithPosts: KyselyFactory.createSeed(
|
|
61
|
+
* async ({ attrs, factory }) => {
|
|
62
|
+
* const user = await factory.insert('user', attrs);
|
|
63
|
+
* await factory.insertMany(3, 'post', { userId: user.id });
|
|
64
|
+
* return user;
|
|
65
|
+
* },
|
|
66
|
+
* ),
|
|
67
|
+
* };
|
|
68
|
+
* ```
|
|
56
69
|
*/
|
|
57
70
|
static createSeed<Seed extends FactorySeed>(seedFn: Seed): Seed;
|
|
58
71
|
/**
|
|
@@ -207,8 +220,8 @@ declare class KyselyFactory<DB, Builders extends Record<string, any>, Seeds exte
|
|
|
207
220
|
* expect(company.departments).toHaveLength(3);
|
|
208
221
|
* ```
|
|
209
222
|
*/
|
|
210
|
-
seed<K extends keyof Seeds>(seedName: K, attrs?:
|
|
223
|
+
seed<K extends keyof Seeds>(seedName: K, attrs?: ExtractSeedAttrs<Seeds[K]>): ReturnType<Seeds[K]>;
|
|
211
224
|
}
|
|
212
225
|
//#endregion
|
|
213
226
|
export { KyselyFactory };
|
|
214
|
-
//# sourceMappingURL=KyselyFactory-
|
|
227
|
+
//# sourceMappingURL=KyselyFactory-Cj-EultY.d.mts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Factory } from "./Factory-
|
|
1
|
+
import { Factory } from "./Factory-BFVnMMCC.mjs";
|
|
2
2
|
import { faker } from "./faker-BGKYFoCT.mjs";
|
|
3
3
|
|
|
4
4
|
//#region src/KyselyFactory.ts
|
|
@@ -46,8 +46,21 @@ var KyselyFactory = class extends Factory {
|
|
|
46
46
|
* Inherits from the base Factory class implementation.
|
|
47
47
|
*
|
|
48
48
|
* @template Seed - The seed function type
|
|
49
|
-
* @param seedFn - The seed function to wrap
|
|
49
|
+
* @param seedFn - The seed function to wrap (receives { attrs, factory, db } object)
|
|
50
50
|
* @returns The same seed function with proper typing
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const seeds = {
|
|
55
|
+
* userWithPosts: KyselyFactory.createSeed(
|
|
56
|
+
* async ({ attrs, factory }) => {
|
|
57
|
+
* const user = await factory.insert('user', attrs);
|
|
58
|
+
* await factory.insertMany(3, 'post', { userId: user.id });
|
|
59
|
+
* return user;
|
|
60
|
+
* },
|
|
61
|
+
* ),
|
|
62
|
+
* };
|
|
63
|
+
* ```
|
|
51
64
|
*/
|
|
52
65
|
static createSeed(seedFn) {
|
|
53
66
|
return Factory.createSeed(seedFn);
|
|
@@ -215,10 +228,14 @@ var KyselyFactory = class extends Factory {
|
|
|
215
228
|
*/
|
|
216
229
|
seed(seedName, attrs) {
|
|
217
230
|
if (!(seedName in this.seeds)) throw new Error(`Seed "${seedName}" is not registered in this factory. Make sure it is correct and registered in src/test/setup.ts`);
|
|
218
|
-
return this.seeds[seedName](
|
|
231
|
+
return this.seeds[seedName]({
|
|
232
|
+
attrs: attrs || {},
|
|
233
|
+
factory: this,
|
|
234
|
+
db: this.db
|
|
235
|
+
});
|
|
219
236
|
}
|
|
220
237
|
};
|
|
221
238
|
|
|
222
239
|
//#endregion
|
|
223
240
|
export { KyselyFactory };
|
|
224
|
-
//# sourceMappingURL=KyselyFactory-
|
|
241
|
+
//# sourceMappingURL=KyselyFactory-DMswpwji.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"KyselyFactory-DMswpwji.mjs","names":["seedFn: Seed","builders: Builders","seeds: Seeds","db: Kysely<DB> | ControlledTransaction<DB, []>","table: TableName","defaults?: (context: {\n attrs: Attrs;\n factory: Factory;\n db: Kysely<DB>;\n faker: FakerFactory;\n }) =>\n | Partial<Insertable<DB[TableName]>>\n | Promise<Partial<Insertable<DB[TableName]>>>","autoInsert?: boolean","attrs: Attrs","factory: Factory","db: Kysely<DB>","fakerInstance: FakerFactory","data: Partial<Insertable<DB[TableName]>>","builderName: K","attrs?: Parameters<Builders[K]>[0]","count: number","attrs?: any","promises: Promise<any>[]","seedName: K","attrs?: ExtractSeedAttrs<Seeds[K]>"],"sources":["../src/KyselyFactory.ts"],"sourcesContent":["import type {\n ControlledTransaction,\n Insertable,\n Kysely,\n Selectable,\n} from 'kysely';\nimport { type ExtractSeedAttrs, Factory, type FactorySeed } from './Factory.ts';\nimport { type FakerFactory, faker } from './faker.ts';\n\n/**\n * Factory implementation for Kysely ORM, providing test data creation utilities.\n * Extends the base Factory class with Kysely-specific database operations.\n *\n * @template DB - The database schema type\n * @template Builders - Record of builder functions for creating entities\n * @template Seeds - Record of seed functions for complex test scenarios\n *\n * @example\n * ```typescript\n * // Define your database schema\n * interface Database {\n * users: UsersTable;\n * posts: PostsTable;\n * }\n *\n * // Create builders\n * const builders = {\n * user: KyselyFactory.createBuilder<Database, 'users'>('users', ({ attrs, faker }) => ({\n * id: faker.string.uuid(),\n * name: faker.person.fullName(),\n * email: faker.internet.email(),\n * ...attrs\n * })),\n * post: KyselyFactory.createBuilder<Database, 'posts'>('posts', ({ attrs }) => ({\n * title: 'Test Post',\n * content: 'Test content',\n * ...attrs\n * }))\n * };\n *\n * // Create factory instance\n * const factory = new KyselyFactory(builders, seeds, db);\n *\n * // Use in tests\n * const user = await factory.insert('user', { name: 'John Doe' });\n * ```\n */\nexport class KyselyFactory<\n DB,\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 (receives { attrs, factory, db } object)\n * @returns The same seed function with proper typing\n *\n * @example\n * ```typescript\n * const seeds = {\n * userWithPosts: KyselyFactory.createSeed(\n * async ({ attrs, factory }) => {\n * const user = await factory.insert('user', attrs);\n * await factory.insertMany(3, 'post', { userId: user.id });\n * return user;\n * },\n * ),\n * };\n * ```\n */\n static createSeed<Seed extends FactorySeed>(seedFn: Seed): Seed {\n return Factory.createSeed(seedFn);\n }\n\n /**\n * Creates a new KyselyFactory 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 - Kysely database instance or controlled transaction\n */\n constructor(\n private builders: Builders,\n private seeds: Seeds,\n private db: Kysely<DB> | ControlledTransaction<DB, []>,\n ) {\n super();\n }\n\n /**\n * Creates a typed builder function for a specific database table.\n * This is a utility method that helps create builders with proper type inference for Kysely.\n *\n * @template DB - The database schema type\n * @template TableName - The name of the table (must be a key of DB)\n * @template Attrs - The attributes type for the builder (defaults to Partial<Insertable>)\n * @template Factory - The factory instance type\n * @template Result - The result type (defaults to Selectable of the table)\n *\n * @param table - The name of the database table\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 = KyselyFactory.createBuilder<DB, 'users'>('users',\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 = KyselyFactory.createBuilder<DB, 'leaveTypes'>('leaveTypes',\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 = KyselyFactory.createBuilder<DB, 'addresses'>('addresses',\n * ({ attrs }) => ({\n * street: '123 Main St',\n * city: 'Anytown',\n * ...attrs\n * }),\n * false // Don't auto-insert\n * );\n * ```\n */\n static createBuilder<\n DB,\n TableName extends keyof DB & string,\n Attrs extends Partial<Insertable<DB[TableName]>> = Partial<\n Insertable<DB[TableName]>\n >,\n Factory = any,\n Result = Selectable<DB[TableName]>,\n >(\n table: TableName,\n defaults?: (context: {\n attrs: Attrs;\n factory: Factory;\n db: Kysely<DB>;\n faker: FakerFactory;\n }) =>\n | Partial<Insertable<DB[TableName]>>\n | Promise<Partial<Insertable<DB[TableName]>>>,\n autoInsert?: boolean,\n ): (\n attrs: Attrs,\n factory: Factory,\n db: Kysely<DB>,\n faker: FakerFactory,\n ) => Promise<Result> {\n return async (\n attrs: Attrs,\n factory: Factory,\n db: Kysely<DB>,\n fakerInstance: FakerFactory,\n ) => {\n // Start with attributes\n let data: Partial<Insertable<DB[TableName]>> = { ...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 // Handle insertion based on autoInsert flag\n if (autoInsert !== false) {\n // Auto insert is enabled by default\n const result = await db\n .insertInto(table)\n .values(data as Insertable<DB[TableName]>)\n .returningAll()\n .executeTakeFirst();\n\n if (!result) {\n throw new Error(`Failed to insert into ${table}`);\n }\n\n return result as Result;\n } else {\n // Return object for factory to handle insertion\n return { table, data } as any;\n }\n };\n }\n\n /**\n * Inserts a single record into the database using the specified builder.\n * The builder function is responsible for generating the record data with defaults\n * and the factory handles the actual database insertion.\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\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 * // Use the inserted record\n * const post = await factory.insert('post', {\n * userId: user.id,\n * title: 'My First Post'\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 // For Kysely, we expect the builder to return an object with table and data properties\n // or to handle the insertion itself and return the inserted record\n if (\n result &&\n typeof result === 'object' &&\n 'table' in result &&\n 'data' in result\n ) {\n // If the builder returns {table: string, data: object}, we insert it\n const inserted = await this.db\n .insertInto(result.table)\n .values(result.data)\n .returningAll()\n .executeTakeFirst();\n\n return inserted as any;\n }\n\n // Otherwise, assume the builder handled the insertion itself\n return result;\n }\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 * @template K - The builder name (must be a key of Builders)\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, faker) => ({\n * title: `Post ${idx + 1}`,\n * content: faker.lorem.paragraph(),\n * publishedAt: faker.date.past()\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: (\n idx: number,\n faker: FakerFactory,\n ) => Parameters<Builders[K]>[0] | Promise<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 promises: Promise<any>[] = [];\n\n for (let i = 0; i < count; i++) {\n const newAttrs =\n typeof attrs === 'function' ? await attrs(i, faker) : attrs;\n promises.push(this.insert(builderName, newAttrs));\n }\n\n return Promise.all(promises);\n }\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\n * const company = await factory.seed('companyWithDepartments', {\n * departmentCount: 3,\n * employeesPerDepartment: 10\n * });\n * expect(company.departments).toHaveLength(3);\n * ```\n */\n seed<K extends keyof Seeds>(\n seedName: K,\n attrs?: ExtractSeedAttrs<Seeds[K]>,\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]({\n attrs: attrs || {},\n factory: this,\n db: this.db,\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CA,IAAa,gBAAb,cAIU,QAAyB;;;;;;;;;;;;;;;;;;;;;;CAsBjC,OAAO,WAAqCA,QAAoB;AAC9D,SAAO,QAAQ,WAAW,OAAO;CAClC;;;;;;;;CASD,YACUC,UACAC,OACAC,IACR;AACA,SAAO;EAJC;EACA;EACA;CAGT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiDD,OAAO,cASLC,OACAC,UAQAC,YAMmB;AACnB,SAAO,OACLC,OACAC,SACAC,IACAC,kBACG;GAEH,IAAIC,OAA2C,EAAE,GAAG,MAAO;AAG3D,OAAI,UAAU;IACZ,MAAM,gBAAgB,MAAM,SAAS;KACnC;KACA;KACA;KACA,OAAO;IACR,EAAC;AACF,WAAO;KAAE,GAAG;KAAe,GAAG;IAAM;GACrC;AAGD,OAAI,eAAe,OAAO;IAExB,MAAM,SAAS,MAAM,GAClB,WAAW,MAAM,CACjB,OAAO,KAAkC,CACzC,cAAc,CACd,kBAAkB;AAErB,SAAK,OACH,OAAM,IAAI,OAAO,wBAAwB,MAAM;AAGjD,WAAO;GACR,MAEC,QAAO;IAAE;IAAO;GAAM;EAEzB;CACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+BD,MAAM,OACJC,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;AAID,MACE,iBACO,WAAW,YAClB,WAAW,UACX,UAAU,QACV;GAEA,MAAM,WAAW,MAAM,KAAK,GACzB,WAAW,OAAO,MAAM,CACxB,OAAO,OAAO,KAAK,CACnB,cAAc,CACd,kBAAkB;AAErB,UAAO;EACR;AAGD,SAAO;CACR;CA8CD,MAAM,WACJC,OACAF,aACAG,OAC6C;AAC7C,QAAM,eAAe,KAAK,UACxB,OAAM,IAAI,OACP,WACC,YACD;EAIL,MAAMC,WAA2B,CAAE;AAEnC,OAAK,IAAI,IAAI,GAAG,IAAI,OAAO,KAAK;GAC9B,MAAM,kBACG,UAAU,aAAa,MAAM,MAAM,GAAG,MAAM,GAAG;AACxD,YAAS,KAAK,KAAK,OAAO,aAAa,SAAS,CAAC;EAClD;AAED,SAAO,QAAQ,IAAI,SAAS;CAC7B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+BD,KACEC,UACAC,OACsB;AACtB,QAAM,YAAY,KAAK,OACrB,OAAM,IAAI,OACP,QACC,SACD;AAIL,SAAO,KAAK,MAAM,UAAU;GAC1B,OAAO,SAAS,CAAE;GAClB,SAAS;GACT,IAAI,KAAK;EACV,EAAC;CACH;AACF"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { FakerFactory } from "./faker-
|
|
2
|
-
import { Factory, FactorySeed } from "./Factory-
|
|
1
|
+
import { FakerFactory } from "./faker-CbYiF-8_.cjs";
|
|
2
|
+
import { ExtractSeedAttrs, Factory, FactorySeed } from "./Factory-IdfEkOeT.cjs";
|
|
3
3
|
import { ControlledTransaction, Insertable, Kysely, Selectable } from "kysely";
|
|
4
4
|
|
|
5
5
|
//#region src/KyselyFactory.d.ts
|
|
@@ -51,8 +51,21 @@ declare class KyselyFactory<DB, Builders extends Record<string, any>, Seeds exte
|
|
|
51
51
|
* Inherits from the base Factory class implementation.
|
|
52
52
|
*
|
|
53
53
|
* @template Seed - The seed function type
|
|
54
|
-
* @param seedFn - The seed function to wrap
|
|
54
|
+
* @param seedFn - The seed function to wrap (receives { attrs, factory, db } object)
|
|
55
55
|
* @returns The same seed function with proper typing
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const seeds = {
|
|
60
|
+
* userWithPosts: KyselyFactory.createSeed(
|
|
61
|
+
* async ({ attrs, factory }) => {
|
|
62
|
+
* const user = await factory.insert('user', attrs);
|
|
63
|
+
* await factory.insertMany(3, 'post', { userId: user.id });
|
|
64
|
+
* return user;
|
|
65
|
+
* },
|
|
66
|
+
* ),
|
|
67
|
+
* };
|
|
68
|
+
* ```
|
|
56
69
|
*/
|
|
57
70
|
static createSeed<Seed extends FactorySeed>(seedFn: Seed): Seed;
|
|
58
71
|
/**
|
|
@@ -207,8 +220,8 @@ declare class KyselyFactory<DB, Builders extends Record<string, any>, Seeds exte
|
|
|
207
220
|
* expect(company.departments).toHaveLength(3);
|
|
208
221
|
* ```
|
|
209
222
|
*/
|
|
210
|
-
seed<K extends keyof Seeds>(seedName: K, attrs?:
|
|
223
|
+
seed<K extends keyof Seeds>(seedName: K, attrs?: ExtractSeedAttrs<Seeds[K]>): ReturnType<Seeds[K]>;
|
|
211
224
|
}
|
|
212
225
|
//#endregion
|
|
213
226
|
export { KyselyFactory };
|
|
214
|
-
//# sourceMappingURL=KyselyFactory-
|
|
227
|
+
//# sourceMappingURL=KyselyFactory-Dxhi_ZkA.d.cts.map
|
package/dist/KyselyFactory.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
require('./Factory-
|
|
1
|
+
require('./Factory-BhjUOBWN.cjs');
|
|
2
2
|
require('./faker-B14IEMIN.cjs');
|
|
3
|
-
const require_KyselyFactory = require('./KyselyFactory-
|
|
3
|
+
const require_KyselyFactory = require('./KyselyFactory-BFqVIn_0.cjs');
|
|
4
4
|
|
|
5
5
|
exports.KyselyFactory = require_KyselyFactory.KyselyFactory;
|
package/dist/KyselyFactory.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import "./faker-
|
|
2
|
-
import "./Factory-
|
|
3
|
-
import { KyselyFactory } from "./KyselyFactory-
|
|
1
|
+
import "./faker-CbYiF-8_.cjs";
|
|
2
|
+
import "./Factory-IdfEkOeT.cjs";
|
|
3
|
+
import { KyselyFactory } from "./KyselyFactory-Dxhi_ZkA.cjs";
|
|
4
4
|
export { KyselyFactory };
|
package/dist/KyselyFactory.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import "./faker-
|
|
2
|
-
import "./Factory-
|
|
3
|
-
import { KyselyFactory } from "./KyselyFactory-
|
|
1
|
+
import "./faker-DHh7xs4u.mjs";
|
|
2
|
+
import "./Factory-BcGJjLc8.mjs";
|
|
3
|
+
import { KyselyFactory } from "./KyselyFactory-Cj-EultY.mjs";
|
|
4
4
|
export { KyselyFactory };
|
package/dist/KyselyFactory.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { FakerFactory } from "./faker-
|
|
2
|
-
import { Factory, FactorySeed } from "./Factory-
|
|
1
|
+
import { FakerFactory } from "./faker-CbYiF-8_.cjs";
|
|
2
|
+
import { ExtractSeedAttrs, Factory, FactorySeed } from "./Factory-IdfEkOeT.cjs";
|
|
3
3
|
import { Knex } from "knex";
|
|
4
4
|
import { Model } from "objection";
|
|
5
5
|
|
|
@@ -50,8 +50,21 @@ declare class ObjectionFactory<Builders extends Record<string, any>, Seeds exten
|
|
|
50
50
|
* Inherits from the base Factory class implementation.
|
|
51
51
|
*
|
|
52
52
|
* @template Seed - The seed function type
|
|
53
|
-
* @param seedFn - The seed function to wrap
|
|
53
|
+
* @param seedFn - The seed function to wrap (receives { attrs, factory, db } object)
|
|
54
54
|
* @returns The same seed function with proper typing
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* const seeds = {
|
|
59
|
+
* userWithPosts: ObjectionFactory.createSeed(
|
|
60
|
+
* async ({ attrs, factory }) => {
|
|
61
|
+
* const user = await factory.insert('user', attrs);
|
|
62
|
+
* await factory.insertMany(3, 'post', { userId: user.id });
|
|
63
|
+
* return user;
|
|
64
|
+
* },
|
|
65
|
+
* ),
|
|
66
|
+
* };
|
|
67
|
+
* ```
|
|
55
68
|
*/
|
|
56
69
|
static createSeed<Seed extends FactorySeed>(seedFn: Seed): Seed;
|
|
57
70
|
/**
|
|
@@ -220,8 +233,8 @@ declare class ObjectionFactory<Builders extends Record<string, any>, Seeds exten
|
|
|
220
233
|
* .withGraphFetched('[departments.employees]');
|
|
221
234
|
* ```
|
|
222
235
|
*/
|
|
223
|
-
seed<K extends keyof Seeds>(seedName: K, attrs?:
|
|
236
|
+
seed<K extends keyof Seeds>(seedName: K, attrs?: ExtractSeedAttrs<Seeds[K]>): ReturnType<Seeds[K]>;
|
|
224
237
|
}
|
|
225
238
|
//#endregion
|
|
226
239
|
export { ObjectionFactory };
|
|
227
|
-
//# sourceMappingURL=ObjectionFactory-
|
|
240
|
+
//# sourceMappingURL=ObjectionFactory-BRAkAByP.d.cts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_Factory = require('./Factory-
|
|
1
|
+
const require_Factory = require('./Factory-BhjUOBWN.cjs');
|
|
2
2
|
const require_faker = require('./faker-B14IEMIN.cjs');
|
|
3
3
|
|
|
4
4
|
//#region src/ObjectionFactory.ts
|
|
@@ -44,8 +44,21 @@ var ObjectionFactory = class extends require_Factory.Factory {
|
|
|
44
44
|
* Inherits from the base Factory class implementation.
|
|
45
45
|
*
|
|
46
46
|
* @template Seed - The seed function type
|
|
47
|
-
* @param seedFn - The seed function to wrap
|
|
47
|
+
* @param seedFn - The seed function to wrap (receives { attrs, factory, db } object)
|
|
48
48
|
* @returns The same seed function with proper typing
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* const seeds = {
|
|
53
|
+
* userWithPosts: ObjectionFactory.createSeed(
|
|
54
|
+
* async ({ attrs, factory }) => {
|
|
55
|
+
* const user = await factory.insert('user', attrs);
|
|
56
|
+
* await factory.insertMany(3, 'post', { userId: user.id });
|
|
57
|
+
* return user;
|
|
58
|
+
* },
|
|
59
|
+
* ),
|
|
60
|
+
* };
|
|
61
|
+
* ```
|
|
49
62
|
*/
|
|
50
63
|
static createSeed(seedFn) {
|
|
51
64
|
return require_Factory.Factory.createSeed(seedFn);
|
|
@@ -241,7 +254,11 @@ var ObjectionFactory = class extends require_Factory.Factory {
|
|
|
241
254
|
*/
|
|
242
255
|
seed(seedName, attrs) {
|
|
243
256
|
if (!(seedName in this.seeds)) throw new Error(`Seed "${seedName}" is not registered in this factory. Make sure it is correct and registered in src/test/setup.ts`);
|
|
244
|
-
return this.seeds[seedName](
|
|
257
|
+
return this.seeds[seedName]({
|
|
258
|
+
attrs: attrs || {},
|
|
259
|
+
factory: this,
|
|
260
|
+
db: this.db
|
|
261
|
+
});
|
|
245
262
|
}
|
|
246
263
|
};
|
|
247
264
|
|
|
@@ -252,4 +269,4 @@ Object.defineProperty(exports, 'ObjectionFactory', {
|
|
|
252
269
|
return ObjectionFactory;
|
|
253
270
|
}
|
|
254
271
|
});
|
|
255
|
-
//# sourceMappingURL=ObjectionFactory-
|
|
272
|
+
//# sourceMappingURL=ObjectionFactory-BeFBYcan.cjs.map
|