-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ts
More file actions
140 lines (133 loc) · 4.66 KB
/
setup.ts
File metadata and controls
140 lines (133 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import type { ClientConfig } from "pg";
import { setupTables } from "../../postgres";
import { commands, optionalArgs, requiredArgs } from "../constants";
import { prepare } from "../prepare";
import { program } from "../program";
program
.command(commands.setup.name("postgres"))
.description(commands.setup.description)
.argument(requiredArgs.jsonPath.name, requiredArgs.jsonPath.description)
.argument(optionalArgs.dbPath.name, optionalArgs.dbPath.description)
.option("--user <user>", "default process.env.PGUSER || process.env.USER")
.option("--password <password>", "default process.env.PGPASSWORD")
.option("--host <host>", "default process.env.PGHOST")
.option("--port <port>", "default process.env.PGPORT")
.option("--database <database>", "default process.env.PGDATABASE || user")
.option(
"--connection-string <connectionString>",
"e.g. postgres://user:password@host:5432/database",
)
.option(
"--ssl <ssl>",
"passed directly to node.TLSSocket, supports all tls.connect options",
)
.option(
"--statement-timeout <statementTimeout>",
"number of milliseconds before a statement in query will time out, default is no timeout",
)
.option(
"--query-timeout <queryTimeout>",
"number of milliseconds before a query call will timeout, default is no timeout",
)
.option(
"--lock-timeout <lockTimeout>",
"number of milliseconds a query is allowed to be en lock state before it's cancelled due to lock timeout",
)
.option(
"--application-name <applicationName>",
"The name of the application that created this Client instance",
)
.option(
"--connection-timeout-millis <connectionTimeoutMillis>",
"number of milliseconds to wait for connection, default is no timeout",
)
.option(
"--keep-alive-initial-delay-millis <keepAliveInitialDelayMillis>",
"set the initial delay before the first keepalive probe is sent on an idle socket",
)
.option(
"--idle-in-transaction-session-timeout <idleInTransactionSessionTimeout>",
"number of milliseconds before terminating any session with an open idle transaction, default is no timeout",
)
.option(
"--client-encoding <clientEncoding>",
"specifies the character set encoding that the database uses for sending data to the client",
)
.option(
"--fallback-application-name <fallbackApplicationName>",
"provide an application name to use if application_name is not set",
)
.option(
"--options <options>",
"command-line options to be sent to the server",
)
.action(setup);
function getNumberOption(value: string | undefined): number | undefined {
return value === undefined ? undefined : Number(value);
}
function getSslOption(value: any): ClientConfig["ssl"] {
try {
return JSON.parse(value);
} catch {
return value == "true" ? true : undefined;
}
}
export async function setup(
jsonPath: string,
dbPath: string | undefined,
options: {
user?: string;
password?: string;
host?: string;
port?: string;
database?: string;
connectionString?: string;
ssl?: string;
statementTimeout?: string;
queryTimeout?: string;
lockTimeout?: string;
applicationName?: string;
connectionTimeoutMillis?: string;
keepAliveInitialDelayMillis?: string;
idleInTransactionSessionTimeout?: string;
clientEncoding?: string;
fallbackApplicationName?: string;
options?: string;
} = {},
) {
const config: ClientConfig | undefined =
dbPath === undefined && Object.keys(options).length
? {
user: options.user,
password: options.password,
host: options.host,
port: getNumberOption(options.port),
database: options.database,
connectionString: options.connectionString,
statement_timeout: getNumberOption(options.statementTimeout),
ssl: getSslOption(options.ssl),
query_timeout: getNumberOption(options.queryTimeout),
lock_timeout: getNumberOption(options.lockTimeout),
application_name: options.applicationName,
connectionTimeoutMillis: getNumberOption(
options.connectionTimeoutMillis,
),
keepAliveInitialDelayMillis: getNumberOption(
options.keepAliveInitialDelayMillis,
),
idle_in_transaction_session_timeout: getNumberOption(
options.idleInTransactionSessionTimeout,
),
client_encoding: options.clientEncoding,
fallback_application_name: options.fallbackApplicationName,
options: options.options,
}
: undefined;
const { data, prefix } = await prepare(jsonPath);
await setupTables({
config,
path: dbPath,
data,
prefix,
});
}