Skip to content

Commit

Permalink
v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
2naive committed Mar 4, 2024
1 parent 4cf7739 commit ecc28b8
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 156 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x, 16.x, 18.x, 20.x, 22.x, 24.x]

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
# - run: npm run coverage
33 changes: 26 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

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

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

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


### Debugging easy
## Debugging easy

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

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

## API

You may use all [MySQL2](https://github.com/sidorares/node-mysql2) methods plus:

### async q(sql, params = [], cache = false, ttl = undefined)

### async insert(table, row)

### async update(table, row, where = false)

### async delete(table, row, where = false)

### stat()

### cacheFlush(sql, params)

### cacheFlushAll()

### cacheStat()

## Getting help

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!
Expand Down
38 changes: 28 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports.connect = (config = {}) => {
pool.q = async (sql, params = [], cache = false, ttl = undefined) => {
qid++
const log = debug.extend(qid)
log(sql, params)
log(sql, params, {cache: cache, ttl: ttl ? ttl : DEFAULT_CACHE_TTL})
// https://medium.com/@chris_72272/what-is-the-fastest-node-js-hashing-algorithm-c15c1a0e164e
const hash = crypto.createHash('sha1').update(sql + JSON.stringify(params)).digest('base64')
if (cache && queryCache.has(hash)) {
Expand Down Expand Up @@ -77,15 +77,7 @@ module.exports.connect = (config = {}) => {
return Array.isArray(rows) && rows.length ? rows[0] : false
}

pool.stat = () => {
return {
ALL: pool.pool._allConnections.toArray().length,
// USE: pool.pool._allConnections.toArray().length - pool.pool._freeConnections.toArray().length,
FRE: pool.pool._freeConnections.toArray().length,
QUE: pool.pool._connectionQueue.toArray().length
}
}

// @todo insert array of objects
pool.insert = pool.i = async (table, row) => {
qid++
const log = debug.extend(qid)
Expand Down Expand Up @@ -128,6 +120,32 @@ module.exports.connect = (config = {}) => {
return rows || false
}

pool.stat = () => {
return {
ALL: pool.pool._allConnections.toArray().length,
// USE: pool.pool._allConnections.toArray().length - pool.pool._freeConnections.toArray().length,
FRE: pool.pool._freeConnections.toArray().length,
QUE: pool.pool._connectionQueue.toArray().length
}
}

pool.cacheFlush = (sql, params) => {
const hash = crypto.createHash('sha1').update(sql + JSON.stringify(params)).digest('base64')
const deleted = queryCache.del(hash)
debug('Cache flush', sql, params, { deleted }, queryCache.getStats())
return deleted
}

pool.cacheFlushAll = () => {
queryCache.flushAll()
debug('Cache flush all', queryCache.getStats())
return true
}

exports.cacheStat = () => {
return queryCache.getStats()
}

pool.on('acquire', (connection) => {
debug('Connection #%s acquired', connection.threadId, pool.stat())
})
Expand Down
108 changes: 34 additions & 74 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "mysql2-cache",
"version": "1.0.1",
"version": "1.3.0",
"description": "✔ MySQL2 upgrade: cache queries, easy shortcuts, logging and debugging.",
"main": "index.js",
"scripts": {
"test": "DEBUG=* node ./test/test.js"
"test": "node ./test/test.js"
},
"repository": {
"type": "git",
Expand All @@ -27,10 +27,12 @@
"url": "https://github.com/2naive/node-mysql2-cache/issues"
},
"homepage": "https://github.com/2naive/node-mysql2-cache#readme",
"engines": {
"node": ">=14"
},
"dependencies": {
"crypto": "^1.0.1",
"debug": "^4.3.3",
"mysql2": "^2.3.3",
"debug": "^4.3.4",
"mysql2": "^3.2.0",
"node-cache": "^5.1.2"
}
}
Loading

0 comments on commit ecc28b8

Please sign in to comment.