@nocobase/database 0.9.1-alpha.1 → 0.9.2-alpha.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.
Files changed (173) hide show
  1. package/lib/collection-group-manager.d.ts +13 -0
  2. package/lib/collection-group-manager.js +91 -0
  3. package/lib/collection-importer.js +0 -24
  4. package/lib/collection.d.ts +26 -4
  5. package/lib/collection.js +190 -234
  6. package/lib/database-utils/index.js +3 -15
  7. package/lib/database.d.ts +9 -0
  8. package/lib/database.js +171 -299
  9. package/lib/decorators/must-have-filter-decorator.js +0 -7
  10. package/lib/decorators/transaction-decorator.js +5 -18
  11. package/lib/errors/identifier-error.js +0 -3
  12. package/lib/features/ReferencesMap.js +1 -14
  13. package/lib/features/referential-integrity-check.js +7 -21
  14. package/lib/field-repository/array-field-repository.js +5 -45
  15. package/lib/fields/array-field.js +0 -13
  16. package/lib/fields/belongs-to-field.js +24 -50
  17. package/lib/fields/belongs-to-many-field.js +29 -47
  18. package/lib/fields/boolean-field.js +0 -7
  19. package/lib/fields/context-field.js +2 -23
  20. package/lib/fields/date-field.d.ts +4 -0
  21. package/lib/fields/date-field.js +15 -7
  22. package/lib/fields/field.js +32 -85
  23. package/lib/fields/has-many-field.js +16 -49
  24. package/lib/fields/has-one-field.js +18 -52
  25. package/lib/fields/index.js +0 -44
  26. package/lib/fields/json-field.js +0 -12
  27. package/lib/fields/number-field.js +0 -23
  28. package/lib/fields/password-field.js +8 -35
  29. package/lib/fields/radio-field.js +0 -18
  30. package/lib/fields/relation-field.js +4 -16
  31. package/lib/fields/set-field.js +0 -8
  32. package/lib/fields/sort-field.js +84 -73
  33. package/lib/fields/string-field.js +0 -7
  34. package/lib/fields/text-field.js +0 -7
  35. package/lib/fields/time-field.js +0 -7
  36. package/lib/fields/uid-field.js +4 -22
  37. package/lib/fields/uuid-field.js +3 -12
  38. package/lib/fields/virtual-field.js +0 -7
  39. package/lib/filter-match.js +7 -22
  40. package/lib/filter-parser.js +38 -102
  41. package/lib/index.d.ts +3 -0
  42. package/lib/index.js +36 -42
  43. package/lib/inherited-collection.js +15 -62
  44. package/lib/inherited-map.js +7 -48
  45. package/lib/listeners/adjacency-list.d.ts +3 -0
  46. package/lib/listeners/adjacency-list.js +91 -0
  47. package/lib/listeners/index.d.ts +2 -0
  48. package/lib/listeners/index.js +12 -0
  49. package/lib/magic-attribute-model.js +58 -114
  50. package/lib/migration.js +7 -28
  51. package/lib/mock-database.d.ts +4 -4
  52. package/lib/mock-database.js +15 -18
  53. package/lib/model-hook.js +4 -35
  54. package/lib/model.js +12 -54
  55. package/lib/operators/array.js +2 -32
  56. package/lib/operators/association.js +0 -6
  57. package/lib/operators/boolean.js +0 -6
  58. package/lib/operators/child-collection.d.ts +2 -0
  59. package/lib/operators/child-collection.js +32 -0
  60. package/lib/operators/date.js +123 -60
  61. package/lib/operators/empty.js +3 -32
  62. package/lib/operators/eq.d.ts +2 -0
  63. package/lib/operators/eq.js +26 -0
  64. package/lib/operators/index.js +4 -7
  65. package/lib/operators/ne.js +5 -5
  66. package/lib/operators/notIn.js +0 -5
  67. package/lib/operators/string.js +0 -11
  68. package/lib/operators/utils.js +0 -6
  69. package/lib/options-parser.d.ts +1 -1
  70. package/lib/options-parser.js +47 -107
  71. package/lib/playground.js +0 -4
  72. package/lib/query-interface/mysql-query-interface.d.ts +18 -0
  73. package/lib/query-interface/mysql-query-interface.js +88 -0
  74. package/lib/query-interface/postgres-query-interface.d.ts +14 -0
  75. package/lib/query-interface/postgres-query-interface.js +99 -0
  76. package/lib/query-interface/query-interface-builder.d.ts +2 -0
  77. package/lib/query-interface/query-interface-builder.js +18 -0
  78. package/lib/query-interface/query-interface.d.ts +21 -0
  79. package/lib/query-interface/query-interface.js +48 -0
  80. package/lib/query-interface/sqlite-query-interface.d.ts +17 -0
  81. package/lib/query-interface/sqlite-query-interface.js +89 -0
  82. package/lib/relation-repository/belongs-to-many-repository.js +21 -78
  83. package/lib/relation-repository/belongs-to-repository.js +0 -3
  84. package/lib/relation-repository/hasmany-repository.js +8 -44
  85. package/lib/relation-repository/hasone-repository.js +0 -3
  86. package/lib/relation-repository/multiple-relation-repository.js +16 -68
  87. package/lib/relation-repository/relation-repository.js +5 -42
  88. package/lib/relation-repository/single-relation-repository.js +6 -43
  89. package/lib/repository.d.ts +1 -0
  90. package/lib/repository.js +36 -182
  91. package/lib/sql-parser/index.js +10527 -0
  92. package/lib/sql-parser/sql.pegjs +1297 -0
  93. package/lib/sync-runner.d.ts +1 -1
  94. package/lib/sync-runner.js +26 -64
  95. package/lib/update-associations.js +58 -157
  96. package/lib/update-guard.js +10 -49
  97. package/lib/utils.js +16 -54
  98. package/lib/value-parsers/array-value-parser.js +3 -21
  99. package/lib/value-parsers/base-value-parser.js +0 -13
  100. package/lib/value-parsers/boolean-value-parser.js +4 -10
  101. package/lib/value-parsers/date-value-parser.js +0 -23
  102. package/lib/value-parsers/index.js +0 -10
  103. package/lib/value-parsers/json-value-parser.js +0 -7
  104. package/lib/value-parsers/number-value-parser.js +0 -9
  105. package/lib/value-parsers/string-value-parser.js +3 -20
  106. package/lib/value-parsers/to-many-value-parser.js +1 -42
  107. package/lib/value-parsers/to-one-value-parser.js +0 -14
  108. package/lib/view/field-type-map.d.ts +47 -0
  109. package/lib/view/field-type-map.js +56 -0
  110. package/lib/view/view-inference.d.ts +31 -0
  111. package/lib/view/view-inference.js +92 -0
  112. package/lib/view-collection.d.ts +6 -0
  113. package/lib/view-collection.js +24 -0
  114. package/package.json +4 -3
  115. package/src/__tests__/collection.test.ts +44 -0
  116. package/src/__tests__/fields/date.test.ts +75 -0
  117. package/src/__tests__/fields/sort-field.test.ts +100 -0
  118. package/src/__tests__/filter.test.ts +60 -0
  119. package/src/__tests__/group.test.ts +50 -0
  120. package/src/__tests__/inhertits/collection-inherits.test.ts +114 -0
  121. package/src/__tests__/operator/date-operator.test.ts +244 -98
  122. package/src/__tests__/operator/eq.test.ts +76 -0
  123. package/src/__tests__/operator/ne.test.ts +19 -1
  124. package/src/__tests__/relation-repository/belongs-to-many-repository.test.ts +82 -0
  125. package/src/__tests__/repository/find.test.ts +33 -0
  126. package/src/__tests__/repository.test.ts +88 -0
  127. package/src/__tests__/sql-parser.test.ts +13 -0
  128. package/src/__tests__/tree.test.ts +217 -0
  129. package/src/__tests__/view/list-view.test.ts +34 -0
  130. package/src/__tests__/view/view-collection.test.ts +199 -0
  131. package/src/__tests__/view/view-inference.test.ts +145 -0
  132. package/src/__tests__/view/view-repository.test.ts +67 -0
  133. package/src/collection-group-manager.ts +94 -0
  134. package/src/collection.ts +126 -16
  135. package/src/database-utils/index.ts +1 -0
  136. package/src/database.ts +98 -17
  137. package/src/features/ReferencesMap.ts +3 -2
  138. package/src/fields/belongs-to-many-field.ts +23 -4
  139. package/src/fields/date-field.ts +18 -0
  140. package/src/fields/field.ts +17 -7
  141. package/src/fields/json-field.ts +1 -0
  142. package/src/fields/sort-field.ts +90 -29
  143. package/src/filter-parser.ts +2 -1
  144. package/src/index.ts +3 -1
  145. package/src/listeners/adjacency-list.ts +60 -0
  146. package/src/listeners/index.ts +7 -0
  147. package/src/mock-database.ts +14 -2
  148. package/src/model.ts +4 -0
  149. package/src/operators/child-collection.ts +24 -0
  150. package/src/operators/date.ts +108 -24
  151. package/src/operators/eq.ts +14 -0
  152. package/src/operators/index.ts +2 -0
  153. package/src/operators/ne.ts +12 -7
  154. package/src/options-parser.ts +25 -11
  155. package/src/query-interface/mysql-query-interface.ts +72 -0
  156. package/src/query-interface/postgres-query-interface.ts +103 -0
  157. package/src/query-interface/query-interface-builder.ts +14 -0
  158. package/src/query-interface/query-interface.ts +43 -0
  159. package/src/query-interface/sqlite-query-interface.ts +79 -0
  160. package/src/relation-repository/belongs-to-many-repository.ts +20 -1
  161. package/src/relation-repository/hasmany-repository.ts +5 -3
  162. package/src/relation-repository/multiple-relation-repository.ts +13 -1
  163. package/src/relation-repository/single-relation-repository.ts +2 -0
  164. package/src/repository.ts +6 -13
  165. package/src/sql-parser/index.js +10698 -0
  166. package/src/sql-parser/readme.md +2 -0
  167. package/src/sql-parser/sql.pegjs +1297 -0
  168. package/src/sync-runner.ts +27 -32
  169. package/src/update-associations.ts +26 -22
  170. package/src/utils.ts +4 -3
  171. package/src/view/field-type-map.ts +56 -0
  172. package/src/view/view-inference.ts +106 -0
  173. package/src/view-collection.ts +21 -0
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.beforeDefineAdjacencyListCollection = exports.afterDefineAdjacencyListCollection = void 0;
7
+ function _lodash() {
8
+ const data = _interopRequireDefault(require("lodash"));
9
+ _lodash = function _lodash() {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
16
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
17
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
18
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
19
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
20
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
21
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
22
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
23
+ 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); } }
24
+ 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); }); }; }
25
+ const beforeDefineAdjacencyListCollection = options => {
26
+ if (!options.tree) {
27
+ return;
28
+ }
29
+ (options.fields || []).forEach(field => {
30
+ if (field.treeParent || field.treeChildren) {
31
+ if (!field.target) {
32
+ field.target = options.name;
33
+ }
34
+ if (!field.foreignKey) {
35
+ field.foreignKey = 'parentId';
36
+ }
37
+ }
38
+ });
39
+ };
40
+ exports.beforeDefineAdjacencyListCollection = beforeDefineAdjacencyListCollection;
41
+ const afterDefineAdjacencyListCollection = collection => {
42
+ if (!collection.options.tree) {
43
+ return;
44
+ }
45
+ collection.model.afterFind( /*#__PURE__*/function () {
46
+ var _ref = _asyncToGenerator(function* (instances, options) {
47
+ var _collection$treeParen, _collection$treeParen2, _collection$treeChild, _collection$treeChild2;
48
+ if (!options.tree) {
49
+ return;
50
+ }
51
+ const foreignKey = (_collection$treeParen = (_collection$treeParen2 = collection.treeParentField) === null || _collection$treeParen2 === void 0 ? void 0 : _collection$treeParen2.foreignKey) !== null && _collection$treeParen !== void 0 ? _collection$treeParen : 'parentId';
52
+ const childrenKey = (_collection$treeChild = (_collection$treeChild2 = collection.treeChildrenField) === null || _collection$treeChild2 === void 0 ? void 0 : _collection$treeChild2.name) !== null && _collection$treeChild !== void 0 ? _collection$treeChild : 'children';
53
+ const arr = Array.isArray(instances) ? instances : [instances];
54
+ let index = 0;
55
+ var _iterator = _createForOfIteratorHelper(arr),
56
+ _step;
57
+ try {
58
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
59
+ const instance = _step.value;
60
+ const opts = _objectSpread({}, _lodash().default.pick(options, ['tree', 'fields', 'appends', 'except', 'sort']));
61
+ let __index = `${index++}`;
62
+ if (options.parentIndex) {
63
+ __index = `${options.parentIndex}.${__index}`;
64
+ }
65
+ instance.setDataValue('__index', __index);
66
+ const children = yield collection.repository.find(_objectSpread(_objectSpread({
67
+ filter: {
68
+ [foreignKey]: instance.id
69
+ },
70
+ transaction: options.transaction
71
+ }, opts), {}, {
72
+ // @ts-ignore
73
+ parentIndex: `${__index}.${childrenKey}`,
74
+ context: options.context
75
+ }));
76
+ if ((children === null || children === void 0 ? void 0 : children.length) > 0) {
77
+ instance.setDataValue(childrenKey, children.map(r => r.toJSON()));
78
+ }
79
+ }
80
+ } catch (err) {
81
+ _iterator.e(err);
82
+ } finally {
83
+ _iterator.f();
84
+ }
85
+ });
86
+ return function (_x, _x2) {
87
+ return _ref.apply(this, arguments);
88
+ };
89
+ }());
90
+ };
91
+ exports.afterDefineAdjacencyListCollection = afterDefineAdjacencyListCollection;
@@ -0,0 +1,2 @@
1
+ import { Database } from '../database';
2
+ export declare const registerBuiltInListeners: (db: Database) => void;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.registerBuiltInListeners = void 0;
7
+ var _adjacencyList = require("./adjacency-list");
8
+ const registerBuiltInListeners = db => {
9
+ db.on('beforeDefineCollection', _adjacencyList.beforeDefineAdjacencyListCollection);
10
+ db.on('afterDefineCollection', _adjacencyList.afterDefineAdjacencyListCollection);
11
+ };
12
+ exports.registerBuiltInListeners = registerBuiltInListeners;
@@ -4,129 +4,97 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.MagicAttributeModel = void 0;
7
-
8
7
  function _utils() {
9
8
  const data = require("@nocobase/utils");
10
-
11
9
  _utils = function _utils() {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  function _lodash() {
19
15
  const data = _interopRequireDefault(require("lodash"));
20
-
21
16
  _lodash = function _lodash() {
22
17
  return data;
23
18
  };
24
-
25
19
  return data;
26
20
  }
27
-
28
21
  function _sequelize() {
29
22
  const data = require("sequelize");
30
-
31
23
  _sequelize = function _sequelize() {
32
24
  return data;
33
25
  };
34
-
35
26
  return data;
36
27
  }
37
-
38
28
  var _model = require("./model");
39
-
40
29
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
41
-
42
30
  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); } }
