@pgpmjs/core 6.8.2 → 6.9.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.
@@ -1,1016 +0,0 @@
1
- import { Parser } from 'csv-to-pg';
2
- import { getPgPool } from 'pg-cache';
3
- /**
4
- * Map PostgreSQL data types to FieldType values.
5
- * Uses udt_name from information_schema which gives the base type name.
6
- */
7
- const mapPgTypeToFieldType = (udtName) => {
8
- switch (udtName) {
9
- case 'uuid':
10
- return 'uuid';
11
- case '_uuid':
12
- return 'uuid[]';
13
- case 'text':
14
- case 'varchar':
15
- case 'bpchar':
16
- case 'name':
17
- return 'text';
18
- case '_text':
19
- case '_varchar':
20
- return 'text[]';
21
- case 'bool':
22
- return 'boolean';
23
- case 'jsonb':
24
- case 'json':
25
- return 'jsonb';
26
- case '_jsonb':
27
- return 'jsonb[]';
28
- case 'int4':
29
- case 'int8':
30
- case 'int2':
31
- case 'numeric':
32
- return 'int';
33
- case 'interval':
34
- return 'interval';
35
- case 'timestamptz':
36
- case 'timestamp':
37
- return 'timestamptz';
38
- default:
39
- return 'text';
40
- }
41
- };
42
- /**
43
- * Query actual columns from information_schema for a given table.
44
- * Returns a map of column_name -> udt_name (PostgreSQL type).
45
- */
46
- const getTableColumns = async (pool, schemaName, tableName) => {
47
- const result = await pool.query(`
48
- SELECT column_name, udt_name
49
- FROM information_schema.columns
50
- WHERE table_schema = $1 AND table_name = $2
51
- ORDER BY ordinal_position
52
- `, [schemaName, tableName]);
53
- const columns = new Map();
54
- for (const row of result.rows) {
55
- columns.set(row.column_name, row.udt_name);
56
- }
57
- return columns;
58
- };
59
- /**
60
- * Build dynamic fields config by intersecting the hardcoded config with actual database columns.
61
- * - Only includes columns that exist in the database
62
- * - Preserves special type hints from config (image, upload, url) for columns that exist
63
- * - Infers types from PostgreSQL for columns not in config
64
- */
65
- const buildDynamicFields = async (pool, tableConfig) => {
66
- const actualColumns = await getTableColumns(pool, tableConfig.schema, tableConfig.table);
67
- if (actualColumns.size === 0) {
68
- // Table doesn't exist, return empty fields
69
- return {};
70
- }
71
- const dynamicFields = {};
72
- // For each column in the hardcoded config, check if it exists in the database
73
- for (const [fieldName, fieldType] of Object.entries(tableConfig.fields)) {
74
- if (actualColumns.has(fieldName)) {
75
- // Column exists - use the config's type hint (preserves special types like 'image', 'upload', 'url')
76
- dynamicFields[fieldName] = fieldType;
77
- }
78
- // If column doesn't exist in database, skip it (this fixes the bug)
79
- }
80
- return dynamicFields;
81
- };
82
- const config = {
83
- // =============================================================================
84
- // metaschema_public tables
85
- // =============================================================================
86
- database: {
87
- schema: 'metaschema_public',
88
- table: 'database',
89
- fields: {
90
- id: 'uuid',
91
- owner_id: 'uuid',
92
- name: 'text',
93
- hash: 'uuid'
94
- }
95
- },
96
- schema: {
97
- schema: 'metaschema_public',
98
- table: 'schema',
99
- fields: {
100
- id: 'uuid',
101
- database_id: 'uuid',
102
- name: 'text',
103
- schema_name: 'text',
104
- description: 'text',
105
- is_public: 'boolean'
106
- }
107
- },
108
- table: {
109
- schema: 'metaschema_public',
110
- table: 'table',
111
- fields: {
112
- id: 'uuid',
113
- database_id: 'uuid',
114
- schema_id: 'uuid',
115
- name: 'text',
116
- description: 'text'
117
- }
118
- },
119
- field: {
120
- schema: 'metaschema_public',
121
- table: 'field',
122
- // Use ON CONFLICT DO NOTHING to handle the unique constraint (databases_field_uniq_names_idx)
123
- // which normalizes UUID field names by stripping suffixes like _id, _uuid, etc.
124
- // This causes collisions when tables have both 'foo' (text) and 'foo_id' (uuid) columns.
125
- conflictDoNothing: true,
126
- fields: {
127
- id: 'uuid',
128
- database_id: 'uuid',
129
- table_id: 'uuid',
130
- name: 'text',
131
- type: 'text',
132
- description: 'text'
133
- }
134
- },
135
- policy: {
136
- schema: 'metaschema_public',
137
- table: 'policy',
138
- fields: {
139
- id: 'uuid',
140
- database_id: 'uuid',
141
- table_id: 'uuid',
142
- name: 'text',
143
- grantee_name: 'text',
144
- privilege: 'text',
145
- permissive: 'boolean',
146
- disabled: 'boolean',
147
- policy_type: 'text',
148
- data: 'jsonb'
149
- }
150
- },
151
- index: {
152
- schema: 'metaschema_public',
153
- table: 'index',
154
- fields: {
155
- id: 'uuid',
156
- database_id: 'uuid',
157
- table_id: 'uuid',
158
- name: 'text',
159
- field_ids: 'uuid[]',
160
- include_field_ids: 'uuid[]',
161
- access_method: 'text',
162
- index_params: 'jsonb',
163
- where_clause: 'jsonb',
164
- is_unique: 'boolean'
165
- }
166
- },
167
- trigger: {
168
- schema: 'metaschema_public',
169
- table: 'trigger',
170
- fields: {
171
- id: 'uuid',
172
- database_id: 'uuid',
173
- table_id: 'uuid',
174
- name: 'text',
175
- event: 'text',
176
- function_name: 'text'
177
- }
178
- },
179
- trigger_function: {
180
- schema: 'metaschema_public',
181
- table: 'trigger_function',
182
- fields: {
183
- id: 'uuid',
184
- database_id: 'uuid',
185
- name: 'text',
186
- code: 'text'
187
- }
188
- },
189
- rls_function: {
190
- schema: 'metaschema_public',
191
- table: 'rls_function',
192
- fields: {
193
- id: 'uuid',
194
- database_id: 'uuid',
195
- table_id: 'uuid',
196
- name: 'text',
197
- label: 'text',
198
- description: 'text',
199
- data: 'jsonb',
200
- inline: 'boolean',
201
- security: 'int'
202
- }
203
- },
204
- foreign_key_constraint: {
205
- schema: 'metaschema_public',
206
- table: 'foreign_key_constraint',
207
- fields: {
208
- id: 'uuid',
209
- database_id: 'uuid',
210
- table_id: 'uuid',
211
- name: 'text',
212
- description: 'text',
213
- smart_tags: 'jsonb',
214
- type: 'text',
215
- field_ids: 'uuid[]',
216
- ref_table_id: 'uuid',
217
- ref_field_ids: 'uuid[]',
218
- delete_action: 'text',
219
- update_action: 'text'
220
- }
221
- },
222
- primary_key_constraint: {
223
- schema: 'metaschema_public',
224
- table: 'primary_key_constraint',
225
- fields: {
226
- id: 'uuid',
227
- database_id: 'uuid',
228
- table_id: 'uuid',
229
- name: 'text',
230
- type: 'text',
231
- field_ids: 'uuid[]'
232
- }
233
- },
234
- unique_constraint: {
235
- schema: 'metaschema_public',
236
- table: 'unique_constraint',
237
- fields: {
238
- id: 'uuid',
239
- database_id: 'uuid',
240
- table_id: 'uuid',
241
- name: 'text',
242
- description: 'text',
243
- smart_tags: 'jsonb',
244
- type: 'text',
245
- field_ids: 'uuid[]'
246
- }
247
- },
248
- check_constraint: {
249
- schema: 'metaschema_public',
250
- table: 'check_constraint',
251
- fields: {
252
- id: 'uuid',
253
- database_id: 'uuid',
254
- table_id: 'uuid',
255
- name: 'text',
256
- type: 'text',
257
- field_ids: 'uuid[]',
258
- expr: 'jsonb'
259
- }
260
- },
261
- full_text_search: {
262
- schema: 'metaschema_public',
263
- table: 'full_text_search',
264
- fields: {
265
- id: 'uuid',
266
- database_id: 'uuid',
267
- table_id: 'uuid',
268
- field_id: 'uuid',
269
- field_ids: 'uuid[]',
270
- weights: 'text[]',
271
- langs: 'text[]'
272
- }
273
- },
274
- schema_grant: {
275
- schema: 'metaschema_public',
276
- table: 'schema_grant',
277
- fields: {
278
- id: 'uuid',
279
- database_id: 'uuid',
280
- schema_id: 'uuid',
281
- grantee_name: 'text'
282
- }
283
- },
284
- table_grant: {
285
- schema: 'metaschema_public',
286
- table: 'table_grant',
287
- fields: {
288
- id: 'uuid',
289
- database_id: 'uuid',
290
- table_id: 'uuid',
291
- privilege: 'text',
292
- grantee_name: 'text',
293
- field_ids: 'uuid[]',
294
- is_grant: 'boolean'
295
- }
296
- },
297
- default_privilege: {
298
- schema: 'metaschema_public',
299
- table: 'default_privilege',
300
- fields: {
301
- id: 'uuid',
302
- database_id: 'uuid',
303
- schema_id: 'uuid',
304
- object_type: 'text',
305
- privilege: 'text',
306
- grantee_name: 'text',
307
- is_grant: 'boolean'
308
- }
309
- },
310
- // =============================================================================
311
- // services_public tables
312
- // =============================================================================
313
- domains: {
314
- schema: 'services_public',
315
- table: 'domains',
316
- fields: {
317
- id: 'uuid',
318
- database_id: 'uuid',
319
- site_id: 'uuid',
320
- api_id: 'uuid',
321
- domain: 'text',
322
- subdomain: 'text'
323
- }
324
- },
325
- sites: {
326
- schema: 'services_public',
327
- table: 'sites',
328
- fields: {
329
- id: 'uuid',
330
- database_id: 'uuid',
331
- title: 'text',
332
- description: 'text',
333
- og_image: 'image',
334
- favicon: 'upload',
335
- apple_touch_icon: 'image',
336
- logo: 'image'
337
- }
338
- },
339
- apis: {
340
- schema: 'services_public',
341
- table: 'apis',
342
- fields: {
343
- id: 'uuid',
344
- database_id: 'uuid',
345
- name: 'text',
346
- is_public: 'boolean',
347
- role_name: 'text',
348
- anon_role: 'text'
349
- }
350
- },
351
- apps: {
352
- schema: 'services_public',
353
- table: 'apps',
354
- fields: {
355
- id: 'uuid',
356
- database_id: 'uuid',
357
- site_id: 'uuid',
358
- name: 'text',
359
- app_image: 'image',
360
- app_store_link: 'url',
361
- app_store_id: 'text',
362
- app_id_prefix: 'text',
363
- play_store_link: 'url'
364
- }
365
- },
366
- site_modules: {
367
- schema: 'services_public',
368
- table: 'site_modules',
369
- fields: {
370
- id: 'uuid',
371
- database_id: 'uuid',
372
- site_id: 'uuid',
373
- name: 'text',
374
- data: 'jsonb'
375
- }
376
- },
377
- site_themes: {
378
- schema: 'services_public',
379
- table: 'site_themes',
380
- fields: {
381
- id: 'uuid',
382
- database_id: 'uuid',
383
- site_id: 'uuid',
384
- theme: 'jsonb'
385
- }
386
- },
387
- site_metadata: {
388
- schema: 'services_public',
389
- table: 'site_metadata',
390
- fields: {
391
- id: 'uuid',
392
- database_id: 'uuid',
393
- site_id: 'uuid',
394
- title: 'text',
395
- description: 'text',
396
- og_image: 'image'
397
- }
398
- },
399
- api_modules: {
400
- schema: 'services_public',
401
- table: 'api_modules',
402
- fields: {
403
- id: 'uuid',
404
- database_id: 'uuid',
405
- api_id: 'uuid',
406
- name: 'text',
407
- data: 'jsonb'
408
- }
409
- },
410
- api_schemas: {
411
- schema: 'services_public',
412
- table: 'api_schemas',
413
- fields: {
414
- id: 'uuid',
415
- database_id: 'uuid',
416
- schema_id: 'uuid',
417
- api_id: 'uuid'
418
- }
419
- },
420
- // =============================================================================
421
- // metaschema_modules_public tables
422
- // =============================================================================
423
- rls_module: {
424
- schema: 'metaschema_modules_public',
425
- table: 'rls_module',
426
- fields: {
427
- id: 'uuid',
428
- database_id: 'uuid',
429
- api_id: 'uuid',
430
- schema_id: 'uuid',
431
- private_schema_id: 'uuid',
432
- session_credentials_table_id: 'uuid',
433
- sessions_table_id: 'uuid',
434
- users_table_id: 'uuid',
435
- authenticate: 'text',
436
- authenticate_strict: 'text',
437
- current_role: 'text',
438
- current_role_id: 'text'
439
- }
440
- },
441
- user_auth_module: {
442
- schema: 'metaschema_modules_public',
443
- table: 'user_auth_module',
444
- fields: {
445
- id: 'uuid',
446
- database_id: 'uuid',
447
- schema_id: 'uuid',
448
- emails_table_id: 'uuid',
449
- users_table_id: 'uuid',
450
- secrets_table_id: 'uuid',
451
- encrypted_table_id: 'uuid',
452
- sessions_table_id: 'uuid',
453
- session_credentials_table_id: 'uuid',
454
- audits_table_id: 'uuid',
455
- audits_table_name: 'text',
456
- sign_in_function: 'text',
457
- sign_up_function: 'text',
458
- sign_out_function: 'text',
459
- sign_in_one_time_token_function: 'text',
460
- one_time_token_function: 'text',
461
- extend_token_expires: 'text',
462
- send_account_deletion_email_function: 'text',
463
- delete_account_function: 'text',
464
- set_password_function: 'text',
465
- reset_password_function: 'text',
466
- forgot_password_function: 'text',
467
- send_verification_email_function: 'text',
468
- verify_email_function: 'text',
469
- verify_password_function: 'text',
470
- check_password_function: 'text'
471
- }
472
- },
473
- memberships_module: {
474
- schema: 'metaschema_modules_public',
475
- table: 'memberships_module',
476
- fields: {
477
- id: 'uuid',
478
- database_id: 'uuid',
479
- schema_id: 'uuid',
480
- private_schema_id: 'uuid',
481
- memberships_table_id: 'uuid',
482
- memberships_table_name: 'text',
483
- members_table_id: 'uuid',
484
- members_table_name: 'text',
485
- membership_defaults_table_id: 'uuid',
486
- membership_defaults_table_name: 'text',
487
- grants_table_id: 'uuid',
488
- grants_table_name: 'text',
489
- actor_table_id: 'uuid',
490
- limits_table_id: 'uuid',
491
- default_limits_table_id: 'uuid',
492
- permissions_table_id: 'uuid',
493
- default_permissions_table_id: 'uuid',
494
- sprt_table_id: 'uuid',
495
- admin_grants_table_id: 'uuid',
496
- admin_grants_table_name: 'text',
497
- owner_grants_table_id: 'uuid',
498
- owner_grants_table_name: 'text',
499
- membership_type: 'int',
500
- entity_table_id: 'uuid',
501
- entity_table_owner_id: 'uuid',
502
- prefix: 'text',
503
- actor_mask_check: 'text',
504
- actor_perm_check: 'text',
505
- entity_ids_by_mask: 'text',
506
- entity_ids_by_perm: 'text',
507
- entity_ids_function: 'text'
508
- }
509
- },
510
- permissions_module: {
511
- schema: 'metaschema_modules_public',
512
- table: 'permissions_module',
513
- fields: {
514
- id: 'uuid',
515
- database_id: 'uuid',
516
- schema_id: 'uuid',
517
- private_schema_id: 'uuid',
518
- table_id: 'uuid',
519
- table_name: 'text',
520
- default_table_id: 'uuid',
521
- default_table_name: 'text',
522
- bitlen: 'int',
523
- membership_type: 'int',
524
- entity_table_id: 'uuid',
525
- actor_table_id: 'uuid',
526
- prefix: 'text',
527
- get_padded_mask: 'text',
528
- get_mask: 'text',
529
- get_by_mask: 'text',
530
- get_mask_by_name: 'text'
531
- }
532
- },
533
- limits_module: {
534
- schema: 'metaschema_modules_public',
535
- table: 'limits_module',
536
- fields: {
537
- id: 'uuid',
538
- database_id: 'uuid',
539
- schema_id: 'uuid',
540
- private_schema_id: 'uuid',
541
- table_id: 'uuid',
542
- table_name: 'text',
543
- default_table_id: 'uuid',
544
- default_table_name: 'text',
545
- limit_increment_function: 'text',
546
- limit_decrement_function: 'text',
547
- limit_increment_trigger: 'text',
548
- limit_decrement_trigger: 'text',
549
- limit_update_trigger: 'text',
550
- limit_check_function: 'text',
551
- prefix: 'text',
552
- membership_type: 'int',
553
- entity_table_id: 'uuid',
554
- actor_table_id: 'uuid'
555
- }
556
- },
557
- levels_module: {
558
- schema: 'metaschema_modules_public',
559
- table: 'levels_module',
560
- fields: {
561
- id: 'uuid',
562
- database_id: 'uuid',
563
- schema_id: 'uuid',
564
- private_schema_id: 'uuid',
565
- steps_table_id: 'uuid',
566
- steps_table_name: 'text',
567
- achievements_table_id: 'uuid',
568
- achievements_table_name: 'text',
569
- levels_table_id: 'uuid',
570
- levels_table_name: 'text',
571
- level_requirements_table_id: 'uuid',
572
- level_requirements_table_name: 'text',
573
- completed_step: 'text',
574
- incompleted_step: 'text',
575
- tg_achievement: 'text',
576
- tg_achievement_toggle: 'text',
577
- tg_achievement_toggle_boolean: 'text',
578
- tg_achievement_boolean: 'text',
579
- upsert_achievement: 'text',
580
- tg_update_achievements: 'text',
581
- steps_required: 'text',
582
- level_achieved: 'text',
583
- prefix: 'text',
584
- membership_type: 'int',
585
- entity_table_id: 'uuid',
586
- actor_table_id: 'uuid'
587
- }
588
- },
589
- users_module: {
590
- schema: 'metaschema_modules_public',
591
- table: 'users_module',
592
- fields: {
593
- id: 'uuid',
594
- database_id: 'uuid',
595
- schema_id: 'uuid',
596
- table_id: 'uuid',
597
- table_name: 'text',
598
- type_table_id: 'uuid',
599
- type_table_name: 'text'
600
- }
601
- },
602
- hierarchy_module: {
603
- schema: 'metaschema_modules_public',
604
- table: 'hierarchy_module',
605
- fields: {
606
- id: 'uuid',
607
- database_id: 'uuid',
608
- schema_id: 'uuid',
609
- private_schema_id: 'uuid',
610
- chart_edges_table_id: 'uuid',
611
- chart_edges_table_name: 'text',
612
- hierarchy_sprt_table_id: 'uuid',
613
- hierarchy_sprt_table_name: 'text',
614
- chart_edge_grants_table_id: 'uuid',
615
- chart_edge_grants_table_name: 'text',
616
- entity_table_id: 'uuid',
617
- users_table_id: 'uuid',
618
- prefix: 'text',
619
- private_schema_name: 'text',
620
- sprt_table_name: 'text',
621
- rebuild_hierarchy_function: 'text',
622
- get_subordinates_function: 'text',
623
- get_managers_function: 'text',
624
- is_manager_of_function: 'text'
625
- }
626
- },
627
- membership_types_module: {
628
- schema: 'metaschema_modules_public',
629
- table: 'membership_types_module',
630
- fields: {
631
- id: 'uuid',
632
- database_id: 'uuid',
633
- schema_id: 'uuid',
634
- table_id: 'uuid',
635
- table_name: 'text'
636
- }
637
- },
638
- invites_module: {
639
- schema: 'metaschema_modules_public',
640
- table: 'invites_module',
641
- fields: {
642
- id: 'uuid',
643
- database_id: 'uuid',
644
- schema_id: 'uuid',
645
- private_schema_id: 'uuid',
646
- emails_table_id: 'uuid',
647
- users_table_id: 'uuid',
648
- invites_table_id: 'uuid',
649
- claimed_invites_table_id: 'uuid',
650
- invites_table_name: 'text',
651
- claimed_invites_table_name: 'text',
652
- submit_invite_code_function: 'text',
653
- prefix: 'text',
654
- membership_type: 'int',
655
- entity_table_id: 'uuid'
656
- }
657
- },
658
- emails_module: {
659
- schema: 'metaschema_modules_public',
660
- table: 'emails_module',
661
- fields: {
662
- id: 'uuid',
663
- database_id: 'uuid',
664
- schema_id: 'uuid',
665
- private_schema_id: 'uuid',
666
- table_id: 'uuid',
667
- owner_table_id: 'uuid',
668
- table_name: 'text'
669
- }
670
- },
671
- sessions_module: {
672
- schema: 'metaschema_modules_public',
673
- table: 'sessions_module',
674
- fields: {
675
- id: 'uuid',
676
- database_id: 'uuid',
677
- schema_id: 'uuid',
678
- sessions_table_id: 'uuid',
679
- session_credentials_table_id: 'uuid',
680
- auth_settings_table_id: 'uuid',
681
- users_table_id: 'uuid',
682
- sessions_default_expiration: 'interval',
683
- sessions_table: 'text',
684
- session_credentials_table: 'text',
685
- auth_settings_table: 'text'
686
- }
687
- },
688
- secrets_module: {
689
- schema: 'metaschema_modules_public',
690
- table: 'secrets_module',
691
- fields: {
692
- id: 'uuid',
693
- database_id: 'uuid',
694
- schema_id: 'uuid',
695
- table_id: 'uuid',
696
- table_name: 'text'
697
- }
698
- },
699
- profiles_module: {
700
- schema: 'metaschema_modules_public',
701
- table: 'profiles_module',
702
- fields: {
703
- id: 'uuid',
704
- database_id: 'uuid',
705
- schema_id: 'uuid',
706
- private_schema_id: 'uuid',
707
- table_id: 'uuid',
708
- table_name: 'text',
709
- profile_permissions_table_id: 'uuid',
710
- profile_permissions_table_name: 'text',
711
- profile_grants_table_id: 'uuid',
712
- profile_grants_table_name: 'text',
713
- profile_definition_grants_table_id: 'uuid',
714
- profile_definition_grants_table_name: 'text',
715
- bitlen: 'int',
716
- membership_type: 'int',
717
- entity_table_id: 'uuid',
718
- actor_table_id: 'uuid',
719
- permissions_table_id: 'uuid',
720
- memberships_table_id: 'uuid',
721
- prefix: 'text'
722
- }
723
- },
724
- encrypted_secrets_module: {
725
- schema: 'metaschema_modules_public',
726
- table: 'encrypted_secrets_module',
727
- fields: {
728
- id: 'uuid',
729
- database_id: 'uuid',
730
- schema_id: 'uuid',
731
- table_id: 'uuid',
732
- table_name: 'text'
733
- }
734
- },
735
- connected_accounts_module: {
736
- schema: 'metaschema_modules_public',
737
- table: 'connected_accounts_module',
738
- fields: {
739
- id: 'uuid',
740
- database_id: 'uuid',
741
- schema_id: 'uuid',
742
- private_schema_id: 'uuid',
743
- table_id: 'uuid',
744
- owner_table_id: 'uuid',
745
- table_name: 'text'
746
- }
747
- },
748
- phone_numbers_module: {
749
- schema: 'metaschema_modules_public',
750
- table: 'phone_numbers_module',
751
- fields: {
752
- id: 'uuid',
753
- database_id: 'uuid',
754
- schema_id: 'uuid',
755
- private_schema_id: 'uuid',
756
- table_id: 'uuid',
757
- owner_table_id: 'uuid',
758
- table_name: 'text'
759
- }
760
- },
761
- crypto_addresses_module: {
762
- schema: 'metaschema_modules_public',
763
- table: 'crypto_addresses_module',
764
- fields: {
765
- id: 'uuid',
766
- database_id: 'uuid',
767
- schema_id: 'uuid',
768
- private_schema_id: 'uuid',
769
- table_id: 'uuid',
770
- owner_table_id: 'uuid',
771
- table_name: 'text',
772
- crypto_network: 'text'
773
- }
774
- },
775
- crypto_auth_module: {
776
- schema: 'metaschema_modules_public',
777
- table: 'crypto_auth_module',
778
- fields: {
779
- id: 'uuid',
780
- database_id: 'uuid',
781
- schema_id: 'uuid',
782
- users_table_id: 'uuid',
783
- sessions_table_id: 'uuid',
784
- session_credentials_table_id: 'uuid',
785
- secrets_table_id: 'uuid',
786
- addresses_table_id: 'uuid',
787
- user_field: 'text',
788
- crypto_network: 'text',
789
- sign_in_request_challenge: 'text',
790
- sign_in_record_failure: 'text',
791
- sign_up_with_key: 'text',
792
- sign_in_with_challenge: 'text'
793
- }
794
- },
795
- field_module: {
796
- schema: 'metaschema_modules_public',
797
- table: 'field_module',
798
- fields: {
799
- id: 'uuid',
800
- database_id: 'uuid',
801
- private_schema_id: 'uuid',
802
- table_id: 'uuid',
803
- field_id: 'uuid',
804
- node_type: 'text',
805
- data: 'jsonb',
806
- triggers: 'text[]',
807
- functions: 'text[]'
808
- }
809
- },
810
- table_module: {
811
- schema: 'metaschema_modules_public',
812
- table: 'table_module',
813
- fields: {
814
- id: 'uuid',
815
- database_id: 'uuid',
816
- private_schema_id: 'uuid',
817
- table_id: 'uuid',
818
- node_type: 'text',
819
- data: 'jsonb',
820
- fields: 'uuid[]'
821
- }
822
- },
823
- secure_table_provision: {
824
- schema: 'metaschema_modules_public',
825
- table: 'secure_table_provision',
826
- fields: {
827
- id: 'uuid',
828
- database_id: 'uuid',
829
- schema_id: 'uuid',
830
- table_id: 'uuid',
831
- table_name: 'text',
832
- node_type: 'text',
833
- use_rls: 'boolean',
834
- node_data: 'jsonb',
835
- grant_roles: 'text[]',
836
- fields: 'jsonb[]',
837
- grant_privileges: 'jsonb[]',
838
- policy_type: 'text',
839
- policy_privileges: 'text[]',
840
- policy_role: 'text',
841
- policy_permissive: 'boolean',
842
- policy_data: 'jsonb',
843
- out_fields: 'uuid[]'
844
- }
845
- },
846
- table_template_module: {
847
- schema: 'metaschema_modules_public',
848
- table: 'table_template_module',
849
- fields: {
850
- id: 'uuid',
851
- database_id: 'uuid',
852
- schema_id: 'uuid',
853
- private_schema_id: 'uuid',
854
- table_id: 'uuid',
855
- owner_table_id: 'uuid',
856
- table_name: 'text',
857
- node_type: 'text',
858
- data: 'jsonb'
859
- }
860
- },
861
- uuid_module: {
862
- schema: 'metaschema_modules_public',
863
- table: 'uuid_module',
864
- fields: {
865
- id: 'uuid',
866
- database_id: 'uuid',
867
- schema_id: 'uuid',
868
- uuid_function: 'text',
869
- uuid_seed: 'text'
870
- }
871
- },
872
- default_ids_module: {
873
- schema: 'metaschema_modules_public',
874
- table: 'default_ids_module',
875
- fields: {
876
- id: 'uuid',
877
- database_id: 'uuid'
878
- }
879
- },
880
- denormalized_table_field: {
881
- schema: 'metaschema_modules_public',
882
- table: 'denormalized_table_field',
883
- fields: {
884
- id: 'uuid',
885
- database_id: 'uuid',
886
- table_id: 'uuid',
887
- field_id: 'uuid',
888
- set_ids: 'uuid[]',
889
- ref_table_id: 'uuid',
890
- ref_field_id: 'uuid',
891
- ref_ids: 'uuid[]',
892
- use_updates: 'boolean',
893
- update_defaults: 'boolean',
894
- func_name: 'text',
895
- func_order: 'int'
896
- }
897
- }
898
- };
899
- export const exportMeta = async ({ opts, dbname, database_id }) => {
900
- const pool = getPgPool({
901
- ...opts.pg,
902
- database: dbname
903
- });
904
- const sql = {};
905
- // Cache for dynamically built parsers
906
- const parsers = {};
907
- // Build parser dynamically by querying actual columns from the database
908
- const getParser = async (key) => {
909
- if (parsers[key]) {
910
- return parsers[key];
911
- }
912
- const tableConfig = config[key];
913
- if (!tableConfig) {
914
- return null;
915
- }
916
- // Build fields dynamically based on actual database columns
917
- const dynamicFields = await buildDynamicFields(pool, tableConfig);
918
- if (Object.keys(dynamicFields).length === 0) {
919
- // No columns found (table doesn't exist or no matching columns)
920
- return null;
921
- }
922
- const parser = new Parser({
923
- schema: tableConfig.schema,
924
- table: tableConfig.table,
925
- conflictDoNothing: tableConfig.conflictDoNothing,
926
- fields: dynamicFields
927
- });
928
- parsers[key] = parser;
929
- return parser;
930
- };
931
- const queryAndParse = async (key, query) => {
932
- try {
933
- const parser = await getParser(key);
934
- if (!parser) {
935
- return;
936
- }
937
- const result = await pool.query(query, [database_id]);
938
- if (result.rows.length) {
939
- const parsed = await parser.parse(result.rows);
940
- if (parsed) {
941
- sql[key] = parsed;
942
- }
943
- }
944
- }
945
- catch (err) {
946
- const pgError = err;
947
- if (pgError.code === '42P01') {
948
- return;
949
- }
950
- throw err;
951
- }
952
- };
953
- // =============================================================================
954
- // metaschema_public tables
955
- // =============================================================================
956
- await queryAndParse('database', `SELECT * FROM metaschema_public.database WHERE id = $1`);
957
- await queryAndParse('database_extension', `SELECT * FROM metaschema_public.database_extension WHERE database_id = $1`);
958
- await queryAndParse('schema', `SELECT * FROM metaschema_public.schema WHERE database_id = $1`);
959
- await queryAndParse('table', `SELECT * FROM metaschema_public.table WHERE database_id = $1`);
960
- await queryAndParse('field', `SELECT * FROM metaschema_public.field WHERE database_id = $1`);
961
- await queryAndParse('policy', `SELECT * FROM metaschema_public.policy WHERE database_id = $1`);
962
- await queryAndParse('index', `SELECT * FROM metaschema_public.index WHERE database_id = $1`);
963
- await queryAndParse('trigger', `SELECT * FROM metaschema_public.trigger WHERE database_id = $1`);
964
- await queryAndParse('trigger_function', `SELECT * FROM metaschema_public.trigger_function WHERE database_id = $1`);
965
- await queryAndParse('rls_function', `SELECT * FROM metaschema_public.rls_function WHERE database_id = $1`);
966
- await queryAndParse('foreign_key_constraint', `SELECT * FROM metaschema_public.foreign_key_constraint WHERE database_id = $1`);
967
- await queryAndParse('primary_key_constraint', `SELECT * FROM metaschema_public.primary_key_constraint WHERE database_id = $1`);
968
- await queryAndParse('unique_constraint', `SELECT * FROM metaschema_public.unique_constraint WHERE database_id = $1`);
969
- await queryAndParse('check_constraint', `SELECT * FROM metaschema_public.check_constraint WHERE database_id = $1`);
970
- await queryAndParse('full_text_search', `SELECT * FROM metaschema_public.full_text_search WHERE database_id = $1`);
971
- await queryAndParse('schema_grant', `SELECT * FROM metaschema_public.schema_grant WHERE database_id = $1`);
972
- await queryAndParse('table_grant', `SELECT * FROM metaschema_public.table_grant WHERE database_id = $1`);
973
- await queryAndParse('default_privilege', `SELECT * FROM metaschema_public.default_privilege WHERE database_id = $1`);
974
- // =============================================================================
975
- // services_public tables
976
- // =============================================================================
977
- await queryAndParse('domains', `SELECT * FROM services_public.domains WHERE database_id = $1`);
978
- await queryAndParse('sites', `SELECT * FROM services_public.sites WHERE database_id = $1`);
979
- await queryAndParse('apis', `SELECT * FROM services_public.apis WHERE database_id = $1`);
980
- await queryAndParse('apps', `SELECT * FROM services_public.apps WHERE database_id = $1`);
981
- await queryAndParse('site_modules', `SELECT * FROM services_public.site_modules WHERE database_id = $1`);
982
- await queryAndParse('site_themes', `SELECT * FROM services_public.site_themes WHERE database_id = $1`);
983
- await queryAndParse('site_metadata', `SELECT * FROM services_public.site_metadata WHERE database_id = $1`);
984
- await queryAndParse('api_modules', `SELECT * FROM services_public.api_modules WHERE database_id = $1`);
985
- await queryAndParse('api_extensions', `SELECT * FROM services_public.api_extensions WHERE database_id = $1`);
986
- await queryAndParse('api_schemas', `SELECT * FROM services_public.api_schemas WHERE database_id = $1`);
987
- // =============================================================================
988
- // metaschema_modules_public tables
989
- // =============================================================================
990
- await queryAndParse('rls_module', `SELECT * FROM metaschema_modules_public.rls_module WHERE database_id = $1`);
991
- await queryAndParse('user_auth_module', `SELECT * FROM metaschema_modules_public.user_auth_module WHERE database_id = $1`);
992
- await queryAndParse('memberships_module', `SELECT * FROM metaschema_modules_public.memberships_module WHERE database_id = $1`);
993
- await queryAndParse('permissions_module', `SELECT * FROM metaschema_modules_public.permissions_module WHERE database_id = $1`);
994
- await queryAndParse('limits_module', `SELECT * FROM metaschema_modules_public.limits_module WHERE database_id = $1`);
995
- await queryAndParse('levels_module', `SELECT * FROM metaschema_modules_public.levels_module WHERE database_id = $1`);
996
- await queryAndParse('users_module', `SELECT * FROM metaschema_modules_public.users_module WHERE database_id = $1`);
997
- await queryAndParse('hierarchy_module', `SELECT * FROM metaschema_modules_public.hierarchy_module WHERE database_id = $1`);
998
- await queryAndParse('membership_types_module', `SELECT * FROM metaschema_modules_public.membership_types_module WHERE database_id = $1`);
999
- await queryAndParse('invites_module', `SELECT * FROM metaschema_modules_public.invites_module WHERE database_id = $1`);
1000
- await queryAndParse('emails_module', `SELECT * FROM metaschema_modules_public.emails_module WHERE database_id = $1`);
1001
- await queryAndParse('sessions_module', `SELECT * FROM metaschema_modules_public.sessions_module WHERE database_id = $1`);
1002
- await queryAndParse('secrets_module', `SELECT * FROM metaschema_modules_public.secrets_module WHERE database_id = $1`);
1003
- await queryAndParse('profiles_module', `SELECT * FROM metaschema_modules_public.profiles_module WHERE database_id = $1`);
1004
- await queryAndParse('encrypted_secrets_module', `SELECT * FROM metaschema_modules_public.encrypted_secrets_module WHERE database_id = $1`);
1005
- await queryAndParse('connected_accounts_module', `SELECT * FROM metaschema_modules_public.connected_accounts_module WHERE database_id = $1`);
1006
- await queryAndParse('phone_numbers_module', `SELECT * FROM metaschema_modules_public.phone_numbers_module WHERE database_id = $1`);
1007
- await queryAndParse('crypto_addresses_module', `SELECT * FROM metaschema_modules_public.crypto_addresses_module WHERE database_id = $1`);
1008
- await queryAndParse('crypto_auth_module', `SELECT * FROM metaschema_modules_public.crypto_auth_module WHERE database_id = $1`);
1009
- await queryAndParse('field_module', `SELECT * FROM metaschema_modules_public.field_module WHERE database_id = $1`);
1010
- await queryAndParse('table_template_module', `SELECT * FROM metaschema_modules_public.table_template_module WHERE database_id = $1`);
1011
- await queryAndParse('secure_table_provision', `SELECT * FROM metaschema_modules_public.secure_table_provision WHERE database_id = $1`);
1012
- await queryAndParse('uuid_module', `SELECT * FROM metaschema_modules_public.uuid_module WHERE database_id = $1`);
1013
- await queryAndParse('default_ids_module', `SELECT * FROM metaschema_modules_public.default_ids_module WHERE database_id = $1`);
1014
- await queryAndParse('denormalized_table_field', `SELECT * FROM metaschema_modules_public.denormalized_table_field WHERE database_id = $1`);
1015
- return sql;
1016
- };