diff --git a/package.json b/package.json index 1bed5ab..2e93354 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,8 @@ }, "dependencies": { "@types/mssql": "^6.0.0", - "mssql": "^6.2.0" + "mssql": "^6.2.0", + "postgres": "^1.0.2" }, "optionalDependencies": { "msnodesqlv8": "^0.8.14" diff --git a/src/repositories/postgres.repository.ts b/src/repositories/postgres.repository.ts new file mode 100644 index 0000000..a9e8ec4 --- /dev/null +++ b/src/repositories/postgres.repository.ts @@ -0,0 +1,85 @@ +import { DatabaseTable } from "../models/database-table.model"; +import { Database } from "../models/database.model"; + + +export const getPostgresDbSchema = async ( + username: string, + password: string, + server: string, + database: string, + trusted: boolean +): Promise => { + let db: Database = { + tables: [], + errors: [] + } as Database; + let sql: any; + try { + + } catch (err) { + db.errors.push('Error getting data: ' + err.message); + } finally { + sql.close(); + } + return db; +}; + +const toTables = (dbResult): DatabaseTable[] => { + const result: DatabaseTable[] = []; + for (let index = 0; index < dbResult.recordset.length; index++) { + const element = dbResult.recordset[index]; + const columnToAdd = { + Name: element.column_name, + ReferenceColumn: element.reference_column, + ReferenceTable: element.reference_table, + ForeignKey: element.foregin_key, + }; + const existing = result.find(t => t.Name === element.table_name); + + if (existing) { + existing.Columns.push(columnToAdd); + } else { + result.push({ + Name: element.table_name, + Columns: [ + columnToAdd + ] + }); + } + } + return result; +}; +const postgresQuery = ` +SELECT t.table_name, + c.column_name, + (SELECT ccu.table_name + FROM information_schema.table_constraints AS tc + LEFT JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name + AND tc.table_schema = kcu.table_schema + LEFT JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name + AND ccu.table_schema = tc.table_schema + WHERE tc.constraint_type = 'FOREIGN KEY' + AND tc.table_name = t.table_name AND kcu.column_name = c.column_name + limit 1) as reference_table, + (SELECT ccu.column_name + FROM information_schema.table_constraints AS tc + LEFT JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name + AND tc.table_schema = kcu.table_schema + LEFT JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name + AND ccu.table_schema = tc.table_schema + WHERE tc.constraint_type = 'FOREIGN KEY' + AND tc.table_name = t.table_name AND kcu.column_name = c.column_name + limit 1) as reference_column, + (SELECT tc.constraint_name + FROM information_schema.table_constraints AS tc + LEFT JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name + AND tc.table_schema = kcu.table_schema + LEFT JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name + AND ccu.table_schema = tc.table_schema + WHERE tc.constraint_type = 'FOREIGN KEY' + AND tc.table_name = t.table_name AND kcu.column_name = c.column_name + limit 1) as foregin_key +FROM information_schema.tables T +INNER JOIN information_schema.columns C ON c.table_name = t.table_name +WHERE T.table_schema = 'public'; +`; diff --git a/yarn.lock b/yarn.lock index 836b31f..cbd59c5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1006,6 +1006,11 @@ performance-now@^2.1.0: resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b" integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns= +postgres@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/postgres/-/postgres-1.0.2.tgz#7b9f6769934f727cec0f1d7ef58d4915a327f5dd" + integrity sha512-zeLgt42KSUNgX/uvo+gbVxTAYwgSY6MIKuU/a8YWuObX4rtGuKrVWopvEAqIAPSO0FeHS1TsSKnqPjoufPy8NA== + psl@^1.1.24, psl@^1.1.28: version "1.6.0" resolved "https://registry.yarnpkg.com/psl/-/psl-1.6.0.tgz#60557582ee23b6c43719d9890fb4170ecd91e110"