43
-
44
31
  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); }); }; }
45
-
46
32
  function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
47
-
48
33
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
49
-
50
34
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
51
-
52
- 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; }
53
-
35
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
36
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
37
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
54
38
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
55
-
56
39
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
57
-
58
40
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
59
-
60
41
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
61
-
62
- function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
63
-
42
+ function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
64
43
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
65
-
66
44
  const Dottie = require('dottie');
67
-
68
45
  class MagicAttributeModel extends _model.Model {
69
46
  get magicAttribute() {
70
47
  const db = this.constructor.database;
71
48
  const collection = db.getCollection(this.constructor.name);
72
49
  return collection.options.magicAttribute || 'options';
73
50
  }
74
-
75
51
  set(key, value, options) {
76
52
  if (typeof key === 'string') {
77
53
  const _key$split = key.split('.'),
78
- _key$split2 = _slicedToArray(_key$split, 1),
79
- column = _key$split2[0];
80
-
54
+ _key$split2 = _slicedToArray(_key$split, 1),
55
+ column = _key$split2[0];
81
56
  if (this.constructor.hasAlias(column)) {
82
57
  return this.setV1(key, value, options);
83
58
  }
84
-
85
59
  if (this.constructor.rawAttributes[column]) {
86
60
  return this.setV1(key, value, options);
87
61
  }
88
-
89
62
  if (_lodash().default.isPlainObject(value)) {
90
63
  const opts = super.get(this.magicAttribute) || {};
91
64
  return this.setV1(`${this.magicAttribute}.${key}`, (0, _utils().merge)(opts === null || opts === void 0 ? void 0 : opts[key], value), options);
92
65
  }
93
-
94
66
  return this.setV1(`${this.magicAttribute}.${key}`, value, options);
95
67
  } else {
96
68
  if (!key) {
97
69
  return;
98
70
  }
99
-
100
71
  Object.keys(key).forEach(k => {
101
72
  this.setV1(k, key[k], options);
102
73
  });
103
74
  }
104
-
105
75
  return this.setV1(key, value, options);
106
76
  }
107
-
108
77
  setV1(key, value, options) {
109
78
  let values;
110
79
  let originalValue;
111
-
112
80
  if (typeof key === 'object' && key !== null) {
113
81
  values = key;
114
82
  options = value || {};
115
-
116
83
  if (options.reset) {
117
84
  // @ts-ignore
118
85
  this.dataValues = {};
119
-
120
86
  for (const key in values) {
121
87
  this.changed(key, false);
122
88
  }
123
- } // If raw, and we're not dealing with includes or special attributes, just set it straight on the dataValues object
89
+ }
90
+ // If raw, and we're not dealing with includes or special attributes, just set it straight on the dataValues object
91
+ // @ts-ignore
92
+ if (options.raw &&
93
+ // @ts-ignore
94
+ !(this._options && this._options.include) && !(options && options.attributes) &&
95
+ // @ts-ignore
96
+ !this.constructor._hasDateAttributes &&
124
97
  // @ts-ignore
125
-
126
-
127
- if (options.raw && // @ts-ignore
128
- !(this._options && this._options.include) && !(options && options.attributes) && // @ts-ignore
129
- !this.constructor._hasDateAttributes && // @ts-ignore
130
98
  !this.constructor._hasBooleanAttributes) {
131
99
  // @ts-ignore
132
100
  if (Object.keys(this.dataValues).length) {
@@ -135,26 +103,22 @@ class MagicAttributeModel extends _model.Model {
135
103
  } else {
136
104
  // @ts-ignore
137
105
  this.dataValues = values;
138
- } // If raw, .changed() shouldn't be true
106
+ }
107
+ // If raw, .changed() shouldn't be true
139
108
  // @ts-ignore
140
-
141
-
142
109
  this._previousDataValues = _objectSpread({}, this.dataValues);
143
110
  } else {
144
111
  // Loop and call set
145
112
  if (options.attributes) {
146
113
  const setKeys = data => {
147
114
  var _iterator = _createForOfIteratorHelper(data),
148
- _step;
149
-
115
+ _step;
150
116
  try {
151
117
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
152
118
  const k = _step.value;
153
-
154
119
  if (values[k] === undefined) {
155
120
  continue;
156
121
  }
157
-
158
122
  this.set(k, values[k], options);
159
123
  }
160
124
  } catch (err) {
@@ -163,15 +127,13 @@ class MagicAttributeModel extends _model.Model {
163
127
  _iterator.f();
164
128
  }
165
129
  };
166
-
167
- setKeys(options.attributes); // @ts-ignore
168
-
130
+ setKeys(options.attributes);
131
+ // @ts-ignore
169
132
  if (this.constructor._hasVirtualAttributes) {
170
133
  // @ts-ignore
171
134
  setKeys(this.constructor._virtualAttributes);
172
- } // @ts-ignore
173
-
174
-
135
+ }
136
+ // @ts-ignore
175
137
  if (this._options.includeNames) {
176
138
  // @ts-ignore
177
139
  setKeys(this._options.includeNames);
@@ -181,35 +143,28 @@ class MagicAttributeModel extends _model.Model {
181
143
  this.set(key, values[key], options);
182
144
  }
183
145
  }
184
-
185
146
  if (options.raw) {
186
147
  // If raw, .changed() shouldn't be true
187
148
  // @ts-ignore
188
149
  this._previousDataValues = _objectSpread({}, this.dataValues);
189
150
  }
190
151
  }
191
-
192
152
  return this;
193
153
  }
194
-
195
154
  if (!options) options = {};
196
-
197
155
  if (!options.raw) {
198
156
  // @ts-ignore
199
157
  originalValue = this.dataValues[key];
200
- } // If not raw, and there's a custom setter
158
+ }
159
+ // If not raw, and there's a custom setter
201
160
  // @ts-ignore
202
-
203
-
204
161
  if (!options.raw && this._customSetters[key]) {
205
162
  // @ts-ignore
206
- this._customSetters[key].call(this, value, key); // custom setter should have changed value, get that changed value
163
+ this._customSetters[key].call(this, value, key);
164
+ // custom setter should have changed value, get that changed value
207
165
  // TODO: v5 make setters return new value instead of changing internal store
208
166
  // @ts-ignore
209
-
210
-
211
167
  const newValue = this.dataValues[key];
212
-
213
168
  if (!_lodash().default.isEqual(newValue, originalValue)) {
214
169
  // @ts-ignore
215
170
  this._previousDataValues[key] = originalValue;
@@ -222,11 +177,9 @@ class MagicAttributeModel extends _model.Model {
222
177
  // Pass it on to the include handler
223
178
  // @ts-ignore
224
179
  this._setInclude(key, value, options);
225
-
226
180
  return this;
227
- } // Bunch of stuff we won't do when it's raw
228
-
229
-
181
+ }
182
+ // Bunch of stuff we won't do when it's raw
230
183
  if (!options.raw) {
231
184
  // If attribute is not in model definition, return
232
185
  // @ts-ignore
@@ -235,96 +188,87 @@ class MagicAttributeModel extends _model.Model {
235
188
  if (key.includes('.') && this.constructor._jsonAttributes.has(key.split('.')[0])) {
236
189
  // @ts-ignore
237
190
  const previousNestedValue = Dottie.get(this.dataValues, key);
238
-
239
191
  if (!_lodash().default.isEqual(previousNestedValue, value)) {
240
192
  // @ts-ignore
241
- this._previousDataValues = _lodash().default.cloneDeep(this._previousDataValues); // @ts-ignore
242
-
193
+ this._previousDataValues = _lodash().default.cloneDeep(this._previousDataValues);
194
+ // @ts-ignore
243
195
  Dottie.set(this.dataValues, key, value);
244
196
  this.changed(key.split('.')[0], true);
245
197
  }
246
198
  }
247
-
248
199
  return this;
249
- } // If attempting to set primary key and primary key is already defined, return
200
+ }
201
+ // If attempting to set primary key and primary key is already defined, return
250
202
  // @ts-ignore
251
-
252
-
253
203
  if (this.constructor._hasPrimaryKeys && originalValue && this.constructor._isPrimaryKey(key)) {
254
204
  return this;
255
- } // If attempting to set read only attributes, return
205
+ }
206
+ // If attempting to set read only attributes, return
207
+ // @ts-ignore
208
+ if (!this.isNewRecord &&
209
+ // @ts-ignore
210
+ this.constructor._hasReadOnlyAttributes &&
256
211
  // @ts-ignore
257
-
258
-
259
- if (!this.isNewRecord && // @ts-ignore
260
- this.constructor._hasReadOnlyAttributes && // @ts-ignore
261
212
  this.constructor._readOnlyAttributes.has(key)) {
262
213
  return this;
263
214
  }
264
- } // If there's a data type sanitizer
265
-
266
-
267
- if (!(value instanceof _sequelize().Utils.SequelizeMethod) && // @ts-ignore
215
+ }
216
+ // If there's a data type sanitizer
217
+ if (!(value instanceof _sequelize().Utils.SequelizeMethod) &&
218
+ // @ts-ignore
268
219
  Object.prototype.hasOwnProperty.call(this.constructor._dataTypeSanitizers, key)) {
269
220
  // @ts-ignore
270
221
  value = this.constructor._dataTypeSanitizers[key].call(this, value, options);
271
- } // Set when the value has changed and not raw
272
-
273
-
274
- if (!options.raw && ( // True when sequelize method
275
- value instanceof _sequelize().Utils.SequelizeMethod || // Check for data type type comparators
222
+ }
223
+ // Set when the value has changed and not raw
224
+ if (!options.raw && (
225
+ // True when sequelize method
226
+ value instanceof _sequelize().Utils.SequelizeMethod ||
227
+ // Check for data type type comparators
276
228
  // @ts-ignore
277
- !(value instanceof _sequelize().Utils.SequelizeMethod) && // @ts-ignore
278
- this.constructor._dataTypeChanges[key] && // @ts-ignore
279
- this.constructor._dataTypeChanges[key].call(this, value, originalValue, options) || // Check default
229
+ !(value instanceof _sequelize().Utils.SequelizeMethod) &&
230
+ // @ts-ignore
231
+ this.constructor._dataTypeChanges[key] &&
232
+ // @ts-ignore
233
+ this.constructor._dataTypeChanges[key].call(this, value, originalValue, options) ||
234
+ // Check default
280
235
  // @ts-ignore
281
236
  !this.constructor._dataTypeChanges[key] && !_lodash().default.isEqual(value, originalValue))) {
282
237
  // @ts-ignore
283
238
  this._previousDataValues[key] = originalValue;
284
239
  this.changed(key, true);
285
- } // set data value
240
+ }
241
+ // set data value
286
242
  // @ts-ignore
287
-
288
-
289
243
  this.dataValues[key] = value;
290
244
  }
