@pgpmjs/export 0.1.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,1159 @@
1
+ import { mkdirSync, rmSync } from 'fs';
2
+ import { sync as glob } from 'glob';
3
+ import { toSnakeCase } from 'komoji';
4
+ import path from 'path';
5
+ import { PgpmPackage, getMissingInstallableModules, parseAuthor } from '@pgpmjs/core';
6
+ // =============================================================================
7
+ // Shared constants
8
+ // =============================================================================
9
+ /**
10
+ * Required extensions for database schema exports.
11
+ * Includes native PostgreSQL extensions and pgpm modules.
12
+ */
13
+ export const DB_REQUIRED_EXTENSIONS = [
14
+ 'plpgsql',
15
+ 'uuid-ossp',
16
+ 'citext',
17
+ 'pgcrypto',
18
+ 'btree_gin',
19
+ 'btree_gist',
20
+ 'pg_textsearch',
21
+ 'pg_trgm',
22
+ 'postgis',
23
+ 'hstore',
24
+ 'vector',
25
+ 'metaschema-schema',
26
+ 'pgpm-inflection',
27
+ 'pgpm-uuid',
28
+ 'pgpm-utils',
29
+ 'pgpm-database-jobs',
30
+ 'pgpm-jwt-claims',
31
+ 'pgpm-stamps',
32
+ 'pgpm-base32',
33
+ 'pgpm-totp',
34
+ 'pgpm-types'
35
+ ];
36
+ /**
37
+ * Map PostgreSQL data types to FieldType values.
38
+ * Uses udt_name from information_schema which gives the base type name.
39
+ */
40
+ const mapPgTypeToFieldType = (udtName) => {
41
+ switch (udtName) {
42
+ case 'uuid':
43
+ return 'uuid';
44
+ case '_uuid':
45
+ return 'uuid[]';
46
+ case 'text':
47
+ case 'varchar':
48
+ case 'bpchar':
49
+ case 'name':
50
+ return 'text';
51
+ case '_text':
52
+ case '_varchar':
53
+ return 'text[]';
54
+ case 'bool':
55
+ return 'boolean';
56
+ case 'jsonb':
57
+ case 'json':
58
+ return 'jsonb';
59
+ case '_jsonb':
60
+ return 'jsonb[]';
61
+ case 'int4':
62
+ case 'int8':
63
+ case 'int2':
64
+ case 'numeric':
65
+ return 'int';
66
+ case 'interval':
67
+ return 'interval';
68
+ case 'timestamptz':
69
+ case 'timestamp':
70
+ return 'timestamptz';
71
+ default:
72
+ return 'text';
73
+ }
74
+ };
75
+ /**
76
+ * Required extensions for service/meta exports.
77
+ * Includes native PostgreSQL extensions and pgpm modules for metadata management.
78
+ */
79
+ export const SERVICE_REQUIRED_EXTENSIONS = [
80
+ 'plpgsql',
81
+ 'metaschema-schema',
82
+ 'metaschema-modules',
83
+ 'services'
84
+ ];
85
+ /**
86
+ * Common SQL header for meta export files.
87
+ * Sets session_replication_role and grants necessary permissions.
88
+ */
89
+ export const META_COMMON_HEADER = `SET session_replication_role TO replica;
90
+ -- using replica in case we are deploying triggers to metaschema_public
91
+
92
+ -- unaccent, postgis affected and require grants
93
+ GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public to public;
94
+
95
+ DO $LQLMIGRATION$
96
+ DECLARE
97
+ BEGIN
98
+
99
+ EXECUTE format('GRANT CONNECT ON DATABASE %I TO %I', current_database(), 'app_user');
100
+ EXECUTE format('GRANT CONNECT ON DATABASE %I TO %I', current_database(), 'app_admin');
101
+
102
+ END;
103
+ $LQLMIGRATION$;`;
104
+ /**
105
+ * Common SQL footer for meta export files.
106
+ */
107
+ export const META_COMMON_FOOTER = `
108
+ SET session_replication_role TO DEFAULT;`;
109
+ /**
110
+ * Ordered list of meta tables for export.
111
+ * Tables are processed in this order to satisfy foreign key dependencies.
112
+ */
113
+ export const META_TABLE_ORDER = [
114
+ 'database',
115
+ 'schema',
116
+ 'table',
117
+ 'field',
118
+ 'policy',
119
+ 'index',
120
+ 'trigger',
121
+ 'trigger_function',
122
+ 'rls_function',
123
+ 'foreign_key_constraint',
124
+ 'primary_key_constraint',
125
+ 'unique_constraint',
126
+ 'check_constraint',
127
+ 'full_text_search',
128
+ 'schema_grant',
129
+ 'table_grant',
130
+ 'default_privilege',
131
+ 'domains',
132
+ 'sites',
133
+ 'apis',
134
+ 'apps',
135
+ 'site_modules',
136
+ 'site_themes',
137
+ 'site_metadata',
138
+ 'api_modules',
139
+ 'api_extensions',
140
+ 'api_schemas',
141
+ 'rls_module',
142
+ 'user_auth_module',
143
+ 'memberships_module',
144
+ 'permissions_module',
145
+ 'limits_module',
146
+ 'levels_module',
147
+ 'users_module',
148
+ 'hierarchy_module',
149
+ 'membership_types_module',
150
+ 'invites_module',
151
+ 'emails_module',
152
+ 'sessions_module',
153
+ 'secrets_module',
154
+ 'profiles_module',
155
+ 'encrypted_secrets_module',
156
+ 'connected_accounts_module',
157
+ 'phone_numbers_module',
158
+ 'crypto_addresses_module',
159
+ 'crypto_auth_module',
160
+ 'field_module',
161
+ 'table_module',
162
+ 'secure_table_provision',
163
+ 'uuid_module',
164
+ 'default_ids_module',
165
+ 'denormalized_table_field',
166
+ 'table_template_module'
167
+ ];
168
+ /**
169
+ * Shared metadata table configuration.
170
+ *
171
+ * This is the **superset** of fields needed by both the SQL export flow
172
+ * (export-meta.ts) and the GraphQL export flow (export-graphql-meta.ts).
173
+ * Each flow dynamically filters to only the fields that actually exist:
174
+ * - SQL flow: uses buildDynamicFields() to intersect with information_schema
175
+ * - GraphQL flow: filters to fields present in the returned data
176
+ *
177
+ * Adding a field here that doesn't exist in a particular environment is safe.
178
+ */
179
+ export const META_TABLE_CONFIG = {
180
+ // =============================================================================
181
+ // metaschema_public tables
182
+ // =============================================================================
183
+ database: {
184
+ schema: 'metaschema_public',
185
+ table: 'database',
186
+ fields: {
187
+ id: 'uuid',
188
+ owner_id: 'uuid',
189
+ name: 'text',
190
+ hash: 'uuid'
191
+ }
192
+ },
193
+ database_extension: {
194
+ schema: 'metaschema_public',
195
+ table: 'database_extension',
196
+ fields: {
197
+ id: 'uuid',
198
+ database_id: 'uuid',
199
+ name: 'text',
200
+ schema_id: 'uuid'
201
+ }
202
+ },
203
+ schema: {
204
+ schema: 'metaschema_public',
205
+ table: 'schema',
206
+ fields: {
207
+ id: 'uuid',
208
+ database_id: 'uuid',
209
+ name: 'text',
210
+ schema_name: 'text',
211
+ description: 'text',
212
+ is_public: 'boolean'
213
+ }
214
+ },
215
+ table: {
216
+ schema: 'metaschema_public',
217
+ table: 'table',
218
+ fields: {
219
+ id: 'uuid',
220
+ database_id: 'uuid',
221
+ schema_id: 'uuid',
222
+ name: 'text',
223
+ description: 'text'
224
+ }
225
+ },
226
+ field: {
227
+ schema: 'metaschema_public',
228
+ table: 'field',
229
+ // Use ON CONFLICT DO NOTHING to handle the unique constraint (databases_field_uniq_names_idx)
230
+ // which normalizes UUID field names by stripping suffixes like _id, _uuid, etc.
231
+ // This causes collisions when tables have both 'foo' (text) and 'foo_id' (uuid) columns.
232
+ conflictDoNothing: true,
233
+ fields: {
234
+ id: 'uuid',
235
+ database_id: 'uuid',
236
+ table_id: 'uuid',
237
+ name: 'text',
238
+ type: 'text',
239
+ description: 'text'
240
+ }
241
+ },
242
+ policy: {
243
+ schema: 'metaschema_public',
244
+ table: 'policy',
245
+ fields: {
246
+ id: 'uuid',
247
+ database_id: 'uuid',
248
+ table_id: 'uuid',
249
+ name: 'text',
250
+ grantee_name: 'text',
251
+ privilege: 'text',
252
+ permissive: 'boolean',
253
+ disabled: 'boolean',
254
+ policy_type: 'text',
255
+ data: 'jsonb'
256
+ }
257
+ },
258
+ index: {
259
+ schema: 'metaschema_public',
260
+ table: 'index',
261
+ fields: {
262
+ id: 'uuid',
263
+ database_id: 'uuid',
264
+ table_id: 'uuid',
265
+ name: 'text',
266
+ field_ids: 'uuid[]',
267
+ include_field_ids: 'uuid[]',
268
+ access_method: 'text',
269
+ index_params: 'jsonb',
270
+ where_clause: 'jsonb',
271
+ is_unique: 'boolean'
272
+ }
273
+ },
274
+ trigger: {
275
+ schema: 'metaschema_public',
276
+ table: 'trigger',
277
+ fields: {
278
+ id: 'uuid',
279
+ database_id: 'uuid',
280
+ table_id: 'uuid',
281
+ name: 'text',
282
+ event: 'text',
283
+ function_name: 'text'
284
+ }
285
+ },
286
+ trigger_function: {
287
+ schema: 'metaschema_public',
288
+ table: 'trigger_function',
289
+ fields: {
290
+ id: 'uuid',
291
+ database_id: 'uuid',
292
+ name: 'text',
293
+ code: 'text'
294
+ }
295
+ },
296
+ rls_function: {
297
+ schema: 'metaschema_public',
298
+ table: 'rls_function',
299
+ fields: {
300
+ id: 'uuid',
301
+ database_id: 'uuid',
302
+ table_id: 'uuid',
303
+ name: 'text',
304
+ label: 'text',
305
+ description: 'text',
306
+ data: 'jsonb',
307
+ inline: 'boolean',
308
+ security: 'int'
309
+ }
310
+ },
311
+ foreign_key_constraint: {
312
+ schema: 'metaschema_public',
313
+ table: 'foreign_key_constraint',
314
+ fields: {
315
+ id: 'uuid',
316
+ database_id: 'uuid',
317
+ table_id: 'uuid',
318
+ name: 'text',
319
+ description: 'text',
320
+ smart_tags: 'jsonb',
321
+ type: 'text',
322
+ field_ids: 'uuid[]',
323
+ ref_table_id: 'uuid',
324
+ ref_field_ids: 'uuid[]',
325
+ delete_action: 'text',
326
+ update_action: 'text'
327
+ }
328
+ },
329
+ primary_key_constraint: {
330
+ schema: 'metaschema_public',
331
+ table: 'primary_key_constraint',
332
+ fields: {
333
+ id: 'uuid',
334
+ database_id: 'uuid',
335
+ table_id: 'uuid',
336
+ name: 'text',
337
+ type: 'text',
338
+ field_ids: 'uuid[]'
339
+ }
340
+ },
341
+ unique_constraint: {
342
+ schema: 'metaschema_public',
343
+ table: 'unique_constraint',
344
+ fields: {
345
+ id: 'uuid',
346
+ database_id: 'uuid',
347
+ table_id: 'uuid',
348
+ name: 'text',
349
+ description: 'text',
350
+ smart_tags: 'jsonb',
351
+ type: 'text',
352
+ field_ids: 'uuid[]'
353
+ }
354
+ },
355
+ check_constraint: {
356
+ schema: 'metaschema_public',
357
+ table: 'check_constraint',
358
+ fields: {
359
+ id: 'uuid',
360
+ database_id: 'uuid',
361
+ table_id: 'uuid',
362
+ name: 'text',
363
+ type: 'text',
364
+ field_ids: 'uuid[]',
365
+ expr: 'jsonb'
366
+ }
367
+ },
368
+ full_text_search: {
369
+ schema: 'metaschema_public',
370
+ table: 'full_text_search',
371
+ fields: {
372
+ id: 'uuid',
373
+ database_id: 'uuid',
374
+ table_id: 'uuid',
375
+ field_id: 'uuid',
376
+ field_ids: 'uuid[]',
377
+ weights: 'text[]',
378
+ langs: 'text[]'
379
+ }
380
+ },
381
+ schema_grant: {
382
+ schema: 'metaschema_public',
383
+ table: 'schema_grant',
384
+ fields: {
385
+ id: 'uuid',
386
+ database_id: 'uuid',
387
+ schema_id: 'uuid',
388
+ grantee_name: 'text'
389
+ }
390
+ },
391
+ table_grant: {
392
+ schema: 'metaschema_public',
393
+ table: 'table_grant',
394
+ fields: {
395
+ id: 'uuid',
396
+ database_id: 'uuid',
397
+ table_id: 'uuid',
398
+ privilege: 'text',
399
+ grantee_name: 'text',
400
+ field_ids: 'uuid[]',
401
+ is_grant: 'boolean'
402
+ }
403
+ },
404
+ default_privilege: {
405
+ schema: 'metaschema_public',
406
+ table: 'default_privilege',
407
+ fields: {
408
+ id: 'uuid',
409
+ database_id: 'uuid',
410
+ schema_id: 'uuid',
411
+ object_type: 'text',
412
+ privilege: 'text',
413
+ grantee_name: 'text',
414
+ is_grant: 'boolean'
415
+ }
416
+ },
417
+ // =============================================================================
418
+ // services_public tables
419
+ // =============================================================================
420
+ domains: {
421
+ schema: 'services_public',
422
+ table: 'domains',
423
+ fields: {
424
+ id: 'uuid',
425
+ database_id: 'uuid',
426
+ site_id: 'uuid',
427
+ api_id: 'uuid',
428
+ domain: 'text',
429
+ subdomain: 'text'
430
+ }
431
+ },
432
+ sites: {
433
+ schema: 'services_public',
434
+ table: 'sites',
435
+ fields: {
436
+ id: 'uuid',
437
+ database_id: 'uuid',
438
+ title: 'text',
439
+ description: 'text',
440
+ og_image: 'image',
441
+ favicon: 'upload',
442
+ apple_touch_icon: 'image',
443
+ logo: 'image'
444
+ }
445
+ },
446
+ apis: {
447
+ schema: 'services_public',
448
+ table: 'apis',
449
+ fields: {
450
+ id: 'uuid',
451
+ database_id: 'uuid',
452
+ name: 'text',
453
+ is_public: 'boolean',
454
+ role_name: 'text',
455
+ anon_role: 'text'
456
+ }
457
+ },
458
+ apps: {
459
+ schema: 'services_public',
460
+ table: 'apps',
461
+ fields: {
462
+ id: 'uuid',
463
+ database_id: 'uuid',
464
+ site_id: 'uuid',
465
+ name: 'text',
466
+ app_image: 'image',
467
+ app_store_link: 'url',
468
+ app_store_id: 'text',
469
+ app_id_prefix: 'text',
470
+ play_store_link: 'url'
471
+ }
472
+ },
473
+ site_modules: {
474
+ schema: 'services_public',
475
+ table: 'site_modules',
476
+ fields: {
477
+ id: 'uuid',
478
+ database_id: 'uuid',
479
+ site_id: 'uuid',
480
+ name: 'text',
481
+ data: 'jsonb'
482
+ }
483
+ },
484
+ site_themes: {
485
+ schema: 'services_public',
486
+ table: 'site_themes',
487
+ fields: {
488
+ id: 'uuid',
489
+ database_id: 'uuid',
490
+ site_id: 'uuid',
491
+ theme: 'jsonb'
492
+ }
493
+ },
494
+ site_metadata: {
495
+ schema: 'services_public',
496
+ table: 'site_metadata',
497
+ fields: {
498
+ id: 'uuid',
499
+ database_id: 'uuid',
500
+ site_id: 'uuid',
501
+ title: 'text',
502
+ description: 'text',
503
+ og_image: 'image'
504
+ }
505
+ },
506
+ api_modules: {
507
+ schema: 'services_public',
508
+ table: 'api_modules',
509
+ fields: {
510
+ id: 'uuid',
511
+ database_id: 'uuid',
512
+ api_id: 'uuid',
513
+ name: 'text',
514
+ data: 'jsonb'
515
+ }
516
+ },
517
+ api_extensions: {
518
+ schema: 'services_public',
519
+ table: 'api_extensions',
520
+ fields: {
521
+ id: 'uuid',
522
+ database_id: 'uuid',
523
+ api_id: 'uuid',
524
+ name: 'text'
525
+ }
526
+ },
527
+ api_schemas: {
528
+ schema: 'services_public',
529
+ table: 'api_schemas',
530
+ fields: {
531
+ id: 'uuid',
532
+ database_id: 'uuid',
533
+ schema_id: 'uuid',
534
+ api_id: 'uuid'
535
+ }
536
+ },
537
+ // =============================================================================
538
+ // metaschema_modules_public tables
539
+ // =============================================================================
540
+ rls_module: {
541
+ schema: 'metaschema_modules_public',
542
+ table: 'rls_module',
543
+ fields: {
544
+ id: 'uuid',
545
+ database_id: 'uuid',
546
+ schema_id: 'uuid',
547
+ private_schema_id: 'uuid',
548
+ session_credentials_table_id: 'uuid',
549
+ sessions_table_id: 'uuid',
550
+ users_table_id: 'uuid',
551
+ authenticate: 'text',
552
+ authenticate_strict: 'text',
553
+ current_role: 'text',
554
+ current_role_id: 'text'
555
+ }
556
+ },
557
+ user_auth_module: {
558
+ schema: 'metaschema_modules_public',
559
+ table: 'user_auth_module',
560
+ fields: {
561
+ id: 'uuid',
562
+ database_id: 'uuid',
563
+ schema_id: 'uuid',
564
+ emails_table_id: 'uuid',
565
+ users_table_id: 'uuid',
566
+ secrets_table_id: 'uuid',
567
+ encrypted_table_id: 'uuid',
568
+ sessions_table_id: 'uuid',
569
+ session_credentials_table_id: 'uuid',
570
+ audits_table_id: 'uuid',
571
+ audits_table_name: 'text',
572
+ sign_in_function: 'text',
573
+ sign_up_function: 'text',
574
+ sign_out_function: 'text',
575
+ sign_in_one_time_token_function: 'text',
576
+ one_time_token_function: 'text',
577
+ extend_token_expires: 'text',
578
+ send_account_deletion_email_function: 'text',
579
+ delete_account_function: 'text',
580
+ set_password_function: 'text',
581
+ reset_password_function: 'text',
582
+ forgot_password_function: 'text',
583
+ send_verification_email_function: 'text',
584
+ verify_email_function: 'text',
585
+ verify_password_function: 'text',
586
+ check_password_function: 'text'
587
+ }
588
+ },
589
+ memberships_module: {
590
+ schema: 'metaschema_modules_public',
591
+ table: 'memberships_module',
592
+ fields: {
593
+ id: 'uuid',
594
+ database_id: 'uuid',
595
+ schema_id: 'uuid',
596
+ private_schema_id: 'uuid',
597
+ memberships_table_id: 'uuid',
598
+ memberships_table_name: 'text',
599
+ members_table_id: 'uuid',
600
+ members_table_name: 'text',
601
+ membership_defaults_table_id: 'uuid',
602
+ membership_defaults_table_name: 'text',
603
+ grants_table_id: 'uuid',
604
+ grants_table_name: 'text',
605
+ actor_table_id: 'uuid',
606
+ limits_table_id: 'uuid',
607
+ default_limits_table_id: 'uuid',
608
+ permissions_table_id: 'uuid',
609
+ default_permissions_table_id: 'uuid',
610
+ sprt_table_id: 'uuid',
611
+ admin_grants_table_id: 'uuid',
612
+ admin_grants_table_name: 'text',
613
+ owner_grants_table_id: 'uuid',
614
+ owner_grants_table_name: 'text',
615
+ membership_type: 'int',
616
+ entity_table_id: 'uuid',
617
+ entity_table_owner_id: 'uuid',
618
+ prefix: 'text',
619
+ actor_mask_check: 'text',
620
+ actor_perm_check: 'text',
621
+ entity_ids_by_mask: 'text',
622
+ entity_ids_by_perm: 'text',
623
+ entity_ids_function: 'text'
624
+ }
625
+ },
626
+ permissions_module: {
627
+ schema: 'metaschema_modules_public',
628
+ table: 'permissions_module',
629
+ fields: {
630
+ id: 'uuid',
631
+ database_id: 'uuid',
632
+ schema_id: 'uuid',
633
+ private_schema_id: 'uuid',
634
+ table_id: 'uuid',
635
+ table_name: 'text',
636
+ default_table_id: 'uuid',
637
+ default_table_name: 'text',
638
+ bitlen: 'int',
639
+ membership_type: 'int',
640
+ entity_table_id: 'uuid',
641
+ actor_table_id: 'uuid',
642
+ prefix: 'text',
643
+ get_padded_mask: 'text',
644
+ get_mask: 'text',
645
+ get_by_mask: 'text',
646
+ get_mask_by_name: 'text'
647
+ }
648
+ },
649
+ limits_module: {
650
+ schema: 'metaschema_modules_public',
651
+ table: 'limits_module',
652
+ fields: {
653
+ id: 'uuid',
654
+ database_id: 'uuid',
655
+ schema_id: 'uuid',
656
+ private_schema_id: 'uuid',
657
+ table_id: 'uuid',
658
+ table_name: 'text',
659
+ default_table_id: 'uuid',
660
+ default_table_name: 'text',
661
+ limit_increment_function: 'text',
662
+ limit_decrement_function: 'text',
663
+ limit_increment_trigger: 'text',
664
+ limit_decrement_trigger: 'text',
665
+ limit_update_trigger: 'text',
666
+ limit_check_function: 'text',
667
+ prefix: 'text',
668
+ membership_type: 'int',
669
+ entity_table_id: 'uuid',
670
+ actor_table_id: 'uuid'
671
+ }
672
+ },
673
+ levels_module: {
674
+ schema: 'metaschema_modules_public',
675
+ table: 'levels_module',
676
+ fields: {
677
+ id: 'uuid',
678
+ database_id: 'uuid',
679
+ schema_id: 'uuid',
680
+ private_schema_id: 'uuid',
681
+ steps_table_id: 'uuid',
682
+ steps_table_name: 'text',
683
+ achievements_table_id: 'uuid',
684
+ achievements_table_name: 'text',
685
+ levels_table_id: 'uuid',
686
+ levels_table_name: 'text',
687
+ level_requirements_table_id: 'uuid',
688
+ level_requirements_table_name: 'text',
689
+ completed_step: 'text',
690
+ incompleted_step: 'text',
691
+ tg_achievement: 'text',
692
+ tg_achievement_toggle: 'text',
693
+ tg_achievement_toggle_boolean: 'text',
694
+ tg_achievement_boolean: 'text',
695
+ upsert_achievement: 'text',
696
+ tg_update_achievements: 'text',
697
+ steps_required: 'text',
698
+ level_achieved: 'text',
699
+ prefix: 'text',
700
+ membership_type: 'int',
701
+ entity_table_id: 'uuid',
702
+ actor_table_id: 'uuid'
703
+ }
704
+ },
705
+ users_module: {
706
+ schema: 'metaschema_modules_public',
707
+ table: 'users_module',
708
+ fields: {
709
+ id: 'uuid',
710
+ database_id: 'uuid',
711
+ schema_id: 'uuid',
712
+ table_id: 'uuid',
713
+ table_name: 'text',
714
+ type_table_id: 'uuid',
715
+ type_table_name: 'text'
716
+ }
717
+ },
718
+ hierarchy_module: {
719
+ schema: 'metaschema_modules_public',
720
+ table: 'hierarchy_module',
721
+ fields: {
722
+ id: 'uuid',
723
+ database_id: 'uuid',
724
+ schema_id: 'uuid',
725
+ private_schema_id: 'uuid',
726
+ chart_edges_table_id: 'uuid',
727
+ chart_edges_table_name: 'text',
728
+ hierarchy_sprt_table_id: 'uuid',
729
+ hierarchy_sprt_table_name: 'text',
730
+ chart_edge_grants_table_id: 'uuid',
731
+ chart_edge_grants_table_name: 'text',
732
+ entity_table_id: 'uuid',
733
+ users_table_id: 'uuid',
734
+ prefix: 'text',
735
+ private_schema_name: 'text',
736
+ sprt_table_name: 'text',
737
+ rebuild_hierarchy_function: 'text',
738
+ get_subordinates_function: 'text',
739
+ get_managers_function: 'text',
740
+ is_manager_of_function: 'text'
741
+ }
742
+ },
743
+ membership_types_module: {
744
+ schema: 'metaschema_modules_public',
745
+ table: 'membership_types_module',
746
+ fields: {
747
+ id: 'uuid',
748
+ database_id: 'uuid',
749
+ schema_id: 'uuid',
750
+ table_id: 'uuid',
751
+ table_name: 'text'
752
+ }
753
+ },
754
+ invites_module: {
755
+ schema: 'metaschema_modules_public',
756
+ table: 'invites_module',
757
+ fields: {
758
+ id: 'uuid',
759
+ database_id: 'uuid',
760
+ schema_id: 'uuid',
761
+ private_schema_id: 'uuid',
762
+ emails_table_id: 'uuid',
763
+ users_table_id: 'uuid',
764
+ invites_table_id: 'uuid',
765
+ claimed_invites_table_id: 'uuid',
766
+ invites_table_name: 'text',
767
+ claimed_invites_table_name: 'text',
768
+ submit_invite_code_function: 'text',
769
+ prefix: 'text',
770
+ membership_type: 'int',
771
+ entity_table_id: 'uuid'
772
+ }
773
+ },
774
+ emails_module: {
775
+ schema: 'metaschema_modules_public',
776
+ table: 'emails_module',
777
+ fields: {
778
+ id: 'uuid',
779
+ database_id: 'uuid',
780
+ schema_id: 'uuid',
781
+ private_schema_id: 'uuid',
782
+ table_id: 'uuid',
783
+ owner_table_id: 'uuid',
784
+ table_name: 'text'
785
+ }
786
+ },
787
+ sessions_module: {
788
+ schema: 'metaschema_modules_public',
789
+ table: 'sessions_module',
790
+ fields: {
791
+ id: 'uuid',
792
+ database_id: 'uuid',
793
+ schema_id: 'uuid',
794
+ sessions_table_id: 'uuid',
795
+ session_credentials_table_id: 'uuid',
796
+ auth_settings_table_id: 'uuid',
797
+ users_table_id: 'uuid',
798
+ sessions_default_expiration: 'interval',
799
+ sessions_table: 'text',
800
+ session_credentials_table: 'text',
801
+ auth_settings_table: 'text'
802
+ }
803
+ },
804
+ secrets_module: {
805
+ schema: 'metaschema_modules_public',
806
+ table: 'secrets_module',
807
+ fields: {
808
+ id: 'uuid',
809
+ database_id: 'uuid',
810
+ schema_id: 'uuid',
811
+ table_id: 'uuid',
812
+ table_name: 'text'
813
+ }
814
+ },
815
+ profiles_module: {
816
+ schema: 'metaschema_modules_public',
817
+ table: 'profiles_module',
818
+ fields: {
819
+ id: 'uuid',
820
+ database_id: 'uuid',
821
+ schema_id: 'uuid',
822
+ private_schema_id: 'uuid',
823
+ table_id: 'uuid',
824
+ table_name: 'text',
825
+ profile_permissions_table_id: 'uuid',
826
+ profile_permissions_table_name: 'text',
827
+ profile_grants_table_id: 'uuid',
828
+ profile_grants_table_name: 'text',
829
+ profile_definition_grants_table_id: 'uuid',
830
+ profile_definition_grants_table_name: 'text',
831
+ membership_type: 'int',
832
+ entity_table_id: 'uuid',
833
+ actor_table_id: 'uuid',
834
+ permissions_table_id: 'uuid',
835
+ memberships_table_id: 'uuid',
836
+ prefix: 'text'
837
+ }
838
+ },
839
+ encrypted_secrets_module: {
840
+ schema: 'metaschema_modules_public',
841
+ table: 'encrypted_secrets_module',
842
+ fields: {
843
+ id: 'uuid',
844
+ database_id: 'uuid',
845
+ schema_id: 'uuid',
846
+ table_id: 'uuid',
847
+ table_name: 'text'
848
+ }
849
+ },
850
+ connected_accounts_module: {
851
+ schema: 'metaschema_modules_public',
852
+ table: 'connected_accounts_module',
853
+ fields: {
854
+ id: 'uuid',
855
+ database_id: 'uuid',
856
+ schema_id: 'uuid',
857
+ private_schema_id: 'uuid',
858
+ table_id: 'uuid',
859
+ owner_table_id: 'uuid',
860
+ table_name: 'text'
861
+ }
862
+ },
863
+ phone_numbers_module: {
864
+ schema: 'metaschema_modules_public',
865
+ table: 'phone_numbers_module',
866
+ fields: {
867
+ id: 'uuid',
868
+ database_id: 'uuid',
869
+ schema_id: 'uuid',
870
+ private_schema_id: 'uuid',
871
+ table_id: 'uuid',
872
+ owner_table_id: 'uuid',
873
+ table_name: 'text'
874
+ }
875
+ },
876
+ crypto_addresses_module: {
877
+ schema: 'metaschema_modules_public',
878
+ table: 'crypto_addresses_module',
879
+ fields: {
880
+ id: 'uuid',
881
+ database_id: 'uuid',
882
+ schema_id: 'uuid',
883
+ private_schema_id: 'uuid',
884
+ table_id: 'uuid',
885
+ owner_table_id: 'uuid',
886
+ table_name: 'text',
887
+ crypto_network: 'text'
888
+ }
889
+ },
890
+ crypto_auth_module: {
891
+ schema: 'metaschema_modules_public',
892
+ table: 'crypto_auth_module',
893
+ fields: {
894
+ id: 'uuid',
895
+ database_id: 'uuid',
896
+ schema_id: 'uuid',
897
+ users_table_id: 'uuid',
898
+ sessions_table_id: 'uuid',
899
+ session_credentials_table_id: 'uuid',
900
+ secrets_table_id: 'uuid',
901
+ addresses_table_id: 'uuid',
902
+ user_field: 'text',
903
+ crypto_network: 'text',
904
+ sign_in_request_challenge: 'text',
905
+ sign_in_record_failure: 'text',
906
+ sign_up_with_key: 'text',
907
+ sign_in_with_challenge: 'text'
908
+ }
909
+ },
910
+ field_module: {
911
+ schema: 'metaschema_modules_public',
912
+ table: 'field_module',
913
+ fields: {
914
+ id: 'uuid',
915
+ database_id: 'uuid',
916
+ private_schema_id: 'uuid',
917
+ table_id: 'uuid',
918
+ field_id: 'uuid',
919
+ node_type: 'text',
920
+ data: 'jsonb',
921
+ triggers: 'text[]',
922
+ functions: 'text[]'
923
+ }
924
+ },
925
+ table_module: {
926
+ schema: 'metaschema_modules_public',
927
+ table: 'table_module',
928
+ fields: {
929
+ id: 'uuid',
930
+ database_id: 'uuid',
931
+ private_schema_id: 'uuid',
932
+ table_id: 'uuid',
933
+ node_type: 'text',
934
+ data: 'jsonb',
935
+ fields: 'uuid[]'
936
+ }
937
+ },
938
+ table_template_module: {
939
+ schema: 'metaschema_modules_public',
940
+ table: 'table_template_module',
941
+ fields: {
942
+ id: 'uuid',
943
+ database_id: 'uuid',
944
+ schema_id: 'uuid',
945
+ private_schema_id: 'uuid',
946
+ table_id: 'uuid',
947
+ owner_table_id: 'uuid',
948
+ table_name: 'text',
949
+ node_type: 'text',
950
+ data: 'jsonb'
951
+ }
952
+ },
953
+ secure_table_provision: {
954
+ schema: 'metaschema_modules_public',
955
+ table: 'secure_table_provision',
956
+ fields: {
957
+ id: 'uuid',
958
+ database_id: 'uuid',
959
+ schema_id: 'uuid',
960
+ table_id: 'uuid',
961
+ table_name: 'text',
962
+ node_type: 'text',
963
+ use_rls: 'boolean',
964
+ node_data: 'jsonb',
965
+ grant_roles: 'text[]',
966
+ fields: 'jsonb[]',
967
+ grant_privileges: 'jsonb[]',
968
+ policy_type: 'text',
969
+ policy_privileges: 'text[]',
970
+ policy_role: 'text',
971
+ policy_permissive: 'boolean',
972
+ policy_data: 'jsonb',
973
+ out_fields: 'uuid[]'
974
+ }
975
+ },
976
+ uuid_module: {
977
+ schema: 'metaschema_modules_public',
978
+ table: 'uuid_module',
979
+ fields: {
980
+ id: 'uuid',
981
+ database_id: 'uuid',
982
+ schema_id: 'uuid',
983
+ uuid_function: 'text',
984
+ uuid_seed: 'text'
985
+ }
986
+ },
987
+ default_ids_module: {
988
+ schema: 'metaschema_modules_public',
989
+ table: 'default_ids_module',
990
+ fields: {
991
+ id: 'uuid',
992
+ database_id: 'uuid'
993
+ }
994
+ },
995
+ denormalized_table_field: {
996
+ schema: 'metaschema_modules_public',
997
+ table: 'denormalized_table_field',
998
+ fields: {
999
+ id: 'uuid',
1000
+ database_id: 'uuid',
1001
+ table_id: 'uuid',
1002
+ field_id: 'uuid',
1003
+ set_ids: 'uuid[]',
1004
+ ref_table_id: 'uuid',
1005
+ ref_field_id: 'uuid',
1006
+ ref_ids: 'uuid[]',
1007
+ use_updates: 'boolean',
1008
+ update_defaults: 'boolean',
1009
+ func_name: 'text',
1010
+ func_order: 'int'
1011
+ }
1012
+ }
1013
+ };
1014
+ // =============================================================================
1015
+ // Shared functions
1016
+ // =============================================================================
1017
+ /**
1018
+ * Checks which pgpm modules from the extensions list are missing from the workspace
1019
+ * and prompts the user if they want to install them.
1020
+ *
1021
+ * This function only does detection and prompting - it does NOT install.
1022
+ * Use installMissingModules() after the module is created to do the actual installation.
1023
+ *
1024
+ * @param project - The PgpmPackage instance (only needs workspace context)
1025
+ * @param extensions - List of extension names (control file names)
1026
+ * @param prompter - Optional prompter for interactive confirmation
1027
+ * @returns Object with missing modules and whether user wants to install them
1028
+ */
1029
+ export const detectMissingModules = async (project, extensions, prompter, argv) => {
1030
+ // Use workspace-level check - doesn't require being inside a module
1031
+ const installed = project.getWorkspaceInstalledModules();
1032
+ const missingModules = getMissingInstallableModules(extensions, installed);
1033
+ if (missingModules.length === 0) {
1034
+ return { missingModules: [], shouldInstall: false };
1035
+ }
1036
+ const missingNames = missingModules.map(m => m.npmName);
1037
+ console.log(`\nMissing pgpm modules detected: ${missingNames.join(', ')}`);
1038
+ if (prompter) {
1039
+ const { install } = await prompter.prompt(argv || {}, [
1040
+ {
1041
+ type: 'confirm',
1042
+ name: 'install',
1043
+ message: `Install missing modules (${missingNames.join(', ')})?`,
1044
+ default: true
1045
+ }
1046
+ ]);
1047
+ return { missingModules, shouldInstall: install };
1048
+ }
1049
+ return { missingModules, shouldInstall: false };
1050
+ };
1051
+ /**
1052
+ * Installs missing modules into a specific module directory.
1053
+ * Must be called after the module has been created.
1054
+ *
1055
+ * @param moduleDir - The directory of the module to install into
1056
+ * @param missingModules - Array of missing modules to install
1057
+ */
1058
+ export const installMissingModules = async (moduleDir, missingModules) => {
1059
+ if (missingModules.length === 0) {
1060
+ return;
1061
+ }
1062
+ const missingNames = missingModules.map(m => m.npmName);
1063
+ console.log('Installing missing modules...');
1064
+ // Create a new PgpmPackage instance pointing to the module directory
1065
+ const moduleProject = new PgpmPackage(moduleDir);
1066
+ await moduleProject.installModules(...missingNames);
1067
+ console.log('Modules installed successfully.');
1068
+ };
1069
+ /**
1070
+ * Generates a function for replacing schema names and extension names in strings.
1071
+ */
1072
+ export const makeReplacer = ({ schemas, name, schemaPrefix }) => {
1073
+ const replacements = ['constructive-extension-name', name];
1074
+ const prefix = schemaPrefix || name;
1075
+ const schemaReplacers = schemas.map((schema) => [
1076
+ schema.schema_name,
1077
+ toSnakeCase(`${prefix}_${schema.name}`)
1078
+ ]);
1079
+ const replace = [...schemaReplacers, replacements].map(([from, to]) => [new RegExp(from, 'g'), to]);
1080
+ const replacer = (str, n = 0) => {
1081
+ if (!str)
1082
+ return '';
1083
+ if (replace[n] && replace[n].length === 2) {
1084
+ return replacer(str.replace(replace[n][0], replace[n][1]), n + 1);
1085
+ }
1086
+ return str;
1087
+ };
1088
+ return { replacer, replace };
1089
+ };
1090
+ /**
1091
+ * Creates a PGPM package directory or resets the deploy/revert/verify directories if it exists.
1092
+ * If the module already exists and a prompter is provided, prompts the user for confirmation.
1093
+ *
1094
+ * @returns The absolute path to the created/prepared module directory
1095
+ */
1096
+ export const preparePackage = async ({ project, author, outdir, name, description, extensions, prompter, repoName, username }) => {
1097
+ const curDir = process.cwd();
1098
+ const pgpmDir = path.resolve(path.join(outdir, name));
1099
+ mkdirSync(pgpmDir, { recursive: true });
1100
+ process.chdir(pgpmDir);
1101
+ try {
1102
+ const plan = glob(path.join(pgpmDir, 'pgpm.plan'));
1103
+ if (!plan.length) {
1104
+ const { fullName, email } = parseAuthor(author);
1105
+ // Always run non-interactively — all answers are pre-filled
1106
+ const effectiveUsername = username || fullName || 'export';
1107
+ await project.initModule({
1108
+ name,
1109
+ description,
1110
+ author,
1111
+ extensions,
1112
+ // Use outputDir to create module directly in the specified location
1113
+ outputDir: outdir,
1114
+ noTty: true,
1115
+ prompter,
1116
+ answers: {
1117
+ moduleName: name,
1118
+ moduleDesc: description,
1119
+ access: 'restricted',
1120
+ license: 'CLOSED',
1121
+ fullName,
1122
+ ...(email && { email }),
1123
+ // Use provided values or sensible defaults
1124
+ repoName: repoName || name,
1125
+ username: effectiveUsername
1126
+ }
1127
+ });
1128
+ }
1129
+ else {
1130
+ if (prompter) {
1131
+ const { overwrite } = await prompter.prompt({}, [
1132
+ {
1133
+ type: 'confirm',
1134
+ name: 'overwrite',
1135
+ message: `Module "${name}" already exists at ${pgpmDir}. Overwrite deploy/revert/verify directories?`,
1136
+ default: true,
1137
+ useDefault: true
1138
+ }
1139
+ ]);
1140
+ if (!overwrite) {
1141
+ throw new Error(`Export cancelled: Module "${name}" already exists.`);
1142
+ }
1143
+ }
1144
+ rmSync(path.resolve(pgpmDir, 'deploy'), { recursive: true, force: true });
1145
+ rmSync(path.resolve(pgpmDir, 'revert'), { recursive: true, force: true });
1146
+ rmSync(path.resolve(pgpmDir, 'verify'), { recursive: true, force: true });
1147
+ }
1148
+ }
1149
+ finally {
1150
+ process.chdir(curDir);
1151
+ }
1152
+ return pgpmDir;
1153
+ };
1154
+ /**
1155
+ * Normalizes an output directory path to ensure it ends with a path separator.
1156
+ */
1157
+ export const normalizeOutdir = (outdir) => {
1158
+ return outdir.endsWith(path.sep) ? outdir : outdir + path.sep;
1159
+ };