Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#9 Add postgres repository #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
85 changes: 85 additions & 0 deletions src/repositories/postgres.repository.ts
Original file line number Diff line number Diff line change
@@ -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<Database> => {
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';
`;
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down