@geekmidas/testkit 0.0.3 → 0.0.4
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/PostgresKyselyMigrator.spec +471 -0
- package/dist/Factory.mjs +1 -1
- package/dist/{KyselyFactory-DiiWtMYe.cjs → KyselyFactory-BGvSMLtd.cjs} +11 -12
- package/dist/{KyselyFactory-DZewtWtJ.mjs → KyselyFactory-ionH4gvk.mjs} +12 -13
- package/dist/KyselyFactory.cjs +2 -1
- package/dist/KyselyFactory.mjs +3 -2
- package/dist/{ObjectionFactory-MAf2m8LI.mjs → ObjectionFactory-CFrtXe7i.mjs} +1 -1
- package/dist/ObjectionFactory.cjs +1 -1
- package/dist/ObjectionFactory.mjs +2 -2
- package/dist/{PostgresKyselyMigrator-ChMJpPrQ.mjs → PostgresKyselyMigrator-CbtiZgfI.mjs} +1 -1
- package/dist/{PostgresKyselyMigrator-rY3hO_-1.cjs → PostgresKyselyMigrator-Cxf2Dp9y.cjs} +3 -2
- package/dist/PostgresKyselyMigrator.cjs +2 -2
- package/dist/PostgresKyselyMigrator.mjs +2 -2
- package/dist/{PostgresMigrator-BJ2-5A_b.cjs → PostgresMigrator-eqyAFSf-.cjs} +2 -39
- package/dist/PostgresMigrator.cjs +1 -1
- package/dist/PostgresMigrator.mjs +1 -1
- package/dist/VitestKyselyTransactionIsolator-DXjWQtDN.mjs +12 -0
- package/dist/VitestKyselyTransactionIsolator-Dh2AgJDd.cjs +17 -0
- package/dist/VitestKyselyTransactionIsolator.cjs +5 -0
- package/dist/VitestKyselyTransactionIsolator.mjs +5 -0
- package/dist/VitestTransactionIsolator-pLwsDo_A.mjs +40 -0
- package/dist/VitestTransactionIsolator-zK5NJ7DQ.cjs +51 -0
- package/dist/VitestTransactionIsolator.cjs +5 -0
- package/dist/VitestTransactionIsolator.mjs +4 -0
- package/dist/__tests__/Factory.spec.cjs +139 -0
- package/dist/__tests__/Factory.spec.mjs +139 -0
- package/dist/__tests__/KyselyFactory.spec.cjs +221 -15008
- package/dist/__tests__/KyselyFactory.spec.mjs +220 -15034
- package/dist/__tests__/ObjectionFactory.spec.cjs +387 -0
- package/dist/__tests__/ObjectionFactory.spec.mjs +386 -0
- package/dist/__tests__/PostgresMigrator.spec.cjs +257 -0
- package/dist/__tests__/PostgresMigrator.spec.mjs +256 -0
- package/dist/__tests__/faker.spec.cjs +115 -0
- package/dist/__tests__/faker.spec.mjs +115 -0
- package/dist/__tests__/integration.spec.cjs +279 -0
- package/dist/__tests__/integration.spec.mjs +279 -0
- package/dist/chunk-DWy1uDak.cjs +39 -0
- package/dist/dist-BM2KvLG1.mjs +5618 -0
- package/dist/dist-DE3gAxQI.cjs +5736 -0
- package/dist/example.cjs +2 -1
- package/dist/example.mjs +3 -2
- package/dist/faker-cGCFcrj2.mjs +85 -0
- package/dist/faker-h6CkRloU.cjs +121 -0
- package/dist/faker.cjs +8 -0
- package/dist/faker.mjs +3 -0
- package/dist/helpers-BnARb5Ap.mjs +19 -0
- package/dist/helpers-C2NH7xcz.cjs +135 -0
- package/dist/helpers-C_RZk04R.cjs +31 -0
- package/dist/helpers-CukcFAU9.mjs +111 -0
- package/dist/helpers.cjs +7 -0
- package/dist/helpers.mjs +6 -0
- package/dist/kysely.cjs +16 -4
- package/dist/kysely.mjs +16 -5
- package/dist/objection.cjs +1 -1
- package/dist/objection.mjs +2 -2
- package/dist/vi.bdSIJ99Y-BgRxGeO2.mjs +9382 -0
- package/dist/vi.bdSIJ99Y-CFuzUeY6.cjs +9393 -0
- package/package.json +7 -2
- package/src/Factory.ts +3 -1
- package/src/KyselyFactory.ts +30 -36
- package/src/VitestKyselyTransactionIsolator.ts +23 -0
- package/src/VitestTransactionIsolator.ts +70 -0
- package/src/__tests__/Factory.spec.ts +164 -0
- package/src/__tests__/KyselyFactory.spec.ts +432 -64
- package/src/__tests__/ObjectionFactory.spec.ts +532 -0
- package/src/__tests__/PostgresMigrator.spec.ts +366 -0
- package/src/__tests__/faker.spec.ts +142 -0
- package/src/__tests__/integration.spec.ts +442 -0
- package/src/faker.ts +112 -0
- package/src/helpers.ts +28 -0
- package/src/kysely.ts +14 -0
- package/test/globalSetup.ts +41 -40
- package/test/helpers.ts +273 -0
- /package/dist/{Factory-DlzMkMzb.mjs → Factory-D52Lsc6Z.mjs} +0 -0
- /package/dist/{ObjectionFactory-DeFYWbzt.cjs → ObjectionFactory-BlkzSEqo.cjs} +0 -0
- /package/dist/{PostgresMigrator-BKaNTth5.mjs → PostgresMigrator-DqeuPy-e.mjs} +0 -0
- /package/dist/{magic-string.es-CxbtJGk_.mjs → magic-string.es-C6yzoryu.mjs} +0 -0
- /package/dist/{magic-string.es-KiPEzMtt.cjs → magic-string.es-jdtJrR0A.cjs} +0 -0
package/dist/example.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
require('./Factory-DREHoms3.cjs');
|
|
2
|
-
|
|
2
|
+
require('./faker-h6CkRloU.cjs');
|
|
3
|
+
const require_KyselyFactory = require('./KyselyFactory-BGvSMLtd.cjs');
|
|
3
4
|
|
|
4
5
|
//#region src/example.ts
|
|
5
6
|
const userBuilder = require_KyselyFactory.KyselyFactory.createBuilder({
|
package/dist/example.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import "./Factory-
|
|
2
|
-
import
|
|
1
|
+
import "./Factory-D52Lsc6Z.mjs";
|
|
2
|
+
import "./faker-cGCFcrj2.mjs";
|
|
3
|
+
import { KyselyFactory } from "./KyselyFactory-ionH4gvk.mjs";
|
|
3
4
|
|
|
4
5
|
//#region src/example.ts
|
|
5
6
|
const userBuilder = KyselyFactory.createBuilder({
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { faker } from "@faker-js/faker";
|
|
2
|
+
|
|
3
|
+
//#region src/faker.ts
|
|
4
|
+
/**
|
|
5
|
+
* Atomic counter implementation for thread-safe sequence generation
|
|
6
|
+
*/
|
|
7
|
+
var AtomicCounter = class {
|
|
8
|
+
value;
|
|
9
|
+
constructor(initialValue = 0) {
|
|
10
|
+
this.value = initialValue;
|
|
11
|
+
}
|
|
12
|
+
increment() {
|
|
13
|
+
return ++this.value;
|
|
14
|
+
}
|
|
15
|
+
get() {
|
|
16
|
+
return this.value;
|
|
17
|
+
}
|
|
18
|
+
reset(value = 0) {
|
|
19
|
+
this.value = value;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Sets the `insertedAt` and `updatedAt` to a random date in the past.
|
|
24
|
+
*/
|
|
25
|
+
function timestamps() {
|
|
26
|
+
const createdAt = faker$1.date.past();
|
|
27
|
+
const updatedAt = faker$1.date.between({
|
|
28
|
+
from: createdAt,
|
|
29
|
+
to: /* @__PURE__ */ new Date()
|
|
30
|
+
});
|
|
31
|
+
createdAt.setMilliseconds(0);
|
|
32
|
+
updatedAt.setMilliseconds(0);
|
|
33
|
+
return {
|
|
34
|
+
createdAt,
|
|
35
|
+
updatedAt
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Returns a reverse domain name identifier.
|
|
40
|
+
*/
|
|
41
|
+
function identifier(suffix) {
|
|
42
|
+
return [
|
|
43
|
+
faker$1.internet.domainSuffix(),
|
|
44
|
+
faker$1.internet.domainWord(),
|
|
45
|
+
suffix ? suffix : faker$1.internet.domainWord() + sequence("identifier")
|
|
46
|
+
].join(".");
|
|
47
|
+
}
|
|
48
|
+
const sequences = /* @__PURE__ */ new Map();
|
|
49
|
+
function sequence(name = "default") {
|
|
50
|
+
if (!sequences.has(name)) sequences.set(name, new AtomicCounter());
|
|
51
|
+
const counter = sequences.get(name);
|
|
52
|
+
return counter.increment();
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Resets a sequence counter to a specific value (default: 0)
|
|
56
|
+
*/
|
|
57
|
+
function resetSequence(name = "default", value = 0) {
|
|
58
|
+
if (sequences.has(name)) {
|
|
59
|
+
const counter = sequences.get(name);
|
|
60
|
+
counter.reset(value);
|
|
61
|
+
} else sequences.set(name, new AtomicCounter(value));
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Resets all sequence counters
|
|
65
|
+
*/
|
|
66
|
+
function resetAllSequences() {
|
|
67
|
+
sequences.clear();
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Returns a random price number.
|
|
71
|
+
*/
|
|
72
|
+
function price() {
|
|
73
|
+
return +faker$1.commerce.price();
|
|
74
|
+
}
|
|
75
|
+
const faker$1 = Object.freeze(Object.assign({}, faker, {
|
|
76
|
+
timestamps,
|
|
77
|
+
identifier,
|
|
78
|
+
sequence,
|
|
79
|
+
resetSequence,
|
|
80
|
+
resetAllSequences,
|
|
81
|
+
price
|
|
82
|
+
}));
|
|
83
|
+
|
|
84
|
+
//#endregion
|
|
85
|
+
export { faker$1 as faker, identifier, resetAllSequences, resetSequence, sequence, timestamps };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-DWy1uDak.cjs');
|
|
2
|
+
const __faker_js_faker = require_chunk.__toESM(require("@faker-js/faker"));
|
|
3
|
+
|
|
4
|
+
//#region src/faker.ts
|
|
5
|
+
/**
|
|
6
|
+
* Atomic counter implementation for thread-safe sequence generation
|
|
7
|
+
*/
|
|
8
|
+
var AtomicCounter = class {
|
|
9
|
+
value;
|
|
10
|
+
constructor(initialValue = 0) {
|
|
11
|
+
this.value = initialValue;
|
|
12
|
+
}
|
|
13
|
+
increment() {
|
|
14
|
+
return ++this.value;
|
|
15
|
+
}
|
|
16
|
+
get() {
|
|
17
|
+
return this.value;
|
|
18
|
+
}
|
|
19
|
+
reset(value = 0) {
|
|
20
|
+
this.value = value;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Sets the `insertedAt` and `updatedAt` to a random date in the past.
|
|
25
|
+
*/
|
|
26
|
+
function timestamps() {
|
|
27
|
+
const createdAt = faker.date.past();
|
|
28
|
+
const updatedAt = faker.date.between({
|
|
29
|
+
from: createdAt,
|
|
30
|
+
to: /* @__PURE__ */ new Date()
|
|
31
|
+
});
|
|
32
|
+
createdAt.setMilliseconds(0);
|
|
33
|
+
updatedAt.setMilliseconds(0);
|
|
34
|
+
return {
|
|
35
|
+
createdAt,
|
|
36
|
+
updatedAt
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Returns a reverse domain name identifier.
|
|
41
|
+
*/
|
|
42
|
+
function identifier(suffix) {
|
|
43
|
+
return [
|
|
44
|
+
faker.internet.domainSuffix(),
|
|
45
|
+
faker.internet.domainWord(),
|
|
46
|
+
suffix ? suffix : faker.internet.domainWord() + sequence("identifier")
|
|
47
|
+
].join(".");
|
|
48
|
+
}
|
|
49
|
+
const sequences = /* @__PURE__ */ new Map();
|
|
50
|
+
function sequence(name = "default") {
|
|
51
|
+
if (!sequences.has(name)) sequences.set(name, new AtomicCounter());
|
|
52
|
+
const counter = sequences.get(name);
|
|
53
|
+
return counter.increment();
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Resets a sequence counter to a specific value (default: 0)
|
|
57
|
+
*/
|
|
58
|
+
function resetSequence(name = "default", value = 0) {
|
|
59
|
+
if (sequences.has(name)) {
|
|
60
|
+
const counter = sequences.get(name);
|
|
61
|
+
counter.reset(value);
|
|
62
|
+
} else sequences.set(name, new AtomicCounter(value));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Resets all sequence counters
|
|
66
|
+
*/
|
|
67
|
+
function resetAllSequences() {
|
|
68
|
+
sequences.clear();
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Returns a random price number.
|
|
72
|
+
*/
|
|
73
|
+
function price() {
|
|
74
|
+
return +faker.commerce.price();
|
|
75
|
+
}
|
|
76
|
+
const faker = Object.freeze(Object.assign({}, __faker_js_faker.faker, {
|
|
77
|
+
timestamps,
|
|
78
|
+
identifier,
|
|
79
|
+
sequence,
|
|
80
|
+
resetSequence,
|
|
81
|
+
resetAllSequences,
|
|
82
|
+
price
|
|
83
|
+
}));
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
86
|
+
Object.defineProperty(exports, 'faker', {
|
|
87
|
+
enumerable: true,
|
|
88
|
+
get: function () {
|
|
89
|
+
return faker;
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
Object.defineProperty(exports, 'identifier', {
|
|
93
|
+
enumerable: true,
|
|
94
|
+
get: function () {
|
|
95
|
+
return identifier;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
Object.defineProperty(exports, 'resetAllSequences', {
|
|
99
|
+
enumerable: true,
|
|
100
|
+
get: function () {
|
|
101
|
+
return resetAllSequences;
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
Object.defineProperty(exports, 'resetSequence', {
|
|
105
|
+
enumerable: true,
|
|
106
|
+
get: function () {
|
|
107
|
+
return resetSequence;
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
Object.defineProperty(exports, 'sequence', {
|
|
111
|
+
enumerable: true,
|
|
112
|
+
get: function () {
|
|
113
|
+
return sequence;
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
Object.defineProperty(exports, 'timestamps', {
|
|
117
|
+
enumerable: true,
|
|
118
|
+
get: function () {
|
|
119
|
+
return timestamps;
|
|
120
|
+
}
|
|
121
|
+
});
|
package/dist/faker.cjs
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
const require_faker = require('./faker-h6CkRloU.cjs');
|
|
2
|
+
|
|
3
|
+
exports.faker = require_faker.faker;
|
|
4
|
+
exports.identifier = require_faker.identifier;
|
|
5
|
+
exports.resetAllSequences = require_faker.resetAllSequences;
|
|
6
|
+
exports.resetSequence = require_faker.resetSequence;
|
|
7
|
+
exports.sequence = require_faker.sequence;
|
|
8
|
+
exports.timestamps = require_faker.timestamps;
|
package/dist/faker.mjs
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { IsolationLevel } from "./VitestTransactionIsolator-pLwsDo_A.mjs";
|
|
2
|
+
import { VitestKyselyTransactionIsolator } from "./VitestKyselyTransactionIsolator-DXjWQtDN.mjs";
|
|
3
|
+
import { CamelCasePlugin, Kysely, PostgresDialect } from "kysely";
|
|
4
|
+
import pg from "pg";
|
|
5
|
+
|
|
6
|
+
//#region src/helpers.ts
|
|
7
|
+
function wrapVitestKyselyTransaction(db, setup, level = IsolationLevel.REPEATABLE_READ) {
|
|
8
|
+
const wrapper = new VitestKyselyTransactionIsolator();
|
|
9
|
+
return wrapper.wrapVitestWithTransaction(db, setup, level);
|
|
10
|
+
}
|
|
11
|
+
function createKyselyDb(config) {
|
|
12
|
+
return new Kysely({
|
|
13
|
+
dialect: new PostgresDialect({ pool: new pg.Pool(config) }),
|
|
14
|
+
plugins: [new CamelCasePlugin()]
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
export { createKyselyDb, wrapVitestKyselyTransaction };
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-DWy1uDak.cjs');
|
|
2
|
+
const kysely = require_chunk.__toESM(require("kysely"));
|
|
3
|
+
const pg = require_chunk.__toESM(require("pg"));
|
|
4
|
+
const knex = require_chunk.__toESM(require("knex"));
|
|
5
|
+
|
|
6
|
+
//#region test/globalSetup.ts
|
|
7
|
+
const TEST_DATABASE_NAME = "geekmidas_test";
|
|
8
|
+
const TEST_DATABASE_CONFIG = {
|
|
9
|
+
host: "localhost",
|
|
10
|
+
port: 5432,
|
|
11
|
+
user: "geekmidas",
|
|
12
|
+
password: "geekmidas",
|
|
13
|
+
database: TEST_DATABASE_NAME
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region test/helpers.ts
|
|
18
|
+
/**
|
|
19
|
+
* Creates a Knex database instance for testing
|
|
20
|
+
*/
|
|
21
|
+
function createKnexDb() {
|
|
22
|
+
return (0, knex.default)({
|
|
23
|
+
client: "pg",
|
|
24
|
+
connection: TEST_DATABASE_CONFIG
|
|
25
|
+
});
|
|
26
|
+
}
|
|
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
|
+
* Creates test tables using Kysely
|
|
46
|
+
*/
|
|
47
|
+
async function createTestTables(db) {
|
|
48
|
+
await db.schema.createTable("users").addColumn("id", "bigserial", (col) => col.primaryKey()).addColumn("name", "varchar", (col) => col.notNull()).addColumn("email", "varchar", (col) => col.notNull().unique()).addColumn("role", "varchar", (col) => col.defaultTo("user")).addColumn("created_at", "timestamp", (col) => col.defaultTo(kysely.sql`now()`).notNull()).addColumn("updated_at", "timestamp", (col) => col.defaultTo(kysely.sql`now()`).notNull()).execute();
|
|
49
|
+
await db.schema.createTable("posts").addColumn("id", "bigserial", (col) => col.primaryKey()).addColumn("title", "varchar", (col) => col.notNull()).addColumn("content", "text", (col) => col.notNull()).addColumn("user_id", "bigint", (col) => col.notNull().references("users.id").onDelete("cascade")).addColumn("published", "boolean", (col) => col.defaultTo(false)).addColumn("created_at", "timestamp", (col) => col.defaultTo(kysely.sql`now()`).notNull()).addColumn("updated_at", "timestamp", (col) => col.defaultTo(kysely.sql`now()`).notNull()).execute();
|
|
50
|
+
await db.schema.createTable("comments").addColumn("id", "bigserial", (col) => col.primaryKey()).addColumn("content", "text", (col) => col.notNull()).addColumn("post_id", "bigint", (col) => col.notNull().references("posts.id").onDelete("cascade")).addColumn("user_id", "bigint", (col) => col.notNull().references("users.id").onDelete("cascade")).addColumn("created_at", "timestamp", (col) => col.defaultTo(kysely.sql`now()`).notNull()).execute();
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Creates test tables using Knex
|
|
54
|
+
*/
|
|
55
|
+
async function createTestTablesKnex(trx) {
|
|
56
|
+
await trx.schema.createTable("users", (table) => {
|
|
57
|
+
table.bigIncrements("id").primary();
|
|
58
|
+
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
|
+
});
|
|
64
|
+
await trx.schema.createTable("posts", (table) => {
|
|
65
|
+
table.bigIncrements("id").primary();
|
|
66
|
+
table.string("title").notNullable();
|
|
67
|
+
table.text("content").notNullable();
|
|
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();
|
|
72
|
+
});
|
|
73
|
+
await trx.schema.createTable("comments", (table) => {
|
|
74
|
+
table.bigIncrements("id").primary();
|
|
75
|
+
table.text("content").notNullable();
|
|
76
|
+
table.bigInteger("post_id").notNullable().references("id").inTable("posts").onDelete("cascade");
|
|
77
|
+
table.bigInteger("user_id").notNullable().references("id").inTable("users").onDelete("cascade");
|
|
78
|
+
table.timestamp("created_at").defaultTo(trx.fn.now()).notNullable();
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Helper for PostgresMigrator tests - creates a separate test database
|
|
83
|
+
*/
|
|
84
|
+
async function createTestDatabase(dbName) {
|
|
85
|
+
const adminConfig = {
|
|
86
|
+
host: TEST_DATABASE_CONFIG.host,
|
|
87
|
+
port: TEST_DATABASE_CONFIG.port,
|
|
88
|
+
user: TEST_DATABASE_CONFIG.user,
|
|
89
|
+
password: TEST_DATABASE_CONFIG.password,
|
|
90
|
+
database: "postgres"
|
|
91
|
+
};
|
|
92
|
+
const client = new pg.default.Client(adminConfig);
|
|
93
|
+
try {
|
|
94
|
+
await client.connect();
|
|
95
|
+
await client.query(`DROP DATABASE IF EXISTS "${dbName}"`);
|
|
96
|
+
await client.query(`CREATE DATABASE "${dbName}"`);
|
|
97
|
+
} finally {
|
|
98
|
+
await client.end();
|
|
99
|
+
}
|
|
100
|
+
return async () => {
|
|
101
|
+
const cleanupClient = new pg.default.Client(adminConfig);
|
|
102
|
+
try {
|
|
103
|
+
await cleanupClient.connect();
|
|
104
|
+
await cleanupClient.query(`DROP DATABASE IF EXISTS "${dbName}"`);
|
|
105
|
+
} finally {
|
|
106
|
+
await cleanupClient.end();
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
//#endregion
|
|
112
|
+
Object.defineProperty(exports, 'TEST_DATABASE_CONFIG', {
|
|
113
|
+
enumerable: true,
|
|
114
|
+
get: function () {
|
|
115
|
+
return TEST_DATABASE_CONFIG;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
Object.defineProperty(exports, 'createTestDatabase', {
|
|
119
|
+
enumerable: true,
|
|
120
|
+
get: function () {
|
|
121
|
+
return createTestDatabase;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
Object.defineProperty(exports, 'createTestTables', {
|
|
125
|
+
enumerable: true,
|
|
126
|
+
get: function () {
|
|
127
|
+
return createTestTables;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
Object.defineProperty(exports, 'setupKnexTest', {
|
|
131
|
+
enumerable: true,
|
|
132
|
+
get: function () {
|
|
133
|
+
return setupKnexTest;
|
|
134
|
+
}
|
|
135
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-DWy1uDak.cjs');
|
|
2
|
+
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-zK5NJ7DQ.cjs');
|
|
3
|
+
const require_VitestKyselyTransactionIsolator = require('./VitestKyselyTransactionIsolator-Dh2AgJDd.cjs');
|
|
4
|
+
const kysely = require_chunk.__toESM(require("kysely"));
|
|
5
|
+
const pg = require_chunk.__toESM(require("pg"));
|
|
6
|
+
|
|
7
|
+
//#region src/helpers.ts
|
|
8
|
+
function wrapVitestKyselyTransaction(db, setup, level = require_VitestTransactionIsolator.IsolationLevel.REPEATABLE_READ) {
|
|
9
|
+
const wrapper = new require_VitestKyselyTransactionIsolator.VitestKyselyTransactionIsolator();
|
|
10
|
+
return wrapper.wrapVitestWithTransaction(db, setup, level);
|
|
11
|
+
}
|
|
12
|
+
function createKyselyDb(config) {
|
|
13
|
+
return new kysely.Kysely({
|
|
14
|
+
dialect: new kysely.PostgresDialect({ pool: new pg.default.Pool(config) }),
|
|
15
|
+
plugins: [new kysely.CamelCasePlugin()]
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
Object.defineProperty(exports, 'createKyselyDb', {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
get: function () {
|
|
23
|
+
return createKyselyDb;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
Object.defineProperty(exports, 'wrapVitestKyselyTransaction', {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
get: function () {
|
|
29
|
+
return wrapVitestKyselyTransaction;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { CamelCasePlugin, Kysely, PostgresDialect, sql } from "kysely";
|
|
2
|
+
import pg, { Client } from "pg";
|
|
3
|
+
import knex from "knex";
|
|
4
|
+
|
|
5
|
+
//#region test/globalSetup.ts
|
|
6
|
+
const TEST_DATABASE_NAME = "geekmidas_test";
|
|
7
|
+
const TEST_DATABASE_CONFIG = {
|
|
8
|
+
host: "localhost",
|
|
9
|
+
port: 5432,
|
|
10
|
+
user: "geekmidas",
|
|
11
|
+
password: "geekmidas",
|
|
12
|
+
database: TEST_DATABASE_NAME
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region test/helpers.ts
|
|
17
|
+
/**
|
|
18
|
+
* Creates a Knex database instance for testing
|
|
19
|
+
*/
|
|
20
|
+
function createKnexDb() {
|
|
21
|
+
return knex({
|
|
22
|
+
client: "pg",
|
|
23
|
+
connection: TEST_DATABASE_CONFIG
|
|
24
|
+
});
|
|
25
|
+
}
|
|
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
|
+
* Creates test tables using Kysely
|
|
45
|
+
*/
|
|
46
|
+
async function createTestTables(db) {
|
|
47
|
+
await db.schema.createTable("users").addColumn("id", "bigserial", (col) => col.primaryKey()).addColumn("name", "varchar", (col) => col.notNull()).addColumn("email", "varchar", (col) => col.notNull().unique()).addColumn("role", "varchar", (col) => col.defaultTo("user")).addColumn("created_at", "timestamp", (col) => col.defaultTo(sql`now()`).notNull()).addColumn("updated_at", "timestamp", (col) => col.defaultTo(sql`now()`).notNull()).execute();
|
|
48
|
+
await db.schema.createTable("posts").addColumn("id", "bigserial", (col) => col.primaryKey()).addColumn("title", "varchar", (col) => col.notNull()).addColumn("content", "text", (col) => col.notNull()).addColumn("user_id", "bigint", (col) => col.notNull().references("users.id").onDelete("cascade")).addColumn("published", "boolean", (col) => col.defaultTo(false)).addColumn("created_at", "timestamp", (col) => col.defaultTo(sql`now()`).notNull()).addColumn("updated_at", "timestamp", (col) => col.defaultTo(sql`now()`).notNull()).execute();
|
|
49
|
+
await db.schema.createTable("comments").addColumn("id", "bigserial", (col) => col.primaryKey()).addColumn("content", "text", (col) => col.notNull()).addColumn("post_id", "bigint", (col) => col.notNull().references("posts.id").onDelete("cascade")).addColumn("user_id", "bigint", (col) => col.notNull().references("users.id").onDelete("cascade")).addColumn("created_at", "timestamp", (col) => col.defaultTo(sql`now()`).notNull()).execute();
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Creates test tables using Knex
|
|
53
|
+
*/
|
|
54
|
+
async function createTestTablesKnex(trx) {
|
|
55
|
+
await trx.schema.createTable("users", (table) => {
|
|
56
|
+
table.bigIncrements("id").primary();
|
|
57
|
+
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
|
+
});
|
|
63
|
+
await trx.schema.createTable("posts", (table) => {
|
|
64
|
+
table.bigIncrements("id").primary();
|
|
65
|
+
table.string("title").notNullable();
|
|
66
|
+
table.text("content").notNullable();
|
|
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();
|
|
71
|
+
});
|
|
72
|
+
await trx.schema.createTable("comments", (table) => {
|
|
73
|
+
table.bigIncrements("id").primary();
|
|
74
|
+
table.text("content").notNullable();
|
|
75
|
+
table.bigInteger("post_id").notNullable().references("id").inTable("posts").onDelete("cascade");
|
|
76
|
+
table.bigInteger("user_id").notNullable().references("id").inTable("users").onDelete("cascade");
|
|
77
|
+
table.timestamp("created_at").defaultTo(trx.fn.now()).notNullable();
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Helper for PostgresMigrator tests - creates a separate test database
|
|
82
|
+
*/
|
|
83
|
+
async function createTestDatabase(dbName) {
|
|
84
|
+
const adminConfig = {
|
|
85
|
+
host: TEST_DATABASE_CONFIG.host,
|
|
86
|
+
port: TEST_DATABASE_CONFIG.port,
|
|
87
|
+
user: TEST_DATABASE_CONFIG.user,
|
|
88
|
+
password: TEST_DATABASE_CONFIG.password,
|
|
89
|
+
database: "postgres"
|
|
90
|
+
};
|
|
91
|
+
const client = new pg.Client(adminConfig);
|
|
92
|
+
try {
|
|
93
|
+
await client.connect();
|
|
94
|
+
await client.query(`DROP DATABASE IF EXISTS "${dbName}"`);
|
|
95
|
+
await client.query(`CREATE DATABASE "${dbName}"`);
|
|
96
|
+
} finally {
|
|
97
|
+
await client.end();
|
|
98
|
+
}
|
|
99
|
+
return async () => {
|
|
100
|
+
const cleanupClient = new pg.Client(adminConfig);
|
|
101
|
+
try {
|
|
102
|
+
await cleanupClient.connect();
|
|
103
|
+
await cleanupClient.query(`DROP DATABASE IF EXISTS "${dbName}"`);
|
|
104
|
+
} finally {
|
|
105
|
+
await cleanupClient.end();
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
//#endregion
|
|
111
|
+
export { TEST_DATABASE_CONFIG, createTestDatabase, createTestTables, setupKnexTest };
|
package/dist/helpers.cjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
require('./dist-DE3gAxQI.cjs');
|
|
2
|
+
require('./VitestTransactionIsolator-zK5NJ7DQ.cjs');
|
|
3
|
+
require('./VitestKyselyTransactionIsolator-Dh2AgJDd.cjs');
|
|
4
|
+
const require_helpers = require('./helpers-C_RZk04R.cjs');
|
|
5
|
+
|
|
6
|
+
exports.createKyselyDb = require_helpers.createKyselyDb;
|
|
7
|
+
exports.wrapVitestKyselyTransaction = require_helpers.wrapVitestKyselyTransaction;
|
package/dist/helpers.mjs
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import "./dist-BM2KvLG1.mjs";
|
|
2
|
+
import "./VitestTransactionIsolator-pLwsDo_A.mjs";
|
|
3
|
+
import "./VitestKyselyTransactionIsolator-DXjWQtDN.mjs";
|
|
4
|
+
import { createKyselyDb, wrapVitestKyselyTransaction } from "./helpers-BnARb5Ap.mjs";
|
|
5
|
+
|
|
6
|
+
export { createKyselyDb, wrapVitestKyselyTransaction };
|
package/dist/kysely.cjs
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
|
-
require('./PostgresMigrator-BJ2-5A_b.cjs');
|
|
2
1
|
require('./Factory-DREHoms3.cjs');
|
|
3
|
-
|
|
4
|
-
const
|
|
2
|
+
require('./faker-h6CkRloU.cjs');
|
|
3
|
+
const require_KyselyFactory = require('./KyselyFactory-BGvSMLtd.cjs');
|
|
4
|
+
require('./PostgresMigrator-eqyAFSf-.cjs');
|
|
5
|
+
const require_PostgresKyselyMigrator = require('./PostgresKyselyMigrator-Cxf2Dp9y.cjs');
|
|
6
|
+
require('./dist-DE3gAxQI.cjs');
|
|
7
|
+
const require_VitestTransactionIsolator = require('./VitestTransactionIsolator-zK5NJ7DQ.cjs');
|
|
8
|
+
const require_VitestKyselyTransactionIsolator = require('./VitestKyselyTransactionIsolator-Dh2AgJDd.cjs');
|
|
5
9
|
|
|
10
|
+
//#region src/kysely.ts
|
|
11
|
+
function wrapVitestKyselyTransaction(db, setup, level = require_VitestTransactionIsolator.IsolationLevel.REPEATABLE_READ) {
|
|
12
|
+
const wrapper = new require_VitestKyselyTransactionIsolator.VitestKyselyTransactionIsolator();
|
|
13
|
+
return wrapper.wrapVitestWithTransaction(db, setup, level);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
6
17
|
exports.KyselyFactory = require_KyselyFactory.KyselyFactory;
|
|
7
|
-
exports.PostgresKyselyMigrator = require_PostgresKyselyMigrator.PostgresKyselyMigrator;
|
|
18
|
+
exports.PostgresKyselyMigrator = require_PostgresKyselyMigrator.PostgresKyselyMigrator;
|
|
19
|
+
exports.wrapVitestKyselyTransaction = wrapVitestKyselyTransaction;
|
package/dist/kysely.mjs
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
|
-
import "./
|
|
2
|
-
import
|
|
3
|
-
import "./
|
|
4
|
-
import {
|
|
1
|
+
import "./dist-BM2KvLG1.mjs";
|
|
2
|
+
import "./Factory-D52Lsc6Z.mjs";
|
|
3
|
+
import "./faker-cGCFcrj2.mjs";
|
|
4
|
+
import { KyselyFactory } from "./KyselyFactory-ionH4gvk.mjs";
|
|
5
|
+
import "./PostgresMigrator-DqeuPy-e.mjs";
|
|
6
|
+
import { PostgresKyselyMigrator } from "./PostgresKyselyMigrator-CbtiZgfI.mjs";
|
|
7
|
+
import { IsolationLevel } from "./VitestTransactionIsolator-pLwsDo_A.mjs";
|
|
8
|
+
import { VitestKyselyTransactionIsolator } from "./VitestKyselyTransactionIsolator-DXjWQtDN.mjs";
|
|
5
9
|
|
|
6
|
-
|
|
10
|
+
//#region src/kysely.ts
|
|
11
|
+
function wrapVitestKyselyTransaction(db, setup, level = IsolationLevel.REPEATABLE_READ) {
|
|
12
|
+
const wrapper = new VitestKyselyTransactionIsolator();
|
|
13
|
+
return wrapper.wrapVitestWithTransaction(db, setup, level);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
export { KyselyFactory, PostgresKyselyMigrator, wrapVitestKyselyTransaction };
|
package/dist/objection.cjs
CHANGED
package/dist/objection.mjs
CHANGED