sqlite-express
Advanced tools
Comparing version 3.0.4 to 3.0.5
@@ -15,5 +15,5 @@ const consoleQuery = require( './submodules/consoleQuery' ); | ||
}else{ | ||
resolve();} | ||
resolve(this.changes);} | ||
}); | ||
}); | ||
}; |
@@ -20,3 +20,3 @@ const signs = require( './submodules/signos' ); | ||
if( logQuery ) console.log( 'Row inserted successfully.' ); | ||
resolve(); | ||
resolve(this.changes === 1); | ||
} | ||
@@ -23,0 +23,0 @@ }); |
@@ -9,3 +9,3 @@ const is = require( './submodules/is' ); | ||
insert : [ 'db', 'table', 'row', 'logQuery' ], | ||
select : [ 'db', 'table', 'select', 'where', 'connector', 'processColumns', 'processRows', 'emptyResult', 'logQuery' ], | ||
select : [ 'db', 'table', 'select', 'where', 'connector', 'join', 'processColumns', 'processRows', 'emptyResult', 'logQuery' ], | ||
update : [ 'db', 'table', 'update', 'where', 'connector', 'logQuery' ], | ||
@@ -28,2 +28,2 @@ delete : [ 'db', 'table', 'where', 'connector', 'logQuery' ], | ||
return finalParams; | ||
} | ||
}; |
const whereConstructor = require( './submodules/where' ); | ||
const is = require( './submodules/is' ); | ||
const joinConstructor = require( './submodules/join' ); | ||
const selectConstructor = require( './submodules/select' ); | ||
const consoleQuery = require( './submodules/consoleQuery' ); | ||
const processResult = require( './submodules/processResult' ); | ||
module.exports = async ( { db, table, select, where, connector, processColumns, processRows, emptyResult, logQuery } ) => { | ||
select = is.a( select ) ? select.join( ', ' ) : select; | ||
module.exports = async ( { db, table, select, where, connector, processColumns, processRows, emptyResult, logQuery, join } ) => { | ||
select = selectConstructor(select); | ||
return await new Promise( ( resolve, reject ) => { | ||
let finalQuery = `SELECT ${ select } FROM ${ table } ${ whereConstructor.query( where, connector ) }`; | ||
let finalQuery = `SELECT ${ select } FROM ${ table } ${ joinConstructor({ table, join }) } ${ whereConstructor.query( where, connector ) }`; | ||
let placeHolders = whereConstructor.placeHolders( where ); | ||
@@ -19,5 +22,45 @@ if ( logQuery ) consoleQuery( finalQuery, placeHolders ); | ||
); | ||
}; | ||
}; | ||
} ); | ||
} ); | ||
}; | ||
/* | ||
EL SELECT PUEDE SER | ||
un string con lo que se desee | ||
ej: | ||
'column_1, columns_2, column_3' | ||
'table_1.column_1, table_1.column_2, table_2.column_3' | ||
'column_1 AS newName, column_2' | ||
... | ||
un array con strings | ||
ej: | ||
[ 'column_1', 'columns_2', 'column_3' ] | ||
[ 'table_1.column_1', 'table_1.column_2', 'table_2.column_3' ] | ||
[ 'column_1 AS newName', 'column_2' ] | ||
... | ||
un array con objetos: | ||
ej: | ||
[ { column : 'column_1' }, { column : 'columns_2' }, { column : 'column_3' } ] | ||
[ | ||
{ table : 'table_1', column : 'column_1' }, | ||
{ table : 'table_1', column : 'column_2' }, | ||
{ table : 'table_2', column : 'column_3' } | ||
] | ||
[ { column : 'column_1', as : 'newName', { column : 'column_2' } ] | ||
... | ||
aprobechando el tiron hay que hacer que insert, delete y update retornen la cantidad de filas | ||
insertadas o modificadas | ||
createTable puede retornar un boolean o algo así y también crear el metodo executeSQL | ||
QUIZAS CON UN SISTEMA DE PLACEHOLDERS MÄS AMENO | ||
*/ |
@@ -52,3 +52,3 @@ const select = require( './select' ); | ||
if ( logQuery ) console.log( `Row updated successfully in table ${ table }.` ); | ||
resolve(); | ||
resolve(this.changes); | ||
}); | ||
@@ -55,0 +55,0 @@ |
{ | ||
"name": "sqlite-express", | ||
"version": "3.0.4", | ||
"version": "3.0.5", | ||
"description": "functions for sqlite3", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
141
README.md
@@ -47,2 +47,3 @@ ### SQLite-Express Version 3 | ||
where | ||
join | ||
columns | ||
@@ -208,2 +209,4 @@ select = '*' | ||
Since version 3.0.5 the method returns a promise that is resolved with a true and rejected with an error. | ||
## where constructor | ||
@@ -334,2 +337,4 @@ | ||
since version 3.0.5 the method returns a promise that resolves to the number of modified rows. | ||
## delete | ||
@@ -358,2 +363,4 @@ | ||
since version 3.0.5 the method returns a promise that resolves to the number of deleted rows. | ||
## select | ||
@@ -399,2 +406,34 @@ | ||
The select parameter accepts several formats for the columns. One is as a simple string, in this case if you want to call more than one column must be separated by commas, this format accepts the use of "AS" directly. | ||
example: | ||
```javascript | ||
-'column_1, columns_2, column_3' | ||
-'table_1.column_1, table_1.column_2, table_2.column_3' | ||
-'column_1 AS newName, column_2' | ||
``` | ||
an array with strings corresponding to the desired columns is also accepted as a parameter. | ||
example: | ||
```javascript | ||
-[ 'column_1', 'columns_2', 'column_3' ] | ||
-[ 'table_1.column_1', 'table_1.column_2', 'table_2.column_3' ] | ||
-[ 'column_1 AS newName', 'column_2' ] | ||
``` | ||
Finally, you can also use an array of objects for better handling of the "AS" clauses. | ||
example: | ||
```javascript | ||
-[ { column : 'column_1' }, { column : 'columns_2' }, { column : 'column_3' } ] | ||
-[ | ||
{ table : 'table_1', column : 'column_1' }, | ||
{ table : 'table_1', column : 'column_2' }, | ||
{ table : 'table_2', column : 'column_3' } | ||
] | ||
-[ { column : 'column_1', as : 'newName' }, { column : 'column_2' } ] | ||
``` | ||
this last form is available as of version 3.0.5 | ||
The program detects if what is passed in is a stringified object and automatically handles it. The same goes for booleans. Additionally, if the method detects a single value being received, it directly provides the value to avoid the common situation of accessing a single-property object inside an array. | ||
@@ -414,2 +453,100 @@ | ||
### Join parameter | ||
Since version 3.0.5 the join parameter is accepted for more complex selections. This parameter in its simplest form looks like this: | ||
```javascript | ||
const joinExample = { | ||
table : 'secondary_table', | ||
on : 'common_column' | ||
} | ||
``` | ||
the resulting join would look similar to this: | ||
```sql | ||
INNER JOIN secondary_table ON main_table.common_column = secondary_table.common_column | ||
``` | ||
this mode assumes that you want to join two tables that have a column with the same name, if you want to join using columns with different names you can use an array in `on` : | ||
```javascript | ||
const joinExample = { | ||
table : 'secondary_table', | ||
on : [ 'main_table_column', 'secondary_table_column' ] | ||
} | ||
``` | ||
the resulting join would look similar to this: | ||
```sql | ||
INNER JOIN secondary_table ON main_table.main_table_column = secondary_table.secondary_table_column | ||
``` | ||
as you can see, an `INNER JOIN` will be used by default. if you want another type of join you must add the type property in this way: | ||
```javascript | ||
const joinExample = { | ||
table : 'secondary_table', | ||
type : 'LEFT', | ||
on : [ 'main_table_column', 'secondary_table_column' ] | ||
} | ||
``` | ||
the resulting join would look similar to this: | ||
```sql | ||
LEFT JOIN secondary_table ON main_table.main_table_column = secondary_table.secondary_table_column | ||
``` | ||
so you can use the `INNER`, `LEFT` and `CROSS` joins. | ||
These options assume that the operator you want to use is `=`. However if you want to use another one you will have to transform `on` into an object with the `columns` and `operator` properties as follows: | ||
```javascript | ||
const joinExample = { | ||
table : 'secondary_table', | ||
type : 'LEFT', | ||
on : { | ||
columns :[ 'main_table_column', 'secondary_table_column' ], | ||
operator : '>' | ||
} | ||
} | ||
``` | ||
the resulting join would look similar to this: | ||
```sql | ||
LEFT JOIN secondary_table ON main_table.main_table_column > secondary_table.secondary_table_column | ||
``` | ||
and finally, if you want to join more than two tables you can pass an array to the join property. This array must have objects like the previous ones: | ||
```javascript | ||
const joinExample = [ | ||
{ | ||
table : 'secondary_table', | ||
type : 'LEFT', | ||
on : { | ||
columns :[ 'main_table_column', 'secondary_table_column' ], | ||
operator : '>' | ||
} | ||
}, | ||
{ | ||
table : 'other_secondary_table', | ||
on : [ 'main_table_column', 'other_secondary_table_column' ], | ||
} | ||
] | ||
``` | ||
the resulting join would look similar to this: | ||
```sql | ||
LEFT JOIN secondary_table | ||
ON main_table.main_table_column > secondary_table.secondary_table_column | ||
INNER JOIN other_secondary_table | ||
ON main_table.main_table_column = other_secondary_table.secondary_table_column | ||
``` | ||
# since version 3.0.4 we have two new methods: 'exist' and 'count'. | ||
@@ -533,3 +670,3 @@ | ||
insert: ['db', 'table', 'row', 'logQuery'], | ||
select: ['db', 'table', 'select', 'where', 'connector', 'processColumns', 'processRows', 'emptyResult', 'logQuery'], | ||
select: ['db', 'table', 'select', 'where', 'connector', 'join', 'processColumns', 'processRows', 'emptyResult', 'logQuery'], | ||
update: ['db', 'table', 'update', 'where', 'connector', 'logQuery'], | ||
@@ -542,2 +679,4 @@ delete: ['db', 'table', 'where', 'connector', 'logQuery'], | ||
note that since version 3.0.5 changed the order of the selcect parameters since join was added, so I recommend to use the parameters as objects. | ||
- **The `logQuery` Parameter**: | ||
@@ -544,0 +683,0 @@ In this version, all methods have a `logQuery` parameter which defaults to `true`. This prints the generated SQL query to the console for further analysis. If you'd prefer less information in the console, you can set its default value to `false` or pass a `false` value to the methods as you see fit. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49911
23
597
679