@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
@@ -6,35 +6,26 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.MockDatabase = void 0;
7
7
  exports.getConfigByEnv = getConfigByEnv;
8
8
  exports.mockDatabase = mockDatabase;
9
-
10
9
  function _utils() {
11
10
  const data = require("@nocobase/utils");
12
-
13
11
  _utils = function _utils() {
14
12
  return data;
15
13
  };
16
-
17
14
  return data;
18
15
  }
19
-
20
16
  function _path() {
21
17
  const data = require("path");
22
-
23
18
  _path = function _path() {
24
19
  return data;
25
20
  };
26
-
27
21
  return data;
28
22
  }
29
-
30
23
  var _database = require("./database");
31
-
32
24
  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; }
33
-
34
25
  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; }
35
-
36
- 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; }
37
-
26
+ 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; }
27
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
28
+ 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); }
38
29
  class MockDatabase extends _database.Database {
39
30
  constructor(options) {
40
31
  super(_objectSpread({
@@ -43,20 +34,17 @@ class MockDatabase extends _database.Database {
43
34
  dialect: 'sqlite'
44
35
  }, options));
45
36
  }
46
-
47
37
  }
48
-
49
38
  exports.MockDatabase = MockDatabase;
50
-
51
39
  function getConfigByEnv() {
52
- return {
40
+ const options = {
53
41
  username: process.env.DB_USER,
54
42
  password: process.env.DB_PASSWORD,
55
43
  database: process.env.DB_DATABASE,
56
44
  host: process.env.DB_HOST,
57
45
  port: process.env.DB_PORT,
58
46
  dialect: process.env.DB_DIALECT || 'sqlite',
59
- logging: process.env.DB_LOGGING === 'on' ? console.log : false,
47
+ logging: process.env.DB_LOGGING === 'on' ? customLogger : false,
60
48
  storage: process.env.DB_STORAGE && process.env.DB_STORAGE !== ':memory:' ? (0, _path().resolve)(process.cwd(), process.env.DB_STORAGE) : ':memory:',
61
49
  define: {
62
50
  charset: 'utf8mb4',
@@ -64,8 +52,17 @@ function getConfigByEnv() {
64
52
  },
65
53
  timezone: process.env.DB_TIMEZONE,
66
54
  underscored: process.env.DB_UNDERSCORED === 'true',
67
- schema: process.env.DB_SCHEMA !== 'public' ? process.env.DB_SCHEMA : undefined
55
+ schema: process.env.DB_SCHEMA !== 'public' ? process.env.DB_SCHEMA : undefined,
56
+ dialectOptions: {}
68
57
  };
58
+ if (process.env.DB_DIALECT == 'postgres') {
59
+ options.dialectOptions['application_name'] = 'nocobase.main';
60
+ }
61
+ return options;
62
+ }
63
+ function customLogger(queryString, queryObject) {
64
+ console.log(queryString); // outputs a string
65
+ console.log(queryObject.bind); // outputs an array
69
66
  }
70
67
 
71
68
  function mockDatabase(options = {}) {
package/lib/model-hook.js CHANGED
@@ -4,76 +4,54 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.ModelHook = void 0;
7
-
8
7
  function _lodash() {
9
8
  const data = _interopRequireDefault(require("lodash"));
10
-
11
9
  _lodash = function _lodash() {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
-
20
15
  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); } }
21
-
22
16
  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); }); }; }
23
-
24
17
  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; } } }; }
25
-
26
18
  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); }
27
-
28
19
  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; }
29
-
30
20
  const _require = require('sequelize/lib/hooks'),
