-
Notifications
You must be signed in to change notification settings - Fork 69
Database metadata #10
Changes from 2 commits
71e1956
2908fd2
923f7cc
2880e34
5a4ac73
2a6523c
e8dcefd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ export function createConnection(server, database) { | |
connect: connect.bind(null, server, database), | ||
disconnect: disconnect.bind(null, server, database), | ||
listTables: listTables.bind(null, server, database), | ||
listViews: listViews.bind(null, server, database), | ||
listRoutines: listRoutines.bind(null, server, database), | ||
executeQuery: executeQuery.bind(null, server, database), | ||
listDatabases: listDatabases.bind(null, server, database), | ||
getQuerySelectTop: getQuerySelectTop.bind(null, server, database), | ||
|
@@ -107,6 +109,15 @@ async function listTables(server, database) { | |
return database.connection.listTables(); | ||
} | ||
|
||
async function listViews(server, database) { | ||
checkIsConnected(server, database); | ||
return database.connection.listViews(); | ||
} | ||
|
||
async function listRoutines(server, database) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. Also I guess, async should be omitted in similar cases with listTables and listDatabases functions? |
||
checkIsConnected(server, database); | ||
return database.connection.listRoutines(); | ||
} | ||
|
||
async function executeQuery(server, database, query) { | ||
checkIsConnected(server, database); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ export default function(server, database) { | |
resolve({ | ||
disconnect: () => disconnect(client), | ||
listTables: () => listTables(client), | ||
listViews: () => listViews(client), | ||
listRoutines: () => listRoutines(client), | ||
executeQuery: (query) => executeQuery(client, query), | ||
listDatabases: () => listDatabases(client), | ||
getQuerySelectTop: (table, limit) => getQuerySelectTop(client, table, limit), | ||
|
@@ -60,6 +62,40 @@ export function listTables(client) { | |
}); | ||
} | ||
|
||
export function listViews(client) { | ||
return new Promise((resolve, reject) => { | ||
const sql = ` | ||
SELECT table_name | ||
FROM information_schema.views | ||
WHERE table_schema = database() | ||
ORDER BY table_name | ||
`; | ||
const params = []; | ||
client.query(sql, params, (err, data) => { | ||
if (err) return reject(_getRealError(client, err)); | ||
resolve(data.map(row => row.table_name)); | ||
}); | ||
}); | ||
} | ||
|
||
export function listRoutines(client) { | ||
return new Promise((resolve, reject) => { | ||
const sql = ` | ||
SELECT routine_name, routine_type | ||
FROM information_schema.routines | ||
WHERE routine_schema = database() | ||
ORDER BY routine_name | ||
`; | ||
const params = []; | ||
client.query(sql, params, (err, data) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as params is not used anywhere else, perhaps you can pass directly one []... client.query(sql, [], (err, data) => { |
||
if (err) return reject(_getRealError(client, err)); | ||
resolve(data.map(row => ({ | ||
routineName: row.routine_name, | ||
routineType: row.routine_type, | ||
}))); | ||
}); | ||
}); | ||
} | ||
|
||
export function executeQuery(client, query) { | ||
return new Promise((resolve, reject) => { | ||
|
@@ -110,6 +146,7 @@ export const truncateAllTables = async (client) => { | |
SELECT table_name | ||
FROM information_schema.tables | ||
WHERE table_schema = '${schema}' | ||
AND table_type NOT LIKE '%VIEW%' | ||
`; | ||
const [result] = await executeQuery(client, sql); | ||
const tables = result.rows.map(row => row.table_name); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ export default function(server, database) { | |
wrapQuery, | ||
disconnect: () => disconnect(client), | ||
listTables: () => listTables(client), | ||
listViews: () => listViews(client), | ||
listRoutines: () => listRoutines(client), | ||
executeQuery: (query) => executeQuery(client, query), | ||
listDatabases: () => listDatabases(client), | ||
getQuerySelectTop: (table, limit) => getQuerySelectTop(client, table, limit), | ||
|
@@ -56,6 +58,44 @@ export function listTables(client) { | |
}); | ||
} | ||
|
||
export function listViews(client) { | ||
return new Promise((resolve, reject) => { | ||
const sql = ` | ||
SELECT table_name | ||
FROM information_schema.views | ||
WHERE table_schema = $1 | ||
ORDER BY table_name | ||
`; | ||
const params = [ | ||
'public', | ||
]; | ||
client.query(sql, params, (err, data) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above perhaps can pass directly ['public'] ... |
||
if (err) return reject(err); | ||
resolve(data.rows.map(row => row.table_name)); | ||
}); | ||
}); | ||
} | ||
|
||
export function listRoutines(client) { | ||
return new Promise((resolve, reject) => { | ||
const sql = ` | ||
SELECT routine_name, routine_type | ||
FROM information_schema.routines | ||
WHERE routine_schema = $1 | ||
ORDER BY routine_name | ||
`; | ||
const params = [ | ||
'public', | ||
]; | ||
client.query(sql, params, (err, data) => { | ||
if (err) return reject(err); | ||
resolve(data.rows.map(row => ({ | ||
routineName: row.routine_name, | ||
routineType: row.routine_type, | ||
}))); | ||
}); | ||
}); | ||
} | ||
|
||
export async function executeQuery(client, query) { | ||
// node-postgres has support for Promise query | ||
|
@@ -107,6 +147,7 @@ export const truncateAllTables = async (connection) => { | |
SELECT table_name | ||
FROM information_schema.tables | ||
WHERE table_schema = '${schema}' | ||
AND table_type NOT LIKE '%VIEW%' | ||
`; | ||
const [result] = await executeQuery(connection, sql); | ||
const tables = result.rows.map(row => row.table_name); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as does not have usage of await inside block it does not need to have async