291
-
292
245
  return this;
293
246
  }
294
-
295
247
  get(key, value) {
296
248
  if (typeof key === 'string') {
297
249
  const _key$split3 = key.split('.'),
298
- _key$split4 = _slicedToArray(_key$split3, 1),
299
- column = _key$split4[0];
300
-
250
+ _key$split4 = _slicedToArray(_key$split3, 1),
251
+ column = _key$split4[0];
301
252
  if (this.constructor.hasAlias(column)) {
302
253
  return super.get(key, value);
303
254
  }
304
-
305
255
  if (this.constructor.rawAttributes[column]) {
306
256
  return super.get(key, value);
307
257
  }
308
-
309
258
  const options = super.get(this.magicAttribute, value);
310
259
  return _lodash().default.get(options, key);
311
260
  }
312
-
313
261
  const data = super.get(key, value);
314
262
  return _objectSpread(_objectSpread({}, _lodash().default.omit(data, this.magicAttribute)), data[this.magicAttribute]);
315
263
  }
316
-
317
264
  update(values, options) {
318
265
  var _superprop_getUpdate = () => super.update,
319
- _this = this;
320
-
266
+ _this = this;
321
267
  return _asyncToGenerator(function* () {
322
268
  // @ts-ignore
323
269
  _this._changed = new Set();
324
270
  return _superprop_getUpdate().call(_this, values, options);
325
271
  })();
326
272
  }
