Skip to content

Commit ecc28b8

Browse files
committed
v1.3.0
1 parent 4cf7739 commit ecc28b8

File tree

6 files changed

+215
-156
lines changed

6 files changed

+215
-156
lines changed

.github/workflows/node.js.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: Node.js CI
5+
6+
on:
7+
push:
8+
branches: [ master ]
9+
pull_request:
10+
branches: [ master ]
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
strategy:
18+
matrix:
19+
node-version: [14.x, 16.x, 18.x, 20.x, 22.x, 24.x]
20+
21+
steps:
22+
- uses: actions/checkout@v3
23+
- name: Use Node.js ${{ matrix.node-version }}
24+
uses: actions/setup-node@v3
25+
with:
26+
node-version: ${{ matrix.node-version }}
27+
- run: npm ci
28+
- run: npm run build --if-present
29+
- run: npm test
30+
# - run: npm run coverage

README.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
[![NPM](https://nodei.co/npm/mysql2-cache.png?downloads=true&stars=true)](https://nodei.co/npm/mysql2-cache/)
44

5-
![GitHub release (latest by date)](https://img.shields.io/github/v/release/2naive/node-mysql2-cache)
5+
![GitHub release (latest by date)](https://img.shields.io/github/v/release/2naive/mysql2-cache)
66
![node-current](https://img.shields.io/node/v/mysql2-cache)
7-
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/2naive/node-mysql2-cache/Node.js%20Package)
8-
![Coveralls github](https://img.shields.io/coveralls/github/2naive/node-mysql2-cache)
7+
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/2naive/mysql2-cache/npm-publish.yml?branch=main)
8+
![Coveralls github](https://img.shields.io/coveralls/github/2naive/mysql2-cache)
99
![Standard - JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)
1010

1111
> ✔ MySQL2 upgrade: cache queries, easy shortcuts, logging and debugging.
@@ -39,17 +39,16 @@ const db = mysql.connect({
3939
database: 'test',
4040
password: 'root'
4141
})
42-
db.q('SELECT * FROM test_table WHERE id=?', 1, true) // use cache with default ttl
42+
db.q('SELECT * FROM test_table WHERE id=?', 1, true) // use cache with default ttl=300s
4343
db.q('SELECT * FROM test_table WHERE id=?', 1, true, 300) // ttl in seconds
4444
```
4545

46-
47-
### Debugging easy
46+
## Debugging easy
4847

4948
Pass `DEBUG=mysql2-cache*` environment variable to pretty debug.
5049

5150
```bash
52-
mysql2-cache:1 SELECT * FROM test_table undefined +0ms
51+
mysql2-cache:1 SELECT * FROM test_table WHERE age > ? [1] +0ms
5352
mysql2-cache:1 ┌─────────┬─────────┬─────┐
5453
mysql2-cache:1 │ (index) │ name │ age │
5554
mysql2-cache:1 ├─────────┼─────────┼─────┤
@@ -59,6 +58,26 @@ Pass `DEBUG=mysql2-cache*` environment variable to pretty debug.
5958
mysql2-cache:1 +32ms
6059
```
6160

61+
## API
62+
63+
You may use all [MySQL2](https://github.com/sidorares/node-mysql2) methods plus:
64+
65+
### async q(sql, params = [], cache = false, ttl = undefined)
66+
67+
### async insert(table, row)
68+
69+
### async update(table, row, where = false)
70+
71+
### async delete(table, row, where = false)
72+
73+
### stat()
74+
75+
### cacheFlush(sql, params)
76+
77+
### cacheFlushAll()
78+
79+
### cacheStat()
80+
6281
## Getting help
6382

6483
If you've found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo!

index.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module.exports.connect = (config = {}) => {
4646
pool.q = async (sql, params = [], cache = false, ttl = undefined) => {
4747
qid++
4848
const log = debug.extend(qid)
49-
log(sql, params)
49+
log(sql, params, {cache: cache, ttl: ttl ? ttl : DEFAULT_CACHE_TTL})
5050
// https://medium.com/@chris_72272/what-is-the-fastest-node-js-hashing-algorithm-c15c1a0e164e
5151
const hash = crypto.createHash('sha1').update(sql + JSON.stringify(params)).digest('base64')
5252
if (cache && queryCache.has(hash)) {
@@ -77,15 +77,7 @@ module.exports.connect = (config = {}) => {
7777
return Array.isArray(rows) && rows.length ? rows[0] : false
7878
}
7979

80-
pool.stat = () => {
81-
return {
82-
ALL: pool.pool._allConnections.toArray().length,
83-
// USE: pool.pool._allConnections.toArray().length - pool.pool._freeConnections.toArray().length,
84-
FRE: pool.pool._freeConnections.toArray().length,
85-
QUE: pool.pool._connectionQueue.toArray().length
86-
}
87-
}
88-
80+
// @todo insert array of objects
8981
pool.insert = pool.i = async (table, row) => {
9082
qid++
9183
const log = debug.extend(qid)
@@ -128,6 +120,32 @@ module.exports.connect = (config = {}) => {
128120
return rows || false
129121
}
130122

123+
pool.stat = () => {
124+
return {
125+
ALL: pool.pool._allConnections.toArray().length,
126+
// USE: pool.pool._allConnections.toArray().length - pool.pool._freeConnections.toArray().length,
127+
FRE: pool.pool._freeConnections.toArray().length,
128+
QUE: pool.pool._connectionQueue.toArray().length
129+
}
130+
}
131+
132+
pool.cacheFlush = (sql, params) => {
133+
const hash = crypto.createHash('sha1').update(sql + JSON.stringify(params)).digest('base64')
134+
const deleted = queryCache.del(hash)
135+
debug('Cache flush', sql, params, { deleted }, queryCache.getStats())
136+
return deleted
137+
}
138+
139+
pool.cacheFlushAll = () => {
140+
queryCache.flushAll()
141+
debug('Cache flush all', queryCache.getStats())
142+
return true
143+
}
144+
145+
exports.cacheStat = () => {
146+
return queryCache.getStats()
147+
}
148+
131149
pool.on('acquire', (connection) => {
132150
debug('Connection #%s acquired', connection.threadId, pool.stat())
133151
})

package-lock.json

Lines changed: 34 additions & 74 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "mysql2-cache",
3-
"version": "1.0.1",
3+
"version": "1.3.0",
44
"description": "✔ MySQL2 upgrade: cache queries, easy shortcuts, logging and debugging.",
55
"main": "index.js",
66
"scripts": {
7-
"test": "DEBUG=* node ./test/test.js"
7+
"test": "node ./test/test.js"
88
},
99
"repository": {
1010
"type": "git",
@@ -27,10 +27,12 @@
2727
"url": "https://github.com/2naive/node-mysql2-cache/issues"
2828
},
2929
"homepage": "https://github.com/2naive/node-mysql2-cache#readme",
30+
"engines": {
31+
"node": ">=14"
32+
},
3033
"dependencies": {
31-
"crypto": "^1.0.1",
32-
"debug": "^4.3.3",
33-
"mysql2": "^2.3.3",
34+
"debug": "^4.3.4",
35+
"mysql2": "^3.2.0",
3436
"node-cache": "^5.1.2"
3537
}
3638
}

0 commit comments

Comments
 (0)