31
- hooks = _require.hooks;
32
-
21
+ hooks = _require.hooks;
33
22
  class ModelHook {
34
23
  constructor(database) {
35
24
  this.database = void 0;
36
25
  this.boundEvents = new Set();
37
26
  this.database = database;
38
27
  }
39
-
40
28
  match(event) {
41
29
  // NOTE: skip Symbol event
42
30
  if (!_lodash().default.isString(event)) {
43
31
  return null;
44
32
  }
45
-
46
33
  const type = event.split('.').pop();
47
34
  return type in hooks ? type : null;
48
35
  }
49
-
50
36
  findModelName(hookArgs) {
51
37
  var _iterator = _createForOfIteratorHelper(hookArgs),
52
- _step;
53
-
38
+ _step;
54
39
  try {
55
40
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
56
41
  const arg = _step.value;
57
-
58
42
  if (arg === null || arg === void 0 ? void 0 : arg._previousDataValues) {
59
43
  return arg.constructor.name;
60
44
  }
61
-
62
45
  if (_lodash().default.isPlainObject(arg)) {
63
46
  var _arg$name, _arg$name2;
64
-
65
47
  if (arg['model']) {
66
48
  return arg['model'].name;
67
49
  }
68
-
69
50
  const plural = arg === null || arg === void 0 ? void 0 : (_arg$name = arg.name) === null || _arg$name === void 0 ? void 0 : _arg$name.plural;
70
-
71
51
  if (this.database.sequelize.isDefined(plural)) {
72
52
  return plural;
73
53
  }
74
-
75
54
  const singular = arg === null || arg === void 0 ? void 0 : (_arg$name2 = arg.name) === null || _arg$name2 === void 0 ? void 0 : _arg$name2.singular;
76
-
77
55
  if (this.database.sequelize.isDefined(singular)) {
78
56
  return singular;
79
57
  }
@@ -84,34 +62,25 @@ class ModelHook {
84
62
  } finally {
85
63
  _iterator.f();
86
64
  }
87
-
88
65
  return null;
89
66
  }
90
-
91
67
  bindEvent(type) {
92
68
  this.boundEvents.add(type);
93
69
  }
94
-
95
70
  hasBoundEvent(type) {
96
71
  return this.boundEvents.has(type);
97
72
  }
98
-
99
73
  buildSequelizeHook(type) {
100
74
  var _this = this;
101
-
102
75
  return /*#__PURE__*/_asyncToGenerator(function* (...args) {
103
76
  const modelName = _this.findModelName(args);
104
-
105
77
  if (modelName) {
106
78
  // emit model event
107
79
  yield _this.database.emitAsync(`${modelName}.${type}`, ...args);
108
- } // emit sequelize global event
109
-
110
-
80
+ }
81
+ // emit sequelize global event
111
82
  yield _this.database.emitAsync(type, ...args);
112
83
  });
113
84
  }
114
-
115
85
  }
116
-
117
86
  exports.ModelHook = ModelHook;
package/lib/model.js CHANGED
@@ -4,114 +4,86 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.Model = void 0;
7
-
8
7
  function _lodash() {
9
8
  const data = _interopRequireDefault(require("lodash"));
10
-
11
9
  _lodash = function _lodash() {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  function _sequelize() {
19
15
  const data = require("sequelize");
20
-
21
16
  _sequelize = function _sequelize() {
22
17
  return data;
23
18
  };
24
-
25
19
  return data;
26
20
  }
27
-
28
21
  var _syncRunner = require("./sync-runner");
29
-
30
22
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
31
-
32
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); } }
33
-
34
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); }); }; }
35
-
36
25
  const _ = _lodash().default;
