-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpgpm.ts
More file actions
414 lines (388 loc) · 10.8 KB
/
Copy pathpgpm.ts
File metadata and controls
414 lines (388 loc) · 10.8 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import { execSync } from 'child_process';
import { PgConfig } from 'pg-env';
import { JobsConfig } from './jobs';
/**
* Authentication options for test client sessions
*/
export interface AuthOptions {
/** Role to assume (defaults to 'authenticated' from RoleMapping config) */
role?: string;
/** User ID to set in session context */
userId?: string | number;
/** Key name for user ID in session context (defaults to 'jwt.claims.user_id') */
userIdKey?: string;
}
/**
* Configuration options for PostgreSQL test database connections
*/
export interface PgTestConnectionOptions {
/** The root database to connect to for creating test databases */
rootDb?: string;
/** Template database to use when creating test databases */
template?: string;
/** Prefix to add to test database names */
prefix?: string;
/** PostgreSQL extensions to install in test databases */
extensions?: string[];
/** Current working directory for database operations */
cwd?: string;
/** Test user credentials for app and admin users */
connections?: TestUserCredentials;
/** Role mapping configuration */
roles?: RoleMapping;
/** Default authentication options for db connections */
auth?: AuthOptions;
/** Use advisory locks for role/user creation (for concurrency safety) */
useLocksForRoles?: boolean;
}
/**
* PostgreSQL session context settings for test clients
* Used to set session variables via set_config() for RLS policies, search_path, etc.
*/
export interface PgTestClientContext {
/** PostgreSQL role to assume */
role?: string | null;
/** Additional session context variables (e.g., 'jwt.claims.user_id', 'search_path') */
[key: string]: string | null | undefined;
}
/**
* Role mapping configuration for database security
*/
export interface RoleMapping {
/** Anonymous (unauthenticated) role name */
anonymous?: string;
/** Authenticated user role name */
authenticated?: string;
/** Administrator role name */
administrator?: string;
/** Default role for new connections */
default?: string;
}
/**
* Database connection credentials
*/
export interface DatabaseConnectionOptions {
/** Database user name */
user?: string;
/** Database password */
password?: string;
/** Database role to assume */
role?: string;
}
/**
* Test user credentials for app and admin users
*/
export interface TestUserCredentials {
/** App user credentials (for RLS simulation) */
app?: DatabaseConnectionOptions;
/** Admin user credentials (for test admin operations) */
admin?: DatabaseConnectionOptions;
}
/**
* HTTP server configuration
*/
export interface ServerOptions {
/** Server host address */
host?: string;
/** Server port number */
port?: number;
/** Whether to trust proxy headers */
trustProxy?: boolean;
/** CORS origin configuration */
origin?: string;
/** Whether to enforce strict authentication */
strictAuth?: boolean;
}
/**
* Storage provider type for CDN/bucket operations
*/
export type BucketProvider = 's3' | 'minio' | 'gcs';
/**
* CDN and file storage configuration
*/
export interface CDNOptions {
/** Storage provider type (s3, minio, gcs). Defaults to 'minio' for local dev */
provider?: BucketProvider;
/** S3 bucket name for file storage */
bucketName?: string;
/** AWS region for S3 bucket */
awsRegion?: string;
/** AWS access key for S3 */
awsAccessKey?: string;
/** AWS secret key for S3 */
awsSecretKey?: string;
/** S3-compatible API endpoint URL (MinIO, R2, DO Spaces, GCS, etc.) */
endpoint?: string;
/** Public URL prefix for generating download URLs (e.g., CDN domain, S3 public URL) */
publicUrlPrefix?: string;
}
/**
* CAPTCHA verification configuration
*/
export interface CaptchaOptions {
/** Secret key used by the server to verify reCAPTCHA tokens */
recaptchaSecretKey?: string;
}
/**
* GraphQL upload configuration
*/
export interface UploadOptions {
/** Maximum accepted GraphQL upload file size in bytes */
maxFileSize?: number;
}
/**
* SMTP email configuration options
*/
export interface SmtpOptions {
/** SMTP server hostname */
host?: string;
/** SMTP server port (defaults to 587 for non-secure, 465 for secure) */
port?: number;
/** Use TLS/SSL connection (defaults based on port: true for 465, false otherwise) */
secure?: boolean;
/** SMTP authentication username */
user?: string;
/** SMTP authentication password */
pass?: string;
/** Default sender email address */
from?: string;
/** Default reply-to email address */
replyTo?: string;
/** Require TLS upgrade via STARTTLS */
requireTLS?: boolean;
/** Reject unauthorized TLS certificates */
tlsRejectUnauthorized?: boolean;
/** Use connection pooling for multiple emails */
pool?: boolean;
/** Maximum number of pooled connections */
maxConnections?: number;
/** Maximum messages per connection before reconnecting */
maxMessages?: number;
/** SMTP client hostname for EHLO/HELO */
name?: string;
/** Enable nodemailer logging */
logger?: boolean;
/** Enable nodemailer debug output */
debug?: boolean;
}
/**
* Code generation settings
*/
export interface CodegenOptions {
/** Whether to wrap generated SQL code in transactions */
useTx?: boolean;
}
/**
* Migration and code generation options
*/
export interface MigrationOptions {
/** Code generation settings */
codegen?: CodegenOptions;
}
/**
* Error output formatting options for controlling verbosity of error messages
*/
export interface ErrorOutputOptions {
/** Maximum number of queries to show in error output (default: 30) */
queryHistoryLimit?: number;
/** Maximum total characters for error output before truncation (default: 10000) */
maxLength?: number;
/** When true, disables all limiting and shows full error output (default: false) */
verbose?: boolean;
}
/**
* Configuration for PGPM workspace
*/
export interface PgpmWorkspaceConfig {
/** Glob patterns for package directories */
packages: string[];
/** Optional workspace metadata */
name?: string;
version?: string;
/** Additional workspace settings */
settings?: {
[key: string]: any;
};
/** Deployment configuration for the workspace */
deployment?: Omit<DeploymentOptions, 'toChange'>;
}
/**
* Configuration options for module deployment
*/
export interface DeploymentOptions {
/** Whether to wrap deployments in database transactions */
useTx?: boolean;
/** Use fast deployment strategy (skip migration system) */
fast?: boolean;
/** Whether to use Sqitch plan files for deployments */
usePlan?: boolean;
/** Enable caching of deployment packages */
cache?: boolean;
/** Deploy up to a specific change (inclusive) - can be a change name or tag reference (e.g., '@v1.0.0') */
toChange?: string;
/** Log-only mode - skip script execution and only record deployment metadata */
logOnly?: boolean;
/**
* Hash method for SQL files:
* - 'content': Hash the raw file content (fast, but sensitive to formatting changes)
* - 'ast': Hash the parsed AST structure (robust, ignores formatting/comments but slower)
*/
hashMethod?: 'content' | 'ast';
}
/**
* Main configuration options for the PGPM framework
* Note: GraphQL/Graphile options (graphile, api, features) are in @constructive-io/graphql-types
*/
export interface PgpmOptions {
/** Test database configuration options */
db?: Partial<PgTestConnectionOptions>;
/** PostgreSQL connection configuration */
pg?: Partial<PgConfig>;
/** HTTP server configuration */
server?: ServerOptions;
/** CDN and file storage configuration */
cdn?: CDNOptions;
/** CAPTCHA verification configuration */
captcha?: CaptchaOptions;
/** GraphQL upload configuration */
upload?: UploadOptions;
/** Module deployment configuration */
deployment?: DeploymentOptions;
/** Migration and code generation options */
migrations?: MigrationOptions;
/** Job system configuration */
jobs?: JobsConfig;
/** Error output formatting options */
errorOutput?: ErrorOutputOptions;
/** SMTP email configuration */
smtp?: SmtpOptions;
}
/**
* Default configuration values for PGPM framework
*/
export const pgpmDefaults: PgpmOptions = {
db: {
rootDb: 'postgres',
prefix: 'db-',
extensions: [],
cwd: process.cwd(),
connections: {
app: {
user: 'app_user',
password: 'app_password'
},
admin: {
user: 'app_admin',
password: 'admin_password'
}
},
roles: {
anonymous: 'anonymous',
authenticated: 'authenticated',
administrator: 'administrator',
default: 'anonymous'
},
useLocksForRoles: false
},
pg: {
host: 'localhost',
port: 5432,
user: 'postgres',
password: 'password',
database: 'postgres',
},
server: {
host: 'localhost',
port: 3000,
trustProxy: false,
strictAuth: false,
},
cdn: {
provider: 'minio',
bucketName: 'test-bucket',
awsRegion: 'us-east-1',
awsAccessKey: 'minioadmin',
awsSecretKey: 'minioadmin',
endpoint: 'http://localhost:9000',
publicUrlPrefix: 'http://localhost:9000'
},
captcha: {},
upload: {
maxFileSize: 10 * 1024 * 1024
},
deployment: {
useTx: true,
fast: false,
usePlan: true,
cache: false,
logOnly: false,
hashMethod: 'content'
},
migrations: {
codegen: {
useTx: false
}
},
jobs: {
schema: {
schema: 'app_jobs'
},
worker: {
schema: 'app_jobs',
hostname: 'worker-0',
supportAny: true,
supported: [],
pollInterval: 1000,
gracefulShutdown: true
},
scheduler: {
schema: 'app_jobs',
hostname: 'scheduler-0',
supportAny: true,
supported: [],
pollInterval: 1000,
gracefulShutdown: true
}
},
errorOutput: {
queryHistoryLimit: 30,
maxLength: 10000,
verbose: false
},
smtp: {
port: 587,
secure: false,
pool: false,
logger: false,
debug: false
}
};
export function getGitConfigInfo(): { username: string; email: string } {
const isTestEnv =
process.env.NODE_ENV === 'test' ||
process.env.NODE_ENV === 'testing' || // fallback
process.env.GITHUB_ACTIONS === 'true'; // GitHub Actions
if (isTestEnv) {
return {
username: 'CI Test User',
email: 'ci@example.com'
};
}
let username = '';
let email = '';
try {
username = execSync('git config --global user.name', {
encoding: 'utf8'
}).trim();
} catch {
username = '';
}
try {
email = execSync('git config --global user.email', {
encoding: 'utf8'
}).trim();
} catch {
email = '';
}
return { username, email };
}