@geekmidas/testkit 0.0.1 → 0.0.2
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-DREHoms3.cjs +15 -0
- package/dist/Factory-DlzMkMzb.mjs +9 -0
- package/dist/Factory.cjs +3 -0
- package/dist/Factory.mjs +3 -0
- package/dist/KyselyFactory-DZewtWtJ.mjs +61 -0
- package/dist/KyselyFactory-DiiWtMYe.cjs +66 -0
- package/dist/KyselyFactory.cjs +4 -0
- package/dist/KyselyFactory.mjs +4 -0
- package/dist/ObjectionFactory-DeFYWbzt.cjs +41 -0
- package/dist/ObjectionFactory-MAf2m8LI.mjs +36 -0
- package/dist/ObjectionFactory.cjs +4 -0
- package/dist/ObjectionFactory.mjs +4 -0
- package/dist/PostgresKyselyMigrator-ChMJpPrQ.mjs +27 -0
- package/dist/PostgresKyselyMigrator-rY3hO_-1.cjs +32 -0
- package/dist/PostgresKyselyMigrator.cjs +4 -0
- package/dist/PostgresKyselyMigrator.mjs +4 -0
- package/dist/PostgresMigrator-BJ2-5A_b.cjs +104 -0
- package/dist/PostgresMigrator-BKaNTth5.mjs +61 -0
- package/dist/PostgresMigrator.cjs +3 -0
- package/dist/PostgresMigrator.mjs +3 -0
- package/dist/__tests__/KyselyFactory.spec.cjs +15022 -0
- package/dist/__tests__/KyselyFactory.spec.mjs +15048 -0
- package/dist/example.cjs +21 -0
- package/dist/example.mjs +21 -0
- package/dist/faker.mjs +0 -0
- package/dist/kysely.cjs +7 -0
- package/dist/kysely.mjs +6 -0
- package/dist/magic-string.es-CxbtJGk_.mjs +1014 -0
- package/dist/magic-string.es-KiPEzMtt.cjs +1015 -0
- package/dist/objection.cjs +4 -0
- package/dist/objection.mjs +4 -0
- package/package.json +15 -5
- package/src/faker.ts +0 -0
- package/src/kysely.ts +2 -0
- package/src/objection.ts +1 -0
- /package/{src/KyselyPostgresMigrator.ts → dist/faker.cjs} +0 -0
package/README.md
CHANGED
|
@@ -221,7 +221,7 @@ describe('User Service', () => {
|
|
|
221
221
|
TestKit includes utilities for managing test database migrations:
|
|
222
222
|
|
|
223
223
|
```typescript
|
|
224
|
-
import { PostgresKyselyMigrator } from '@geekmidas/testkit/
|
|
224
|
+
import { PostgresKyselyMigrator } from '@geekmidas/testkit/kysely';
|
|
225
225
|
|
|
226
226
|
const migrator = new PostgresKyselyMigrator({
|
|
227
227
|
database: 'test_db',
|
package/dist/Factory.cjs
ADDED
package/dist/Factory.mjs
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Factory } from "./Factory-DlzMkMzb.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/KyselyFactory.ts
|
|
4
|
+
var KyselyFactory = class extends Factory {
|
|
5
|
+
static createSeed(seedFn) {
|
|
6
|
+
return Factory.createSeed(seedFn);
|
|
7
|
+
}
|
|
8
|
+
constructor(builders, seeds, db) {
|
|
9
|
+
super();
|
|
10
|
+
this.builders = builders;
|
|
11
|
+
this.seeds = seeds;
|
|
12
|
+
this.db = db;
|
|
13
|
+
}
|
|
14
|
+
static createBuilder(config) {
|
|
15
|
+
return async (attrs, factory, db) => {
|
|
16
|
+
let data = { ...attrs };
|
|
17
|
+
if (config.defaults) {
|
|
18
|
+
const defaults = await config.defaults(attrs, factory, db);
|
|
19
|
+
data = {
|
|
20
|
+
...defaults,
|
|
21
|
+
...data
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
if (config.transform) data = await config.transform(data, factory, db);
|
|
25
|
+
if (config.autoInsert !== false) {
|
|
26
|
+
const result = await db.insertInto(config.table).values(data).returningAll().executeTakeFirst();
|
|
27
|
+
if (!result) throw new Error(`Failed to insert into ${config.table}`);
|
|
28
|
+
if (config.relations) await config.relations(result, attrs, factory, db);
|
|
29
|
+
return result;
|
|
30
|
+
} else return {
|
|
31
|
+
table: config.table,
|
|
32
|
+
data
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
async insert(builderName, attrs) {
|
|
37
|
+
if (!(builderName in this.builders)) throw new Error(`Factory "${builderName}" does not exist. Make sure it is correct and registered in src/test/setup.ts`);
|
|
38
|
+
const result = await this.builders[builderName](attrs || {}, this, this.db);
|
|
39
|
+
if (result && typeof result === "object" && "table" in result && "data" in result) {
|
|
40
|
+
const inserted = await this.db.insertInto(result.table).values(result.data).returningAll().executeTakeFirst();
|
|
41
|
+
return inserted;
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
async insertMany(count, builderName, attrs) {
|
|
46
|
+
if (!(builderName in this.builders)) throw new Error(`Builder "${builderName}" is not registered in this factory. Make sure it is correct and registered in src/test/setup.ts`);
|
|
47
|
+
const promises = [];
|
|
48
|
+
for (let i = 0; i < count; i++) {
|
|
49
|
+
const newAttrs = typeof attrs === "function" ? attrs(i) : attrs;
|
|
50
|
+
promises.push(this.insert(builderName, newAttrs));
|
|
51
|
+
}
|
|
52
|
+
return Promise.all(promises);
|
|
53
|
+
}
|
|
54
|
+
seed(seedName, attrs) {
|
|
55
|
+
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`);
|
|
56
|
+
return this.seeds[seedName](attrs || {}, this, this.db);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
export { KyselyFactory };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const require_Factory = require('./Factory-DREHoms3.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/KyselyFactory.ts
|
|
4
|
+
var KyselyFactory = class extends require_Factory.Factory {
|
|
5
|
+
static createSeed(seedFn) {
|
|
6
|
+
return require_Factory.Factory.createSeed(seedFn);
|
|
7
|
+
}
|
|
8
|
+
constructor(builders, seeds, db) {
|
|
9
|
+
super();
|
|
10
|
+
this.builders = builders;
|
|
11
|
+
this.seeds = seeds;
|
|
12
|
+
this.db = db;
|
|
13
|
+
}
|
|
14
|
+
static createBuilder(config) {
|
|
15
|
+
return async (attrs, factory, db) => {
|
|
16
|
+
let data = { ...attrs };
|
|
17
|
+
if (config.defaults) {
|
|
18
|
+
const defaults = await config.defaults(attrs, factory, db);
|
|
19
|
+
data = {
|
|
20
|
+
...defaults,
|
|
21
|
+
...data
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
if (config.transform) data = await config.transform(data, factory, db);
|
|
25
|
+
if (config.autoInsert !== false) {
|
|
26
|
+
const result = await db.insertInto(config.table).values(data).returningAll().executeTakeFirst();
|
|
27
|
+
if (!result) throw new Error(`Failed to insert into ${config.table}`);
|
|
28
|
+
if (config.relations) await config.relations(result, attrs, factory, db);
|
|
29
|
+
return result;
|
|
30
|
+
} else return {
|
|
31
|
+
table: config.table,
|
|
32
|
+
data
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
async insert(builderName, attrs) {
|
|
37
|
+
if (!(builderName in this.builders)) throw new Error(`Factory "${builderName}" does not exist. Make sure it is correct and registered in src/test/setup.ts`);
|
|
38
|
+
const result = await this.builders[builderName](attrs || {}, this, this.db);
|
|
39
|
+
if (result && typeof result === "object" && "table" in result && "data" in result) {
|
|
40
|
+
const inserted = await this.db.insertInto(result.table).values(result.data).returningAll().executeTakeFirst();
|
|
41
|
+
return inserted;
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
async insertMany(count, builderName, attrs) {
|
|
46
|
+
if (!(builderName in this.builders)) throw new Error(`Builder "${builderName}" is not registered in this factory. Make sure it is correct and registered in src/test/setup.ts`);
|
|
47
|
+
const promises = [];
|
|
48
|
+
for (let i = 0; i < count; i++) {
|
|
49
|
+
const newAttrs = typeof attrs === "function" ? attrs(i) : attrs;
|
|
50
|
+
promises.push(this.insert(builderName, newAttrs));
|
|
51
|
+
}
|
|
52
|
+
return Promise.all(promises);
|
|
53
|
+
}
|
|
54
|
+
seed(seedName, attrs) {
|
|
55
|
+
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`);
|
|
56
|
+
return this.seeds[seedName](attrs || {}, this, this.db);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
Object.defineProperty(exports, 'KyselyFactory', {
|
|
62
|
+
enumerable: true,
|
|
63
|
+
get: function () {
|
|
64
|
+
return KyselyFactory;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const require_Factory = require('./Factory-DREHoms3.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/ObjectionFactory.ts
|
|
4
|
+
var ObjectionFactory = class extends require_Factory.Factory {
|
|
5
|
+
static createSeed(seedFn) {
|
|
6
|
+
return require_Factory.Factory.createSeed(seedFn);
|
|
7
|
+
}
|
|
8
|
+
constructor(builders, seeds, db) {
|
|
9
|
+
super();
|
|
10
|
+
this.builders = builders;
|
|
11
|
+
this.seeds = seeds;
|
|
12
|
+
this.db = db;
|
|
13
|
+
}
|
|
14
|
+
insert(factory, attrs = {}) {
|
|
15
|
+
if (!(factory in this.builders)) throw new Error(`Factory "${factory}" does not exist. Make sure it is correct and registered in src/test/setup.ts`);
|
|
16
|
+
return this.builders[factory](attrs, {}, this.db).then((record) => {
|
|
17
|
+
return record.$query(this.db).insertGraph(record).execute();
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
insertMany(count, builderName, attrs = {}) {
|
|
21
|
+
if (!(builderName in this.builders)) throw new Error(`Builder "${builderName}" is not registered in this factory. Make sure it is correct and registered in src/test/setup.ts`);
|
|
22
|
+
const records = [];
|
|
23
|
+
for (let i = 0; i < count; i++) {
|
|
24
|
+
const newAttrs = typeof attrs === "function" ? attrs(i) : attrs;
|
|
25
|
+
records.push(this.builders[builderName](newAttrs, {}, this.db).then((record) => record.$query(this.db).insertGraph(record).execute()));
|
|
26
|
+
}
|
|
27
|
+
return Promise.all(records);
|
|
28
|
+
}
|
|
29
|
+
seed(seedName, attrs = {}) {
|
|
30
|
+
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`);
|
|
31
|
+
return this.seeds[seedName](attrs, this, this.db);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
//#endregion
|
|
36
|
+
Object.defineProperty(exports, 'ObjectionFactory', {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
get: function () {
|
|
39
|
+
return ObjectionFactory;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Factory } from "./Factory-DlzMkMzb.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/ObjectionFactory.ts
|
|
4
|
+
var ObjectionFactory = class extends Factory {
|
|
5
|
+
static createSeed(seedFn) {
|
|
6
|
+
return Factory.createSeed(seedFn);
|
|
7
|
+
}
|
|
8
|
+
constructor(builders, seeds, db) {
|
|
9
|
+
super();
|
|
10
|
+
this.builders = builders;
|
|
11
|
+
this.seeds = seeds;
|
|
12
|
+
this.db = db;
|
|
13
|
+
}
|
|
14
|
+
insert(factory, attrs = {}) {
|
|
15
|
+
if (!(factory in this.builders)) throw new Error(`Factory "${factory}" does not exist. Make sure it is correct and registered in src/test/setup.ts`);
|
|
16
|
+
return this.builders[factory](attrs, {}, this.db).then((record) => {
|
|
17
|
+
return record.$query(this.db).insertGraph(record).execute();
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
insertMany(count, builderName, attrs = {}) {
|
|
21
|
+
if (!(builderName in this.builders)) throw new Error(`Builder "${builderName}" is not registered in this factory. Make sure it is correct and registered in src/test/setup.ts`);
|
|
22
|
+
const records = [];
|
|
23
|
+
for (let i = 0; i < count; i++) {
|
|
24
|
+
const newAttrs = typeof attrs === "function" ? attrs(i) : attrs;
|
|
25
|
+
records.push(this.builders[builderName](newAttrs, {}, this.db).then((record) => record.$query(this.db).insertGraph(record).execute()));
|
|
26
|
+
}
|
|
27
|
+
return Promise.all(records);
|
|
28
|
+
}
|
|
29
|
+
seed(seedName, attrs = {}) {
|
|
30
|
+
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`);
|
|
31
|
+
return this.seeds[seedName](attrs, this, this.db);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
//#endregion
|
|
36
|
+
export { ObjectionFactory };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { PostgresMigrator } from "./PostgresMigrator-BKaNTth5.mjs";
|
|
2
|
+
import { Migrator } from "kysely";
|
|
3
|
+
|
|
4
|
+
//#region src/PostgresKyselyMigrator.ts
|
|
5
|
+
const logger = console;
|
|
6
|
+
var PostgresKyselyMigrator = class extends PostgresMigrator {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
super(options.uri);
|
|
9
|
+
this.options = options;
|
|
10
|
+
}
|
|
11
|
+
async migrate() {
|
|
12
|
+
const migrator = new Migrator({
|
|
13
|
+
db: this.options.db,
|
|
14
|
+
provider: this.options.provider
|
|
15
|
+
});
|
|
16
|
+
const migrations = await migrator.migrateToLatest();
|
|
17
|
+
if (migrations.error) {
|
|
18
|
+
logger.error(migrations.error, `Failed to apply migrations`);
|
|
19
|
+
throw migrations.error;
|
|
20
|
+
}
|
|
21
|
+
await this.options.db.destroy();
|
|
22
|
+
logger.log(`Applied ${migrations.results?.length} migrations successfully`);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
export { PostgresKyselyMigrator };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const require_PostgresMigrator = require('./PostgresMigrator-BJ2-5A_b.cjs');
|
|
2
|
+
const kysely = require_PostgresMigrator.__toESM(require("kysely"));
|
|
3
|
+
|
|
4
|
+
//#region src/PostgresKyselyMigrator.ts
|
|
5
|
+
const logger = console;
|
|
6
|
+
var PostgresKyselyMigrator = class extends require_PostgresMigrator.PostgresMigrator {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
super(options.uri);
|
|
9
|
+
this.options = options;
|
|
10
|
+
}
|
|
11
|
+
async migrate() {
|
|
12
|
+
const migrator = new kysely.Migrator({
|
|
13
|
+
db: this.options.db,
|
|
14
|
+
provider: this.options.provider
|
|
15
|
+
});
|
|
16
|
+
const migrations = await migrator.migrateToLatest();
|
|
17
|
+
if (migrations.error) {
|
|
18
|
+
logger.error(migrations.error, `Failed to apply migrations`);
|
|
19
|
+
throw migrations.error;
|
|
20
|
+
}
|
|
21
|
+
await this.options.db.destroy();
|
|
22
|
+
logger.log(`Applied ${migrations.results?.length} migrations successfully`);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
Object.defineProperty(exports, 'PostgresKyselyMigrator', {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function () {
|
|
30
|
+
return PostgresKyselyMigrator;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __commonJS = (cb, mod) => function() {
|
|
9
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
13
|
+
key = keys[i];
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
15
|
+
get: ((k) => from[k]).bind(null, key),
|
|
16
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
22
|
+
value: mod,
|
|
23
|
+
enumerable: true
|
|
24
|
+
}) : target, mod));
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
const pg = __toESM(require("pg"));
|
|
28
|
+
|
|
29
|
+
//#region src/PostgresMigrator.ts
|
|
30
|
+
async function setupClient(uri) {
|
|
31
|
+
const url = new URL(uri);
|
|
32
|
+
const db = new pg.Client({
|
|
33
|
+
user: url.username,
|
|
34
|
+
password: url.password,
|
|
35
|
+
host: url.hostname,
|
|
36
|
+
port: parseInt(url.port),
|
|
37
|
+
database: "postgres"
|
|
38
|
+
});
|
|
39
|
+
let database = url.pathname.slice(1);
|
|
40
|
+
if (database.includes("?")) database = database.substring(0, database.indexOf("?"));
|
|
41
|
+
return {
|
|
42
|
+
database,
|
|
43
|
+
db
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const logger = console;
|
|
47
|
+
var PostgresMigrator = class PostgresMigrator {
|
|
48
|
+
constructor(uri) {
|
|
49
|
+
this.uri = uri;
|
|
50
|
+
}
|
|
51
|
+
static async create(uri) {
|
|
52
|
+
const { database, db } = await setupClient(uri);
|
|
53
|
+
try {
|
|
54
|
+
await db.connect();
|
|
55
|
+
const result = await db.query(`SELECT * FROM pg_catalog.pg_database WHERE datname = '${database}'`);
|
|
56
|
+
if (result.rowCount === 0) await db.query(`CREATE DATABASE "${database}"`);
|
|
57
|
+
return { alreadyExisted: result.rowCount ? result.rowCount > 0 : false };
|
|
58
|
+
} finally {
|
|
59
|
+
await db.end();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
static async drop(uri) {
|
|
63
|
+
const { database, db } = await setupClient(uri);
|
|
64
|
+
try {
|
|
65
|
+
await db.connect();
|
|
66
|
+
await db.query(`DROP DATABASE "${database}"`);
|
|
67
|
+
} finally {
|
|
68
|
+
await db.end();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async start() {
|
|
72
|
+
const { database, db } = await setupClient(this.uri);
|
|
73
|
+
try {
|
|
74
|
+
await PostgresMigrator.create(this.uri);
|
|
75
|
+
await this.migrate();
|
|
76
|
+
logger.log(`Migrating database: ${database}`);
|
|
77
|
+
} finally {
|
|
78
|
+
await db.end();
|
|
79
|
+
}
|
|
80
|
+
return async () => {
|
|
81
|
+
await PostgresMigrator.drop(this.uri);
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
//#endregion
|
|
87
|
+
Object.defineProperty(exports, 'PostgresMigrator', {
|
|
88
|
+
enumerable: true,
|
|
89
|
+
get: function () {
|
|
90
|
+
return PostgresMigrator;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
Object.defineProperty(exports, '__commonJS', {
|
|
94
|
+
enumerable: true,
|
|
95
|
+
get: function () {
|
|
96
|
+
return __commonJS;
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
Object.defineProperty(exports, '__toESM', {
|
|
100
|
+
enumerable: true,
|
|
101
|
+
get: function () {
|
|
102
|
+
return __toESM;
|
|
103
|
+
}
|
|
104
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Client } from "pg";
|
|
2
|
+
|
|
3
|
+
//#region src/PostgresMigrator.ts
|
|
4
|
+
async function setupClient(uri) {
|
|
5
|
+
const url = new URL(uri);
|
|
6
|
+
const db = new Client({
|
|
7
|
+
user: url.username,
|
|
8
|
+
password: url.password,
|
|
9
|
+
host: url.hostname,
|
|
10
|
+
port: parseInt(url.port),
|
|
11
|
+
database: "postgres"
|
|
12
|
+
});
|
|
13
|
+
let database = url.pathname.slice(1);
|
|
14
|
+
if (database.includes("?")) database = database.substring(0, database.indexOf("?"));
|
|
15
|
+
return {
|
|
16
|
+
database,
|
|
17
|
+
db
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
const logger = console;
|
|
21
|
+
var PostgresMigrator = class PostgresMigrator {
|
|
22
|
+
constructor(uri) {
|
|
23
|
+
this.uri = uri;
|
|
24
|
+
}
|
|
25
|
+
static async create(uri) {
|
|
26
|
+
const { database, db } = await setupClient(uri);
|
|
27
|
+
try {
|
|
28
|
+
await db.connect();
|
|
29
|
+
const result = await db.query(`SELECT * FROM pg_catalog.pg_database WHERE datname = '${database}'`);
|
|
30
|
+
if (result.rowCount === 0) await db.query(`CREATE DATABASE "${database}"`);
|
|
31
|
+
return { alreadyExisted: result.rowCount ? result.rowCount > 0 : false };
|
|
32
|
+
} finally {
|
|
33
|
+
await db.end();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
static async drop(uri) {
|
|
37
|
+
const { database, db } = await setupClient(uri);
|
|
38
|
+
try {
|
|
39
|
+
await db.connect();
|
|
40
|
+
await db.query(`DROP DATABASE "${database}"`);
|
|
41
|
+
} finally {
|
|
42
|
+
await db.end();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async start() {
|
|
46
|
+
const { database, db } = await setupClient(this.uri);
|
|
47
|
+
try {
|
|
48
|
+
await PostgresMigrator.create(this.uri);
|
|
49
|
+
await this.migrate();
|
|
50
|
+
logger.log(`Migrating database: ${database}`);
|
|
51
|
+
} finally {
|
|
52
|
+
await db.end();
|
|
53
|
+
}
|
|
54
|
+
return async () => {
|
|
55
|
+
await PostgresMigrator.drop(this.uri);
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
export { PostgresMigrator };
|