327
-
328
273
  }
329
-
330
274
  exports.MagicAttributeModel = MagicAttributeModel;
package/lib/migration.js CHANGED
@@ -4,64 +4,48 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.Migrations = exports.Migration = void 0;
7
-
8
7
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
9
-
10
8
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
11
-
12
- 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; }
13
-
9
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
10
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
11
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
14
12
  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); } }
15
-
16
13
  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); }); }; }
17
-
18
14
  class Migration {
19
15
  constructor(context) {
20
16
  this.name = void 0;
21
17
  this.context = void 0;
22
18
  this.context = context;
23
19
  }
24
-
25
20
  get db() {
26
21
  return this.context.db;
27
22
  }
28
-
29
23
  get sequelize() {
30
24
  return this.context.db.sequelize;
31
25
  }
32
-
33
26
  get queryInterface() {
34
27
  return this.context.db.sequelize.getQueryInterface();
35
28
  }
36
-
37
- up() {// todo
38
-
29
+ up() {
39
30
  return _asyncToGenerator(function* () {})();
40
- }
41
-
42
- down() {// todo
31
+ } // todo
43
32
 
33
+ down() {
44
34
  return _asyncToGenerator(function* () {})();
45
- }
46
-
35
+ } // todo
47
36
  }
48
-
49
37
  exports.Migration = Migration;
