dbctx 1.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/.prettierrc.cjs +7 -0
- package/.turbo/turbo-build.log +5 -0
- package/dist/App.d.ts +23 -0
- package/dist/App.d.ts.map +1 -0
- package/dist/App.js +9 -0
- package/dist/components/Error.d.ts +7 -0
- package/dist/components/Error.d.ts.map +1 -0
- package/dist/components/Error.js +6 -0
- package/dist/db/analyze.d.mts +3 -0
- package/dist/db/analyze.d.mts.map +1 -0
- package/dist/db/analyze.mjs +16 -0
- package/dist/db/attributes.d.mts +16 -0
- package/dist/db/attributes.d.mts.map +1 -0
- package/dist/db/attributes.mjs +37 -0
- package/dist/db/enums.d.mts +8 -0
- package/dist/db/enums.d.mts.map +1 -0
- package/dist/db/enums.mjs +24 -0
- package/dist/db/file-stats.d.mts +11 -0
- package/dist/db/file-stats.d.mts.map +1 -0
- package/dist/db/file-stats.mjs +43 -0
- package/dist/db/foreign-keys.d.mts +14 -0
- package/dist/db/foreign-keys.d.mts.map +1 -0
- package/dist/db/foreign-keys.mjs +44 -0
- package/dist/db/index.d.mts +10 -0
- package/dist/db/index.d.mts.map +1 -0
- package/dist/db/index.mjs +10 -0
- package/dist/db/indexes.d.mts +16 -0
- package/dist/db/indexes.d.mts.map +1 -0
- package/dist/db/indexes.mjs +38 -0
- package/dist/db/relations.d.mts +10 -0
- package/dist/db/relations.d.mts.map +1 -0
- package/dist/db/relations.mjs +23 -0
- package/dist/db/stats.d.mts +12 -0
- package/dist/db/stats.d.mts.map +1 -0
- package/dist/db/stats.mjs +34 -0
- package/dist/db/version.d.mts +13 -0
- package/dist/db/version.d.mts.map +1 -0
- package/dist/db/version.mjs +19 -0
- package/dist/index.d.mts +17 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +75 -0
- package/dist/logger.d.mts +3 -0
- package/dist/logger.d.mts.map +1 -0
- package/dist/logger.mjs +7 -0
- package/dist/schemas/index.d.mts +2 -0
- package/dist/schemas/index.d.mts.map +1 -0
- package/dist/schemas/index.mjs +91 -0
- package/dist/validatePaths.d.mts +12 -0
- package/dist/validatePaths.d.mts.map +1 -0
- package/dist/validatePaths.mjs +48 -0
- package/package.json +43 -0
- package/src/App.tsx +44 -0
- package/src/components/Error.tsx +16 -0
- package/src/db/analyze.mts +22 -0
- package/src/db/attributes.mts +58 -0
- package/src/db/enums.mts +43 -0
- package/src/db/file-stats.mts +57 -0
- package/src/db/foreign-keys.mts +61 -0
- package/src/db/index.mts +9 -0
- package/src/db/indexes.mts +57 -0
- package/src/db/relations.mts +35 -0
- package/src/db/stats.mts +51 -0
- package/src/db/version.mts +34 -0
- package/src/index.mts +116 -0
- package/src/logger.mts +10 -0
- package/src/schemas/index.mts +102 -0
- package/src/validatePaths.mts +77 -0
- package/tsconfig.json +104 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Pool } from "pg";
|
|
2
|
+
|
|
3
|
+
type TForeignKeyAction = "NO ACTION" | "RESTRICT" | "CASCADE" | "SET NULL" | "SET DEFAULT";
|
|
4
|
+
|
|
5
|
+
export type TForeignKeyInfo = {
|
|
6
|
+
constraint_name: string;
|
|
7
|
+
attributes: string[];
|
|
8
|
+
referenced_relation: string;
|
|
9
|
+
referenced_attributes: string[];
|
|
10
|
+
on_update: TForeignKeyAction;
|
|
11
|
+
on_delete: TForeignKeyAction;
|
|
12
|
+
comment: string | null;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const fetchRelationForeignKeys = async (pool: Pool, relationName: string): Promise<TForeignKeyInfo[]> => {
|
|
16
|
+
const result = await pool.query<TForeignKeyInfo>(
|
|
17
|
+
`
|
|
18
|
+
SELECT
|
|
19
|
+
con.conname AS constraint_name,
|
|
20
|
+
ARRAY(
|
|
21
|
+
SELECT a.attname
|
|
22
|
+
FROM unnest(con.conkey) WITH ORDINALITY AS cols(attnum, ord)
|
|
23
|
+
JOIN pg_attribute a ON a.attrelid = con.conrelid AND a.attnum = cols.attnum
|
|
24
|
+
ORDER BY cols.ord
|
|
25
|
+
) AS attributes,
|
|
26
|
+
ref_cl.relname AS referenced_relation,
|
|
27
|
+
ARRAY(
|
|
28
|
+
SELECT a.attname
|
|
29
|
+
FROM unnest(con.confkey) WITH ORDINALITY AS cols(attnum, ord)
|
|
30
|
+
JOIN pg_attribute a ON a.attrelid = con.confrelid AND a.attnum = cols.attnum
|
|
31
|
+
ORDER BY cols.ord
|
|
32
|
+
) AS referenced_attributes,
|
|
33
|
+
CASE con.confupdtype
|
|
34
|
+
WHEN 'a' THEN 'NO ACTION'
|
|
35
|
+
WHEN 'r' THEN 'RESTRICT'
|
|
36
|
+
WHEN 'c' THEN 'CASCADE'
|
|
37
|
+
WHEN 'n' THEN 'SET NULL'
|
|
38
|
+
WHEN 'd' THEN 'SET DEFAULT'
|
|
39
|
+
END AS on_update,
|
|
40
|
+
CASE con.confdeltype
|
|
41
|
+
WHEN 'a' THEN 'NO ACTION'
|
|
42
|
+
WHEN 'r' THEN 'RESTRICT'
|
|
43
|
+
WHEN 'c' THEN 'CASCADE'
|
|
44
|
+
WHEN 'n' THEN 'SET NULL'
|
|
45
|
+
WHEN 'd' THEN 'SET DEFAULT'
|
|
46
|
+
END AS on_delete,
|
|
47
|
+
obj_description(con.oid, 'pg_constraint') AS comment
|
|
48
|
+
FROM pg_catalog.pg_constraint con
|
|
49
|
+
JOIN pg_catalog.pg_class cl ON cl.oid = con.conrelid
|
|
50
|
+
JOIN pg_catalog.pg_class ref_cl ON ref_cl.oid = con.confrelid
|
|
51
|
+
JOIN pg_catalog.pg_namespace n ON n.oid = cl.relnamespace
|
|
52
|
+
WHERE con.contype = 'f'
|
|
53
|
+
AND n.nspname = 'public'
|
|
54
|
+
AND cl.relname = $1
|
|
55
|
+
ORDER BY con.conname
|
|
56
|
+
`,
|
|
57
|
+
[relationName]
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
return result.rows;
|
|
61
|
+
};
|
package/src/db/index.mts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { fetchPublicRelations, type TRelationType, type TRelationInfo, type TGroupedRelations } from "./relations.mjs";
|
|
2
|
+
export { fetchRelationAttributes, type TAttributeKind, type TGeneratedStorage, type TAttributeInfo } from "./attributes.mjs";
|
|
3
|
+
export { fetchPublicEnums, type TEnumInfo, type TGroupedEnums } from "./enums.mjs";
|
|
4
|
+
export { fetchRelationIndexes, type TIndexInfo } from "./indexes.mjs";
|
|
5
|
+
export { fetchRelationForeignKeys, type TForeignKeyInfo } from "./foreign-keys.mjs";
|
|
6
|
+
export { fetchPostgresVersion, type TPostgresVersion, fetchDatabaseIdentifier, type TDatabaseIdentifier } from "./version.mjs";
|
|
7
|
+
export { fetchRelationFileStats, type TRelationFileStats } from "./file-stats.mjs";
|
|
8
|
+
export { analyzeRelation } from "./analyze.mjs";
|
|
9
|
+
export { fetchRelationStats, type TRelationStats, type TAttributeStats } from "./stats.mjs";
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Pool } from "pg";
|
|
2
|
+
|
|
3
|
+
export type TIndexInfo = {
|
|
4
|
+
index_name: string;
|
|
5
|
+
relation_name: string;
|
|
6
|
+
is_unique: boolean;
|
|
7
|
+
is_primary: boolean;
|
|
8
|
+
is_exclusion: boolean;
|
|
9
|
+
is_partial: boolean;
|
|
10
|
+
partial_predicate: string | null;
|
|
11
|
+
attributes: string[];
|
|
12
|
+
exclusion_operators: string[] | null;
|
|
13
|
+
definition: string;
|
|
14
|
+
comment: string | null;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const fetchRelationIndexes = async (pool: Pool, relationName: string): Promise<TIndexInfo[]> => {
|
|
18
|
+
const result = await pool.query<TIndexInfo>(
|
|
19
|
+
`
|
|
20
|
+
SELECT
|
|
21
|
+
i.relname AS index_name,
|
|
22
|
+
t.relname AS relation_name,
|
|
23
|
+
ix.indisunique AS is_unique,
|
|
24
|
+
ix.indisprimary AS is_primary,
|
|
25
|
+
ix.indisexclusion AS is_exclusion,
|
|
26
|
+
ix.indpred IS NOT NULL AS is_partial,
|
|
27
|
+
pg_get_expr(ix.indpred, ix.indrelid) AS partial_predicate,
|
|
28
|
+
ARRAY(
|
|
29
|
+
SELECT pg_get_indexdef(ix.indexrelid, k + 1, true)
|
|
30
|
+
FROM generate_subscripts(ix.indkey, 1) AS k
|
|
31
|
+
ORDER BY k
|
|
32
|
+
) AS attributes,
|
|
33
|
+
CASE
|
|
34
|
+
WHEN ix.indisexclusion THEN (
|
|
35
|
+
SELECT ARRAY_AGG(op.oprname ORDER BY ord.n)
|
|
36
|
+
FROM pg_constraint con
|
|
37
|
+
CROSS JOIN LATERAL unnest(con.conexclop) WITH ORDINALITY AS ord(opoid, n)
|
|
38
|
+
JOIN pg_operator op ON op.oid = ord.opoid
|
|
39
|
+
WHERE con.conindid = ix.indexrelid
|
|
40
|
+
)
|
|
41
|
+
ELSE NULL
|
|
42
|
+
END AS exclusion_operators,
|
|
43
|
+
pg_get_indexdef(ix.indexrelid) AS definition,
|
|
44
|
+
obj_description(i.oid, 'pg_class') AS comment
|
|
45
|
+
FROM pg_catalog.pg_index ix
|
|
46
|
+
JOIN pg_catalog.pg_class i ON i.oid = ix.indexrelid
|
|
47
|
+
JOIN pg_catalog.pg_class t ON t.oid = ix.indrelid
|
|
48
|
+
JOIN pg_catalog.pg_namespace n ON n.oid = t.relnamespace
|
|
49
|
+
WHERE n.nspname = 'public'
|
|
50
|
+
AND t.relname = $1
|
|
51
|
+
ORDER BY i.relname
|
|
52
|
+
`,
|
|
53
|
+
[relationName]
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
return result.rows;
|
|
57
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Pool } from "pg";
|
|
2
|
+
import { groupBy } from "es-toolkit";
|
|
3
|
+
|
|
4
|
+
export type TRelationType = "partitioned_table" | "table" | "view" | "materialized_view";
|
|
5
|
+
|
|
6
|
+
export type TRelationInfo = {
|
|
7
|
+
name: string;
|
|
8
|
+
type: TRelationType;
|
|
9
|
+
comment: string | null;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type TGroupedRelations = Record<TRelationType, TRelationInfo[]>;
|
|
13
|
+
|
|
14
|
+
export const fetchPublicRelations = async (pool: Pool): Promise<TGroupedRelations> => {
|
|
15
|
+
const result = await pool.query<TRelationInfo>(`
|
|
16
|
+
SELECT relname AS name,
|
|
17
|
+
CASE relkind
|
|
18
|
+
WHEN 'p' THEN 'partitioned_table'
|
|
19
|
+
WHEN 'r' THEN 'table'
|
|
20
|
+
WHEN 'v' THEN 'view'
|
|
21
|
+
WHEN 'm' THEN 'materialized_view'
|
|
22
|
+
END AS type,
|
|
23
|
+
obj_description(oid, 'pg_class') AS comment
|
|
24
|
+
FROM pg_catalog.pg_class
|
|
25
|
+
WHERE relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = 'public')
|
|
26
|
+
AND (
|
|
27
|
+
relkind = 'p'
|
|
28
|
+
OR (relkind = 'r' AND NOT relispartition)
|
|
29
|
+
OR relkind IN ('v', 'm')
|
|
30
|
+
)
|
|
31
|
+
ORDER BY type, name
|
|
32
|
+
`);
|
|
33
|
+
|
|
34
|
+
return groupBy(result.rows, (row) => row.type) as TGroupedRelations;
|
|
35
|
+
};
|
package/src/db/stats.mts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { Pool } from "pg";
|
|
2
|
+
|
|
3
|
+
export type TAttributeStats = {
|
|
4
|
+
attribute_name: string;
|
|
5
|
+
estimated_distinct: number;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export type TRelationStats = {
|
|
9
|
+
relation_name: string;
|
|
10
|
+
estimated_row_count: number;
|
|
11
|
+
attributes: TAttributeStats[];
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const fetchRelationStats = async (pool: Pool, relationName: string): Promise<TRelationStats> => {
|
|
15
|
+
const result = await pool.query<TAttributeStats & { relation_name: string; estimated_row_count: number }>(
|
|
16
|
+
`
|
|
17
|
+
SELECT
|
|
18
|
+
s.tablename AS relation_name,
|
|
19
|
+
c.reltuples::bigint AS estimated_row_count,
|
|
20
|
+
s.attname AS attribute_name,
|
|
21
|
+
CASE
|
|
22
|
+
WHEN s.n_distinct > 0 THEN s.n_distinct
|
|
23
|
+
ELSE abs(s.n_distinct) * c.reltuples
|
|
24
|
+
END::bigint AS estimated_distinct
|
|
25
|
+
FROM pg_catalog.pg_stats s
|
|
26
|
+
JOIN pg_catalog.pg_class c ON c.relname = s.tablename
|
|
27
|
+
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace AND n.nspname = s.schemaname
|
|
28
|
+
WHERE s.schemaname = 'public'
|
|
29
|
+
AND s.tablename = $1
|
|
30
|
+
ORDER BY s.attnum
|
|
31
|
+
`,
|
|
32
|
+
[relationName]
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
if (result.rows.length === 0) {
|
|
36
|
+
return {
|
|
37
|
+
relation_name: relationName,
|
|
38
|
+
estimated_row_count: 0,
|
|
39
|
+
attributes: [],
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
relation_name: result.rows[0].relation_name,
|
|
45
|
+
estimated_row_count: result.rows[0].estimated_row_count,
|
|
46
|
+
attributes: result.rows.map(({ attribute_name, estimated_distinct }) => ({
|
|
47
|
+
attribute_name,
|
|
48
|
+
estimated_distinct,
|
|
49
|
+
})),
|
|
50
|
+
};
|
|
51
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Pool } from "pg";
|
|
2
|
+
|
|
3
|
+
export type TPostgresVersion = {
|
|
4
|
+
version: string;
|
|
5
|
+
version_num: number;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const fetchPostgresVersion = async (pool: Pool): Promise<TPostgresVersion> => {
|
|
9
|
+
const result = await pool.query<TPostgresVersion>(`
|
|
10
|
+
SELECT
|
|
11
|
+
current_setting('server_version') AS version,
|
|
12
|
+
current_setting('server_version_num')::int AS version_num
|
|
13
|
+
`);
|
|
14
|
+
|
|
15
|
+
return result.rows[0];
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type TDatabaseIdentifier = {
|
|
19
|
+
system_identifier: string;
|
|
20
|
+
database_oid: number;
|
|
21
|
+
database_name: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const fetchDatabaseIdentifier = async (pool: Pool): Promise<TDatabaseIdentifier> => {
|
|
25
|
+
const result = await pool.query<TDatabaseIdentifier>(`
|
|
26
|
+
SELECT
|
|
27
|
+
system_identifier::text AS system_identifier,
|
|
28
|
+
(SELECT oid::int FROM pg_database WHERE datname = current_database()) AS database_oid,
|
|
29
|
+
current_database() AS database_name
|
|
30
|
+
FROM pg_control_system()
|
|
31
|
+
`);
|
|
32
|
+
|
|
33
|
+
return result.rows[0];
|
|
34
|
+
};
|
package/src/index.mts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from "commander";
|
|
3
|
+
import { render } from "ink";
|
|
4
|
+
import { Error } from "./components/Error.js";
|
|
5
|
+
import { App, type TConnectionConfig } from "./App.js";
|
|
6
|
+
import { validatePathOptions, type TPathValidationError } from "./validatePaths.mjs";
|
|
7
|
+
|
|
8
|
+
export type TSSLMode = "disable" | "allow" | "prefer" | "require" | "verify-ca" | "verify-full";
|
|
9
|
+
|
|
10
|
+
export interface ConnectionOptions {
|
|
11
|
+
hostname?: string;
|
|
12
|
+
port?: number;
|
|
13
|
+
database?: string;
|
|
14
|
+
username?: string;
|
|
15
|
+
sslmode?: TSSLMode;
|
|
16
|
+
sslcert?: string;
|
|
17
|
+
sslkey?: string;
|
|
18
|
+
sslrootcert?: string;
|
|
19
|
+
sshHost?: string;
|
|
20
|
+
sshPort?: number;
|
|
21
|
+
sshUsername?: string;
|
|
22
|
+
sshPrivateKey?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const program = new Command();
|
|
26
|
+
|
|
27
|
+
program
|
|
28
|
+
.name("dbctx")
|
|
29
|
+
.description("AI generated docs from your application database")
|
|
30
|
+
.version("1.0.0")
|
|
31
|
+
.argument("[connection-string]", "PostgreSQL connection string (env: DATABASE_URL)")
|
|
32
|
+
.option("-h, --hostname <host>", "database hostname (env: PGHOST, default: localhost)")
|
|
33
|
+
.option("-p, --port <port>", "database port (env: PGPORT, default: 5432)", parseInt)
|
|
34
|
+
.option("-d, --database <name>", "database name (env: PGDATABASE, default: postgres)")
|
|
35
|
+
.option("-U, --username <user>", "database username (env: PGUSER, default: postgres)")
|
|
36
|
+
.option("--sslmode <mode>", "SSL mode: disable|allow|prefer|require|verify-ca|verify-full (env: PGSSLMODE, default: prefer)")
|
|
37
|
+
.option("--sslcert <path>", "path to client certificate (env: PGSSLCERT)")
|
|
38
|
+
.option("--sslkey <path>", "path to client private key (env: PGSSLKEY)")
|
|
39
|
+
.option("--sslrootcert <path>", "path to root CA certificate (env: PGSSLROOTCERT)")
|
|
40
|
+
.option("--ssh-host <host>", "SSH tunnel host (bastion/jump server)")
|
|
41
|
+
.option("--ssh-port <port>", "SSH tunnel port (default: 22)", parseInt)
|
|
42
|
+
.option("--ssh-username <user>", "SSH tunnel username (default: $USER)")
|
|
43
|
+
.option("--ssh-private-key <path>", "path to SSH private key (default: ~/.ssh/id_rsa)")
|
|
44
|
+
.action((connectionString: string | undefined, options: ConnectionOptions) => {
|
|
45
|
+
const hasConnectionString = Boolean(connectionString);
|
|
46
|
+
const hasIndividualOptions = Boolean(
|
|
47
|
+
options.hostname || options.port || options.database || options.username
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
if (hasConnectionString && hasIndividualOptions) {
|
|
51
|
+
render(
|
|
52
|
+
Error({
|
|
53
|
+
message:
|
|
54
|
+
"Cannot use both connection string and individual options (-h, -p, -d, -U). Use one or the other.",
|
|
55
|
+
})
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
process.exit(1);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Validate path options exist on filesystem
|
|
62
|
+
const { errors: pathErrors, expandedPaths } = validatePathOptions(options);
|
|
63
|
+
|
|
64
|
+
if (pathErrors.length > 0) {
|
|
65
|
+
const errorMessages = pathErrors
|
|
66
|
+
.map(({ option, path }: TPathValidationError) => ` ${option}: ${path}`)
|
|
67
|
+
.join("\n");
|
|
68
|
+
|
|
69
|
+
render(
|
|
70
|
+
Error({
|
|
71
|
+
message: `The following paths do not exist:\n${errorMessages}`,
|
|
72
|
+
})
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
process.exit(1);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Replace original paths with expanded paths (~ resolved to home directory)
|
|
79
|
+
const validatedOptions: ConnectionOptions = {
|
|
80
|
+
...options,
|
|
81
|
+
...expandedPaths,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
if (hasIndividualOptions) {
|
|
85
|
+
const requiredOptions = [
|
|
86
|
+
{ key: "hostname", flag: "-h/--hostname" },
|
|
87
|
+
{ key: "port", flag: "-p/--port" },
|
|
88
|
+
{ key: "database", flag: "-d/--database" },
|
|
89
|
+
{ key: "username", flag: "-U/--username" },
|
|
90
|
+
] as const;
|
|
91
|
+
|
|
92
|
+
const missing = requiredOptions
|
|
93
|
+
.filter(({ key }) => !validatedOptions[key])
|
|
94
|
+
.map(({ flag }) => flag);
|
|
95
|
+
|
|
96
|
+
if (missing.length > 0) {
|
|
97
|
+
render(
|
|
98
|
+
Error({
|
|
99
|
+
message: `Missing required options: ${missing.join(", ")}`,
|
|
100
|
+
})
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
process.exit(1);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// All validations passed - build config and render the app
|
|
108
|
+
const config: TConnectionConfig = {
|
|
109
|
+
connectionString,
|
|
110
|
+
...validatedOptions,
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
render(App({ config }));
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
program.parse();
|
package/src/logger.mts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import winston from 'winston';
|
|
2
|
+
|
|
3
|
+
export const logger = winston.createLogger({
|
|
4
|
+
level: process.env.NODE_ENV === 'development' ? 'debug' : 'info',
|
|
5
|
+
format: winston.format.combine(
|
|
6
|
+
winston.format.timestamp(),
|
|
7
|
+
winston.format.json(),
|
|
8
|
+
),
|
|
9
|
+
transports: [new winston.transports.Console()],
|
|
10
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import {z} from 'zod';
|
|
2
|
+
|
|
3
|
+
const relationAttribute = z
|
|
4
|
+
.object({
|
|
5
|
+
name: z.string(),
|
|
6
|
+
data_type: z.string(),
|
|
7
|
+
is_nullable: z.boolean(),
|
|
8
|
+
comment: z.string().nullable(),
|
|
9
|
+
})
|
|
10
|
+
.and(
|
|
11
|
+
z.discriminatedUnion('kind', [
|
|
12
|
+
z.discriminatedUnion('has_default', [
|
|
13
|
+
z.object({
|
|
14
|
+
kind: z.literal('regular'),
|
|
15
|
+
has_default: z.literal(false),
|
|
16
|
+
}),
|
|
17
|
+
z.object({
|
|
18
|
+
kind: z.literal('regular'),
|
|
19
|
+
has_default: z.literal(true),
|
|
20
|
+
default_expression: z.string(),
|
|
21
|
+
}),
|
|
22
|
+
]),
|
|
23
|
+
z.object({
|
|
24
|
+
kind: z.literal('generated'),
|
|
25
|
+
generated_storage: z.enum(['stored', 'virtual']),
|
|
26
|
+
generated_expression: z.string(),
|
|
27
|
+
}),
|
|
28
|
+
]),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
const indexInfo = z
|
|
32
|
+
.object({
|
|
33
|
+
index_name: z.string(),
|
|
34
|
+
relation_name: z.string(),
|
|
35
|
+
attributes: z.array(z.string()),
|
|
36
|
+
definition: z.string(),
|
|
37
|
+
comment: z.string().nullable(),
|
|
38
|
+
})
|
|
39
|
+
.and(
|
|
40
|
+
z.discriminatedUnion('index_kind', [
|
|
41
|
+
z.object({ index_kind: z.literal('primary') }),
|
|
42
|
+
z.object({ index_kind: z.literal('unique') }),
|
|
43
|
+
z.object({
|
|
44
|
+
index_kind: z.literal('exclusion'),
|
|
45
|
+
exclusion_operators: z.array(z.string()),
|
|
46
|
+
}),
|
|
47
|
+
z.object({ index_kind: z.literal('plain') }),
|
|
48
|
+
]),
|
|
49
|
+
)
|
|
50
|
+
.and(
|
|
51
|
+
z.discriminatedUnion('is_partial', [
|
|
52
|
+
z.object({ is_partial: z.literal(false) }),
|
|
53
|
+
z.object({
|
|
54
|
+
is_partial: z.literal(true),
|
|
55
|
+
partial_predicate: z.string(),
|
|
56
|
+
}),
|
|
57
|
+
]),
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const reference = z.object({
|
|
61
|
+
constraint_name: z.string(),
|
|
62
|
+
attributes: z.array(z.string()),
|
|
63
|
+
referenced_relation: z.string(),
|
|
64
|
+
referenced_attributes: z.array(z.string()),
|
|
65
|
+
on_update: z.enum([
|
|
66
|
+
'NO ACTION',
|
|
67
|
+
'RESTRICT',
|
|
68
|
+
'CASCADE',
|
|
69
|
+
'SET NULL',
|
|
70
|
+
'SET DEFAULT',
|
|
71
|
+
]),
|
|
72
|
+
on_delete: z.enum([
|
|
73
|
+
'NO ACTION',
|
|
74
|
+
'RESTRICT',
|
|
75
|
+
'CASCADE',
|
|
76
|
+
'SET NULL',
|
|
77
|
+
'SET DEFAULT',
|
|
78
|
+
]),
|
|
79
|
+
comment: z.string().nullable(),
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const relation = z.object({
|
|
83
|
+
name: z.string(),
|
|
84
|
+
comment: z.string().nullable(),
|
|
85
|
+
kind: z.enum(['partitioned_table', 'table', 'view', 'materialized_view']),
|
|
86
|
+
summary: z.string().optional(),
|
|
87
|
+
details: z.string().optional(),
|
|
88
|
+
attributes: z.record(z.string(), relationAttribute),
|
|
89
|
+
references: z.record(z.string(), reference),
|
|
90
|
+
indexes: z.record(z.string(), indexInfo),
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const relations = z.record(z.string(), relation);
|
|
94
|
+
|
|
95
|
+
const database = z.object({
|
|
96
|
+
type: z.enum(['postgresql']),
|
|
97
|
+
name: z.string(),
|
|
98
|
+
comment: z.string().nullable(),
|
|
99
|
+
summary: z.string().optional(),
|
|
100
|
+
details: z.string().optional(),
|
|
101
|
+
relations: relations,
|
|
102
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
|
|
5
|
+
type TPathValidationResult =
|
|
6
|
+
| { valid: true; expandedPath: string }
|
|
7
|
+
| { valid: false; error: string };
|
|
8
|
+
|
|
9
|
+
type TPathOptionKey = "sslcert" | "sslkey" | "sslrootcert" | "sshPrivateKey";
|
|
10
|
+
|
|
11
|
+
export type TPathValidationError = {
|
|
12
|
+
option: string;
|
|
13
|
+
path: string;
|
|
14
|
+
error: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const expandTilde = (path: string): string =>
|
|
18
|
+
path.startsWith("~/") || path === "~"
|
|
19
|
+
? path.replace(/^~/, homedir())
|
|
20
|
+
: path;
|
|
21
|
+
|
|
22
|
+
const normalizePath = (path: string): string =>
|
|
23
|
+
resolve(expandTilde(path));
|
|
24
|
+
|
|
25
|
+
const validatePathExists = (path: string): TPathValidationResult => {
|
|
26
|
+
const expandedPath = normalizePath(path);
|
|
27
|
+
|
|
28
|
+
return existsSync(expandedPath)
|
|
29
|
+
? { valid: true, expandedPath }
|
|
30
|
+
: { valid: false, error: `Path does not exist: ${path}` };
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const PATH_OPTION_LABELS: Record<TPathOptionKey, string> = {
|
|
34
|
+
sslcert: "--sslcert",
|
|
35
|
+
sslkey: "--sslkey",
|
|
36
|
+
sslrootcert: "--sslrootcert",
|
|
37
|
+
sshPrivateKey: "--ssh-private-key",
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const validatePathOptions = <T extends Partial<Record<TPathOptionKey, string>>>(
|
|
41
|
+
options: T
|
|
42
|
+
): { errors: TPathValidationError[]; expandedPaths: Partial<Record<TPathOptionKey, string>> } => {
|
|
43
|
+
const pathKeys: TPathOptionKey[] = ["sslcert", "sslkey", "sslrootcert", "sshPrivateKey"];
|
|
44
|
+
|
|
45
|
+
const results = pathKeys
|
|
46
|
+
.filter((key) => options[key] !== undefined)
|
|
47
|
+
.map((key) => {
|
|
48
|
+
const path = options[key] as string;
|
|
49
|
+
const result = validatePathExists(path);
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
key,
|
|
53
|
+
path,
|
|
54
|
+
result,
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const errors = results
|
|
59
|
+
.filter((r) => !r.result.valid)
|
|
60
|
+
.map((r) => ({
|
|
61
|
+
option: PATH_OPTION_LABELS[r.key],
|
|
62
|
+
path: r.path,
|
|
63
|
+
error: (r.result as { valid: false; error: string }).error,
|
|
64
|
+
}));
|
|
65
|
+
|
|
66
|
+
const expandedPaths = results
|
|
67
|
+
.filter((r) => r.result.valid)
|
|
68
|
+
.reduce(
|
|
69
|
+
(acc, r) => ({
|
|
70
|
+
...acc,
|
|
71
|
+
[r.key]: (r.result as { valid: true; expandedPath: string }).expandedPath,
|
|
72
|
+
}),
|
|
73
|
+
{} as Partial<Record<TPathOptionKey, string>>
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
return { errors, expandedPaths };
|
|
77
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
{
|
|
2
|
+
"include": ["./src/**/*", "./src/**/*.json"],
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"target": "ESNext",
|
|
5
|
+
"module": "nodenext",
|
|
6
|
+
"rootDir": "./src",
|
|
7
|
+
"moduleResolution": "nodenext",
|
|
8
|
+
"outDir": "./dist",
|
|
9
|
+
"sourceRoot": "./src",
|
|
10
|
+
"lib": ["ESNext"],
|
|
11
|
+
/* Visit https://aka.ms/tsconfig.json to read more about this file */
|
|
12
|
+
|
|
13
|
+
/* Projects */
|
|
14
|
+
"incremental": true /* Enable incremental compilation */,
|
|
15
|
+
"composite": true /* Enable constraints that allow a TypeScript project to be used with project references. */,
|
|
16
|
+
// "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
|
|
17
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
|
|
18
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
19
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
20
|
+
|
|
21
|
+
/* Language and Environment */
|
|
22
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
23
|
+
"experimentalDecorators": true /* Enable experimental support for TC39 stage 2 draft decorators. */,
|
|
24
|
+
"emitDecoratorMetadata": true /* Emit design-type metadata for decorated declarations in source files. */,
|
|
25
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
|
|
26
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
27
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
|
|
28
|
+
// "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
|
|
29
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
30
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
31
|
+
|
|
32
|
+
/* Modules */
|
|
33
|
+
|
|
34
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
35
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
36
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
37
|
+
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
|
|
38
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
39
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
40
|
+
"resolveJsonModule": true /* Enable importing .json files */,
|
|
41
|
+
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
|
|
42
|
+
|
|
43
|
+
/* JavaScript Support */
|
|
44
|
+
"allowJs": false /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */,
|
|
45
|
+
"checkJs": false /* Enable error reporting in type-checked JavaScript files. */,
|
|
46
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
|
|
47
|
+
|
|
48
|
+
/* Emit */
|
|
49
|
+
"declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
|
|
50
|
+
"declarationMap": true /* Create sourcemaps for d.ts files. */,
|
|
51
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
52
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
53
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
|
|
54
|
+
|
|
55
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
56
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
57
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
58
|
+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
|
|
59
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
60
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
61
|
+
"inlineSourceMap": true /* Include sourcemap files inside the emitted JavaScript. */,
|
|
62
|
+
"inlineSources": true /* Include source code in the sourcemaps inside the emitted JavaScript. */,
|
|
63
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
64
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
65
|
+
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
|
|
66
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
|
|
67
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
68
|
+
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
|
|
69
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
70
|
+
|
|
71
|
+
/* Interop Constraints */
|
|
72
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
73
|
+
"allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */,
|
|
74
|
+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
|
|
75
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
76
|
+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
|
77
|
+
|
|
78
|
+
/* Type Checking */
|
|
79
|
+
"strict": true /* Enable all strict type-checking options. */,
|
|
80
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
|
|
81
|
+
// "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
|
|
82
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
83
|
+
// "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
|
|
84
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
85
|
+
// "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
|
|
86
|
+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
|
|
87
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
88
|
+
// "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
|
|
89
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
|
|
90
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
91
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
92
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
93
|
+
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
|
|
94
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
95
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
|
|
96
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
97
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
98
|
+
|
|
99
|
+
/* Completeness */
|
|
100
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
101
|
+
"skipLibCheck": true,
|
|
102
|
+
"jsx": "react-jsx"
|
|
103
|
+
}
|
|
104
|
+
}
|