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

node-mysql-easy

v2.0.4

Published

Easy way to interaact with mysql using nodejs

Downloads

33

Readme

node-mysql-easy

Open an issue on GitHub if you notice any bug or want me to add a new feature

Import

const { ConnectToDatabase, QueryDatabase } = require('node-mysql-easy');

To connect to the mysql database

const connectToDatabase = ConnectToDatabase(host,username,password,databasename);

To create instance query database class of table

const queryDatabase = new QueryDatabase(connectToDatabase, tablename);

Insert Function Documentation

Description

The Insert function is an asynchronous method that performs an SQL INSERT operation. It takes an object params as input, where the keys represent the column names and the values represent the corresponding values to be inserted.

Parameters

  • params (object): An object containing key-value pairs representing the column names and values to be inserted into the table.

Return Value

The function returns the result of the INSERT operation.

Syntax

queryDatabase.Insert(params).then((result) => {
  console.log(result);
});

OR

const insertedData = await queryDatabase.Insert(params);

Usage

queryDatabase
  .Insert({
    username: "idris",
    password: "password",
    email: "[email protected]",
  })
  .then((result) => {
    console.log(result);
  });

OR

const insertedData = await queryDatabase.Insert({
  username: "idris",
  password: "password",
  email: "[email protected]",
});

InsertMultiple Function Documentation

Description

The InsertMultiple function is an asynchronous function that allows inserting multiple records into a database table. It takes an array of objects as a parameter, where each object represents a set of values to be inserted as a new record.

Parameters

  • params (array): An array of objects representing the values to be inserted. Each object should have keys corresponding to the column names and their respective values.

Return Value

The function returns a Promise that resolves to the query result after executing the insert operation. The result can be any value returned by the database query execution.

Syntax

queryDatabase.InsertMultiple(param).then((result) => {
  console.log(result);
});

OR

const insertedData = await queryDatabase.InsertMultiple(param);

Usage

queryDatabase
  .InsertMultiple([
    {
      username: "idris",
      password: "password",
      email: "[email protected]",
    },
    {
      username: "silas",
      password: "milexswags",
      email: "[email protected]",
    },
    {
      username: "baba",
      password: "moh210",
      email: "[email protected]",
    },
  ])
  .then((result) => {
    console.log(result);
  });

OR

const insertedData = await queryDatabase.InsertMultiple([
  {
    username: "idris",
    password: "password",
    email: "[email protected]",
  },
  {
    username: "silas",
    password: "milexswags",
    email: "[email protected]",
  },
  {
    username: "baba",
    password: "moh210",
    email: "[email protected]",
  },
]);

Get Function Documentation

Description

The Get method use to get a value of column

Parameters

  • column_name (string): Specifies the name of the column to retrieve from the table.
  • where (string): Specifies the column name to match for the filtering condition.
  • value (string): Specifies the value to compare against the where column.
  • limit (number, optional): Specifies the maximum number of rows to retrieve. If not provided, all matching rows are returned.

Return Value

Returns a Promise that resolves to the result of the SQL query execution.

Usage

Suppose we want to get username where password = milexswags

queryDatabase.Get("username", "password", "milexswags", 1).then((result) => {
  console.log(result);
});

OR

const getData = await queryDatabase.Get(
  "username",
  "password",
  "milexswags",
  1
);

GetAdvanced Function Documentation

Description

The GetAdvanced function is used to retrieve data from a database table based on advanced filtering conditions. It supports filtering with multiple AND and OR conditions. The function constructs the SQL query dynamically based on the provided parameters and executes the query using a database connection. The query result is then returned.

Syntax

queryDatabase.GetAdvanced(params, limit).then((result) => {
  console.log(result);
});

Parameters

  • params (object): An object containing the parameters for the advanced query.
    • column_name (array of strings): An array specifying the columns to retrieve from the table.
    • and (object, optional): An object representing the AND conditions for the query.
    • or (object, optional): An object representing the OR conditions for the query.
    • joinwith (string, optional): Specifies the logical operator to join the AND and OR conditions ("and" or "or"). Defaults to "or".
    • debug (boolean, optional): If set to true, the constructed SQL query will be logged to the console for debugging purposes.
  • limit (number, optional): The maximum number of rows to retrieve from the table. Defaults to null, which retrieves all matching rows.
queryDatabase
  .GetAdvanced({
    column_names: ["username,email"],
    and: {
      password: "password",
      name: "idris",
    },
    or: {
      username: "idris",
      name: "silas",
    },
    joinwith: "or",
    debug: true,
  })
  .then((result) => {
    console.log(result);
  });

GetAll Function Documentation

Description

'GetAll' retrieves data from a database table. It allows fetching all records from the table or a specified number of records using an optional limit parameter.

Parameters

  • limit (optional): A number indicating the maximum number of records to retrieve. If not provided or set to null, all records from the table will be fetched.

Return Value

The method returns a Promise that resolves to the result of the executed query. The result contains the retrieved records from the database table.

Usage

queryDatabase.GetAll().then((result) => {
  console.log(result);
});

OR

const insertedData = await queryDatabase.GetAll();

Update Function Documentation

Description

The Update method use to update a value of column

Parameters

  • column_name (string): Specifies the name of the column to update from the table.
  • where (string): Specifies the column name to match for the filtering condition.
  • value (string): Specifies the value to compare against the where column.
  • limit (number, optional): Specifies the maximum number of rows to update. If not provided, all matching rows are returned.

