Skip to content

Commit b293283

Browse files
Multi-dialect support now marked stable,
redundent console log removed from legacy file, switch case removed to check execution, handleExecution object functions created, alias identifiers optimized, multi-query support patched to rawQuery method using keyword, Fallback handler IfNull method patched as property to all aggregate methods, prepared statement now defaults to '' instead of null, constants now also contains null as key, regular null value is now handled by prepWhere function, placeholder helper optimized, type definitions patched, code cleanup, code optimization
1 parent 63c7fdf commit b293283

10 files changed

+173
-223
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

Readme.md

+29-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
![NPM Downloads](https://img.shields.io/npm/dm/unsql)
66
![NPM License](https://img.shields.io/npm/l/unsql "UnSQL License")
77

8-
**UnSQL** is a lightweight, open-source JavaScript library that facilitates class based, schemaless interactions with the structured databases viz. `MySQL`, `PostgreSQL` and `SQLite` through dynamic query generation. It is compatible with javascript runtime environments like NodeJS and NextJS.
8+
**UnSQL** is a lightweight, open-source JavaScript library that facilitates class based, schemaless interactions with the structured databases viz. `MySQL`, `PostgreSQL` and `SQLite` through dynamic query generation. It is the only library that supports single codebase across all dialects. It is compatible with javascript runtime environments like NodeJS and NextJS.
99

1010
## Table of Contents
1111
1. [Overview](#1-overview)
@@ -34,7 +34,7 @@
3434

3535
## 1. Overview
3636

37-
**UnSQL** simplifies working with structured databases by dynamically generating SQLs under the hood. It provides developer friendly interface while eliminating the complexities of SQL. UnSQL also utilizes placeholders and prepared statements to prevent SQL-injections.
37+
**UnSQL** simplifies working with structured databases by dynamically generating SQLs under the hood. It provides developer friendly interface while eliminating the complexities of SQL. UnSQL also utilizes placeholders and parameterized SQL statements to prevent SQL-injections.
3838

3939
### 1.1 Breaking Changes
4040

@@ -88,9 +88,12 @@ UnSQL can work with three different `dialect` of SQL (`'mysql'`, `'postgresql'`
8888
import mysql2 from 'mysql2/promise'
8989

9090
export const pool = createPool({
91-
...,
91+
host: 'localhost', // or link to remote database
92+
database: 'test_db',
93+
user: 'your_username',
94+
password: 'your_password',
9295
namedPlaceholders: true, // (optional) required if using rawQuery with named placeholders
93-
multipleStatements: true // (optional) required if using Encryption/Decryption features
96+
multipleStatements: true // (optional) required if using multiple statements in rawQuery
9497
})
9598
```
9699

@@ -101,7 +104,12 @@ export const pool = createPool({
101104
```javascript
102105
import { Pool } from 'pg'
103106

104-
export const pool = new Pool({...})
107+
export const pool = new Pool({
108+
host: 'localhost',
109+
database: 'test_db',
110+
user: 'your_username',
111+
password: 'your_password'
112+
})
105113
```
106114

107115
- **SQLite** (`dialect: 'sqlite'`)
@@ -460,6 +468,8 @@ Each of these properties is explained below:
460468

461469
`rawQuery` method is the most powerful method among all, unlike other methods that are limited to the base mapping, this method is not tied to any particular table, but utilizes the connection pool to execute queries on that database itself. It is capable of executing any and all types of queries including **DDL, DML etc** (In `sqlite`, set `methodType: 'exec'`). It also supports execution of multiple SQL statements in one query. When multiple `SELECT` statements are executed (not supported by `sqlite`), `result` contains nested array one for each `SELECT` statement.
462470

471+
In `mysql`, use `multiQuery: true` to enable execution of multiple SQL statements in single query
472+
463473
For `sqlite`, UnSQL supports various types of methods (as mentioned below) that can be set manually, each method has specific capabilities:
464474

465475
| Method Type | Description |
@@ -502,7 +512,7 @@ const response = await User.rawQuery({ // here user model is used just to utiliz
502512
lastUpdatedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
503513
status TINYINT(1) DEFAULT 0
504514
);`,
505-
methodType: 'query' // this supports multiple statements in single query string
515+
multiQuery: true // this enables multiple SQL statements in single query string, only for MySQL
506516
})
507517
```
508518

@@ -961,8 +971,8 @@ All objects are explained below:
961971
{
962972
sum: {
963973
value: 'some column', // or conditional object {...}
964-
distinct: false, // when true, ignore duplicate column values
965-
distinct: false, // when true, distinct column values will be considered
974+
distinct: false, // when true, ignore duplicate columns
975+
ifNull: undefined, // provide default value if incase this method returns null
966976
cast: null, // type cast value to 'signed', 'unsigned' etc
967977
as: null, // local reference name to this object 'value'
968978
compare: {} // comparator object
@@ -974,6 +984,7 @@ All objects are explained below:
974984
select: [
975985
{ sum: {
976986
value: 'salary',
987+
ifNull: 0,
977988
cast: 'signed', // convert to 'singed' (number)
978989
as: 'totalSalary'
979990
}
@@ -983,6 +994,7 @@ All objects are explained below:
983994
having: {
984995
sum: {
985996
value: 'salary',
997+
ifNull: 0,
986998
compare: { gt: 5000 }
987999
}
9881000
}
@@ -999,6 +1011,7 @@ All objects are explained below:
9991011
avg: {
10001012
value: 'some column', // or conditional object {...}
10011013
distinct: false, // when true, distinct column values will be considered
1014+
ifNull: undefined, // provide default value if incase this method returns null
10021015
cast: null, // type cast value to 'signed', 'unsigned' etc
10031016
as: null, // local reference name to this object 'value'
10041017
compare: {} // comparator object
@@ -1011,6 +1024,7 @@ All objects are explained below:
10111024
{
10121025
avg: {
10131026
value: 'salary',
1027+
ifNull: 0,
10141028
cast: 'unsigned',
10151029
as: 'averageSalary',
10161030
}
@@ -1036,6 +1050,7 @@ All objects are explained below:
10361050
count: {
10371051
value: 'some column', // or conditional object {...}
10381052
distinct: false, // when true, distinct column values will be considered
1053+
ifNull: undefined, // provide default value if incase this method returns null
10391054
cast: null, // type cast value to 'signed', 'unsigned' etc
10401055
as: null, // local reference name to this object 'value'
10411056
compare: {} // comparator object
@@ -1049,6 +1064,7 @@ All objects are explained below:
10491064
count: {
10501065
value: '*',
10511066
distinct: true,
1067+
ifNull: 0,
10521068
as: 'totalEmployees',
10531069
}
10541070
}
@@ -1067,6 +1083,7 @@ All objects are explained below:
10671083
min: {
10681084
value: 'some column', // or conditional object {...}
10691085
distinct: false, // when true, distinct column values will be considered
1086+
ifNull: undefined, // provide default value if incase this method returns null
10701087
cast: null, // type cast value to 'signed', 'unsigned' etc
10711088
as: null, // local reference name to this object 'value'
10721089
compare: {} // comparator object
@@ -1079,6 +1096,7 @@ All objects are explained below:
10791096
{
10801097
min: {
10811098
value: 'salary',
1099+
ifNull: 0,
10821100
cast: 'unsigned',
10831101
as: 'lowestSalary'
10841102
}
@@ -1097,6 +1115,7 @@ All objects are explained below:
10971115
max: {
10981116
value: 'some column', // or conditional object {...}
10991117
distinct: false, // when true, distinct column values will be considered
1118+
ifNull: undefined, // provide default value if incase this method returns null
11001119
cast: null, // type cast value to 'signed', 'unsigned' etc
11011120
as: null, // local reference name to this object 'value'
11021121
compare: {} // comparator object
@@ -1110,6 +1129,7 @@ All objects are explained below:
11101129
max: {
11111130
value: 'salary',
11121131
distinct: true,
1132+
ifNull: 0,
11131133
cast: 'unsigned',
11141134
as: 'highestSalary'
11151135
}
@@ -1378,7 +1398,7 @@ router.get('/users', async (req, res) => {
13781398
{
13791399
json: {
13801400
value: 'address',
1381-
extract: 'permanent.city'
1401+
extract: 'permanent.city' // this will extract 'city' from 'address' json object
13821402
as: 'city'
13831403
}
13841404
}

defs/types.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* @prop {Object} value
4949
* @prop {boolean} [distinct]
5050
* @prop {Object} [compare]
51+
* @prop {string|number|boolean} [ifNull]
5152
* @prop {CastingTypes} [cast]
5253
* @prop {string} [as]
5354
*/
@@ -146,14 +147,14 @@
146147
* when then object
147148
* @typedef {Object} WhenThenCondition
148149
* @prop {boolean|WhereObject} when
149-
* @prop {string|number|boolean} then
150+
* @prop {ValuesObject} then
150151
*/
151152

152153
/**
153154
* Switch object
154155
* @typedef {Object} SwitchObject
155156
* @prop {Array<WhenThenCondition>} check
156-
* @prop {string|number|boolean} [else]
157+
* @prop {ValuesObject} [else]
157158
* @prop {string} [as]
158159
*/
159160

helpers/console.helper.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const colors = Object.freeze({
88
})
99

1010

11-
const handleQueryDebug = (debug, sql, values, prepared = null) => {
11+
const handleQueryDebug = (debug, sql, values, prepared = '') => {
1212
if (debug === true || debug === 'benchmark-query' || debug == 'query') {
1313
console.info(`\n${colors.blue}******************************************************************${colors.reset}`)
1414
console.info(`${colors.cyan} UnSQL Debug Query Begins${colors.reset}`)

helpers/constants.helper.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const constantFunctions = Object.freeze({
99
localTime: 'LOCALTIME',
1010
localTimestamp: 'LOCALTIMESTAMP',
1111
pi: 'PI()',
12+
null: 'IS NULL',
1213
isNull: 'IS NULL',
1314
isNotNull: 'IS NOT NULL'
1415
})

0 commit comments

Comments
 (0)