@trap_stevo/liveql
Version:
Supercharge your database workflow with a visually clean, ultra-intuitive SQL layer. Chain elegant queries, trigger instant real-time events, and manage schemas effortlessly — all without ORM overhead while blending raw SQL power with modern developer erg
158 lines (157 loc) • 7.71 kB
JavaScript
"use strict";
function _classPrivateMethodInitSpec(e, a) { _checkPrivateRedeclaration(e, a), a.add(e); }
function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); }
function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }
function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; }
function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
var _client = /*#__PURE__*/new WeakMap();
var _SchemaInspector_brand = /*#__PURE__*/new WeakSet();
class SchemaInspector {
constructor(client) {
_classPrivateMethodInitSpec(this, _SchemaInspector_brand);
_classPrivateFieldInitSpec(this, _client, void 0);
_classPrivateFieldSet(_client, this, client);
}
async listTables() {
if (_assertClassBrand(_SchemaInspector_brand, this, _isPostgres).call(this)) {
const res = await _classPrivateFieldGet(_client, this).query(`
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name;
`);
return res.rows.map(r => r.table_name);
} else if (_assertClassBrand(_SchemaInspector_brand, this, _isMySQL).call(this)) {
const [rows] = await _classPrivateFieldGet(_client, this).query(`
SHOW TABLES;
`);
return rows.map(r => Object.values(r)[0]);
} else if (_assertClassBrand(_SchemaInspector_brand, this, _isSQLite).call(this)) {
const res = await _classPrivateFieldGet(_client, this).all(`
SELECT name FROM sqlite_master
WHERE type='table'
ORDER BY name;
`);
return res.map(r => r.name);
}
return [];
}
async getColumns(table) {
if (_assertClassBrand(_SchemaInspector_brand, this, _isPostgres).call(this)) {
const res = await _classPrivateFieldGet(_client, this).query(`
SELECT column_name, data_type, is_nullable, column_default
FROM information_schema.columns
WHERE table_name = $1
ORDER BY ordinal_position;
`, [table]);
return res.rows;
} else if (_assertClassBrand(_SchemaInspector_brand, this, _isMySQL).call(this)) {
const [rows] = await _classPrivateFieldGet(_client, this).query(`SHOW COLUMNS FROM \`${table}\``);
return rows.map(r => ({
column_name: r.Field,
data_type: r.Type,
is_nullable: r.Null === "YES",
column_default: r.Default
}));
} else if (_assertClassBrand(_SchemaInspector_brand, this, _isSQLite).call(this)) {
const res = await _classPrivateFieldGet(_client, this).all(`PRAGMA table_info(${table});`);
return res.map(r => ({
column_name: r.name,
data_type: r.type,
is_nullable: !r.notnull,
column_default: r.dflt_value
}));
}
return [];
}
async getPrimaryKeys(table) {
if (_assertClassBrand(_SchemaInspector_brand, this, _isPostgres).call(this)) {
const res = await _classPrivateFieldGet(_client, this).query(`
SELECT a.attname AS column_name
FROM pg_index i
JOIN pg_attribute a ON a.attrelid = i.indrelid
AND a.attnum = ANY(i.indkey)
WHERE i.indrelid = $1::regclass
AND i.indisprimary;
`, [table]);
return res.rows.map(r => r.column_name);
} else if (_assertClassBrand(_SchemaInspector_brand, this, _isMySQL).call(this)) {
const [rows] = await _classPrivateFieldGet(_client, this).query(`SHOW KEYS FROM \`${table}\` WHERE Key_name = 'PRIMARY'`);
return rows.map(r => r.Column_name);
} else if (_assertClassBrand(_SchemaInspector_brand, this, _isSQLite).call(this)) {
const res = await _classPrivateFieldGet(_client, this).all(`PRAGMA table_info(${table});`);
return res.filter(r => r.pk !== 0).map(r => r.name);
}
return [];
}
async getIndexes(table) {
if (_assertClassBrand(_SchemaInspector_brand, this, _isPostgres).call(this)) {
const res = await _classPrivateFieldGet(_client, this).query(`
SELECT indexname, indexdef
FROM pg_indexes
WHERE tablename = $1;
`, [table]);
return res.rows;
} else if (_assertClassBrand(_SchemaInspector_brand, this, _isMySQL).call(this)) {
const [rows] = await _classPrivateFieldGet(_client, this).query(`SHOW INDEX FROM \`${table}\``);
return rows;
} else if (_assertClassBrand(_SchemaInspector_brand, this, _isSQLite).call(this)) {
const res = await _classPrivateFieldGet(_client, this).all(`PRAGMA index_list(${table});`);
return res;
}
return [];
}
async getForeignKeys(table) {
if (_assertClassBrand(_SchemaInspector_brand, this, _isPostgres).call(this)) {
const res = await _classPrivateFieldGet(_client, this).query(`
SELECT
conname AS constraint_name,
conrelid::regclass AS table_name,
a.attname AS column_name,
confrelid::regclass AS foreign_table_name,
af.attname AS foreign_column_name
FROM pg_constraint
JOIN pg_class ON conrelid = pg_class.oid
JOIN pg_attribute a ON a.attnum = ANY (conkey) AND a.attrelid = conrelid
JOIN pg_attribute af ON af.attnum = ANY (confkey) AND af.attrelid = confrelid
WHERE contype = 'f'
AND conrelid = $1::regclass;
`, [table]);
return res.rows;
} else if (_assertClassBrand(_SchemaInspector_brand, this, _isMySQL).call(this)) {
const [rows] = await _classPrivateFieldGet(_client, this).query(`
SELECT
CONSTRAINT_NAME as constraint_name,
TABLE_NAME as table_name,
COLUMN_NAME as column_name,
REFERENCED_TABLE_NAME as foreign_table_name,
REFERENCED_COLUMN_NAME as foreign_column_name
FROM information_schema.KEY_COLUMN_USAGE
WHERE TABLE_NAME = ?
AND REFERENCED_TABLE_NAME IS NOT NULL;
`, [table]);
return rows;
} else if (_assertClassBrand(_SchemaInspector_brand, this, _isSQLite).call(this)) {
const res = await _classPrivateFieldGet(_client, this).all(`PRAGMA foreign_key_list(${table});`);
return res.map(r => ({
constraint_name: null,
table_name: table,
column_name: r.from,
foreign_table_name: r.table,
foreign_column_name: r.to
}));
}
return [];
}
}
function _isPostgres() {
return _classPrivateFieldGet(_client, this)?.constructor?.name === "Client" || _classPrivateFieldGet(_client, this)?.constructor?.name === "Pool";
}
function _isMySQL() {
return typeof _classPrivateFieldGet(_client, this).execute === "function" || typeof _classPrivateFieldGet(_client, this).query === "function";
}
function _isSQLite() {
return _classPrivateFieldGet(_client, this)?.constructor?.name?.toLowerCase().includes("sqlite");
}
module.exports = SchemaInspector;