Return Value

Returns a Promise that resolves to the result of the SQL query execution.

Usage

Suppose we want to update username where password = milexswags

queryDatabase.Update("username", "password", "milexswags", 1).then((result) => {
  console.log(result);
});

OR

const updateData = await queryDatabase.Update(
  "username",
  "password",
  "milexswags",
  1
);

UpdateAdvanced Function Documentation

Description

The GetAdvanced function is used to update data from a database table based on advanced filtering conditions. It supports filtering with multiple AND and OR conditions. The function constructs the SQL query dynamically based on the provided parameters and executes the query using a database connection. The query result is then returned.

Syntax

queryDatabase.UpadateAdvanced(params, limit).then((result) => {
  console.log(result);
});

Parameters

  • params (object): An object containing the parameters for the advanced query.
    • update (object): An object specifying the columns and the value you want to set.
    • and (object, optional): An object representing the AND conditions for the query.
    • or (object, optional): An object representing the OR conditions for the query.
    • joinwith (string, optional): Specifies the logical operator to join the AND and OR conditions ("and" or "or"). Defaults to "or".
    • debug (boolean, optional): If set to true, the constructed SQL query will be logged to the console for debugging purposes.
  • limit (number, optional): The maximum number of rows to retrieve from the table. Defaults to null, which retrieves all matching rows.
queryDatabase
  .UpdateAdvanced({
    update: {
      username: "baba",
      email: "[email protected]",
    },
    and: {
      password: "password",
      name: "baba",
    },
    or: {
      password: "password",
      age: 15,
    },
    debug: true,
  })
  .then((result) => {
    console.log(result);
  });

or

const udatedData = await queryDatabase.UpdateAdvanced({
  update: {
    username: "baba",
    email: "[email protected]",
  },
  and: {
    password: "password",
    name: "baba",
  },
  or: {
    password: "password",
    age: 15,
  },
  debug: true,
});

UpdateAll Function Documentation

Description

'UpdateAll' updates data from a database table. It allows fetching all records from the table or a specified number of records using an optional limit parameter.

Parameters

  • limit (optional): A number indicating the maximum number of records to update. If not provided or set to null, all records from the table will be fetched.

Return Value

The method returns a Promise that resolves to the result of the executed query. The result contains the updated records from the database table.

Usage

queryDatabase.UpdateAll().then((result) => {
  console.log(result);
});

OR

const insertedData = await queryDatabase.UpdateAll();

Delete Function Documentation

Description

The Delete method use to delete a value of column

Parameters

  • column_name (string): Specifies the name of the column to delete from the table.
  • where (string): Specifies the column name to match for the filtering condition.
  • value (string): Specifies the value to compare against the where column.
  • limit (number, optional): Specifies the maximum number of rows to delete. If not provided, all matching rows are returned.

Return Value

Returns a Promise that resolves to the result of the SQL query execution.

Usage

Suppose we want to delete username where password = milexswags

queryDatabase.Delete("username", "password", "milexswags", 1).then((result) => {
  console.log(result);
});

OR

const deleteData = await queryDatabase.Delete(
  "username",
  "password",
  "milexswags",
  1
);

DeleteAdvanced Function Documentation

Description

The GetAdvanced function is used to delete data from a database table based on advanced filtering conditions. It supports filtering with multiple AND and OR conditions. The function constructs the SQL query dynamically based on the provided parameters and executes the query using a database connection. The query result is then returned.

Syntax

queryDatabase.UpadateAdvanced(params, limit).then((result) => {
  console.log(result);
});

Parameters

  • params (object): An object containing the parameters for the advanced query.
    • and (object, optional): An object representing the AND conditions for the query.
    • or (object, optional): An object representing the OR conditions for the query.
    • joinwith (string, optional): Specifies the logical operator to join the AND and OR conditions ("and" or "or"). Defaults to "or".
    • debug (boolean, optional): If set to true, the constructed SQL query will be logged to the console for debugging purposes.
  • limit (number, optional): The maximum number of rows to retrieve from the table. Defaults to null, which retrieves all matching rows.
queryDatabase
  .DeleteAdvanced({
    and: {
      password: "password",
      name: "baba",
    },
    or: {
      password: "password",
      age: 15,
    },
    debug: true,
  })
  .then((result) => {
    console.log(result);
  });

or

const udatedData = await queryDatabase.DeleteAdvanced({
  and: {
    password: "password",
    name: "baba",
  },
  or: {
    password: "password",
    age: 15,
  },
  debug: true,
});

DeleteAll Function Documentation

Description

'DeleteAll' deletes data from a database table. It allows deleting all records from the table or a specified number of records using an optional limit parameter.

Parameters

  • limit (optional): A number indicating the maximum number of records to delete. If not provided or set to null, all records from the table will be fetched.

Return Value

The method returns a Promise that resolves to the result of the executed query. The result contains the deleted records from the database table.

Usage

queryDatabase.DeleteAll().then((result) => {
  console.log(result);
});

OR

const insertedData = await queryDatabase.DeleteAll();

Custom SQL:

queryDatabase
.CustomSQL(sqlString)
.then((result) => {
console.log(result);
});

Example

queryDatabase
.CustomSQL(SELECT id, name, email
FROM users
WHERE age > 18
ORDER BY name ASC)
.then((result) => {
console.log(result);
});
```