4.9.0 • Published 9 days ago

mysql-all-in-one v4.9.0

Weekly downloads
11
License
ISC
Repository
github
Last release
9 days ago

mysql-all-in-one

All tools to connect, execute commands and build raw queries for Mysql databases.

It's safe, fast and easy to implement. This package is a Wrapper around mysql2 package

Installation

Installation is done using the npm install command:

$ npm install mysql-all-in-one

Features

  • Data access object;
  • SQL Injection proof;
  • Prepared statements to execute faster queries;
  • All basic commands: SELECT, INSERT, UPDATE, DELETE and UPSERT;
  • Create database dumps;
  • Easy to execute commands on multiple databases;

Quick start

Data Access Object

Data access object (DAO) will connect to the database and execute commands (as prepared statements by default).

const { DataAccessObject } = require('mysql-all-in-one');

const dao = new DataAccessObject({
	host: 'localhost',
	user: 'root',
	password: '1234',
	port: 3306,
	database: 'test_database',
});
const main = async () => {
	const result = await dao.select({ from: 'my_table' });
	console.log(result); //Will list all rows from a table "my_table" located at a database "test_database"
};
main();

Query Builder

Query builder simply returns Mysql Query commands as string. (This module is used by DataAccessObject to build queries).

const { QueryBuilder } = require('mysql-all-in-one');
// OR
// const QueryBuilder = require('mysql-all-in-one/QueryBuilder');

const query = QueryBuilder.select({ from: 'my_table', where: { id: 1 } });
console.log(query);
// >>> SELECT `my_table`.* FROM `my_table` WHERE (`my_table`.`id` = 1);

Examples

Data Access Object

DataAccessObject uses QueryBuilder under the hood. So for example, everything that works on the method select from QueryBuilder also works on select command from DataAccessObject.

SELECT

WHERE

Simple where object:

dao.select({
	from: 'table_user',
	where: {
		name: { like: 'foo' },
		id: 1,
		active: null,
		permission: {isnot: null}
	},
});

Or between conditions:

dao.select({
	from: 'table_user',
	where: {
		__or: true, // All conditions inside this object will use OR between them.
		id: 1,
		active: 1,
	},
});

Array with multiple objects:

dao.select({
	from: 'table_user',
	where: [
		'__or', // If first position is "__or" all conditions inside this array will use OR between them.
		{ id: 1, },
		{ id: 2, },
	]
});

Complex nesting to achieve any AND/OR relation needed:

dao.select({
	from: 'table_user',
	where: [
		'__or',
		[ // Unlimited nestings are possible, allowing to create any condition combination
			[ // Every array or object will be inside it's own bracket.
				'__or',
				{
					name: { like: 'foo_bar' },
					id: 4,
				},
			],
			{name: { like: 'bar' },}
		],
		{
			name: { like: 'foo' },
			id: 1,
			active: null,
			permission: {isnot: null}
		},
		{
			id: 2,
		}
	]
});

Custom SQL Expressions is also allowed:

const { sqlExpression } = QueryBuilder;

const name = 'john';
const birth = new Date(2002, 8, 30);

dao.select({
	from: 'table_user',
	where: [
		// Also accepts sqlExpression objects to create custom where commands (automatically escape variables).
		sqlExpression`table_user.name LIKE ${name}`, // >>> table_user.name LIKE "john"
		sqlExpression`table_user.birthdate = ${birth}`, // >>> table_user.birthdate = '2002-09-30 00:00:00.000'
	],
});
4.9.0

9 days ago

4.8.1

3 months ago

4.8.0

3 months ago

4.7.3

3 months ago

4.7.2

4 months ago

4.7.1

4 months ago

4.7.0

4 months ago

4.6.3

9 months ago

4.6.2

9 months ago

4.6.1

1 year ago

4.6.0

1 year ago

4.3.6

2 years ago

4.3.5

2 years ago

4.4.0

2 years ago

4.5.0

2 years ago

4.3.4

2 years ago

4.0.10

2 years ago

4.0.7

2 years ago

4.2.0

2 years ago

4.0.11

2 years ago

4.0.9

2 years ago

4.0.8

2 years ago

4.3.2

2 years ago

4.3.1

2 years ago

4.3.3

2 years ago

4.1.0

2 years ago

4.3.0

2 years ago

4.1.2

2 years ago

4.1.1

2 years ago

3.0.13

2 years ago

3.2.0

2 years ago

3.0.16

2 years ago

3.0.17

2 years ago

3.0.14

2 years ago

3.0.15

2 years ago

4.0.5

2 years ago

4.0.4

2 years ago

4.0.6

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

4.0.3

2 years ago

4.0.2

2 years ago

3.0.20

2 years ago

3.0.18

2 years ago

3.0.19

2 years ago

3.1.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

3.0.12

2 years ago

3.0.4

2 years ago

3.0.3

2 years ago

3.0.10

2 years ago

3.0.11

2 years ago

3.0.1

2 years ago

3.0.8

2 years ago

3.0.7

2 years ago

3.0.6

2 years ago

3.0.5

2 years ago

3.0.0

2 years ago

1.5.3

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

3.0.9

2 years ago

1.4.7

2 years ago

1.2.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.4.6

2 years ago

1.4.5

2 years ago

1.4.4

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.4.0

2 years ago

1.2.2

2 years ago

1.1.3

2 years ago

1.3.0

2 years ago

1.2.1

2 years ago

1.1.2

2 years ago

1.0.11

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.10

2 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago