@geekmidas/testkit 0.0.12 → 0.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/{Factory-Bm44VKa-.d.cts → Factory-D7P3bKKb.d.mts} +2 -2
- package/dist/{Factory-tjCDNgUK.d.mts → Factory-pNV7ZQ7-.d.cts} +2 -2
- package/dist/Factory.d.cts +2 -2
- package/dist/Factory.d.mts +2 -2
- package/dist/{KyselyFactory-C3Bc3p4L.mjs → KyselyFactory-BcYkC0t2.mjs} +1 -1
- package/dist/{KyselyFactory-BoPDDitt.d.cts → KyselyFactory-BrWADI77.d.mts} +3 -3
- package/dist/{KyselyFactory-CXtfmMfK.cjs → KyselyFactory-Cf0o2YxO.cjs} +1 -1
- package/dist/{KyselyFactory-D82j74t9.d.mts → KyselyFactory-DM2dnUXU.d.cts} +3 -3
- package/dist/KyselyFactory.cjs +1 -1
- package/dist/KyselyFactory.d.cts +3 -3
- package/dist/KyselyFactory.d.mts +3 -3
- package/dist/KyselyFactory.mjs +1 -1
- package/dist/{ObjectionFactory-qIICOph3.mjs → ObjectionFactory-8hebmnai.mjs} +20 -4
- package/dist/{ObjectionFactory-BWMTXsxH.d.cts → ObjectionFactory-B40NQWSe.d.mts} +2 -2
- package/dist/{ObjectionFactory-DxIxJagq.cjs → ObjectionFactory-CDriunkS.cjs} +20 -4
- package/dist/{ObjectionFactory-CEG5qUrm.d.mts → ObjectionFactory-D3l1VuyX.d.cts} +2 -2
- package/dist/ObjectionFactory.cjs +1 -1
- package/dist/ObjectionFactory.d.cts +3 -3
- package/dist/ObjectionFactory.d.mts +3 -3
- package/dist/ObjectionFactory.mjs +1 -1
- package/dist/__tests__/KyselyFactory.spec.cjs +2 -2
- package/dist/__tests__/KyselyFactory.spec.mjs +2 -2
- package/dist/__tests__/ObjectionFactory.spec.cjs +288 -450
- package/dist/__tests__/ObjectionFactory.spec.mjs +289 -451
- package/dist/__tests__/PostgresKyselyMigrator.spec.cjs +397 -0
- package/dist/__tests__/PostgresKyselyMigrator.spec.d.cts +1 -0
- package/dist/__tests__/PostgresKyselyMigrator.spec.d.mts +1 -0
- package/dist/__tests__/PostgresKyselyMigrator.spec.mjs +396 -0
- package/dist/__tests__/PostgresMigrator.spec.cjs +1 -1
- package/dist/__tests__/PostgresMigrator.spec.mjs +1 -1
- package/dist/__tests__/PostgresObjectionMigrator.spec.cjs +1 -1
- package/dist/__tests__/PostgresObjectionMigrator.spec.mjs +1 -1
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.cjs +3 -11
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.mjs +3 -11
- package/dist/__tests__/integration.spec.cjs +2 -2
- package/dist/__tests__/integration.spec.mjs +2 -2
- package/dist/{faker-km9UhOS6.d.cts → faker-BSH1EMtg.d.cts} +2 -2
- package/dist/{faker-ChuHaYMR.d.mts → faker-C-Iuk_R1.d.mts} +2 -2
- package/dist/faker.d.cts +1 -1
- package/dist/faker.d.mts +1 -1
- package/dist/{helpers-BEmjyUVE.mjs → helpers-B4TXg3Wp.mjs} +11 -36
- package/dist/{helpers-CNMBePuj.cjs → helpers-Bf0nXhbu.cjs} +10 -41
- package/dist/kysely.cjs +1 -1
- package/dist/kysely.d.cts +3 -3
- package/dist/kysely.d.mts +3 -3
- package/dist/kysely.mjs +1 -1
- package/dist/objection.cjs +1 -1
- package/dist/objection.d.cts +3 -3
- package/dist/objection.d.mts +3 -3
- package/dist/objection.mjs +1 -1
- package/package.json +2 -2
- package/src/Factory.ts +4 -1
- package/src/KyselyFactory.ts +6 -2
- package/src/ObjectionFactory.ts +31 -4
- package/src/__tests__/ObjectionFactory.spec.ts +423 -542
- package/src/__tests__/PostgresKyselyMigrator.spec.ts +690 -0
- package/src/__tests__/VitestObjectionTransactionIsolator.spec.ts +0 -8
- package/test/helpers.ts +13 -21
- package/dist/example.cjs +0 -22
- package/dist/example.d.cts +0 -26
- package/dist/example.d.mts +0 -26
- package/dist/example.mjs +0 -22
- package/src/example.ts +0 -45
|
@@ -24,23 +24,6 @@ function createKnexDb() {
|
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
|
-
* Test setup helper for Knex/Objection that creates tables within a transaction
|
|
28
|
-
*/
|
|
29
|
-
async function setupKnexTest() {
|
|
30
|
-
const db = createKnexDb();
|
|
31
|
-
const trx = await db.transaction();
|
|
32
|
-
await createTestTablesKnex(trx);
|
|
33
|
-
const cleanup = async () => {
|
|
34
|
-
await trx.rollback();
|
|
35
|
-
await db.destroy();
|
|
36
|
-
};
|
|
37
|
-
return {
|
|
38
|
-
db,
|
|
39
|
-
trx,
|
|
40
|
-
cleanup
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
27
|
* Creates test tables using Kysely
|
|
45
28
|
*/
|
|
46
29
|
async function createTestTables(db) {
|
|
@@ -52,29 +35,21 @@ async function createTestTables(db) {
|
|
|
52
35
|
* Creates test tables using Knex
|
|
53
36
|
*/
|
|
54
37
|
async function createTestTablesKnex(trx) {
|
|
55
|
-
await trx.
|
|
56
|
-
|
|
38
|
+
await trx.raw(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`);
|
|
39
|
+
await trx.schema.createTableIfNotExists("users", (table) => {
|
|
40
|
+
table.uuid("id").primary().defaultTo(trx.raw("uuid_generate_v4()"));
|
|
57
41
|
table.string("name").notNullable();
|
|
58
|
-
table.string("email").notNullable().unique();
|
|
59
|
-
table.string("role").defaultTo("user");
|
|
60
|
-
table.timestamp("created_at").defaultTo(trx.fn.now()).notNullable();
|
|
61
|
-
table.timestamp("updated_at").defaultTo(trx.fn.now()).notNullable();
|
|
62
42
|
});
|
|
63
|
-
await trx.schema.
|
|
64
|
-
table.
|
|
43
|
+
await trx.schema.createTableIfNotExists("posts", (table) => {
|
|
44
|
+
table.uuid("id").primary().defaultTo(trx.raw("uuid_generate_v4()"));
|
|
65
45
|
table.string("title").notNullable();
|
|
66
|
-
table.
|
|
67
|
-
table.bigInteger("user_id").notNullable().references("id").inTable("users").onDelete("cascade");
|
|
68
|
-
table.boolean("published").defaultTo(false);
|
|
69
|
-
table.timestamp("created_at").defaultTo(trx.fn.now()).notNullable();
|
|
70
|
-
table.timestamp("updated_at").defaultTo(trx.fn.now()).notNullable();
|
|
46
|
+
table.uuid("user_id").notNullable().references("id").inTable("users").onDelete("cascade");
|
|
71
47
|
});
|
|
72
|
-
await trx.schema.
|
|
73
|
-
table.
|
|
48
|
+
await trx.schema.createTableIfNotExists("comments", (table) => {
|
|
49
|
+
table.uuid("id").primary().defaultTo(trx.raw("uuid_generate_v4()"));
|
|
74
50
|
table.text("content").notNullable();
|
|
75
|
-
table.
|
|
76
|
-
table.
|
|
77
|
-
table.timestamp("created_at").defaultTo(trx.fn.now()).notNullable();
|
|
51
|
+
table.uuid("post_id").notNullable().references("id").inTable("posts").onDelete("cascade");
|
|
52
|
+
table.uuid("user_id").notNullable().references("id").inTable("users").onDelete("cascade");
|
|
78
53
|
});
|
|
79
54
|
}
|
|
80
55
|
/**
|
|
@@ -108,4 +83,4 @@ async function createTestDatabase(dbName) {
|
|
|
108
83
|
}
|
|
109
84
|
|
|
110
85
|
//#endregion
|
|
111
|
-
export { TEST_DATABASE_CONFIG, createKnexDb, createTestDatabase, createTestTables, createTestTablesKnex
|
|
86
|
+
export { TEST_DATABASE_CONFIG, createKnexDb, createTestDatabase, createTestTables, createTestTablesKnex };
|
|
@@ -25,23 +25,6 @@ function createKnexDb() {
|
|
|
25
25
|
});
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
|
-
* Test setup helper for Knex/Objection that creates tables within a transaction
|
|
29
|
-
*/
|
|
30
|
-
async function setupKnexTest() {
|
|
31
|
-
const db = createKnexDb();
|
|
32
|
-
const trx = await db.transaction();
|
|
33
|
-
await createTestTablesKnex(trx);
|
|
34
|
-
const cleanup = async () => {
|
|
35
|
-
await trx.rollback();
|
|
36
|
-
await db.destroy();
|
|
37
|
-
};
|
|
38
|
-
return {
|
|
39
|
-
db,
|
|
40
|
-
trx,
|
|
41
|
-
cleanup
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
28
|
* Creates test tables using Kysely
|
|
46
29
|
*/
|
|
47
30
|
async function createTestTables(db) {
|
|
@@ -53,29 +36,21 @@ async function createTestTables(db) {
|
|
|
53
36
|
* Creates test tables using Knex
|
|
54
37
|
*/
|
|
55
38
|
async function createTestTablesKnex(trx) {
|
|
56
|
-
await trx.
|
|
57
|
-
|
|
39
|
+
await trx.raw(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`);
|
|
40
|
+
await trx.schema.createTableIfNotExists("users", (table) => {
|
|
41
|
+
table.uuid("id").primary().defaultTo(trx.raw("uuid_generate_v4()"));
|
|
58
42
|
table.string("name").notNullable();
|
|
59
|
-
table.string("email").notNullable().unique();
|
|
60
|
-
table.string("role").defaultTo("user");
|
|
61
|
-
table.timestamp("created_at").defaultTo(trx.fn.now()).notNullable();
|
|
62
|
-
table.timestamp("updated_at").defaultTo(trx.fn.now()).notNullable();
|
|
63
43
|
});
|
|
64
|
-
await trx.schema.
|
|
65
|
-
table.
|
|
44
|
+
await trx.schema.createTableIfNotExists("posts", (table) => {
|
|
45
|
+
table.uuid("id").primary().defaultTo(trx.raw("uuid_generate_v4()"));
|
|
66
46
|
table.string("title").notNullable();
|
|
67
|
-
table.
|
|
68
|
-
table.bigInteger("user_id").notNullable().references("id").inTable("users").onDelete("cascade");
|
|
69
|
-
table.boolean("published").defaultTo(false);
|
|
70
|
-
table.timestamp("created_at").defaultTo(trx.fn.now()).notNullable();
|
|
71
|
-
table.timestamp("updated_at").defaultTo(trx.fn.now()).notNullable();
|
|
47
|
+
table.uuid("user_id").notNullable().references("id").inTable("users").onDelete("cascade");
|
|
72
48
|
});
|
|
73
|
-
await trx.schema.
|
|
74
|
-
table.
|
|
49
|
+
await trx.schema.createTableIfNotExists("comments", (table) => {
|
|
50
|
+
table.uuid("id").primary().defaultTo(trx.raw("uuid_generate_v4()"));
|
|
75
51
|
table.text("content").notNullable();
|
|
76
|
-
table.
|
|
77
|
-
table.
|
|
78
|
-
table.timestamp("created_at").defaultTo(trx.fn.now()).notNullable();
|
|
52
|
+
table.uuid("post_id").notNullable().references("id").inTable("posts").onDelete("cascade");
|
|
53
|
+
table.uuid("user_id").notNullable().references("id").inTable("users").onDelete("cascade");
|
|
79
54
|
});
|
|
80
55
|
}
|
|
81
56
|
/**
|
|
@@ -138,10 +113,4 @@ Object.defineProperty(exports, 'createTestTablesKnex', {
|
|
|
138
113
|
get: function () {
|
|
139
114
|
return createTestTablesKnex;
|
|
140
115
|
}
|
|
141
|
-
});
|
|
142
|
-
Object.defineProperty(exports, 'setupKnexTest', {
|
|
143
|
-
enumerable: true,
|
|
144
|
-
get: function () {
|
|
145
|
-
return setupKnexTest;
|
|
146
|
-
}
|
|
147
116
|
});
|
package/dist/kysely.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
require('./Factory-WMhTNZ9S.cjs');
|
|
2
2
|
require('./faker-B14IEMIN.cjs');
|
|
3
|
-
const require_KyselyFactory = require('./KyselyFactory-
|
|
3
|
+
const require_KyselyFactory = require('./KyselyFactory-Cf0o2YxO.cjs');
|
|
4
4
|
require('./PostgresMigrator-DFcNdCvD.cjs');
|
|
5
5
|
const require_PostgresKyselyMigrator = require('./PostgresKyselyMigrator-CfytARcA.cjs');
|
|
6
6
|
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-CruLTRRi.cjs');
|
package/dist/kysely.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import "./faker-
|
|
2
|
-
import "./Factory-
|
|
3
|
-
import { KyselyFactory } from "./KyselyFactory-
|
|
1
|
+
import "./faker-BSH1EMtg.cjs";
|
|
2
|
+
import "./Factory-pNV7ZQ7-.cjs";
|
|
3
|
+
import { KyselyFactory } from "./KyselyFactory-DM2dnUXU.cjs";
|
|
4
4
|
import "./PostgresMigrator-D5UkK1_K.cjs";
|
|
5
5
|
import { PostgresKyselyMigrator } from "./PostgresKyselyMigrator-CQ3aUoy_.cjs";
|
|
6
6
|
import { IsolationLevel } from "./VitestTransactionIsolator-DHf2MxmC.cjs";
|
package/dist/kysely.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import "./faker-
|
|
2
|
-
import "./Factory-
|
|
3
|
-
import { KyselyFactory } from "./KyselyFactory-
|
|
1
|
+
import "./faker-C-Iuk_R1.mjs";
|
|
2
|
+
import "./Factory-D7P3bKKb.mjs";
|
|
3
|
+
import { KyselyFactory } from "./KyselyFactory-BrWADI77.mjs";
|
|
4
4
|
import "./PostgresMigrator-BlvuQl7d.mjs";
|
|
5
5
|
import { PostgresKyselyMigrator } from "./PostgresKyselyMigrator-_6yHZigp.mjs";
|
|
6
6
|
import { IsolationLevel } from "./VitestTransactionIsolator-Xqyjlmw6.mjs";
|
package/dist/kysely.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import "./Factory-z2m01hMj.mjs";
|
|
2
2
|
import "./faker-BGKYFoCT.mjs";
|
|
3
|
-
import { KyselyFactory } from "./KyselyFactory-
|
|
3
|
+
import { KyselyFactory } from "./KyselyFactory-BcYkC0t2.mjs";
|
|
4
4
|
import "./PostgresMigrator-DxPC_gGu.mjs";
|
|
5
5
|
import { PostgresKyselyMigrator } from "./PostgresKyselyMigrator-Bdhl251C.mjs";
|
|
6
6
|
import { IsolationLevel } from "./VitestTransactionIsolator-BWwK-ca6.mjs";
|
package/dist/objection.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
require('./Factory-WMhTNZ9S.cjs');
|
|
2
2
|
require('./faker-B14IEMIN.cjs');
|
|
3
|
-
const require_ObjectionFactory = require('./ObjectionFactory-
|
|
3
|
+
const require_ObjectionFactory = require('./ObjectionFactory-CDriunkS.cjs');
|
|
4
4
|
require('./PostgresMigrator-DFcNdCvD.cjs');
|
|
5
5
|
const require_PostgresObjectionMigrator = require('./PostgresObjectionMigrator-BG6ymgnt.cjs');
|
|
6
6
|
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-CruLTRRi.cjs');
|
package/dist/objection.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import "./faker-
|
|
2
|
-
import "./Factory-
|
|
3
|
-
import { ObjectionFactory } from "./ObjectionFactory-
|
|
1
|
+
import "./faker-BSH1EMtg.cjs";
|
|
2
|
+
import "./Factory-pNV7ZQ7-.cjs";
|
|
3
|
+
import { ObjectionFactory } from "./ObjectionFactory-D3l1VuyX.cjs";
|
|
4
4
|
import "./PostgresMigrator-D5UkK1_K.cjs";
|
|
5
5
|
import { PostgresObjectionMigrator } from "./PostgresObjectionMigrator-CZHHcCOv.cjs";
|
|
6
6
|
import { IsolationLevel } from "./VitestTransactionIsolator-DHf2MxmC.cjs";
|
package/dist/objection.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import "./faker-
|
|
2
|
-
import "./Factory-
|
|
3
|
-
import { ObjectionFactory } from "./ObjectionFactory-
|
|
1
|
+
import "./faker-C-Iuk_R1.mjs";
|
|
2
|
+
import "./Factory-D7P3bKKb.mjs";
|
|
3
|
+
import { ObjectionFactory } from "./ObjectionFactory-B40NQWSe.mjs";
|
|
4
4
|
import "./PostgresMigrator-BlvuQl7d.mjs";
|
|
5
5
|
import { PostgresObjectionMigrator } from "./PostgresObjectionMigrator-C69n7vzr.mjs";
|
|
6
6
|
import { IsolationLevel } from "./VitestTransactionIsolator-Xqyjlmw6.mjs";
|
package/dist/objection.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import "./Factory-z2m01hMj.mjs";
|
|
2
2
|
import "./faker-BGKYFoCT.mjs";
|
|
3
|
-
import { ObjectionFactory } from "./ObjectionFactory-
|
|
3
|
+
import { ObjectionFactory } from "./ObjectionFactory-8hebmnai.mjs";
|
|
4
4
|
import "./PostgresMigrator-DxPC_gGu.mjs";
|
|
5
5
|
import { PostgresObjectionMigrator } from "./PostgresObjectionMigrator-G4h5FLvU.mjs";
|
|
6
6
|
import { IsolationLevel } from "./VitestTransactionIsolator-BWwK-ca6.mjs";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geekmidas/testkit",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.13",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -37,6 +37,6 @@
|
|
|
37
37
|
"objection": "~3.1.5",
|
|
38
38
|
"db-errors": "~0.2.3",
|
|
39
39
|
"vitest": "~3.2.4",
|
|
40
|
-
"@geekmidas/envkit": "0.0.
|
|
40
|
+
"@geekmidas/envkit": "0.0.6"
|
|
41
41
|
}
|
|
42
42
|
}
|
package/src/Factory.ts
CHANGED
|
@@ -71,7 +71,10 @@ export abstract class Factory<
|
|
|
71
71
|
builderName: K,
|
|
72
72
|
attrs?:
|
|
73
73
|
| Parameters<Builders[K]>[0]
|
|
74
|
-
| ((
|
|
74
|
+
| ((
|
|
75
|
+
idx: number,
|
|
76
|
+
faker: FakerFactory,
|
|
77
|
+
) => Promise<Parameters<Builders[K]>[0]>),
|
|
75
78
|
): Promise<Awaited<ReturnType<Builders[K]>>[]>;
|
|
76
79
|
|
|
77
80
|
/**
|
package/src/KyselyFactory.ts
CHANGED
|
@@ -286,7 +286,10 @@ export class KyselyFactory<
|
|
|
286
286
|
async insertMany<K extends keyof Builders>(
|
|
287
287
|
count: number,
|
|
288
288
|
builderName: K,
|
|
289
|
-
attrs: (
|
|
289
|
+
attrs: (
|
|
290
|
+
idx: number,
|
|
291
|
+
faker: FakerFactory,
|
|
292
|
+
) => Promise<Parameters<Builders[K]>[0]>,
|
|
290
293
|
): Promise<Awaited<ReturnType<Builders[K]>>[]>;
|
|
291
294
|
async insertMany<K extends keyof Builders>(
|
|
292
295
|
count: number,
|
|
@@ -304,7 +307,8 @@ export class KyselyFactory<
|
|
|
304
307
|
const promises: Promise<any>[] = [];
|
|
305
308
|
|
|
306
309
|
for (let i = 0; i < count; i++) {
|
|
307
|
-
const newAttrs =
|
|
310
|
+
const newAttrs =
|
|
311
|
+
typeof attrs === 'function' ? await attrs(i, faker) : attrs;
|
|
308
312
|
promises.push(this.insert(builderName, newAttrs));
|
|
309
313
|
}
|
|
310
314
|
|
package/src/ObjectionFactory.ts
CHANGED
|
@@ -144,8 +144,17 @@ export class ObjectionFactory<
|
|
|
144
144
|
// Handle insertion based on autoInsert flag
|
|
145
145
|
if (autoInsert !== false) {
|
|
146
146
|
// Auto insert is enabled by default
|
|
147
|
+
// Extract only defined values for insertion
|
|
148
|
+
const insertData = Object.entries(model).reduce((acc, [key, value]) => {
|
|
149
|
+
if (value !== undefined && key !== 'id') {
|
|
150
|
+
acc[key] = value;
|
|
151
|
+
}
|
|
152
|
+
return acc;
|
|
153
|
+
}, {} as any);
|
|
154
|
+
|
|
155
|
+
// Use static query method to insert data directly
|
|
147
156
|
// @ts-ignore
|
|
148
|
-
const result = await
|
|
157
|
+
const result = await ModelClass.query(db).insert(insertData);
|
|
149
158
|
return result as Result;
|
|
150
159
|
} else {
|
|
151
160
|
// Return model for factory to handle insertion
|
|
@@ -221,7 +230,16 @@ export class ObjectionFactory<
|
|
|
221
230
|
|
|
222
231
|
// If the builder returns a model instance, insert it
|
|
223
232
|
if (result && typeof result.$query === 'function') {
|
|
224
|
-
|
|
233
|
+
// Extract data from model, excluding undefined values and id
|
|
234
|
+
const insertData = Object.entries(result).reduce((acc, [key, value]) => {
|
|
235
|
+
if (value !== undefined && key !== 'id') {
|
|
236
|
+
acc[key] = value;
|
|
237
|
+
}
|
|
238
|
+
return acc;
|
|
239
|
+
}, {} as any);
|
|
240
|
+
|
|
241
|
+
// Use the model's constructor to get the query builder
|
|
242
|
+
return await result.constructor.query(this.db).insert(insertData);
|
|
225
243
|
}
|
|
226
244
|
|
|
227
245
|
// Otherwise, assume the builder handled insertion itself
|
|
@@ -283,14 +301,23 @@ export class ObjectionFactory<
|
|
|
283
301
|
const records: any[] = [];
|
|
284
302
|
for (let i = 0; i < count; i++) {
|
|
285
303
|
const newAttrs =
|
|
286
|
-
typeof attrs === 'function' ? (attrs as any)(i, faker) : attrs;
|
|
304
|
+
typeof attrs === 'function' ? await (attrs as any)(i, faker) : attrs;
|
|
287
305
|
|
|
288
306
|
records.push(
|
|
289
307
|
this.builders[builderName](newAttrs, this, this.db, faker).then(
|
|
290
308
|
(record: any) => {
|
|
291
309
|
// If the builder returns a model instance, insert it
|
|
292
310
|
if (record && typeof record.$query === 'function') {
|
|
293
|
-
|
|
311
|
+
// Extract data from model, excluding undefined values and id
|
|
312
|
+
const insertData = Object.entries(record).reduce((acc, [key, value]) => {
|
|
313
|
+
if (value !== undefined && key !== 'id') {
|
|
314
|
+
acc[key] = value;
|
|
315
|
+
}
|
|
316
|
+
return acc;
|
|
317
|
+
}, {} as any);
|
|
318
|
+
|
|
319
|
+
// Use the model's constructor to get the query builder
|
|
320
|
+
return record.constructor.query(this.db).insert(insertData);
|
|
294
321
|
}
|
|
295
322
|
// Otherwise, assume the builder handled insertion itself
|
|
296
323
|
return record;
|