jxp 2.13.1 → 2.13.2

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/libs/cache.js CHANGED
@@ -2,7 +2,8 @@ const NodeCache = require('node-cache');
2
2
  const config = require('config');
3
3
  const cache = new NodeCache({ stdTTL: config.cache?.ttl || 5 * 60 });
4
4
 
5
- const generateKey = (req) => {
5
+ const generateKey = (req, res) => {
6
+ if (res.jxp_cache_key) return res.jxp_cache_key;
6
7
  let key = `${req.modelname}/`;
7
8
  if (req.params.item_id) {
8
9
  key += `${req.params.item_id}`
@@ -10,21 +11,22 @@ const generateKey = (req) => {
10
11
  if (req.query) {
11
12
  key += `?${JSON.stringify(req.query)}`
12
13
  }
14
+ res.jxp_cache_key = key;
13
15
  return key;
14
16
  }
15
17
 
16
18
  const set = async (req, res) => {
17
19
  if (!config.cache?.enabled) return;
18
- const key = generateKey(req)
20
+ const key = generateKey(req, res)
19
21
  cache.set(key, res.result)
20
22
  if (config.cache?.debug) {
21
23
  console.log('cache set', key)
22
24
  }
23
25
  }
24
26
 
25
- const get = async (req, res) => {
27
+ const get = (req, res, next) => {
26
28
  if (!config.cache?.enabled) return;
27
- const key = generateKey(req)
29
+ const key = generateKey(req, res)
28
30
  res.header('jxp-cache-key', key);
29
31
  const cached = cache.get(key)
30
32
  if (cached) {
@@ -39,6 +41,7 @@ const get = async (req, res) => {
39
41
  console.log('cache miss', key)
40
42
  }
41
43
  res.header('jxp-cache', 'miss')
44
+ next()
42
45
  }
43
46
 
44
47
  const clear = async (req, res) => {
@@ -63,7 +66,10 @@ const clearAll = async () => {
63
66
  }
64
67
 
65
68
  const stats = async (req, res) => {
66
- if (!config.cache?.enabled) return {};
69
+ if (!config.cache?.enabled) {
70
+ res.result = { cache_enabled: false }
71
+ return;
72
+ }
67
73
  res.result = cache.getStats()
68
74
  }
69
75
 
package/libs/jxp.js CHANGED
@@ -932,7 +932,7 @@ const JXP = function(options) {
932
932
  middlewarePasswords,
933
933
  config.pre_hooks.post,
934
934
  actionPost,
935
- cache.clear,
935
+ cache.clearAll,
936
936
  (req, res, next) => {
937
937
  next();
938
938
  },
@@ -946,7 +946,7 @@ const JXP = function(options) {
946
946
  middlewareCheckAdmin,
947
947
  config.pre_hooks.put,
948
948
  actionPut,
949
- cache.clear,
949
+ cache.clearAll,
950
950
  (req, res, next) => {
951
951
  next();
952
952
  },
@@ -958,7 +958,7 @@ const JXP = function(options) {
958
958
  security.auth,
959
959
  config.pre_hooks.delete,
960
960
  actionDelete,
961
- cache.clear,
961
+ cache.clearAll,
962
962
  );
963
963
 
964
964
  // Count
@@ -1022,7 +1022,7 @@ const JXP = function(options) {
1022
1022
  middlewareCheckAdmin,
1023
1023
  config.pre_hooks.update,
1024
1024
  actionUpdate,
1025
- cache.clear,
1025
+ cache.clearAll,
1026
1026
  );
1027
1027
 
1028
1028
  /* Batch routes - ROLLED BACK FOR NOW */
@@ -1035,7 +1035,7 @@ const JXP = function(options) {
1035
1035
  security.login,
1036
1036
  security.auth,
1037
1037
  actionCall,
1038
- cache.clear,
1038
+ cache.clearAll,
1039
1039
  );
1040
1040
  server.post(
1041
1041
  "/call/:modelname/:method_name",
@@ -1043,7 +1043,7 @@ const JXP = function(options) {
1043
1043
  security.login,
1044
1044
  security.auth,
1045
1045
  actionCall,
1046
- cache.clear,
1046
+ cache.clearAll,
1047
1047
  );
1048
1048
  server.get(
1049
1049
  "/call/:modelname/:item_id/:method_name",
@@ -1051,7 +1051,7 @@ const JXP = function(options) {
1051
1051
  security.login,
1052
1052
  security.auth,
1053
1053
  actionCallItem,
1054
- cache.clear,
1054
+ cache.clearAll,
1055
1055
  );
1056
1056
 
1057
1057
  /* Login and authentication */
@@ -1101,7 +1101,7 @@ const JXP = function(options) {
1101
1101
 
1102
1102
  /* Cache */
1103
1103
  server.get("/cache/stats", cache.stats, outputJSON);
1104
- server.get("/cache/clear", cache.clear, outputJSON);
1104
+ server.get("/cache/clear", cache.clearAll, outputJSON);
1105
1105
 
1106
1106
  return server;
1107
1107
  };
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.1",
4
+ "version": "2.13.2",
5
5
  "private": false,
6
6
  "main": "libs/jxp.js",
7
7
  "scripts": {
package/test/test.js CHANGED
@@ -1142,7 +1142,7 @@ describe('Test', () => {
1142
1142
  chai.request(server)
1143
1143
  .get("/cache/clear")
1144
1144
  .end((err, res) => {
1145
- console.log(res.body);
1145
+ // console.log(res.body);
1146
1146
  res.should.have.status(200);
1147
1147
  done();
1148
1148
  });
@@ -1181,4 +1181,116 @@ describe('Test', () => {
1181
1181
  });
1182
1182
  })
1183
1183
  });
1184
+ describe("Cache invalidating", () => {
1185
+ let test_id;
1186
+ it("should get a test record", done => {
1187
+ chai.request(server)
1188
+ .get(`/api/test?populate=link&limit=1`)
1189
+ .auth(init.email, init.password)
1190
+ .end((err, res) => {
1191
+ // console.log(res.headers)
1192
+ res.should.have.status(200);
1193
+ res.body.data[0].should.have.property("_id");
1194
+ test_id = res.body.data[0]._id;
1195
+ res.body.data[0].should.have.property("link");
1196
+ should.equal(res.body.data[0].link, null);
1197
+ res.headers["jxp-cache"].should.equal("miss");
1198
+ done();
1199
+ });
1200
+ });
1201
+ it("should get a cached test record", done => {
1202
+ chai.request(server)
1203
+ .get(`/api/test?populate=link&limit=1`)
1204
+ .auth(init.email, init.password)
1205
+ .end((err, res) => {
1206
+ // console.log(res.headers)
1207
+ res.should.have.status(200);
1208
+ res.body.data[0].should.have.property("_id");
1209
+ res.headers["jxp-cache"].should.equal("hit");
1210
+ should.equal(res.body.data[0].link, null);
1211
+ done();
1212
+ });
1213
+ });
1214
+ let link_id;
1215
+ it("should add a link record", done => {
1216
+ chai.request(server)
1217
+ .post(`/api/link`)
1218
+ .auth(init.email, init.password)
1219
+ .send({
1220
+ name: "cache_test",
1221
+ val: "YoYoYo"
1222
+ })
1223
+ .end((err, res) => {
1224
+ // console.log(res.body)
1225
+ res.should.have.status(200);
1226
+ res.body.data.should.have.property("_id");
1227
+ link_id = res.body.data._id;
1228
+ done();
1229
+ });
1230
+ });
1231
+ it("should put link_id into test record", done => {
1232
+ chai.request(server)
1233
+ .put(`/api/test/${test_id}`)
1234
+ .auth(init.admin_email, init.admin_password)
1235
+ .send({
1236
+ link_id
1237
+ })
1238
+ .end((err, res) => {
1239
+ // console.log(res.body)
1240
+ res.should.have.status(200);
1241
+ res.body.data.should.have.property("_id");
1242
+ res.body.data.link_id.should.equal(link_id);
1243
+ done();
1244
+ });
1245
+ });
1246
+ it("should get a test record with link", done => {
1247
+ chai.request(server)
1248
+ .get(`/api/test?populate=link&limit=1`)
1249
+ .auth(init.email, init.password)
1250
+ .end((err, res) => {
1251
+ // console.log(res.body)
1252
+ res.should.have.status(200);
1253
+ res.body.data[0].should.have.property("_id");
1254
+ res.body.data[0].should.have.property("link");
1255
+ res.body.data[0].link.should.not.equal(null);
1256
+ res.body.data[0].link._id.should.equal(link_id);
1257
+ res.body.data[0].link.name.should.equal("cache_test");
1258
+ res.body.data[0].link.val.should.equal("YoYoYo");
1259
+ res.headers["jxp-cache"].should.equal("miss");
1260
+ done();
1261
+ });
1262
+ });
1263
+ it("should update link record", done => {
1264
+ chai.request(server)
1265
+ .put(`/api/link/${link_id}`)
1266
+ .auth(init.admin_email, init.admin_password)
1267
+ .send({
1268
+ val: "YoYoYo2"
1269
+ })
1270
+ .end((err, res) => {
1271
+ // console.log(res.body)
1272
+ res.should.have.status(200);
1273
+ res.body.data.should.have.property("_id");
1274
+ res.body.data.val.should.equal("YoYoYo2");
1275
+ done();
1276
+ });
1277
+ });
1278
+ it("should get a test record with updated link", done => {
1279
+ chai.request(server)
1280
+ .get(`/api/test?populate=link&limit=1`)
1281
+ .auth(init.email, init.password)
1282
+ .end((err, res) => {
1283
+ // console.log(res.body.data[0].link)
1284
+ res.should.have.status(200);
1285
+ res.body.data[0].should.have.property("_id");
1286
+ res.body.data[0].should.have.property("link");
1287
+ res.body.data[0].link.should.not.equal(null);
1288
+ res.body.data[0].link._id.should.equal(link_id);
1289
+ res.body.data[0].link.name.should.equal("cache_test");
1290
+ res.body.data[0].link.val.should.equal("YoYoYo2");
1291
+ res.headers["jxp-cache"].should.equal("miss");
1292
+ done();
1293
+ });
1294
+ });
1295
+ })
1184
1296
  });