50
-
51
38
  class Migrations {
52
39
  constructor(context) {
53
40
  this.items = [];
54
41
  this.context = void 0;
55
42
  this.context = context;
56
43
  }
57
-
58
44
  clear() {
59
45
  this.items = [];
60
46
  }
61
-
62
47
  add(item) {
63
48
  const Migration = item.migration;
64
-
65
49
  if (Migration) {
66
50
  const migration = new Migration(_objectSpread(_objectSpread({}, this.context), item.context));
67
51
  migration.name = item.name;
@@ -70,21 +54,16 @@ class Migrations {
70
54
  this.items.push(item);
71
55
  }
72
56
  }
73
-
74
57
  callback() {
75
58
  var _this = this;
76
-
77
59
  return /*#__PURE__*/function () {
78
60
  var _ref = _asyncToGenerator(function* (ctx) {
79
61
  return _this.items;
80
62
  });
81
-
82
63
  return function (_x) {
83
64
  return _ref.apply(this, arguments);
84
65
  };
85
66
  }();
86
67
  }
87
-
88
68
  }
89
-
90
69
  exports.Migrations = Migrations;
@@ -9,10 +9,7 @@ export declare function getConfigByEnv(): {
9
9
  host: string;
10
10
  port: string;
11
11
  dialect: string;
12
- logging: boolean | {
13
- (...data: any[]): void;
14
- (message?: any, ...optionalParams: any[]): void;
15
- };
12
+ logging: boolean | typeof customLogger;
16
13
  storage: string;
17
14
  define: {
18
15
  charset: string;
@@ -21,5 +18,8 @@ export declare function getConfigByEnv(): {
21
18
  timezone: string;
22
19
  underscored: boolean;
23
20
  schema: string;
21
+ dialectOptions: {};
24
22
  };
23
+ declare function customLogger(queryString: any, queryObject: any): void;
25
24
  export declare function mockDatabase(options?: IDatabaseOptions): MockDatabase;
25
+ export {};