UNPKG

morsel-sql.db

Version:

Morsel, advanced SQL database for Node.js

300 lines (196 loc) 4.89 kB
# Morsel SQL Database Morsel SQL Database is a comprehensive SQL database management module for Node.js. It offers functionalities for various database operations, including querying, transactions, and schema management. ## Installation To use the `morsel-sql-db` module, ensure you have Node.js installed, then install the module using npm: ```bash npm install morsel-sql-db ``` ## Usage ### Import and Configuration First, import the `morselSQL` class and create an instance with your database configuration: ```javascript const morselSQL = require('morsel-sql-db'); // Import the module const config = { host: 'localhost', user: 'root', password: 'password', database: 'my_database' }; const db = new morselSQL(config); ``` ### Connecting to the Database To establish a connection: ```javascript await db.connect(); ``` ### Disconnecting from the Database To close the connection: ```javascript await db.disconnect(); ``` ### Querying Run a raw SQL query: ```javascript const result = await db.query('SELECT * FROM my_table WHERE id = ?', [1]); console.log(result); ``` ### Transactions Execute multiple queries in a transaction: ```javascript const queries = [ { sql: 'INSERT INTO my_table (name) VALUES (?)', params: ['John'] }, { sql: 'UPDATE my_table SET name = ? WHERE id = ?', params: ['Doe', 1] } ]; const results = await db.transaction(queries); console.log(results); ``` ### CRUD Operations #### Insert Insert a row: ```javascript await db.insert('my_table', { name: 'John', age: 30 }); ``` Bulk insert multiple rows: ```javascript const rows = [ { name: 'John', age: 30 }, { name: 'Jane', age: 25 } ]; await db.bulkInsert('my_table', rows); ``` #### Select Select rows: ```javascript const rows = await db.select('my_table', { age: 30 }); console.log(rows); ``` Select with a join: ```javascript const rows = await db.selectWithJoin('my_table', 'INNER', 'another_table', 'my_table.id = another_table.my_table_id', { age: 30 }); console.log(rows); ``` #### Update Update rows: ```javascript await db.update('my_table', { name: 'Jane' }, { id: 1 }); ``` #### Delete Delete rows: ```javascript await db.delete('my_table', { id: 1 }); ``` ### Advanced Operations #### Count Count rows: ```javascript const count = await db.count('my_table', { age: 30 }); console.log(count); ``` #### Exists Check if rows exist: ```javascript const exists = await db.exists('my_table', { id: 1 }); console.log(exists); ``` #### Truncate Truncate a table: ```javascript await db.truncate('my_table'); ``` #### Drop Table Drop a table: ```javascript await db.dropTable('my_table'); ``` #### Create Table Create a table: ```javascript const columns = { id: 'INT AUTO_INCREMENT PRIMARY KEY', name: 'VARCHAR(255)', age: 'INT' }; await db.createTable('my_table', columns); ``` #### Get Table Columns Retrieve table columns: ```javascript const columns = await db.getTableColumns('my_table'); console.log(columns); ``` #### Get Tables List all tables: ```javascript const tables = await db.getTables(); console.log(tables); ``` #### Get Database Size Get the database size: ```javascript const size = await db.getDatabaseSize(); console.log(`Database size: ${size} MB`); ``` #### Optimize Table Optimize a table: ```javascript await db.optimizeTable('my_table'); ``` #### Repair Table Repair a table: ```javascript await db.repairTable('my_table'); ``` #### Show Process List Show the current processes: ```javascript const processes = await db.showProcessList(); console.log(processes); ``` #### Show Status Show database status: ```javascript const status = await db.showStatus(); console.log(status); ``` #### Show Variables Show database variables: ```javascript const variables = await db.showVariables(); console.log(variables); ``` #### Set Variable Set a database variable: ```javascript await db.setVariable('max_connections', 200); ``` #### Get User Privileges Retrieve user privileges: ```javascript const privileges = await db.getUserPrivileges('user'); console.log(privileges); ``` #### Grant Privileges Grant privileges to a user: ```javascript await db.grantPrivileges('user', 'ALL PRIVILEGES'); ``` #### Revoke Privileges Revoke privileges from a user: ```javascript await db.revokePrivileges('user', 'ALL PRIVILEGES'); ``` ## Error Handling All methods throw errors if something goes wrong. Use `try-catch` blocks to handle errors: ```javascript try { await db.connect(); } catch (error) { console.error('Error:', error.message); } ``` ## License This module is licensed under the ISC License. See the [LICENSE](./LICENSE) file for details. ## Contributing To contribute, please submit a pull request or open an issue on GitHub. ## Contact For questions or feedback, contact [morselprojeler@gmail.com](mailto:morselprojeler@gmail.com).