@kunk/docker 3.0.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/.turbo/turbo-build.log +12 -0
- package/CHANGELOG.md +15 -0
- package/dist/index.d.mts +17 -0
- package/dist/index.mjs +1 -0
- package/package.json +23 -0
- package/src/containers/PostgresContainer.ts +98 -0
- package/src/containers/_utils.ts +27 -0
- package/src/index.ts +1 -0
- package/tsconfig.json +16 -0
- package/tsdown.config.ts +14 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
$ tsdown
|
|
2
|
+
[34mℹ[39m tsdown [2mv0.20.3[22m powered by rolldown [2mv1.0.0-rc.3[22m
|
|
3
|
+
[34mℹ[39m config file: [4m/home/runner/work/kunk/kunk/packages/docker/tsdown.config.ts[24m (unrun)
|
|
4
|
+
[34mℹ[39m entry: [34msrc/index.ts[39m
|
|
5
|
+
[34mℹ[39m tsconfig: [34mtsconfig.json[39m
|
|
6
|
+
[34mℹ[39m Build start
|
|
7
|
+
[34mℹ[39m [2mdist/[22m[1mindex.mjs[22m [2m1.93 kB[22m [2m│ gzip: 0.94 kB[22m
|
|
8
|
+
[34mℹ[39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m0.44 kB[22m [2m│ gzip: 0.24 kB[22m
|
|
9
|
+
[34mℹ[39m 2 files, total: 2.38 kB
|
|
10
|
+
[33m[PLUGIN_TIMINGS] Warning:[0m Your build spent significant time in plugin `rolldown-plugin-dts:generate`. See https://rolldown.rs/options/checks#plugintimings for more details.
|
|
11
|
+
|
|
12
|
+
[32m✔[39m Build complete in [32m4043ms[39m
|
package/CHANGELOG.md
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//#region src/containers/PostgresContainer.d.ts
|
|
2
|
+
declare class PostgresContainer {
|
|
3
|
+
private container;
|
|
4
|
+
static CLEAR_TEST_DATA: () => Promise<void>;
|
|
5
|
+
static start(options?: {
|
|
6
|
+
hostname?: string;
|
|
7
|
+
port?: string | number;
|
|
8
|
+
username?: string;
|
|
9
|
+
password?: string;
|
|
10
|
+
database?: string;
|
|
11
|
+
}): Promise<PostgresContainer>;
|
|
12
|
+
private start;
|
|
13
|
+
stop(): Promise<void>;
|
|
14
|
+
clear(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { PostgresContainer };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{PostgreSqlContainer}from"@testcontainers/postgresql";import{Wait}from"testcontainers";import{migrate}from"@kunk/database";import{DatabaseConfig}from"@kunk/server";async function dockerExec(e,t){let n=new(await(import(`dockerode`).then(e=>e.default)));try{let r=await(await n.getContainer(e.getId()??``).exec({Cmd:t,AttachStdout:!0,AttachStderr:!0})).start({abortSignal:AbortSignal.timeout(2e3)}),i=``;r.on(`data`,e=>{i+=e.toString()}),await new Promise(e=>r.on(`end`,e)),console.log(`Output:`,i)}catch{}}var PostgresContainer=class a{constructor(){this.container=null}static async start(e={}){let t=new a;return await t.start(e),t}async start(i){let a=i.username??`kunk`,o=i.password??`kunk`,s=i.database??`kunk`,c=!!process.env.LOGGING,l=new PostgreSqlContainer(`postgres:18`).withWaitStrategy(Wait.forLogMessage(`database system is ready to accept connections`,1)).withName(`postgres-${s}`).withDatabase(s).withUsername(a).withPassword(o).withResourcesQuota({memory:.5,cpu:1}).withLogConsumer(e=>{e.on(`data`,e=>{c&&console.debug(`PostgreSQL: `,e.toString())})});i.port&&(l=l.withReuse().withExposedPorts({container:5432,host:Number(i.port)})),this.container=await l.start(),await new Promise(e=>setTimeout(e,1e3)),await migrate({adapter:`postgres`,hostname:this.container.getHost(),port:this.container.getMappedPort(5432),username:a,password:o,database:s,max:1}),DatabaseConfig.set({uri:`postgresql://app_user:${o}@${this.container.getHost()}:${this.container.getMappedPort(5432)}/${s}`,maxPoolSize:3,idleTimeout:5,connectionTimeout:30}),console.log(`DATABASE CONFIG FINISHED`)}async stop(){await this.container?.stop(),this.container=null}async clear(){this.container&&await dockerExec(this.container,[`psql`,`--username`,`kunk`,`--password`,`kunk`,`--host`,this.container.getHost(),`--port`,this.container.getMappedPort(5432).toString(),`--database`,`kunk`,`--command`,`DROP DATABASE kunk`])}};export{PostgresContainer};
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kunk/docker",
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.ts",
|
|
7
|
+
"module": "./src/index.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsdown"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@kunk/api": "workspace:*",
|
|
13
|
+
"@kunk/database": "workspace:*",
|
|
14
|
+
"@kunk/server": "workspace:*",
|
|
15
|
+
"@kunk/tsconfig": "workspace:*",
|
|
16
|
+
"@testcontainers/postgresql": "^11.12.0",
|
|
17
|
+
"testcontainers": "^11.12.0",
|
|
18
|
+
"dockerode": "4.0.9"
|
|
19
|
+
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { type StartedTestContainer } from "testcontainers"
|
|
2
|
+
import { PostgreSqlContainer } from "@testcontainers/postgresql"
|
|
3
|
+
import { Wait } from "testcontainers"
|
|
4
|
+
import { migrate } from "@kunk/database"
|
|
5
|
+
import { DatabaseConfig } from "@kunk/server"
|
|
6
|
+
import { dockerExec } from "./_utils"
|
|
7
|
+
|
|
8
|
+
export class PostgresContainer {
|
|
9
|
+
|
|
10
|
+
private container: StartedTestContainer | null = null
|
|
11
|
+
static CLEAR_TEST_DATA: () => Promise<void>
|
|
12
|
+
|
|
13
|
+
static async start(options: { hostname?: string, port?: string | number, username?: string, password?: string, database?: string } = {}) {
|
|
14
|
+
const pg = new PostgresContainer()
|
|
15
|
+
await pg.start(options)
|
|
16
|
+
return pg
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
private async start(options: { hostname?: string, port?: string | number, username?: string, password?: string, database?: string }) {
|
|
20
|
+
|
|
21
|
+
const root_username = options.username ?? "kunk"
|
|
22
|
+
const root_password = options.password ?? "kunk"
|
|
23
|
+
const root_database = options.database ?? "kunk"
|
|
24
|
+
|
|
25
|
+
const logging = Boolean(process.env['LOGGING'])
|
|
26
|
+
let container = new PostgreSqlContainer("postgres:18")
|
|
27
|
+
.withWaitStrategy(Wait.forLogMessage("database system is ready to accept connections", 1) as any)
|
|
28
|
+
.withName(`postgres-${root_database}`)
|
|
29
|
+
.withDatabase(root_database)
|
|
30
|
+
.withUsername(root_username)
|
|
31
|
+
.withPassword(root_password)
|
|
32
|
+
.withResourcesQuota({
|
|
33
|
+
memory: 0.5,
|
|
34
|
+
cpu: 1
|
|
35
|
+
})
|
|
36
|
+
.withLogConsumer((stream) => {
|
|
37
|
+
stream.on("data", (chunk) => {
|
|
38
|
+
if (!logging) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
console.debug("PostgreSQL: ", chunk.toString());
|
|
42
|
+
});
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
if (options.port) {
|
|
47
|
+
container = container.withReuse()
|
|
48
|
+
.withExposedPorts({
|
|
49
|
+
container: 5432,
|
|
50
|
+
host: Number(options.port),
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
this.container = await container.start()
|
|
55
|
+
|
|
56
|
+
await new Promise((resolve) => setTimeout(resolve, 1000))
|
|
57
|
+
|
|
58
|
+
await migrate({
|
|
59
|
+
adapter: 'postgres',
|
|
60
|
+
hostname: this.container.getHost(),
|
|
61
|
+
port: this.container.getMappedPort(5432),
|
|
62
|
+
username: root_username,
|
|
63
|
+
password: root_password,
|
|
64
|
+
database: root_database,
|
|
65
|
+
max: 1
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
DatabaseConfig.set({
|
|
70
|
+
uri: `postgresql://app_user:${root_password}@${this.container.getHost()}:${this.container.getMappedPort(5432)}/${root_database}`,
|
|
71
|
+
maxPoolSize: 3,
|
|
72
|
+
idleTimeout: 5,
|
|
73
|
+
connectionTimeout: 30,
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
console.log("DATABASE CONFIG FINISHED")
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async stop() {
|
|
80
|
+
await this.container?.stop()
|
|
81
|
+
this.container = null
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async clear() {
|
|
85
|
+
if (this.container) {
|
|
86
|
+
await dockerExec(this.container, [
|
|
87
|
+
"psql",
|
|
88
|
+
"--username", "kunk",
|
|
89
|
+
"--password", "kunk",
|
|
90
|
+
"--host", this.container.getHost(),
|
|
91
|
+
"--port", this.container.getMappedPort(5432).toString(),
|
|
92
|
+
"--database", "kunk",
|
|
93
|
+
"--command", "DROP DATABASE kunk"
|
|
94
|
+
])
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { StartedTestContainer } from "testcontainers";
|
|
2
|
+
|
|
3
|
+
export async function dockerExec(container: StartedTestContainer, command: string[]) {
|
|
4
|
+
const Dockerode = await import("dockerode").then(module => module.default)
|
|
5
|
+
const docker = new Dockerode();
|
|
6
|
+
try {
|
|
7
|
+
const exec = await docker.getContainer(container.getId() ?? "").exec({
|
|
8
|
+
Cmd: command,
|
|
9
|
+
AttachStdout: true,
|
|
10
|
+
AttachStderr: true,
|
|
11
|
+
});
|
|
12
|
+
const stream = await exec.start({
|
|
13
|
+
abortSignal: AbortSignal.timeout(2000),
|
|
14
|
+
});
|
|
15
|
+
let output = "";
|
|
16
|
+
|
|
17
|
+
stream.on("data", (chunk) => {
|
|
18
|
+
output += chunk.toString();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
await new Promise((resolve) => stream.on("end", resolve));
|
|
22
|
+
|
|
23
|
+
console.log("Output:", output);
|
|
24
|
+
} catch (error) {
|
|
25
|
+
debugger
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./containers/PostgresContainer"
|
package/tsconfig.json
ADDED