jxp 2.13.0 → 2.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/caching.md CHANGED
@@ -6,6 +6,7 @@ Enable caching to speed up your queries. Caching is disabled by default. Enable
6
6
  {
7
7
  "cache": {
8
8
  "enabled": true,
9
+ "debug": false,
9
10
  "ttl": 3600
10
11
  }
11
12
  }
package/libs/cache.js CHANGED
@@ -3,45 +3,74 @@ const config = require('config');
3
3
  const cache = new NodeCache({ stdTTL: config.cache?.ttl || 5 * 60 });
4
4
 
5
5
  const generateKey = (req) => {
6
- const url = req.url
7
- return url
6
+ let key = `${req.modelname}/`;
7
+ if (req.params.item_id) {
8
+ key += `${req.params.item_id}`
9
+ }
10
+ if (req.query) {
11
+ key += `?${JSON.stringify(req.query)}`
12
+ }
13
+ return key;
8
14
  }
9
15
 
10
- const set = (req, res, next) => {
11
- if (!config.cache?.enabled) return next();
16
+ const set = async (req, res) => {
17
+ if (!config.cache?.enabled) return;
12
18
  const key = generateKey(req)
13
19
  cache.set(key, res.result)
14
- next()
20
+ if (config.cache?.debug) {
21
+ console.log('cache set', key)
22
+ }
15
23
  }
16
24
 
17
- const get = (req, res, next) => {
18
- if (!config.cache?.enabled) return next();
25
+ const get = async (req, res) => {
26
+ if (!config.cache?.enabled) return;
19
27
  const key = generateKey(req)
28
+ res.header('jxp-cache-key', key);
20
29
  const cached = cache.get(key)
21
30
  if (cached) {
31
+ if (config.cache?.debug) {
32
+ console.log('cache hit', key)
33
+ }
22
34
  res.header('jxp-cache', 'hit')
23
35
  res.result = cached
24
36
  return res.send(res.result)
25
37
  }
38
+ if (config.cache?.debug) {
39
+ console.log('cache miss', key)
40
+ }
26
41
  res.header('jxp-cache', 'miss')
27
- next()
28
42
  }
29
43
 
30
- const clear = (req, res, next) => {
31
- if (!config.cache?.enabled) return next();
44
+ const clear = async (req, res) => {
45
+ if (!config.cache?.enabled) return;
46
+ const keys = cache.keys()
47
+ keys.forEach(key => {
48
+ if (key.startsWith(`${req.modelname}/`)) {
49
+ if (config.cache?.debug) {
50
+ console.log('cache del', key)
51
+ }
52
+ cache.del(key)
53
+ }
54
+ })
55
+ }
56
+
57
+ const clearAll = async () => {
58
+ if (!config.cache?.enabled) return;
59
+ if (config.cache?.debug) {
60
+ console.log('cache flushAll')
61
+ }
32
62
  cache.flushAll()
33
- next()
34
63
  }
35
64
 
36
- const stats = (req, res, next) => {
65
+ const stats = async (req, res) => {
37
66
  if (!config.cache?.enabled) return {};
38
67
  res.result = cache.getStats()
39
- next()
40
68
  }
41
69
 
42
70
  module.exports = {
43
71
  set,
44
72
  get,
45
73
  clear,
46
- stats
74
+ clearAll,
75
+ stats,
47
76
  }
package/libs/jxp.js CHANGED
@@ -54,10 +54,9 @@ const middlewareCheckAdmin = (req, res, next) => {
54
54
  };
55
55
 
56
56
  // Outputs whatever is in res.result as JSON
57
- const outputJSON = (req, res, next) => {
57
+ const outputJSON = async (req, res) => {
58
58
  try {
59
59
  res.send(res.result);
60
- next();
61
60
  } catch (err) {
62
61
  console.error(new Date(), err);
63
62
  throw new errors.InternalServerError(err.toString());
@@ -919,7 +918,6 @@ const JXP = function(options) {
919
918
  middlewareModel,
920
919
  security.login,
921
920
  security.auth,
922
- cache.get,
923
921
  config.pre_hooks.getOne,
924
922
  cache.get,
925
923
  actionGetOne,
@@ -949,6 +947,9 @@ const JXP = function(options) {
949
947
  config.pre_hooks.put,
950
948
  actionPut,
951
949
  cache.clear,
950
+ (req, res, next) => {
951
+ next();
952
+ },
952
953
  );
953
954
  server.del(
954
955
  "/api/:modelname/:item_id",
@@ -967,9 +968,9 @@ const JXP = function(options) {
967
968
  security.login,
968
969
  security.auth,
969
970
  config.pre_hooks.get,
970
- cache.get,
971
+ // cache.get,
971
972
  actionCount,
972
- cache.set,
973
+ // cache.set,
973
974
  outputJSON
974
975
  );
975
976
 
package/mkdocs.yml CHANGED
@@ -11,6 +11,7 @@ nav:
11
11
  - Aggregations: aggregations.md
12
12
  - Bulk Writes: bulk_writes.md
13
13
  - Queries: queries.md
14
+ - Caching: caching.md
14
15
  - Websocket: websocket.md
15
16
  - Special Features: special.md
16
17
  - Changelog: changelog.md
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "jxp",
3
3
  "description:": "An opinionated RESTful API library based on Mongoose and Restify. Make an API by just writing Mongoose models.",
4
- "version": "2.13.0",
4
+ "version": "2.13.1",
5
5
  "private": false,
6
6
  "main": "libs/jxp.js",
7
7
  "scripts": {
package/test/init.js CHANGED
@@ -8,6 +8,7 @@ global.JXPSchema = require("../libs/schema");
8
8
  const User = require(path.join(model_dir, "user_model"));
9
9
  const Apikey = require(path.join(model_dir, "apikey_model"));
10
10
  const Test = require(path.join(model_dir, "test_model"));
11
+ const cache = require("../libs/cache");
11
12
 
12
13
  const security = require("../libs/security");
13
14
 
@@ -38,6 +39,7 @@ const admin_password = "SecretPassword";
38
39
 
39
40
  const init = async () => {
40
41
  try {
42
+ await cache.clearAll();
41
43
  await empty_user_collections();
42
44
  await post(User, { name: "Admin User", email: admin_email, password: security.encPassword(admin_password), urlid: "admin-user", admin: true });
43
45
  await post(User, { name: "Test User", email, password: security.encPassword(password), urlid: "test-user" });
package/test/test.js CHANGED
@@ -1173,11 +1173,10 @@ describe('Test', () => {
1173
1173
  chai.request(server)
1174
1174
  .get("/cache/stats")
1175
1175
  .end((err, res) => {
1176
- // console.log(res.body);
1177
1176
  res.should.have.status(200);
1178
1177
  res.body.should.have.property("hits");
1179
- res.body.hits.should.equal(1);
1180
- res.body.misses.should.equal(1);
1178
+ res.body.hits.should.be.greaterThan(0);
1179
+ res.body.misses.should.be.greaterThan(0);
1181
1180
  done();
1182
1181
  });
1183
1182
  })