npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

morsel-sql.db

v0.0.1

Published

Morsel, advanced SQL database for Node.js

Downloads

72

Readme

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:

npm install morsel-sql-db

Usage

Import and Configuration

First, import the morselSQL class and create an instance with your database configuration:

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:

await db.connect();

Disconnecting from the Database

To close the connection:

await db.disconnect();

Querying

Run a raw SQL query:

const result = await db.query('SELECT * FROM my_table WHERE id = ?', [1]);
console.log(result);

Transactions

Execute multiple queries in a transaction:

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:

await db.insert('my_table', { name: 'John', age: 30 });

Bulk insert multiple rows:

const rows = [
    { name: 'John', age: 30 },
    { name: 'Jane', age: 25 }
];

await db.bulkInsert('my_table', rows);

Select

Select rows:

const rows = await db.select('my_table', { age: 30 });
console.log(rows);

Select with a join:

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:

await db.update('my_table', { name: 'Jane' }, { id: 1 });

Delete

Delete rows:

await db.delete('my_table', { id: 1 });

Advanced Operations

Count

Count rows:

const count = await db.count('my_table', { age: 30 });
console.log(count);

Exists

Check if rows exist:

const exists = await db.exists('my_table', { id: 1 });
console.log(exists);

Truncate

Truncate a table:

await db.truncate('my_table');

Drop Table

Drop a table:

await db.dropTable('my_table');

Create Table

Create a table:

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:

const columns = await db.getTableColumns('my_table');
console.log(columns);

Get Tables

List all tables:

const tables = await db.getTables();
console.log(tables);

Get Database Size

Get the database size:

const size = await db.getDatabaseSize();
console.log(`Database size: ${size} MB`);

Optimize Table

Optimize a table:

await db.optimizeTable('my_table');

Repair Table

Repair a table:

await db.repairTable('my_table');

Show Process List

Show the current processes:

const processes = await db.showProcessList();
console.log(processes);

Show Status

Show database status:

const status = await db.showStatus();
console.log(status);

Show Variables

Show database variables:

const variables = await db.showVariables();
console.log(variables);

Set Variable

Set a database variable:

await db.setVariable('max_connections', 200);

Get User Privileges

Retrieve user privileges:

const privileges = await db.getUserPrivileges('user');
console.log(privileges);

Grant Privileges

Grant privileges to a user:

await db.grantPrivileges('user', 'ALL PRIVILEGES');

Revoke Privileges

Revoke privileges from a user:

await db.revokePrivileges('user', 'ALL PRIVILEGES');

Error Handling

All methods throw errors if something goes wrong. Use try-catch blocks to handle errors:

try {
    await db.connect();
} catch (error) {
    console.error('Error:', error.message);
}

License

This module is licensed under the ISC License. See the LICENSE file for details.

Contributing

To contribute, please submit a pull request or open an issue on GitHub.

Contact

For questions or feedback, contact [email protected].