37
-
38
26
  class Model extends _sequelize().Model {
39
27
  constructor(...args) {
40
28
  super(...args);
41
29
  this._changedWithAssociations = new Set();
42
30
  this._previousDataValuesWithAssociations = {};
43
31
  }
44
-
45
32
  // TODO
46
33
  toChangedWithAssociations() {
47
34
  // @ts-ignore
48
- this._changedWithAssociations = new Set([...this._changedWithAssociations, ...this._changed]); // @ts-ignore
49
-
35
+ this._changedWithAssociations = new Set([...this._changedWithAssociations, ...this._changed]);
36
+ // @ts-ignore
50
37
  this._previousDataValuesWithAssociations = this._previousDataValues;
51
38
  }
52
-
53
39
  changedWithAssociations(key, value) {
54
40
  if (key === undefined) {
55
41
  if (this._changedWithAssociations.size > 0) {
56
42
  return Array.from(this._changedWithAssociations);
57
43
  }
58
-
59
44
  return false;
60
45
  }
61
-
62
46
  if (value === true) {
63
47
  this._changedWithAssociations.add(key);
64
-
65
48
  return this;
66
49
  }
67
-
68
50
  if (value === false) {
69
51
  this._changedWithAssociations.delete(key);
70
-
71
52
  return this;
72
53
  }
73
-
74
54
  return this._changedWithAssociations.has(key);
75
55
  }
76
-
77
56
  clearChangedWithAssociations() {
78
57
  this._changedWithAssociations = new Set();
79
58
  }
80
-
81
59
  toJSON() {
82
60
  const handleObj = (obj, options) => {
83
61
  const handles = [data => {
84
62
  if (data instanceof Model) {
85
63
  return data.toJSON();
86
64
  }
87
-
88
65
  return data;
89
66
  }, this.hiddenObjKey];
90
67
  return handles.reduce((carry, fn) => fn.apply(this, [carry, options]), obj);
91
68
  };
92
-
93
69
  const handleArray = (arrayOfObj, options) => {
94
70
  const handles = [this.sortAssociations];
95
71
  return handles.reduce((carry, fn) => fn.apply(this, [carry, options]), arrayOfObj || []);
96
72
  };
97
-
98
73
  const opts = {
99
74
  model: this.constructor,
100
75
  collection: this.constructor.collection,
101
76
  db: this.constructor.database
102
77
  };
103
-
104
78
  const traverseJSON = (data, options) => {
105
79
  const model = options.model,
106
- db = options.db,
107
- collection = options.collection; // handle Object
108
-
80
+ db = options.db,
81
+ collection = options.collection;
82
+ // handle Object
109
83
  data = handleObj(data, options);
110
84
  const result = {};
111
-
112
85
  for (var _i = 0, _Object$keys = Object.keys(data); _i < _Object$keys.length; _i++) {
113
86
  const key = _Object$keys[_i];
114
-
115
87
  // @ts-ignore
116
88
  if (model.hasAlias(key)) {
117
89
  const association = model.associations[key];
@@ -122,7 +94,6 @@ class Model extends _sequelize().Model {
122
94
  key,
123
95
  field: collection.getField(key)
124
96
  };
125
-
126
97
  if (['HasMany', 'BelongsToMany'].includes(association.associationType)) {
127
98
  result[key] = handleArray(data[key], opts).map(item => traverseJSON(item, opts));
128
99
  } else {
@@ -132,30 +103,24 @@ class Model extends _sequelize().Model {
132
103
  result[key] = data[key];
133
104
  }
134
105
  }
135
-
136
106
  return result;
137
107
  };
138
-
139
108
  return traverseJSON(super.toJSON(), opts);
140
109
  }
141
-
142
110
  hiddenObjKey(obj, options) {
143
111
  const hiddenFields = Array.from(options.collection.fields.values()).filter(field => field.options.hidden).map(field => field.options.name);
144
112
  return _lodash().default.omit(obj, hiddenFields);
145
113
  }
146
-
147
114
  sortAssociations(data, {
148
115
  field
149
116
  }) {
150
117
  const sortBy = field.options.sortBy;
151
118
  return sortBy ? this.sortArray(data, sortBy) : data;
152
119
  }
153
-
154
120
  sortArray(data, sortBy) {
155
121
  if (!_lodash().default.isArray(sortBy)) {
156
122
  sortBy = [sortBy];
157
123
  }
158
-
159
124
  const orderItems = [];
160
125
  const orderDirections = [];
161
126
  sortBy.forEach(sortItem => {
@@ -164,31 +129,28 @@ class Model extends _sequelize().Model {
164
129
  });
165
130
  return _lodash().default.orderBy(data, orderItems, orderDirections);
166
131
  }
167
-
168
132
  static sync(options) {
169
133
  var _this = this;
170
-
171
134
  return _asyncToGenerator(function* () {
135
+ if (_this.collection.isView()) {
136
+ return;
137
+ }
172
138
  const model = _this;
173
139
  const _schema = model._schema;
174
-
175
140
  if (_schema && _schema != 'public') {
176
141
  yield _this.sequelize.query(`CREATE SCHEMA IF NOT EXISTS "${_schema}";`, {
177
142
  raw: true,
178
143
  transaction: options === null || options === void 0 ? void 0 : options.transaction
179
144
  });
180
- } // fix sequelize sync with model that not have any column
181
-
182
-
145
+ }
146
+ // fix sequelize sync with model that not have any column
183
147
  if (Object.keys(model.tableAttributes).length === 0) {
184
148
  if (_this.database.inDialect('sqlite', 'mysql')) {
185
149
  console.error(`Zero-column tables aren't supported in ${_this.database.sequelize.getDialect()}`);
186
150
  return;
187
- } // @ts-ignore
188
-
189
-
151
+ }
152
+ // @ts-ignore
190
153
  const queryInterface = _this.sequelize.queryInterface;
191
-
192
154
  if (!queryInterface.patched) {
193
155
  const oldDescribeTable = queryInterface.describeTable;
194
156
  queryInterface.describeTable = /*#__PURE__*/_asyncToGenerator(function* (...args) {
@@ -205,17 +167,13 @@ class Model extends _sequelize().Model {
205
167
  queryInterface.patched = true;
206
168
  }
207
169
  }
208
-
209
170
  if (_this.collection.isInherited()) {
210
171
  return _syncRunner.SyncRunner.syncInheritModel(model, options);
211
172
  }
212
-
213
173
  return _sequelize().Model.sync.call(_this, options);
214
174
  })();
215
175
  }
216
-
217
176
  }
218
-
219
177
  exports.Model = Model;
220
178
  Model.database = void 0;
221
179
  Model.collection = void 0;
@@ -4,29 +4,22 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  function _sequelize() {
9
8
  const data = require("sequelize");
10
-
11
9
  _sequelize = function _sequelize() {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  var _utils = require("./utils");
19
-
20
15
  const getFieldName = ctx => {
21
16
  const fieldName = ctx.fieldName;
22
17
  return fieldName;
23
18
  };
24
-
25
19
  const escape = (value, ctx) => {
26
20
  const sequelize = ctx.db.sequelize;
27
21
  return sequelize.escape(value);
28
22
  };
29
-
30
23
  const sqliteExistQuery = (value, ctx) => {
31
24
  const fieldName = getFieldName(ctx);
32
25
  const name = ctx.fullName === fieldName ? `"${ctx.model.name}"."${fieldName}"` : `"${fieldName}"`;
@@ -34,91 +27,72 @@ const sqliteExistQuery = (value, ctx) => {
34
27
  const subQuery = `exists (select * from json_each(${name}) where json_each.value in ${sqlArray})`;
35
28
  return subQuery;
36
29
  };
37
-
38
30
  const emptyQuery = (ctx, operator) => {
39
31
  const fieldName = getFieldName(ctx);
40
32
  let funcName = 'json_array_length';
41
33
  let ifNull = 'IFNULL';
42
-
43
34
  if ((0, _utils.isPg)(ctx)) {
44
35
  funcName = 'jsonb_array_length';
45
36
  ifNull = 'coalesce';
46
37
  }
47
-
48
38
  if ((0, _utils.isMySQL)(ctx)) {
49
39
  funcName = 'json_length';
50
40
  }
51
-
52
41
  return `(select ${ifNull}(${funcName}(${fieldName}), 0) ${operator} 0)`;
53
42
  };
54
-
55
43
  var _default = {
56
44
  $match(value, ctx) {
57
45
  const fieldName = getFieldName(ctx);
58
-
59
46
  if ((0, _utils.isPg)(ctx)) {
60
47
  return {
61
48
  [_sequelize().Op.contained]: value,
62
49
  [_sequelize().Op.contains]: value
63
50
  };
64
51
  }
65
-
66
52
  value = escape(JSON.stringify(value.sort()), ctx);
67
-
68
53
  if ((0, _utils.isMySQL)(ctx)) {
69
54
  const name = ctx.fullName === fieldName ? `\`${ctx.model.name}\`.\`${fieldName}\`` : `\`${fieldName}\``;
70
55
  return _sequelize().Sequelize.literal(`JSON_CONTAINS(${name}, ${value}) AND JSON_CONTAINS(${value}, ${name})`);
71
56
  }
72
-
73
57
  return {
74
58
  [_sequelize().Op.eq]: _sequelize().Sequelize.literal(`json(${value})`)
75
59
  };
76
60
  },
77
-
78
61
  $notMatch(value, ctx) {
79
62
  const fieldName = getFieldName(ctx);
80
63
  value = escape(JSON.stringify(value), ctx);
81
-
82
64
  if ((0, _utils.isPg)(ctx)) {
83
65
  const name = ctx.fullName === fieldName ? `"${ctx.model.name}"."${fieldName}"` : `"${fieldName}"`;
84
66
  return _sequelize().Sequelize.literal(`not (${name} <@ ${value}::JSONB and ${name} @> ${value}::JSONB)`);
85
67
  }
86
-
87
68
  if ((0, _utils.isMySQL)(ctx)) {
88
69
  const name = ctx.fullName === fieldName ? `\`${ctx.model.name}\`.\`${fieldName}\`` : `\`${fieldName}\``;
89
70
  return _sequelize().Sequelize.literal(`not (JSON_CONTAINS(${name}, ${value}) AND JSON_CONTAINS(${value}, ${name}))`);
90
71
  }
91
-
92
72
  return {
93
73
  [_sequelize().Op.ne]: _sequelize().Sequelize.literal(`json(${value})`)
94
74
  };
95
75
  },
96
-
97
76
  $anyOf(value, ctx) {
98
77
  const fieldName = getFieldName(ctx);
99
-
100
78
  if ((0, _utils.isPg)(ctx)) {
101
79
  const name = ctx.fullName === fieldName ? `"${ctx.model.name}"."${fieldName}"` : `"${fieldName}"`;
102
80
  return _sequelize().Sequelize.literal(`${name} ?| ${escape(value.map(i => `${i}`), ctx)}`);
103
81
  }
104
-
105
82
  if ((0, _utils.isMySQL)(ctx)) {
106
83
  value = escape(JSON.stringify(value), ctx);
107
84
  const name = ctx.fullName === fieldName ? `\`${ctx.model.name}\`.\`${fieldName}\`` : `\`${fieldName}\``;
108
85
  return _sequelize().Sequelize.literal(`JSON_OVERLAPS(${name}, ${value})`);
109
86
  }
110
-
111
87
  const subQuery = sqliteExistQuery(value, ctx);
112
88
  return _sequelize().Sequelize.literal(subQuery);
113
89
  },
114
-
115
90
  $noneOf(value, ctx) {
116
91
  let where;
117
-
118
92
  if ((0, _utils.isPg)(ctx)) {
119
93
  const fieldName = getFieldName(ctx);
120
- const name = ctx.fullName === fieldName ? `"${ctx.model.name}"."${fieldName}"` : `"${fieldName}"`; // pg single quote
121
-
94
+ const name = ctx.fullName === fieldName ? `"${ctx.model.name}"."${fieldName}"` : `"${fieldName}"`;
95
+ // pg single quote
122
96
  where = _sequelize().Sequelize.literal(`not (${name} ?| ${escape(value.map(i => `${i}`), ctx)})`);
123
97
  } else if ((0, _utils.isMySQL)(ctx)) {
124
98
  const fieldName = getFieldName(ctx);
@@ -129,27 +103,23 @@ var _default = {
129
103
  const subQuery = sqliteExistQuery(value, ctx);
130
104
  where = _sequelize().Sequelize.literal(`not ${subQuery}`);
131
105
  }
132
-
133
106
  return {
134
107
  [_sequelize().Op.or]: [where, {
135
108
  [_sequelize().Op.is]: null
136
109
  }]
137
110
  };
138
111
  },
139
-
140
112
  $arrayEmpty(value, ctx) {
141
113
  const subQuery = emptyQuery(ctx, '=');
142
114
  return {
143
115
  [_sequelize().Op.and]: [_sequelize().Sequelize.literal(`${subQuery}`)]
144
116
  };
145
117
  },
146
-
147
118
  $arrayNotEmpty(value, ctx) {
148
119
  const subQuery = emptyQuery(ctx, '>');
149
120
  return {
150
121
  [_sequelize().Op.and]: [_sequelize().Sequelize.literal(`${subQuery}`)]
151
122
  };
152
123
  }
153
-
154
124
  };
155
125
  exports.default = _default;
@@ -4,29 +4,23 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  function _sequelize() {
9
8
  const data = require("sequelize");
10
-
11
9
  _sequelize = function _sequelize() {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  var _default = {
19
15
  $exists(value, ctx) {
20
16
  return {
21
17
  [_sequelize().Op.not]: null
22
18
  };
23
19
  },
24
-
25
20
  $notExists(value, ctx) {
26
21
  return {
27
22
  [_sequelize().Op.is]: null
28
23
  };
29
24
  }
30
-
31
25
  };
32
26
  exports.default = _default;
@@ -4,17 +4,13 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  function _sequelize() {
9
8
  const data = require("sequelize");
10
-
11
9
  _sequelize = function _sequelize() {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  var _default = {
19
15
  $isFalsy() {
20
16
  return {
@@ -24,12 +20,10 @@ var _default = {
24
20
  }
25
21
  };
26
22
  },
27
-
28
23
  $isTruly() {
29
24
  return {
30
25
  [_sequelize().Op.eq]: true
31
26
  };
32
27
  }
33
-
34
28
  };
35
29
  exports.default = _default;
@@ -0,0 +1,2 @@
1
+ declare const _default: Record<string, any>;
2
+ export default _default;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ function _sequelize() {
8
+ const data = require("sequelize");
9
+ _sequelize = function _sequelize() {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ const mapVal = (values, db) => values.map(v => {
15
+ const collection = db.getCollection(v);
16
+ return _sequelize().Sequelize.literal(`'${collection.tableNameAsString()}'::regclass`);
17
+ });
18
+ var _default = {
19
+ $childIn(values, ctx) {
20
+ const db = ctx.db;
21
+ return {
22
+ [_sequelize().Op.in]: mapVal(values, db)
23
+ };
24
+ },
25
+ $childNotIn(values, ctx) {
26
+ const db = ctx.db;
27
+ return {
28
+ [_sequelize().Op.notIn]: mapVal(values, db)
29
+ };
30
+ }
31
+ };
32
+ exports.default = _default;