Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Starting the pg-admin library

Paul Copplestone3ae731da30a5
diff --git a/packages/pg-admin-js/src/api/config.js b/packages/pg-admin-js/src/api/config.js
new file mode 100644
index 00000000000..6b287bd97ac
--- /dev/null
+++ b/packages/pg-admin-js/src/api/config.js
@@ -0,0 +1,50 @@
+import * as constants from '../utils/constants.js'
+import * as postgres from '../utils/postgres.js'
+
+var fs = require('fs')
+var path = require('path')
+var getAllSql = fs.readFileSync(path.join(__dirname, '../sql/getAllConfig.sql')).toString()
+
+/**
+ * Gets all the config for a
+ * @param {Object} config The database config
+ * @param {string} config.host
+ * @param {string} config.user
+ * @param {string} config.password
+ * @param {string} config.database
+ * @param {number} config.port
+ * @param {Object} options
+ * @param {string} options.schema
+ *
+ * @example
+ * await getAll()
+ * //=>
+ * [
+ * {
+ * name: 'autovacuum',
+ * setting: 'on',
+ * unit: null,
+ * category: 'Autovacuum',
+ * short_desc: 'Starts the autovacuum subprocess.',
+ * extra_desc: null,
+ * context: 'sighup',
+ * vartype: 'bool',
+ * source: 'default',
+ * min_val: null,
+ * max_val: null,
+ * enumvals: null,
+ * boot_val: 'on',
+ * reset_val: 'on',
+ * sourcefile: null,
+ * sourceline: null,
+ * pending_restart: false
+ * }
+ * ]
+ */
+export const getAll = async (config = {}) => {
+ const dbConfig = { ...constants.POSTGRES_CONFIG, ...config }
+ const conn = await postgres.connect(dbConfig)
+ const results = await postgres.runQuery(conn, getAllSql)
+ postgres.disconnect(conn)
+ return results.rows
+}
diff --git a/packages/pg-admin-js/src/api/plugins.js b/packages/pg-admin-js/src/api/plugins.js
new file mode 100644
index 00000000000..cef7e0f1d86
--- /dev/null
+++ b/packages/pg-admin-js/src/api/plugins.js
@@ -0,0 +1,31 @@
+import * as constants from '../utils/constants.js'
+import * as postgres from '../utils/postgres.js'
+
+var fs = require('fs')
+var path = require('path')
+var getAllSql = fs.readFileSync(path.join(__dirname, '../sql/getAllPLugins.sql')).toString()
+
+/**
+ * Queries a given schema for all the tables
+ * @param {Object} config The database config
+ * @param {Object} config The database config
+ * @param {string} config.host
+ * @param {string} config.user
+ * @param {string} config.password
+ * @param {string} config.database
+ * @param {number} config.port
+ *
+ * @example
+ * await getAll()
+ * //=>
+ * [
+ * { name: 'plpgsql', version: '1.0', schema: 'pg_catalog', description: 'PL/pgSQL procedural language' }
+ * ]
+ */
+export const getAll = async (config = {}, options = {}) => {
+ const dbConfig = { ...constants.POSTGRES_CONFIG, ...config }
+ const conn = await postgres.connect(dbConfig)
+ const results = await postgres.runQuery(conn, getAllSql)
+ postgres.disconnect(conn)
+ return results.rows
+}
diff --git a/packages/pg-admin-js/src/api/tables.js b/packages/pg-admin-js/src/api/tables.js
index 9b7fd4118ab..0ca48c1db36 100644
--- a/packages/pg-admin-js/src/api/tables.js
+++ b/packages/pg-admin-js/src/api/tables.js
@@ -3,12 +3,10 @@ import * as postgres from '../utils/postgres.js'
var fs = require('fs')
var path = require('path')
-var getAllTablesSql = fs.readFileSync(path.join(__dirname, '../sql/getAllTables.sql')).toString()
+var getAllSql = fs.readFileSync(path.join(__dirname, '../sql/getAllTables.sql')).toString()
/**
- * Takes an array of columns and an object of string values then converts each string value
- * to its mapped type
- * @param {string} schema The database schema containing the tables
+ * Queries a given schema for all the tables
* @param {Object} config The database config
* @param {string} config.host
* @param {string} config.user
@@ -16,7 +14,7 @@ var getAllTablesSql = fs.readFileSync(path.join(__dirname, '../sql/getAllTables.
* @param {string} config.database
* @param {number} config.port
* @param {Object} options
- * @param {string} options.schema
+ * @param {string} options.schema The database schema containing the tables. Defaults to 'public'
*
* @example
* await getAll()
@@ -29,7 +27,7 @@ export const getAll = async (config = {}, options = {}) => {
const dbConfig = { ...constants.POSTGRES_CONFIG, ...config }
const schema = options.schema || constants.DEFAULT_SCHEMA
const conn = await postgres.connect(dbConfig)
- const results = await postgres.runQuery(conn, schema, getAllTablesSql)
+ const results = await postgres.runQuery(conn, getAllSql, [schema])
postgres.disconnect(conn)
return results.rows
}
diff --git a/packages/pg-admin-js/src/api/types.js b/packages/pg-admin-js/src/api/types.js
new file mode 100644
index 00000000000..3b148c0da54
--- /dev/null
+++ b/packages/pg-admin-js/src/api/types.js
@@ -0,0 +1,32 @@
+import * as constants from '../utils/constants.js'
+import * as postgres from '../utils/postgres.js'
+
+var fs = require('fs')
+var path = require('path')
+var getAllSql = fs.readFileSync(path.join(__dirname, '../sql/getAllTypes.sql')).toString()
+
+/**
+ * Queries a given schema for all the tables
+ * @param {Object} config The database config
+ * @param {Object} config The database config
+ * @param {string} config.host
+ * @param {string} config.user
+ * @param {string} config.password
+ * @param {string} config.database
+ * @param {number} config.port
+ *
+ * @example
+ * await getAll()
+ * //=>
+ * [
+ * { name: 'boolean', internal_name: 'bool', size: '1', enums: '{}', description: 'boolean, \'true\'/\'false\'' },
+ * { name: 'smallint', internal_name: 'int2', size: '2', enums: '{}', description: '-32 thousand to 32 thousand, 2-byte storage' }
+ * ]
+ */
+export const getAll = async (config = {}, options = {}) => {
+ const dbConfig = { ...constants.POSTGRES_CONFIG, ...config }
+ const conn = await postgres.connect(dbConfig)
+ const results = await postgres.runQuery(conn, getAllSql)
+ postgres.disconnect(conn)
+ return results.rows
+}
diff --git a/packages/pg-admin-js/src/api/views.js b/packages/pg-admin-js/src/api/views.js
new file mode 100644
index 00000000000..fa7df48a2be
--- /dev/null
+++ b/packages/pg-admin-js/src/api/views.js
@@ -0,0 +1,42 @@
+import * as constants from '../utils/constants.js'
+import * as postgres from '../utils/postgres.js'
+
+var fs = require('fs')
+var path = require('path')
+var getAllSql = fs.readFileSync(path.join(__dirname, '../sql/getAllViews.sql')).toString()
+
+/**
+ * Queries a given schema for all the tables
+ * @param {Object} config The database config
+ * @param {string} config.host
+ * @param {string} config.user
+ * @param {string} config.password
+ * @param {string} config.database
+ * @param {number} config.port
+ * @param {Object} options
+ * @param {string} options.schema The database schema containing the tables. Defaults to 'public'
+ *
+ * @example
+ * await getAll()
+ * //=>
+ * [
+ * {
+ * table_schema: 'information_schema',
+ * table_name: 'attributes',
+ * check_option: 'NONE',
+ * is_updatable: 'NO',
+ * is_insertable_into: 'NO',
+ * is_trigger_updatable: 'NO',
+ * is_trigger_deletable: 'NO',
+ * is_trigger_insertable_into: 'NO',
+ * }
+ * ]
+ */
+export const getAll = async (config = {}, options = {}) => {
+ const dbConfig = { ...constants.POSTGRES_CONFIG, ...config }
+ const schema = options.schema || constants.DEFAULT_SCHEMA
+ const conn = await postgres.connect(dbConfig)
+ const results = await postgres.runQuery(conn, getAllSql, [schema])
+ postgres.disconnect(conn)
+ return results.rows
+}
\ No newline at end of file
diff --git a/packages/pg-admin-js/src/sql/getAllConfig.sql b/packages/pg-admin-js/src/sql/getAllConfig.sql
index a49465e386a..9024c5a6d30 100644
--- a/packages/pg-admin-js/src/sql/getAllConfig.sql
+++ b/packages/pg-admin-js/src/sql/getAllConfig.sql
@@ -1,7 +1,22 @@
-SELECT * FROM pg_settings;
-SHOW max_connections;
-SHOW wal_level;
-SHOW max_wal_senders;
-SHOW max_replication_slots;
-SHOW config_file;
\ No newline at end of file
+SELECT
+ name,
+ setting,
+ unit,
+ category,
+ short_desc,
+ extra_desc,
+ context,
+ vartype,
+ source,
+ min_val,
+ max_val,
+ enumvals,
+ boot_val,
+ reset_val,
+ sourcefile,
+ sourceline,
+ pending_restart
+
+FROM pg_settings
+ORDER BY name;
\ No newline at end of file
diff --git a/packages/pg-admin-js/src/sql/getAllTypes.sql b/packages/pg-admin-js/src/sql/getAllTypes.sql
index 2425dd0545d..bdfa0e97baa 100644
--- a/packages/pg-admin-js/src/sql/getAllTypes.sql
+++ b/packages/pg-admin-js/src/sql/getAllTypes.sql
@@ -24,7 +24,7 @@ and NOT EXISTS (
FROM pg_catalog.pg_type el
WHERE el.oid = t.typelem AND el.typarray = t.oid
)
-AND n.nspname = ?
-AND n.nspname not in ('information_schema', 'pg_catalog')
+-- AND n.nspname = ?
+-- AND n.nspname not in ('information_schema', 'pg_catalog')
AND pg_catalog.pg_type_is_visible ( t.oid )
ORDER BY 1, 2;
\ No newline at end of file
diff --git a/packages/pg-admin-js/src/sql/getAllViews.sql b/packages/pg-admin-js/src/sql/getAllViews.sql
index c9e6db47db2..ae33f351419 100644
--- a/packages/pg-admin-js/src/sql/getAllViews.sql
+++ b/packages/pg-admin-js/src/sql/getAllViews.sql
@@ -9,4 +9,5 @@ select
is_trigger_insertable_into
from information_schema.views
-where table_schema = ? and table_schema not in ('information_schema', 'pg_catalog')
+where table_schema = ?
+-- and table_schema not in ('information_schema', 'pg_catalog')
diff --git a/packages/pg-admin-js/src/utils/postgres.js b/packages/pg-admin-js/src/utils/postgres.js
index efde64ec6f0..6873b5e8f93 100644
--- a/packages/pg-admin-js/src/utils/postgres.js
+++ b/packages/pg-admin-js/src/utils/postgres.js
@@ -9,6 +9,12 @@ export async function disconnect(conn) {
conn.destroy(cb)
}
-export function runQuery(conn, schema, query) {
- return conn.raw(query, [schema])
+/**
+ * Executes a SQL string against the database
+ * @param conn an open Knex connection
+ * @param {string} query The SQL query
+ * @param {sting[]} params An optional array of paramaters to feed into the query
+ */
+export function runQuery(conn, query, params = {}) {
+ return conn.raw(query, params)
}
diff --git a/packages/pg-admin-js/test/_helpers.js b/packages/pg-admin-js/test/_helpers.js
new file mode 100644
index 00000000000..bc566e78f00
--- /dev/null
+++ b/packages/pg-admin-js/test/_helpers.js
@@ -0,0 +1,5 @@
+import assert from 'assert'
+
+export function compareKeys (first, second) {
+ Object.keys(first).map(key => assert(Object.keys(second).includes(key)))
+}
\ No newline at end of file
diff --git a/packages/pg-admin-js/test/config_spec.js b/packages/pg-admin-js/test/config_spec.js
new file mode 100644
index 00000000000..9c6e420a69c
--- /dev/null
+++ b/packages/pg-admin-js/test/config_spec.js
@@ -0,0 +1,35 @@
+import assert from 'assert'
+import * as config from '../src/api/config.js'
+import { compareKeys } from './_helpers'
+
+describe('Config', function() {
+ it('getAll() should return all settings', async () => {
+ let res = await config.getAll()
+
+ // Check that we have a result
+ assert(Array.isArray(res))
+
+ // Compare one of the values
+ const valid = {
+ name: 'autovacuum',
+ setting: 'on',
+ unit: null,
+ category: 'Autovacuum',
+ short_desc: 'Starts the autovacuum subprocess.',
+ extra_desc: null,
+ context: 'sighup',
+ vartype: 'bool',
+ source: 'default',
+ min_val: null,
+ max_val: null,
+ enumvals: null,
+ boot_val: 'on',
+ reset_val: 'on',
+ sourcefile: null,
+ sourceline: null,
+ pending_restart: false,
+ }
+ const compare = res.find(setting => setting.name === valid.name)
+ compareKeys(valid, compare)
+ })
+})
diff --git a/packages/pg-admin-js/test/plugins_spec.js b/packages/pg-admin-js/test/plugins_spec.js
new file mode 100644
index 00000000000..29586c4bb23
--- /dev/null
+++ b/packages/pg-admin-js/test/plugins_spec.js
@@ -0,0 +1,22 @@
+import assert from 'assert'
+import * as types from '../src/api/plugins'
+import { compareKeys } from './_helpers'
+
+describe('Plugins', function() {
+ it('getAll() should return an array of tables', async () => {
+ let res = await types.getAll('public')
+
+ // Check that we have a result
+ assert(Array.isArray(res))
+
+ // Compare one of the values
+ const valid = {
+ name: 'plpgsql',
+ version: '1.0',
+ schema: 'pg_catalog',
+ description: 'PL/pgSQL procedural language',
+ }
+ const compare = res.find(setting => setting.name === valid.name)
+ compareKeys(valid, compare)
+ })
+})
diff --git a/packages/pg-admin-js/test/tables_spec.js b/packages/pg-admin-js/test/tables_spec.js
index 44f4e3b0786..ec327052f64 100644
--- a/packages/pg-admin-js/test/tables_spec.js
+++ b/packages/pg-admin-js/test/tables_spec.js
@@ -1,9 +1,11 @@
-var assert = require('assert')
-var tables = require('../src/api/tables.js')
+import assert from 'assert'
+import * as tables from '../src/api/tables.js'
describe('Tables', function() {
it('getAll() should return an array of tables', async () => {
- let allTables = await tables.getAll()
- assert(Array.isArray(allTables))
+ let res = await tables.getAll()
+
+ // Check that we have a result
+ assert(Array.isArray(res))
})
})
diff --git a/packages/pg-admin-js/test/types_spec.js b/packages/pg-admin-js/test/types_spec.js
new file mode 100644
index 00000000000..e320c98ea0d
--- /dev/null
+++ b/packages/pg-admin-js/test/types_spec.js
@@ -0,0 +1,23 @@
+import assert from 'assert'
+import * as types from '../src/api/types.js'
+import { compareKeys } from './_helpers'
+
+describe('Types', function() {
+ it('getAll() should return an array of tables', async () => {
+ let res = await types.getAll('public')
+
+ // Check that we have a result
+ assert(Array.isArray(res))
+
+ // Compare one of the values
+ const valid = {
+ name: 'smallint',
+ internal_name: 'int2',
+ size: '2',
+ enums: '{}',
+ description: '-32 thousand to 32 thousand, 2-byte storage',
+ }
+ const compare = res.find(setting => setting.name === valid.name)
+ compareKeys(valid, compare)
+ })
+})
diff --git a/packages/pg-admin-js/test/views_spec.js b/packages/pg-admin-js/test/views_spec.js
new file mode 100644
index 00000000000..02e832aa48f
--- /dev/null
+++ b/packages/pg-admin-js/test/views_spec.js
@@ -0,0 +1,26 @@
+import assert from 'assert'
+import * as views from '../src/api/views.js'
+import { compareKeys } from './_helpers'
+
+describe('Views', function() {
+ it('getAll() should return an array of tables', async () => {
+ let res = await views.getAll({}, { schema: 'information_schema' })
+
+ // Check that we have a result
+ assert(Array.isArray(res))
+
+ // Compare one of the values
+ const valid = {
+ table_schema: 'information_schema',
+ table_name: 'attributes',
+ check_option: 'NONE',
+ is_updatable: 'NO',
+ is_insertable_into: 'NO',
+ is_trigger_updatable: 'NO',
+ is_trigger_deletable: 'NO',
+ is_trigger_insertable_into: 'NO',
+ }
+ const compare = res.find(setting => setting.name === valid.name)
+ compareKeys(valid, compare)
+ })
+})