smart-mysql-cache
v0.3.17
Published
Smart Keeping Memory Storage in MySQL
Downloads
6
Readme
Smart MySQL Cache
smart-mysql-cache Storing Content The Database In Disk.
So let's build it with JavaScript.
How to Install :
$ npm install smart-mysql-cache
How to connect :
To use this library should we calling cache class.
host , user , password , charset , database , collection is required .
var {cache} = require('smart-mysql-cache');
var collection = {
tbl_user : {
attributes : [
{ name : 'id' , empty : false },
{ name : 'fullname' , empty : '' },
{ name : 'username' , empty : '' },
{ name : 'password' , empty : '' }
]
},
tbl_post : {
attributes : [
{ name : 'id' , empty : false },
{ name : 'title' , empty : '' },
{ name : 'description' , empty : '' },
{ name : 'user' , empty : false , collection : 'tbl_user' }
]
}
};
var db = new cache({ mysql : {
host : 'localhost',
user : 'root',
password : '',
charset : 'utf8_unicode_ci',
database : 'avl'
} , collection });
Specify your table :
It’s easy to define which table you wants to work with. You can specify your table name as we have done below.
db.collection('table_name');
Inserting Rows :
We'll show you, That's simple to insert the records.
db.collection('tbl_user').add({ fullname : 'hamid afsordeh' , username : 'hamid' , password : '******' });
db.collection('tbl_post').add({ title : 'The First Post' , description : 'This Is First Post' , user : 1 });
Updating Rows :
Update your table simply.
This line update all of your rows with condition.
Use put new data in first parameter.
Next line will update all of your columns named username
.
db.collection('tbl_user').get(5).data = { username : 'hamid_afsordeh' };
This sql query happen for last command:
Update
tbl_user
setusername
= 'hamid_afsordeh'
Delete a Row :
It’s simple to delete a row from table with that condition you want or Without any condition as we have done below.
db.collection('tbl_post').get(5).delete;
Last command return this query string:
Delete from
tbl_user
whereid
= 5
Selecting Rows :
There is three way to use select function
- find
- first
- get
- all
- In all function you will take all of rows that you want to be. You can use this parameters for table function.
db.collection('tbl_post').all
You can set condition in second parameter for select
db.collection('tbl_post').filter((post) => { return post.id == 5 });
OR use many condition
db.collection('tbl_post').filter((post) => { return post.id == 5 && post.user == 1 });
find , first and get:
- In find , fist and get function you take only one row of table as we have done below. You can use this function like table function. But different is this function fetch first row.
find(filter);
first();
get(id);
db.collection('tbl_user').find({ id: 5 });
db.collection('tbl_user').first();
db.collection('tbl_user').get(5);
Last commands return this query, string.
Select *
from tbl_user
where id
= 5
Select *
from tbl_user
Limit 1
Select *
from tbl_user
where id
= 5