UNPKG

@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

103 lines (102 loc) 5.66 kB
"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 _table = /*#__PURE__*/new WeakMap(); var _SchemaBuilder_brand = /*#__PURE__*/new WeakSet(); class SchemaBuilder { constructor(client, table) { _classPrivateMethodInitSpec(this, _SchemaBuilder_brand); _classPrivateFieldInitSpec(this, _client, void 0); _classPrivateFieldInitSpec(this, _table, void 0); _classPrivateFieldSet(_client, this, client); _classPrivateFieldSet(_table, this, table); } createColumnDef(column, type, options = {}) { let def = `${column} ${type}`; if (options.primaryKey) def += " PRIMARY KEY"; if (options.autoIncrement) { if (_assertClassBrand(_SchemaBuilder_brand, this, _isPostgres).call(this)) def += " GENERATED ALWAYS AS IDENTITY";else if (_assertClassBrand(_SchemaBuilder_brand, this, _isMySQL).call(this)) def += " AUTO_INCREMENT"; } if (options.notNull) def += " NOT NULL"; if (options.unique) def += " UNIQUE"; if (options.default !== undefined) def += ` DEFAULT ${_assertClassBrand(_SchemaBuilder_brand, this, _formatDefault).call(this, options.default)}`; return def; } async create(columns = [], options = {}) { const colDefs = columns.map(col => this.createColumnDef(col.name, col.type, col)); let sql = `CREATE TABLE ${_classPrivateFieldGet(_table, this)} (${colDefs.join(", ")})`; if (options.ifNotExists) sql = sql.replace("CREATE TABLE", "CREATE TABLE IF NOT EXISTS"); return _classPrivateFieldGet(_client, this).query(sql); } async drop(options = {}) { let sql = `DROP TABLE ${_classPrivateFieldGet(_table, this)}`; if (options.ifExists) sql = sql.replace("DROP TABLE", "DROP TABLE IF EXISTS"); return _classPrivateFieldGet(_client, this).query(sql); } async rename(newName) { const sql = `ALTER TABLE ${_classPrivateFieldGet(_table, this)} RENAME TO ${newName}`; return _classPrivateFieldGet(_client, this).query(sql); } async addColumn(column, type, options = {}) { const colDef = this.createColumnDef(column, type, options); const sql = `ALTER TABLE ${_classPrivateFieldGet(_table, this)} ADD COLUMN ${colDef}`; return _classPrivateFieldGet(_client, this).query(sql); } async dropColumn(column) { const sql = `ALTER TABLE ${_classPrivateFieldGet(_table, this)} DROP COLUMN ${column}`; return _classPrivateFieldGet(_client, this).query(sql); } async alterColumn(column, type, options = {}) { const parts = []; if (type) parts.push(`TYPE ${type}`); if (options.notNull !== undefined) { parts.push(options.notNull ? "SET NOT NULL" : "DROP NOT NULL"); } if (options.default !== undefined) { parts.push(`SET DEFAULT ${_assertClassBrand(_SchemaBuilder_brand, this, _formatDefault).call(this, options.default)}`); } const sql = `ALTER TABLE ${_classPrivateFieldGet(_table, this)} ALTER COLUMN ${column} ${parts.join(", ")}`; return _classPrivateFieldGet(_client, this).query(sql); } async addPrimaryKey(columns) { const cols = Array.isArray(columns) ? columns.join(", ") : columns; const sql = `ALTER TABLE ${_classPrivateFieldGet(_table, this)} ADD PRIMARY KEY (${cols})`; return _classPrivateFieldGet(_client, this).query(sql); } async addForeignKey(column, refTable, refColumn, options = {}) { let sql = `ALTER TABLE ${_classPrivateFieldGet(_table, this)} ADD FOREIGN KEY (${column}) REFERENCES ${refTable}(${refColumn})`; if (options.onDelete) sql += ` ON DELETE ${options.onDelete.toUpperCase()}`; if (options.onUpdate) sql += ` ON UPDATE ${options.onUpdate.toUpperCase()}`; return _classPrivateFieldGet(_client, this).query(sql); } async addIndex(indexName, columns, options = {}) { const cols = Array.isArray(columns) ? columns.join(", ") : columns; let sql = `CREATE INDEX ${indexName} ON ${_classPrivateFieldGet(_table, this)} (${cols})`; if (options.unique) sql = sql.replace("CREATE INDEX", "CREATE UNIQUE INDEX"); if (options.ifNotExists && _assertClassBrand(_SchemaBuilder_brand, this, _isPostgres).call(this)) { sql = sql.replace("CREATE", "CREATE IF NOT EXISTS"); } return _classPrivateFieldGet(_client, this).query(sql); } async dropIndex(indexName) { let sql = `DROP INDEX ${indexName}`; if (_assertClassBrand(_SchemaBuilder_brand, this, _isPostgres).call(this)) sql += " CASCADE"; return _classPrivateFieldGet(_client, this).query(sql); } } 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 _formatDefault(value) { if (typeof value === "string" && !value.toUpperCase().includes("CURRENT_")) return `'${value}'`; return value; } module.exports = SchemaBuilder;