spice-js 2.5.37 → 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.
@@ -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");
@@ -43,7 +43,6 @@ export default class SpiceModel {
43
43
  let Database = require(`spice-${dbtype}`);
44
44
  this.type = "";
45
45
  this.collection = args.connection;
46
- //console.log("Args -- Type::", args.type);
47
46
  this[_args] = args.args;
48
47
  this[_external_modifier_loaded] = false;
49
48
  this[_disable_lifecycle_events] =
@@ -160,19 +159,6 @@ export default class SpiceModel {
160
159
  }
161
160
  // }
162
161
  }
163
- /* get type() {
164
- return this._type;
165
- }
166
-
167
- // Setter for 'type'
168
- set type(value) {
169
- if (typeof value !== "string") {
170
- // example validation
171
- throw new Error("Type must be a string.");
172
- }
173
- this._type = value;
174
- //console.log("Type Setted::", value);
175
- } */
176
162
 
177
163
  get database() {
178
164
  return this[_database];
@@ -379,6 +365,27 @@ export default class SpiceModel {
379
365
  return return_string;
380
366
  }
381
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
+
382
389
  async get(args) {
383
390
  try {
384
391
  if (!args) {
@@ -386,7 +393,33 @@ export default class SpiceModel {
386
393
  }
387
394
  if (_.isString(args.id)) {
388
395
  await this.run_hook(args, "get", "before");
389
- 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
+ }
390
423
 
391
424
  if (results.type != undefined) {
392
425
  if (results.type != this.type)
@@ -434,9 +467,26 @@ export default class SpiceModel {
434
467
 
435
468
  await this.run_hook(this, "list", "before");
436
469
  _.remove(args.ids, (o) => o == undefined);
470
+ let key = `${this.type}::${_.join(args.ids, "|")}`;
437
471
  let results = [];
438
472
  if (args.ids.length > 0) {
439
- 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
+ }
440
490
  }
441
491
  _.remove(results, (o) => o.type != this.type);
442
492
 
@@ -489,9 +539,7 @@ export default class SpiceModel {
489
539
  try {
490
540
  this.updated_at = new SDate().now();
491
541
  let results = await this.database.get(args.id);
492
- console.log("Results", results);
493
542
  let item_exist = await this.exist(results);
494
- console.log("Item Exist", item_exist);
495
543
  if (!item_exist) {
496
544
  throw new Error(`${this.type} does not exist. in update`);
497
545
  }
@@ -511,13 +559,8 @@ export default class SpiceModel {
511
559
  form = await this.do_serialize(this, "write", cover_obj.new, args);
512
560
  }
513
561
  let db_data = form || this;
514
- //console.log("New Data Inside Update::::", db_data);
515
-
516
- this.implementTypeFix(db_data);
517
- // console.log("After ITF::::", db_data);
518
562
 
519
563
  await this.database.update(args.id, db_data);
520
- //console.log("Old Data Inside Update::::", results);
521
564
  if (args.skip_hooks != true) {
522
565
  await this.run_hook(
523
566
  {
@@ -546,19 +589,6 @@ export default class SpiceModel {
546
589
  }
547
590
  }
548
591
 
549
- implementTypeFix(item) {
550
- /* console.log("Type Before Fix", item.type, item._type);
551
- let _new_type = item.type || item._type;
552
- //item.type = type;
553
- console.log("New type", _new_type);
554
-
555
- item = { ...item, type: _new_type };
556
- delete item._type; */
557
- //console.log("Type After Fix", item.type, item._type);
558
- //if (item._type) delete item["_type"];
559
- //console.log("Type After Fix", item.type);
560
- }
561
-
562
592
  async create(args) {
563
593
  try {
564
594
  let form;
@@ -577,9 +607,7 @@ export default class SpiceModel {
577
607
  id = args.id;
578
608
  }
579
609
  await this.run_hook(workingForm, "create", "before");
580
- workingForm = this.implementTypeFix(
581
- await this.do_serialize(workingForm, "write", {}, args)
582
- );
610
+ workingForm = await this.do_serialize(workingForm, "write", {}, args);
583
611
 
584
612
  let results = await this.database.insert(id, workingForm, args.expiry);
585
613
  await this.run_hook(
@@ -648,7 +676,7 @@ export default class SpiceModel {
648
676
  query = args.query;
649
677
  } else {
650
678
  if (args.filters) {
651
- query = await this.makeQueryFromFilter(args.filters);
679
+ query = this.makeQueryFromFilter(args.filters);
652
680
  } else {
653
681
  if (args.query) {
654
682
  query =
@@ -671,28 +699,102 @@ export default class SpiceModel {
671
699
  }
672
700
 
673
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
+
674
709
  let results;
675
710
  if (args.is_custom_query && args.is_custom_query === "true") {
676
- 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
+ }
677
730
  } else {
678
731
  if (args.is_full_text && args.is_full_text === "true") {
679
- results = await this.database.full_text_search(
680
- this.type,
681
- query || "",
682
- args.limit,
683
- args.offset
684
- );
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
+ }
685
759
  } else {
686
- results = await this.database.search(
687
- this.type,
688
- args.columns || "",
689
- query || "",
690
- args.limit,
691
- args.offset,
692
- args.sort,
693
- args.do_count,
694
- args.statement_consistent
695
- );
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
+ }
696
798
  }
697
799
  }
698
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
- } */