spice-js 2.5.38 → 2.6.0

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,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,27 @@ 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
+
368
389
  async get(args) {
369
390
  try {
370
391
  if (!args) {
@@ -372,7 +393,33 @@ export default class SpiceModel {
372
393
  }
373
394
  if (_.isString(args.id)) {
374
395
  await this.run_hook(args, "get", "before");
375
- let results = await this.database.get(args.id);
396
+ let key = `${this.type}::${args.id}`;
397
+ let results = {};
398
+ if (this.shouldCache(this.type)) {
399
+ let cached_results = await this.getCacheProviderObject(this.type).get(
400
+ key
401
+ );
402
+ if (cached_results) {
403
+ //calculate time taken to get data from Cache
404
+ results = cached_results;
405
+
406
+ /* if (this.type == "user") {
407
+ console.log("results from Cache", results);
408
+ } */
409
+ } else {
410
+ results = await this.database.get(args.id);
411
+ /* if (this.type == "user") {
412
+ console.log("results from DB", results);
413
+ } */
414
+ await this.getCacheProviderObject(this.type).set(
415
+ key,
416
+ results,
417
+ this.getCacheConfig(this.type)
418
+ );
419
+ }
420
+ } else {
421
+ results = await this.database.get(args.id);
422
+ }
376
423
 
377
424
  if (results.type != undefined) {
378
425
  if (results.type != this.type)
@@ -420,9 +467,26 @@ export default class SpiceModel {
420
467
 
421
468
  await this.run_hook(this, "list", "before");
422
469
  _.remove(args.ids, (o) => o == undefined);
470
+ let key = `${this.type}::${_.join(args.ids, "|")}`;
423
471
  let results = [];
424
472
  if (args.ids.length > 0) {
425
- results = await this.database.multi_get(args.ids, true);
473
+ if (this.shouldCache(this.type)) {
474
+ let cached_results = await this.getCacheProviderObject(this.type).get(
475
+ key
476
+ );
477
+ if (cached_results) {
478
+ results = cached_results;
479
+ } else {
480
+ results = await this.database.multi_get(args.ids, true);
481
+ this.getCacheProviderObject(this.type).set(
482
+ key,
483
+ results,
484
+ this.getCacheConfig(this.type)
485
+ );
486
+ }
487
+ } else {
488
+ results = await this.database.multi_get(args.ids, true);
489
+ }
426
490
  }
427
491
  _.remove(results, (o) => o.type != this.type);
428
492
 
@@ -495,7 +559,7 @@ export default class SpiceModel {
495
559
  form = await this.do_serialize(this, "write", cover_obj.new, args);
496
560
  }
497
561
  let db_data = form || this;
498
-
562
+
499
563
  await this.database.update(args.id, db_data);
500
564
  if (args.skip_hooks != true) {
501
565
  await this.run_hook(
@@ -612,7 +676,7 @@ export default class SpiceModel {
612
676
  query = args.query;
613
677
  } else {
614
678
  if (args.filters) {
615
- query = await this.makeQueryFromFilter(args.filters);
679
+ query = this.makeQueryFromFilter(args.filters);
616
680
  } else {
617
681
  if (args.query) {
618
682
  query =
@@ -635,28 +699,102 @@ export default class SpiceModel {
635
699
  }
636
700
 
637
701
  await this.run_hook(this, "list", "before");
702
+ function removeSpaceAndSpecialCharacters(str) {
703
+ return str.replace(/[^a-zA-Z0-9]/g, "");
704
+ }
705
+ let key = removeSpaceAndSpecialCharacters(
706
+ `${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}`
707
+ );
708
+
638
709
  let results;
639
710
  if (args.is_custom_query && args.is_custom_query === "true") {
640
- results = await this.database.query(query);
711
+ if (args.ids.length > 0) {
712
+ if (this.shouldCache(this.type)) {
713
+ let cached_results = await this.getCacheProviderObject(
714
+ this.type
715
+ ).get(key);
716
+ if (cached_results) {
717
+ results = cached_results;
718
+ } else {
719
+ results = await this.database.query(query);
720
+ this.getCacheProviderObject(this.type).set(
721
+ key,
722
+ results,
723
+ this.getCacheConfig(this.type)
724
+ );
725
+ }
726
+ } else {
727
+ results = await this.database.query(query);
728
+ }
729
+ }
641
730
  } else {
642
731
  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
- );
732
+ if (this.shouldCache(this.type)) {
733
+ let cached_results = await this.getCacheProviderObject(
734
+ this.type
735
+ ).get(key);
736
+ if (cached_results) {
737
+ results = cached_results;
738
+ } else {
739
+ results = await this.database.full_text_search(
740
+ this.type,
741
+ query || "",
742
+ args.limit,
743
+ args.offset
744
+ );
745
+ this.getCacheProviderObject(this.type).set(
746
+ key,
747
+ results,
748
+ this.getCacheConfig(this.type)
749
+ );
750
+ }
751
+ } else {
752
+ results = await this.database.full_text_search(
753
+ this.type,
754
+ query || "",
755
+ args.limit,
756
+ args.offset
757
+ );
758
+ }
649
759
  } 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
- );
760
+ if (this.shouldCache(this.type)) {
761
+ let cached_results = await this.getCacheProviderObject(
762
+ this.type
763
+ ).get(key);
764
+ if (cached_results) {
765
+ results = cached_results;
766
+ } else {
767
+ results = await this.database.search(
768
+ this.type,
769
+ args.columns || "",
770
+ query || "",
771
+ args.limit,
772
+ args.offset,
773
+ args.sort,
774
+ args.do_count,
775
+ args.statement_consistent
776
+ );
777
+ this.getCacheProviderObject(this.type).set(
778
+ key,
779
+ results,
780
+ this.getCacheConfig(this.type)
781
+ );
782
+ }
783
+ } else {
784
+ results = await this.database.search(
785
+ this.type,
786
+ args.columns || "",
787
+ query || "",
788
+ args.limit,
789
+ args.offset,
790
+ args.sort,
791
+ args.do_count,
792
+ args.statement_consistent
793
+ );
794
+ if (this.type == "user") {
795
+ console.log("results from DB No Chache available", results);
796
+ }
797
+ }
660
798
  }
661
799
  }
662
800
  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
- } */