spice-js 2.5.38 → 2.6.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.
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ async function connect() {
4
+ try {
5
+ spice.cache_providers = {};
6
+ spice.monitor = {};
7
+ for (let key of Object.keys(spice.config.cache.providers)) {
8
+ spice.cache_providers[key] = new spice.config.cache.providers[key](
9
+ spice.config.cache.drivers[key] || {}
10
+ );
11
+ await spice.cache_providers[key].initialize();
12
+ }
13
+ } catch (e) {
14
+ console.log(e.stack);
15
+ throw e;
16
+ }
17
+ }
18
+ module.exports = connect;
@@ -1,7 +1,7 @@
1
1
  import path from "path";
2
2
  import _ from "lodash";
3
3
  try {
4
- let resources = ["models", "controllers", "schemas"];
4
+ let resources = ["models", "controllers", "schemas", "cache"];
5
5
  _.each(resources, async function (resource) {
6
6
  let paths = path.join(spice.root_path, resource);
7
7
  spice[resource] = {};
@@ -16,7 +16,7 @@ try {
16
16
  if (resource == "models") {
17
17
  new spice[resource][file_name]({});
18
18
  }
19
- }
19
+ }
20
20
  });
21
21
  } catch (error) {
22
22
  console.log("Error Happened", error);
File without changes
@@ -0,0 +1,30 @@
1
+ const NodeCache = require("node-cache");
2
+ module.exports = class SpiceCache {
3
+ constructor(args = {}) {
4
+ this.options = args;
5
+ this.client = null;
6
+ }
7
+ async initialize(options = {}) {
8
+ this.options = { ...this.options, ...options };
9
+ this.client = new NodeCache();
10
+ }
11
+
12
+ async set(key, value, options = {}) {
13
+ let working_options = { ...this.options, ...options };
14
+ const { ttl, namespace = "" } = working_options;
15
+ if (value === undefined) return console.log("Value is undefined");
16
+ this.client.set(namespace + key, value, ttl || 60);
17
+ }
18
+
19
+ async get(key, options = {}) {
20
+ try {
21
+ return this.client.get(key);
22
+ } catch (e) {
23
+ return null;
24
+ }
25
+ }
26
+
27
+ async exists(key) {
28
+ return await this.client.exists(key);
29
+ }
30
+ };
@@ -0,0 +1,3 @@
1
+ module.exports = {
2
+
3
+ }
package/src/index.js CHANGED
@@ -35,6 +35,7 @@ export { default as LocalStorage } from "./storage/providers/Local";
35
35
  export { default as Storage } from "./storage/Storage";
36
36
 
37
37
  export { default as MailFile } from "./mail/providers/File";
38
+ export { default as SpiceCache } from "./cache/providers/SpiceCache";
38
39
  export { default as MapType } from "./utility/MapType";
39
40
  export { default as addTask } from "./bootstrap/tasks";
40
41
  const convert = require("koa-convert");
@@ -365,6 +365,44 @@ export default class SpiceModel {
365
365
  return return_string;
366
366
  }
367
367
 
368
+ shouldCache(resource_type) {
369
+ return spice.cache[resource_type] != undefined;
370
+ }
371
+
372
+ getCacheConfig(resource_type) {
373
+ return spice.cache[resource_type] || {};
374
+ }
375
+
376
+ getCacheProviderObject(resource_type) {
377
+ return spice.cache_providers[
378
+ this.getCacheConfig(resource_type).driver ||
379
+ spice.config.cache.default_driver
380
+ ];
381
+ }
382
+
383
+ async exists(item_type, key) {
384
+ let obj = this.getCacheProviderObject(item_type);
385
+ if (obj) return await obj.exists(key);
386
+ return false;
387
+ }
388
+
389
+ shouldForceRefresh(response) {
390
+ let monitor_record = _.get(spice.monitor, this.type);
391
+ if (monitor_record == undefined) {
392
+ return false;
393
+ }
394
+ if (monitor_record > response?.time) {
395
+ return true;
396
+ }
397
+ return false;
398
+ }
399
+
400
+ setMonitor() {
401
+ let current_time = new Date().getTime();
402
+ spice.monitor[this.type] = current_time;
403
+ //console.log("Monitor Set", spice.monitor);
404
+ }
405
+
368
406
  async get(args) {
369
407
  try {
370
408
  if (!args) {
@@ -372,7 +410,27 @@ export default class SpiceModel {
372
410
  }
373
411
  if (_.isString(args.id)) {
374
412
  await this.run_hook(args, "get", "before");
375
- let results = await this.database.get(args.id);
413
+ let key = `${this.type}::${args.id}`;
414
+ let results = {};
415
+ if (this.shouldCache(this.type)) {
416
+ let cached_results = await this.getCacheProviderObject(this.type).get(
417
+ key
418
+ );
419
+ results = cached_results?.value;
420
+ if (
421
+ cached_results?.value == undefined ||
422
+ this.shouldForceRefresh(cached_results)
423
+ ) {
424
+ results = await this.database.get(args.id);
425
+ await this.getCacheProviderObject(this.type).set(
426
+ key,
427
+ { value: results, time: new Date().getTime() },
428
+ this.getCacheConfig(this.type)
429
+ );
430
+ }
431
+ } else {
432
+ results = await this.database.get(args.id);
433
+ }
376
434
 
377
435
  if (results.type != undefined) {
378
436
  if (results.type != this.type)
@@ -420,9 +478,28 @@ export default class SpiceModel {
420
478
 
421
479
  await this.run_hook(this, "list", "before");
422
480
  _.remove(args.ids, (o) => o == undefined);
481
+ let key = `${this.type}::${_.join(args.ids, "|")}`;
423
482
  let results = [];
424
483
  if (args.ids.length > 0) {
425
- results = await this.database.multi_get(args.ids, true);
484
+ if (this.shouldCache(this.type)) {
485
+ let cached_results = await this.getCacheProviderObject(this.type).get(
486
+ key
487
+ );
488
+ results = cached_results?.value;
489
+ if (
490
+ cached_results?.value == undefined ||
491
+ this.shouldForceRefresh(cached_results)
492
+ ) {
493
+ results = await this.database.multi_get(args.ids, true);
494
+ this.getCacheProviderObject(this.type).set(
495
+ key,
496
+ { value: results, time: new Date().getTime() },
497
+ this.getCacheConfig(this.type)
498
+ );
499
+ }
500
+ } else {
501
+ results = await this.database.multi_get(args.ids, true);
502
+ }
426
503
  }
427
504
  _.remove(results, (o) => o.type != this.type);
428
505
 
@@ -495,8 +572,10 @@ export default class SpiceModel {
495
572
  form = await this.do_serialize(this, "write", cover_obj.new, args);
496
573
  }
497
574
  let db_data = form || this;
498
-
575
+
499
576
  await this.database.update(args.id, db_data);
577
+ this.setMonitor();
578
+
500
579
  if (args.skip_hooks != true) {
501
580
  await this.run_hook(
502
581
  {
@@ -546,6 +625,7 @@ export default class SpiceModel {
546
625
  workingForm = await this.do_serialize(workingForm, "write", {}, args);
547
626
 
548
627
  let results = await this.database.insert(id, workingForm, args.expiry);
628
+ this.setMonitor();
549
629
  await this.run_hook(
550
630
  {
551
631
  ...results,
@@ -580,6 +660,7 @@ export default class SpiceModel {
580
660
  let delete_response = {};
581
661
  if (args.hard) {
582
662
  delete_response = await this.database.delete(args.id);
663
+ this.setMonitor();
583
664
  } else {
584
665
  delete results["id"];
585
666
  results.deleted = true;
@@ -612,7 +693,7 @@ export default class SpiceModel {
612
693
  query = args.query;
613
694
  } else {
614
695
  if (args.filters) {
615
- query = await this.makeQueryFromFilter(args.filters);
696
+ query = this.makeQueryFromFilter(args.filters);
616
697
  } else {
617
698
  if (args.query) {
618
699
  query =
@@ -635,28 +716,108 @@ export default class SpiceModel {
635
716
  }
636
717
 
637
718
  await this.run_hook(this, "list", "before");
719
+ function removeSpaceAndSpecialCharacters(str) {
720
+ return str.replace(/[^a-zA-Z0-9]/g, "");
721
+ }
722
+ let key = removeSpaceAndSpecialCharacters(
723
+ `${this.type}::${query}::${args.limit}::${args.offset}::${args.sort}::${args.do_count}::${args.statement_consistent}::${args.columns}::${args.is_full_text}::${args.is_custom_query}`
724
+ );
725
+
638
726
  let results;
639
727
  if (args.is_custom_query && args.is_custom_query === "true") {
640
- results = await this.database.query(query);
728
+ if (args.ids.length > 0) {
729
+ if (this.shouldCache(this.type)) {
730
+ let cached_results = await this.getCacheProviderObject(
731
+ this.type
732
+ ).get(key);
733
+ results = cached_results?.value;
734
+ if (
735
+ cached_results?.value == undefined ||
736
+ this.shouldForceRefresh(cached_results)
737
+ ) {
738
+ results = await this.database.query(query);
739
+ this.getCacheProviderObject(this.type).set(
740
+ key,
741
+ { value: results, time: new Date().getTime() },
742
+ this.getCacheConfig(this.type)
743
+ );
744
+ }
745
+ } else {
746
+ results = await this.database.query(query);
747
+ }
748
+ }
641
749
  } else {
642
750
  if (args.is_full_text && args.is_full_text === "true") {
643
- results = await this.database.full_text_search(
644
- this.type,
645
- query || "",
646
- args.limit,
647
- args.offset
648
- );
751
+ if (this.shouldCache(this.type)) {
752
+ let cached_results = await this.getCacheProviderObject(
753
+ this.type
754
+ ).get(key);
755
+ results = cached_results?.value;
756
+ if (
757
+ cached_results?.value == undefined ||
758
+ this.shouldForceRefresh(cached_results)
759
+ ) {
760
+ results = await this.database.full_text_search(
761
+ this.type,
762
+ query || "",
763
+ args.limit,
764
+ args.offset
765
+ );
766
+ this.getCacheProviderObject(this.type).set(
767
+ key,
768
+ { value: results, time: new Date().getTime() },
769
+ this.getCacheConfig(this.type)
770
+ );
771
+ }
772
+ } else {
773
+ results = await this.database.full_text_search(
774
+ this.type,
775
+ query || "",
776
+ args.limit,
777
+ args.offset
778
+ );
779
+ }
649
780
  } else {
650
- results = await this.database.search(
651
- this.type,
652
- args.columns || "",
653
- query || "",
654
- args.limit,
655
- args.offset,
656
- args.sort,
657
- args.do_count,
658
- args.statement_consistent
659
- );
781
+ if (this.shouldCache(this.type)) {
782
+ let cached_results = await this.getCacheProviderObject(
783
+ this.type
784
+ ).get(key);
785
+ results = cached_results?.value;
786
+ if (
787
+ cached_results?.value == undefined ||
788
+ this.shouldForceRefresh(cached_results)
789
+ ) {
790
+ results = await this.database.search(
791
+ this.type,
792
+ args.columns || "",
793
+ query || "",
794
+ args.limit,
795
+ args.offset,
796
+ args.sort,
797
+ args.do_count,
798
+ args.statement_consistent
799
+ );
800
+ this.getCacheProviderObject(this.type).set(
801
+ key,
802
+ { value: results, time: new Date().getTime() },
803
+ this.getCacheConfig(this.type)
804
+ );
805
+ }
806
+ } else {
807
+ results = await this.database.search(
808
+ this.type,
809
+ args.columns || "",
810
+ query || "",
811
+ args.limit,
812
+ args.offset,
813
+ args.sort,
814
+ args.do_count,
815
+ args.statement_consistent
816
+ );
817
+ if (this.type == "user") {
818
+ console.log("results from DB No Chache available", results);
819
+ }
820
+ }
660
821
  }
661
822
  }
662
823
  try {
@@ -1,36 +0,0 @@
1
- "use strict";
2
-
3
- var _path = _interopRequireDefault(require("path"));
4
-
5
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
-
7
- /**
8
- * Created by chadfraser on 25/02/2017.
9
- */
10
- module.exports = function () {
11
- return new Promise(function (resolve, reject) {
12
- try {
13
- var locations = [spice.module_root_path + '/tasks', spice.root_path + '/tasks'];
14
-
15
- var _loop = function _loop(i) {
16
- var files = require('fs').readdirSync(i);
17
-
18
- files.forEach(function (file) {
19
- file.split('.')[0];
20
-
21
- require(_path.default.join(i, file));
22
-
23
- console.log('Loading Task', _path.default.join(i, file));
24
- });
25
- };
26
-
27
- for (var i of locations) {
28
- _loop(i);
29
- }
30
-
31
- resolve({});
32
- } catch (e) {
33
- reject(e);
34
- }
35
- });
36
- };
@@ -1,24 +0,0 @@
1
- /**
2
- * Created by chadfraser on 09/12/15.
3
- */
4
-
5
- /*
6
- var _ = require('underscore');
7
- var Date = require('sonover-date');
8
- var Event = function(stage, data)
9
- {
10
-
11
- this.stage = stage;
12
- this.data = data;
13
- this.time = new Date().now();
14
-
15
- if(_.isUndefined(this.stage)){
16
- this.stage = "";
17
- }
18
- if(_.isUndefined(this.data)){
19
- this.data = null;
20
- }
21
- }
22
-
23
- module.exports = Event;*/
24
- "use strict";
@@ -1,12 +0,0 @@
1
- /*import { inherits } from 'util';
2
- var EventRegistry;
3
- var EventRegistryInstance;
4
-
5
- EventRegistry = function () {};
6
- inherits(EventRegistry, EventEmitter);
7
-
8
- EventRegistryInstance = new EventRegistry();
9
- EventRegistryInstance.(1000);
10
-
11
- export default EventRegistryInstance;*/
12
- "use strict";
@@ -1,23 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- class SpiceEventDriver {
9
- dispatch() {
10
- var {
11
- topic,
12
- data
13
- } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
14
- spice.EventEmitter.emit(topic, data);
15
- }
16
-
17
- on(topic, callback) {
18
- spice.EventEmitter.on(topic, callback);
19
- }
20
-
21
- }
22
-
23
- exports.default = SpiceEventDriver;
@@ -1,53 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
9
-
10
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
11
-
12
- var fs = require('fs-extra');
13
-
14
- var path = require('path');
15
-
16
- var open = require("open");
17
-
18
- class File {
19
- constructor() {
20
- var args = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
21
- this.sender = args.sender;
22
- this.subject = args.subject;
23
- this.htmlText = args.htmlText;
24
- this.dest = path.join(path.resolve('./'), 'storage/mail');
25
-
26
- if (args.path) {
27
- this.dest = path.resolve(args.path);
28
- }
29
- }
30
-
31
- send(htmlText) {
32
- var _arguments = arguments,
33
- _this = this;
34
-
35
- return _asyncToGenerator(function* () {
36
- var options = _arguments.length > 1 && _arguments[1] !== undefined ? _arguments[1] : {};
37
- var email = {
38
- to: options.to,
39
- from: options.sender || _this.sender,
40
- subject: options.subject || _this.subject,
41
- htmlText: htmlText || _this.htmlText
42
- };
43
- var time = new Date().getTime();
44
- fs.ensureDirSync("".concat(_this.dest, "/").concat(email.subject, "/").concat(time));
45
- var full_path = "".concat(_this.dest, "/").concat(email.subject, "/").concat(time, "/to:").concat(email.to, " from:").concat(email.from, ".html");
46
- fs.writeFileSync(full_path, email.htmlText);
47
- open(full_path);
48
- })();
49
- }
50
-
51
- }
52
-
53
- exports.default = File;
@@ -1,28 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports["default"] = void 0;
7
-
8
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
9
-
10
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
11
-
12
- var DataType = function DataType() {
13
- _classCallCheck(this, DataType);
14
- };
15
-
16
- exports["default"] = DataType;
17
-
18
- _defineProperty(DataType, "ARRAY", "array");
19
-
20
- _defineProperty(DataType, "BOOLEAN", "boolean");
21
-
22
- _defineProperty(DataType, "OBJECT", "object");
23
-
24
- _defineProperty(DataType, "STRING", "string");
25
-
26
- _defineProperty(DataType, "NUMBER", "number");
27
-
28
- _defineProperty(DataType, "DATE", "date");
@@ -1,44 +0,0 @@
1
- "use strict";
2
-
3
- var _UserGroup = _interopRequireDefault(require("../models/UserGroup"));
4
-
5
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
-
7
- var Rules = spice.classes.validate.Rules;
8
-
9
- Rules.prototype.shouldExist = function* (field, value, args, message) {
10
- try {
11
- var loaded = require("../models/".concat(args));
12
-
13
- var Class = loaded.default;
14
- var obj = new Class();
15
- var found = yield obj.exist(value);
16
-
17
- if (found) {
18
- return true;
19
- }
20
- } catch (e) {
21
- this.validator.addError(field, 'rule', field + 'ShouldExist', 'Cannot Validate ' + args);
22
- }
23
-
24
- return false;
25
- };
26
-
27
- Rules.prototype.userGroupIsUnique = function* (field, value, message) {
28
- try {
29
- var group = new _UserGroup.default();
30
- var group_found = yield group.get({
31
- id: value
32
- });
33
- console.log(group_found);
34
- this.validator.addError(field, 'rule', 'usergroupIsUnique', message || 'This usergroup has already been created');
35
- return false;
36
- } catch (e) {
37
- if (e.message == 'user_group does not exist') {
38
- return true;
39
- } else {
40
- this.validator.addError(field, 'rule', 'userGroupIsUnique', 'Cannot Validate usergroup');
41
- return false;
42
- }
43
- }
44
- };
@@ -1,17 +0,0 @@
1
- "use strict";
2
- /* let Rules = spice.classes.validate.Rules;
3
-
4
- Rules.prototype.shouldExist = function* (field, value, args, message) {
5
- try {
6
- let loaded = require(spice.root_path + `/models/${args}`);
7
- let Class = loaded.default;
8
- let obj = new Class();
9
- let found = yield obj.exist(value);
10
- if (found) {
11
- return true;
12
- }
13
- } catch (e) {
14
- this.validator.addError(field, 'rule', field + 'ShouldExist', 'Cannot Validate ' + args);
15
- }
16
- return false;
17
- } */