@dbml/connector 10.4.0-alpha.1 → 10.4.0-alpha.10

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@dbml/connector",
4
- "version": "10.4.0-alpha.1",
4
+ "version": "10.4.0-alpha.10",
5
5
  "description": "This package was created to fetch the schema JSON from many kind of databases.",
6
6
  "author": "huy.phung.sw@gmail.com",
7
7
  "license": "MIT",
@@ -17,7 +17,7 @@
17
17
  "ts-node": "^10.9.2"
18
18
  },
19
19
  "scripts": {
20
- "build": "rm -rf ./dist && vite build",
20
+ "build": "rimraf ./dist && vite build",
21
21
  "dev": "vite build --watch",
22
22
  "start": "node dist/index.js",
23
23
  "test:watch": "vitest watch",
@@ -39,5 +39,5 @@
39
39
  "engines": {
40
40
  "node": ">=18"
41
41
  },
42
- "gitHead": "2abacb40597047ca589b2c18117f57f8fe3978e6"
42
+ "gitHead": "803873f5165a628d3f9050b9dff0173adab072b2"
43
43
  }
@@ -135,7 +135,7 @@ function getDbdefault (columnDefault: string | null, defaultType: string): Defau
135
135
 
136
136
  // Field type utils
137
137
 
138
- // These datatypes are never qualified: data size, data scale and data precision are all ignore.
138
+ // These datatypes are never qualified: data size, data scale and data precision
139
139
  // Ref: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Data-Types.html
140
140
  const UNQUALIFIED_DATA_TYPES = new Set([
141
141
  // datetime
@@ -54,7 +54,9 @@ const getFieldType = (data_type: string, udt_name: string, character_maximum_len
54
54
  };
55
55
 
56
56
  const getDbdefault = (data_type: string, column_default: string, default_type: DefaultType): DefaultInfo => {
57
- if (data_type === 'ARRAY') {
57
+ // Only a non-empty ARRAY[...] constructor is rewritten here. Other array defaults, such as
58
+ // '{a,b}'::text[], ARRAY[]::text[] or a function call, are handled below like any other default
59
+ if (data_type === 'ARRAY' && column_default.startsWith('ARRAY[') && !column_default.startsWith('ARRAY[]')) {
58
60
  const values = column_default.slice(6, -1).split(',').map((value) => {
59
61
  return value.split('::')[0];
60
62
  });
@@ -228,40 +230,59 @@ const generateRawRefs = async (client: Client, schemas: string[]): Promise<Ref[]
228
230
 
229
231
  const refsListSql = `
230
232
  SELECT
231
- tc.table_schema,
232
- tc.table_name,
233
- tc.constraint_name as fk_constraint_name,
234
- STRING_AGG(DISTINCT kcu.column_name, ',') AS column_names,
235
- ccu.table_schema AS foreign_table_schema,
236
- ccu.table_name AS foreign_table_name,
237
- STRING_AGG(DISTINCT ccu.column_name, ',') AS foreign_column_names,
238
- tc.constraint_type,
239
- rc.delete_rule AS on_delete,
240
- rc.update_rule AS on_update
241
- FROM information_schema.table_constraints AS tc
242
- JOIN information_schema.key_column_usage AS kcu
243
- ON tc.constraint_name = kcu.constraint_name
244
- AND tc.table_schema = kcu.table_schema
245
- JOIN information_schema.constraint_column_usage AS ccu
246
- ON ccu.constraint_name = tc.constraint_name
247
- JOIN information_schema.referential_constraints AS rc
248
- ON tc.constraint_name = rc.constraint_name
249
- AND tc.table_schema = rc.constraint_schema
250
- WHERE tc.constraint_type = 'FOREIGN KEY'
251
- AND tc.table_schema NOT IN ('pg_catalog', 'information_schema')
252
- ${buildSchemaQuery('tc.table_schema', schemas)}
233
+ source_ns.nspname AS table_schema,
234
+ source_table.relname AS table_name,
235
+ constraint_row.conname AS fk_constraint_name,
236
+ STRING_AGG(source_column.attname, ',' ORDER BY key_column.ordinality) AS column_names,
237
+ target_ns.nspname AS foreign_table_schema,
238
+ target_table.relname AS foreign_table_name,
239
+ STRING_AGG(target_column.attname, ',' ORDER BY key_column.ordinality) AS foreign_column_names,
240
+ CASE constraint_row.confdeltype
241
+ WHEN 'a' THEN 'NO ACTION'
242
+ WHEN 'r' THEN 'RESTRICT'
243
+ WHEN 'c' THEN 'CASCADE'
244
+ WHEN 'n' THEN 'SET NULL'
245
+ WHEN 'd' THEN 'SET DEFAULT'
246
+ END AS on_delete,
247
+ CASE constraint_row.confupdtype
248
+ WHEN 'a' THEN 'NO ACTION'
249
+ WHEN 'r' THEN 'RESTRICT'
250
+ WHEN 'c' THEN 'CASCADE'
251
+ WHEN 'n' THEN 'SET NULL'
252
+ WHEN 'd' THEN 'SET DEFAULT'
253
+ END AS on_update
254
+ FROM pg_constraint AS constraint_row
255
+ JOIN pg_class AS source_table
256
+ ON constraint_row.conrelid = source_table.oid
257
+ JOIN pg_namespace AS source_ns
258
+ ON source_table.relnamespace = source_ns.oid
259
+ JOIN pg_class AS target_table
260
+ ON constraint_row.confrelid = target_table.oid
261
+ JOIN pg_namespace AS target_ns
262
+ ON target_table.relnamespace = target_ns.oid
263
+ JOIN LATERAL unnest(constraint_row.conkey, constraint_row.confkey)
264
+ WITH ORDINALITY AS key_column(source_attnum, target_attnum, ordinality)
265
+ ON TRUE
266
+ JOIN pg_attribute AS source_column
267
+ ON source_column.attrelid = source_table.oid
268
+ AND source_column.attnum = key_column.source_attnum
269
+ JOIN pg_attribute AS target_column
270
+ ON target_column.attrelid = target_table.oid
271
+ AND target_column.attnum = key_column.target_attnum
272
+ WHERE constraint_row.contype = 'f'
273
+ AND source_ns.nspname NOT IN ('pg_catalog', 'information_schema')
274
+ ${buildSchemaQuery('source_ns.nspname', schemas)}
253
275
  GROUP BY
254
- tc.table_schema,
255
- tc.table_name,
256
- tc.constraint_name,
257
- ccu.table_schema,
258
- ccu.table_name,
259
- tc.constraint_type,
260
- rc.delete_rule,
261
- rc.update_rule
276
+ source_ns.nspname,
277
+ source_table.relname,
278
+ constraint_row.conname,
279
+ target_ns.nspname,
280
+ target_table.relname,
281
+ constraint_row.confdeltype,
282
+ constraint_row.confupdtype
262
283
  ORDER BY
263
- tc.table_schema,
264
- tc.table_name;
284
+ source_ns.nspname,
285
+ source_table.relname;
265
286
  `;
266
287
 
267
288
  const refsQueryResult = await client.query(refsListSql);