@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.
@@ -0,0 +1,12 @@
1
+ $ tsdown
2
+ ℹ tsdown v0.20.3 powered by rolldown v1.0.0-rc.3
3
+ ℹ config file: /home/runner/work/kunk/kunk/packages/docker/tsdown.config.ts (unrun)
4
+ ℹ entry: src/index.ts
5
+ ℹ tsconfig: tsconfig.json
6
+ ℹ Build start
7
+ ℹ dist/index.mjs 1.93 kB │ gzip: 0.94 kB
8
+ ℹ dist/index.d.mts 0.44 kB │ gzip: 0.24 kB
9
+ ℹ 2 files, total: 2.38 kB
10
+ [PLUGIN_TIMINGS] Warning: Your build spent significant time in plugin `rolldown-plugin-dts:generate`. See https://rolldown.rs/options/checks#plugintimings for more details.
11
+
12
+ ✔ Build complete in 4043ms
package/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # @kunk/docker
2
+
3
+ ## 3.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - version 2
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies
12
+ - @kunk/database@3.0.0
13
+ - @kunk/server@3.0.0
14
+ - @kunk/tsconfig@3.0.0
15
+ - @kunk/api@3.0.0
@@ -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
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "rootDir": ".",
4
+ "baseUrl": ".",
5
+ "paths": {
6
+ "@/*": ["./src/*"]
7
+ }
8
+ },
9
+ "include": [
10
+ "src/**/*.ts"
11
+ ],
12
+ "exclude": [
13
+ "node_modules",
14
+ "dist"
15
+ ]
16
+ }
@@ -0,0 +1,14 @@
1
+ import { defineConfig } from 'tsdown'
2
+
3
+ export default defineConfig({
4
+ entry: [
5
+ './src/index.ts'
6
+ ],
7
+ external: [
8
+ "dockerode"
9
+ ],
10
+ dts: true,
11
+ minify: {
12
+ mangle: true,
13
+ }
14
+ })