slicks-mysql
slicks-mysql allows the expressive writing of database queries and routines. slicks-mysql permit chaining, which is intuitive as you can nearly guess what should come next even if you are just getting started with slicks-mysql. slicks-mysql is not an ORM. It was developed to allow folks coming from relational databases background write expressive queries with object interactions in mind.
Inspired by Codeigniter Active Record
.
slicks-mysql options
slicks-mysql takes all the options/config allowed by node-mysql
. Please see https://www.npmjs.org/package/mysql for details. It also has, in addition, debug_db
option which could be true/false
. debug_db
enables the logging of the raw queries to the console when it is set to true, useful while developing.
Installation
npm install slicks-mysql --save
Usage
Using slicks-mysql is pure joy:
var options = host: 'localhost' user: 'steve' dateStrings: true database: 'todo' password: 'steve-secret' //If the following was enabled, your queries will be logged to console //debug_db: true slicks_mysql = options; //Let us now connect and get a db object slicks_mysql;
slicks-mysql management
Now that we have a valid db
object, how do we manage it? Well, all connections on db
are automatically pooled, thus, to release a db
object, it is done with db.release()
; this returns the current connection on the db
to the pool, however, to actually close the connection, use db.destroy()
; this does the cleanup and closes the underlying connection to database.
fetch
ing records
db;
The above is used when all record fields are needed. However, if a subset of the fields are of interest, select
with from
and fetch
is the way to go.
select
ing records
db ;
query
ing records with query
var q = "select * from todo"; db;
Note: The use of ONLY fetch
or in conjunction with select
and from
does not change the outcome. I think it just depends on what flavour you like or the need at hand. That being said, all the examples are written in one or other flavour but what was done in one flavour can equally be done in the other flavour.
where
db ;
db ;
db ;
db ;
db ;
where
, orWhere
, whereIn
, orWhereIn
, whereNotIn
, orWhereNotIn
conditions
Please, note that all the variations that apply to where
also apply to the following: orWhere
, whereIn
, orWhereIn
, whereNotIn
, orWhereNotIn
.
orWhere
db ;
whereIn
db //I could have used fetch directly here too ;
orWhereIn
db //I could have used fetch directly here too ;
whereNotIn
db //I could have used fetch directly here too ;
orWhereNotIn
db //I could have used fetch directly here too ;
like
Generates task like %vacuum%
, b
or both
for both ends are allowed.
db //I could have used fetch directly here too ;
orLike
Generates task like '%vacuum' or task like 'iron%'
, l
or left
for left end are allowed, while r
or right
for right end are allowed.
db //I could have used fetch directly here too ;
notLike
Generates task NOT like '%vacuum%'
, b
or both
for both ends are allowed.
db //I could have used fetch directly here too ;
orNotLike
Generates OR task NOT like '%dishes'
, l
or left
for left end are allowed.
db //I could have used fetch directly here too ;
limit
db //I could have used select, from + fetch here too ;
limit
with offset
db //I could have used select, from + fetch here too ;
orderBy (desc)
db //I could have used select, from + fetch here too ;
orderBy ([asc])
the direction is optional if ascending order is desired
db //I could have used select, from + fetch here too ;
Same as below:
db //I could have used select, from + fetch here too ;
join
ing tables
db //'left', for left join, also 'right', 'outer' etc are allowed ;
groupBy
for aggregates
db ;
having
for aggregates
db ;
orHaving
for aggregates
db ;
insert
ing records
insert
- single record per insert
db;
query
inserting multiple records with var q = "insert into todo (id, task, task_owner) values (2,'Vacuum the floor',1), (3, 'Iron my shirt', 1)";//could be more db;
update
ing records
db ;
delete
ing records
db ;
Test
Before running the tests, load the included script test_scripts.sql onto your mysql database. Ensure to load the script as 'root' for you need to grant privileges. Thereafter, run;
npm test