@tachybase/database 0.23.8

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 (226) hide show
  1. package/.turbo/turbo-build.log +10 -0
  2. package/LICENSE +201 -0
  3. package/lib/collection-factory.d.ts +17 -0
  4. package/lib/collection-factory.js +52 -0
  5. package/lib/collection-group-manager.d.ts +26 -0
  6. package/lib/collection-group-manager.js +57 -0
  7. package/lib/collection-importer.d.ts +7 -0
  8. package/lib/collection-importer.js +74 -0
  9. package/lib/collection.d.ts +138 -0
  10. package/lib/collection.js +686 -0
  11. package/lib/database-utils/index.d.ts +9 -0
  12. package/lib/database-utils/index.js +71 -0
  13. package/lib/database-utils/traverseJSON.d.ts +10 -0
  14. package/lib/database-utils/traverseJSON.js +126 -0
  15. package/lib/database.d.ts +201 -0
  16. package/lib/database.js +765 -0
  17. package/lib/decorators/must-have-filter-decorator.d.ts +2 -0
  18. package/lib/decorators/must-have-filter-decorator.js +38 -0
  19. package/lib/decorators/target-collection-decorator.d.ts +2 -0
  20. package/lib/decorators/target-collection-decorator.js +47 -0
  21. package/lib/decorators/transaction-decorator.d.ts +1 -0
  22. package/lib/decorators/transaction-decorator.js +88 -0
  23. package/lib/eager-loading/eager-loading-tree.d.ts +31 -0
  24. package/lib/eager-loading/eager-loading-tree.js +439 -0
  25. package/lib/errors/identifier-error.d.ts +3 -0
  26. package/lib/errors/identifier-error.js +35 -0
  27. package/lib/errors/zero-column-table-error.d.ts +2 -0
  28. package/lib/errors/zero-column-table-error.js +31 -0
  29. package/lib/features/references-map.d.ts +18 -0
  30. package/lib/features/references-map.js +109 -0
  31. package/lib/features/referential-integrity-check.d.ts +8 -0
  32. package/lib/features/referential-integrity-check.js +83 -0
  33. package/lib/field-repository/array-field-repository.d.ts +28 -0
  34. package/lib/field-repository/array-field-repository.js +197 -0
  35. package/lib/fields/array-field.d.ts +11 -0
  36. package/lib/fields/array-field.js +57 -0
  37. package/lib/fields/belongs-to-field.d.ts +17 -0
  38. package/lib/fields/belongs-to-field.js +154 -0
  39. package/lib/fields/belongs-to-many-field.d.ts +20 -0
  40. package/lib/fields/belongs-to-many-field.js +187 -0
  41. package/lib/fields/boolean-field.d.ts +8 -0
  42. package/lib/fields/boolean-field.js +36 -0
  43. package/lib/fields/context-field.d.ts +14 -0
  44. package/lib/fields/context-field.js +70 -0
  45. package/lib/fields/date-field.d.ts +13 -0
  46. package/lib/fields/date-field.js +64 -0
  47. package/lib/fields/field.d.ts +43 -0
  48. package/lib/fields/field.js +156 -0
  49. package/lib/fields/has-inverse-field.d.ts +4 -0
  50. package/lib/fields/has-inverse-field.js +15 -0
  51. package/lib/fields/has-many-field.d.ts +68 -0
  52. package/lib/fields/has-many-field.js +156 -0
  53. package/lib/fields/has-one-field.d.ts +68 -0
  54. package/lib/fields/has-one-field.js +150 -0
  55. package/lib/fields/index.d.ts +46 -0
  56. package/lib/fields/index.js +65 -0
  57. package/lib/fields/json-field.d.ts +14 -0
  58. package/lib/fields/json-field.js +54 -0
  59. package/lib/fields/nanoid-field.d.ts +13 -0
  60. package/lib/fields/nanoid-field.js +58 -0
  61. package/lib/fields/number-field.d.ts +41 -0
  62. package/lib/fields/number-field.js +85 -0
  63. package/lib/fields/password-field.d.ts +21 -0
  64. package/lib/fields/password-field.js +95 -0
  65. package/lib/fields/radio-field.d.ts +16 -0
  66. package/lib/fields/radio-field.js +66 -0
  67. package/lib/fields/relation-field.d.ts +21 -0
  68. package/lib/fields/relation-field.js +79 -0
  69. package/lib/fields/set-field.d.ts +10 -0
  70. package/lib/fields/set-field.js +46 -0
  71. package/lib/fields/sort-field.d.ts +16 -0
  72. package/lib/fields/sort-field.js +187 -0
  73. package/lib/fields/string-field.d.ts +8 -0
  74. package/lib/fields/string-field.js +36 -0
  75. package/lib/fields/text-field.d.ts +8 -0
  76. package/lib/fields/text-field.js +36 -0
  77. package/lib/fields/time-field.d.ts +8 -0
  78. package/lib/fields/time-field.js +36 -0
  79. package/lib/fields/uid-field.d.ts +13 -0
  80. package/lib/fields/uid-field.js +63 -0
  81. package/lib/fields/uuid-field.d.ts +9 -0
  82. package/lib/fields/uuid-field.js +45 -0
  83. package/lib/fields/virtual-field.d.ts +8 -0
  84. package/lib/fields/virtual-field.js +36 -0
  85. package/lib/filter-match.d.ts +1 -0
  86. package/lib/filter-match.js +66 -0
  87. package/lib/filter-parser.d.ts +21 -0
  88. package/lib/filter-parser.js +230 -0
  89. package/lib/helpers.d.ts +3 -0
  90. package/lib/helpers.js +167 -0
  91. package/lib/index.d.ts +30 -0
  92. package/lib/index.js +114 -0
  93. package/lib/inherited-collection.d.ts +14 -0
  94. package/lib/inherited-collection.js +138 -0
  95. package/lib/inherited-map.d.ts +21 -0
  96. package/lib/inherited-map.js +113 -0
  97. package/lib/inherited-sync-runner.d.ts +4 -0
  98. package/lib/inherited-sync-runner.js +175 -0
  99. package/lib/listeners/adjacency-list.d.ts +2 -0
  100. package/lib/listeners/adjacency-list.js +42 -0
  101. package/lib/listeners/append-child-collection-name-after-repository-find.d.ts +6 -0
  102. package/lib/listeners/append-child-collection-name-after-repository-find.js +66 -0
  103. package/lib/listeners/index.d.ts +2 -0
  104. package/lib/listeners/index.js +33 -0
  105. package/lib/magic-attribute-model.d.ts +8 -0
  106. package/lib/magic-attribute-model.js +203 -0
  107. package/lib/migration.d.ts +35 -0
  108. package/lib/migration.js +103 -0
  109. package/lib/mock-database.d.ts +25 -0
  110. package/lib/mock-database.js +126 -0
  111. package/lib/model-hook.d.ts +12 -0
  112. package/lib/model-hook.js +92 -0
  113. package/lib/model.d.ts +23 -0
  114. package/lib/model.js +152 -0
  115. package/lib/operators/array.d.ts +2 -0
  116. package/lib/operators/array.js +158 -0
  117. package/lib/operators/association.d.ts +2 -0
  118. package/lib/operators/association.js +35 -0
  119. package/lib/operators/boolean.d.ts +2 -0
  120. package/lib/operators/boolean.js +38 -0
  121. package/lib/operators/child-collection.d.ts +2 -0
  122. package/lib/operators/child-collection.js +67 -0
  123. package/lib/operators/date.d.ts +2 -0
  124. package/lib/operators/date.js +140 -0
  125. package/lib/operators/empty.d.ts +5 -0
  126. package/lib/operators/empty.js +89 -0
  127. package/lib/operators/eq.d.ts +2 -0
  128. package/lib/operators/eq.js +35 -0
  129. package/lib/operators/index.d.ts +5 -0
  130. package/lib/operators/index.js +56 -0
  131. package/lib/operators/jsonb.d.ts +2 -0
  132. package/lib/operators/jsonb.js +50 -0
  133. package/lib/operators/ne.d.ts +2 -0
  134. package/lib/operators/ne.js +40 -0
  135. package/lib/operators/notIn.d.ts +2 -0
  136. package/lib/operators/notIn.js +33 -0
  137. package/lib/operators/string.d.ts +2 -0
  138. package/lib/operators/string.js +109 -0
  139. package/lib/operators/utils.d.ts +4 -0
  140. package/lib/operators/utils.js +40 -0
  141. package/lib/options-parser.d.ts +37 -0
  142. package/lib/options-parser.js +332 -0
  143. package/lib/playground.d.ts +1 -0
  144. package/lib/playground.js +43 -0
  145. package/lib/query-interface/mysql-query-interface.d.ts +35 -0
  146. package/lib/query-interface/mysql-query-interface.js +122 -0
  147. package/lib/query-interface/postgres-query-interface.d.ts +32 -0
  148. package/lib/query-interface/postgres-query-interface.js +189 -0
  149. package/lib/query-interface/query-interface-builder.d.ts +2 -0
  150. package/lib/query-interface/query-interface-builder.js +46 -0
  151. package/lib/query-interface/query-interface.d.ts +42 -0
  152. package/lib/query-interface/query-interface.js +46 -0
  153. package/lib/query-interface/sqlite-query-interface.d.ts +35 -0
  154. package/lib/query-interface/sqlite-query-interface.js +131 -0
  155. package/lib/relation-repository/belongs-to-many-repository.d.ts +21 -0
  156. package/lib/relation-repository/belongs-to-many-repository.js +280 -0
  157. package/lib/relation-repository/belongs-to-repository.d.ts +6 -0
  158. package/lib/relation-repository/belongs-to-repository.js +39 -0
  159. package/lib/relation-repository/hasmany-repository.d.ts +10 -0
  160. package/lib/relation-repository/hasmany-repository.js +182 -0
  161. package/lib/relation-repository/hasone-repository.d.ts +6 -0
  162. package/lib/relation-repository/hasone-repository.js +39 -0
  163. package/lib/relation-repository/multiple-relation-repository.d.ts +20 -0
  164. package/lib/relation-repository/multiple-relation-repository.js +229 -0
  165. package/lib/relation-repository/relation-repository.d.ts +29 -0
  166. package/lib/relation-repository/relation-repository.js +202 -0
  167. package/lib/relation-repository/single-relation-repository.d.ts +25 -0
  168. package/lib/relation-repository/single-relation-repository.js +162 -0
  169. package/lib/relation-repository/types.d.ts +7 -0
  170. package/lib/relation-repository/types.js +15 -0
  171. package/lib/repositories/tree-repository/adjacency-list-repository.d.ts +18 -0
  172. package/lib/repositories/tree-repository/adjacency-list-repository.js +183 -0
  173. package/lib/repositories/view-repository.d.ts +3 -0
  174. package/lib/repositories/view-repository.js +32 -0
  175. package/lib/repository.d.ts +206 -0
  176. package/lib/repository.js +597 -0
  177. package/lib/sql-collection/index.d.ts +2 -0
  178. package/lib/sql-collection/index.js +23 -0
  179. package/lib/sql-collection/query-generator.d.ts +10 -0
  180. package/lib/sql-collection/query-generator.js +70 -0
  181. package/lib/sql-collection/sql-collection.d.ts +7 -0
  182. package/lib/sql-collection/sql-collection.js +69 -0
  183. package/lib/sql-collection/sql-model.d.ts +16 -0
  184. package/lib/sql-collection/sql-model.js +168 -0
  185. package/lib/sql-parser/index.d.ts +10 -0
  186. package/lib/sql-parser/index.js +10699 -0
  187. package/lib/sql-parser/postgres.d.ts +10 -0
  188. package/lib/sql-parser/postgres.js +31506 -0
  189. package/lib/sql-parser/sql.pegjs +1297 -0
  190. package/lib/sync-runner.d.ts +30 -0
  191. package/lib/sync-runner.js +277 -0
  192. package/lib/types.d.ts +49 -0
  193. package/lib/types.js +15 -0
  194. package/lib/update-associations.d.ts +60 -0
  195. package/lib/update-associations.js +405 -0
  196. package/lib/update-guard.d.ts +28 -0
  197. package/lib/update-guard.js +202 -0
  198. package/lib/utils.d.ts +7 -0
  199. package/lib/utils.js +127 -0
  200. package/lib/value-parsers/array-value-parser.d.ts +8 -0
  201. package/lib/value-parsers/array-value-parser.js +58 -0
  202. package/lib/value-parsers/base-value-parser.d.ts +12 -0
  203. package/lib/value-parsers/base-value-parser.js +63 -0
  204. package/lib/value-parsers/boolean-value-parser.d.ts +4 -0
  205. package/lib/value-parsers/boolean-value-parser.js +52 -0
  206. package/lib/value-parsers/date-value-parser.d.ts +5 -0
  207. package/lib/value-parsers/date-value-parser.js +88 -0
  208. package/lib/value-parsers/index.d.ts +12 -0
  209. package/lib/value-parsers/index.js +78 -0
  210. package/lib/value-parsers/json-value-parser.d.ts +4 -0
  211. package/lib/value-parsers/json-value-parser.js +47 -0
  212. package/lib/value-parsers/number-value-parser.d.ts +4 -0
  213. package/lib/value-parsers/number-value-parser.js +58 -0
  214. package/lib/value-parsers/string-value-parser.d.ts +8 -0
  215. package/lib/value-parsers/string-value-parser.js +64 -0
  216. package/lib/value-parsers/to-many-value-parser.d.ts +13 -0
  217. package/lib/value-parsers/to-many-value-parser.js +108 -0
  218. package/lib/value-parsers/to-one-value-parser.d.ts +4 -0
  219. package/lib/value-parsers/to-one-value-parser.js +48 -0
  220. package/lib/view/field-type-map.d.ts +97 -0
  221. package/lib/view/field-type-map.js +90 -0
  222. package/lib/view/view-inference.d.ts +32 -0
  223. package/lib/view/view-inference.js +146 -0
  224. package/lib/view-collection.d.ts +6 -0
  225. package/lib/view-collection.js +45 -0
  226. package/package.json +38 -0
@@ -0,0 +1,203 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var magic_attribute_model_exports = {};
30
+ __export(magic_attribute_model_exports, {
31
+ MagicAttributeModel: () => MagicAttributeModel
32
+ });
33
+ module.exports = __toCommonJS(magic_attribute_model_exports);
34
+ var import_utils = require("@tachybase/utils");
35
+ var import_lodash = __toESM(require("lodash"));
36
+ var import_sequelize = require("sequelize");
37
+ var import_model = require("./model");
38
+ const Dottie = require("dottie");
39
+ const _MagicAttributeModel = class _MagicAttributeModel extends import_model.Model {
40
+ get magicAttribute() {
41
+ const db = this.constructor.database;
42
+ const collection = db.getCollection(this.constructor.name);
43
+ return collection.options.magicAttribute || "options";
44
+ }
45
+ set(key, value, options) {
46
+ if (typeof key === "string") {
47
+ const [column] = key.split(".");
48
+ if (this.constructor.hasAlias(column)) {
49
+ return this.setV1(key, value, options);
50
+ }
51
+ if (this.constructor.rawAttributes[column]) {
52
+ return this.setV1(key, value, options);
53
+ }
54
+ if (import_lodash.default.isPlainObject(value)) {
55
+ const opts = super.get(this.magicAttribute) || {};
56
+ return this.setV1(`${this.magicAttribute}.${key}`, (0, import_utils.merge)(opts == null ? void 0 : opts[key], value), options);
57
+ }
58
+ return this.setV1(`${this.magicAttribute}.${key}`, value, options);
59
+ } else {
60
+ if (!key) {
61
+ return;
62
+ }
63
+ Object.keys(key).forEach((k) => {
64
+ this.setV1(k, key[k], options);
65
+ });
66
+ }
67
+ return this.setV1(key, value, options);
68
+ }
69
+ setV1(key, value, options) {
70
+ let values;
71
+ let originalValue;
72
+ if (typeof key === "object" && key !== null) {
73
+ values = key;
74
+ options = value || {};
75
+ if (options.reset) {
76
+ this.dataValues = {};
77
+ for (const key2 in values) {
78
+ this.changed(key2, false);
79
+ }
80
+ }
81
+ if (options.raw && // @ts-ignore
82
+ !(this._options && this._options.include) && !(options && options.attributes) && // @ts-ignore
83
+ !this.constructor._hasDateAttributes && // @ts-ignore
84
+ !this.constructor._hasBooleanAttributes) {
85
+ if (Object.keys(this.dataValues).length) {
86
+ Object.assign(this.dataValues, values);
87
+ } else {
88
+ this.dataValues = values;
89
+ }
90
+ this._previousDataValues = { ...this.dataValues };
91
+ } else {
92
+ if (options.attributes) {
93
+ const setKeys = /* @__PURE__ */ __name((data) => {
94
+ for (const k of data) {
95
+ if (values[k] === void 0) {
96
+ continue;
97
+ }
98
+ this.set(k, values[k], options);
99
+ }
100
+ }, "setKeys");
101
+ setKeys(options.attributes);
102
+ if (this.constructor._hasVirtualAttributes) {
103
+ setKeys(this.constructor._virtualAttributes);
104
+ }
105
+ if (this._options.includeNames) {
106
+ setKeys(this._options.includeNames);
107
+ }
108
+ } else {
109
+ for (const key2 in values) {
110
+ this.set(key2, values[key2], options);
111
+ }
112
+ }
113
+ if (options.raw) {
114
+ this._previousDataValues = { ...this.dataValues };
115
+ }
116
+ }
117
+ return this;
118
+ }
119
+ if (!options) options = {};
120
+ if (!options.raw) {
121
+ originalValue = this.dataValues[key];
122
+ }
123
+ if (!options.raw && this._customSetters[key]) {
124
+ this._customSetters[key].call(this, value, key);
125
+ const newValue = this.dataValues[key];
126
+ if (!import_lodash.default.isEqual(newValue, originalValue)) {
127
+ this._previousDataValues[key] = originalValue;
128
+ this.changed(key, true);
129
+ }
130
+ } else {
131
+ if (this._options && this._options.include && this._options.includeNames.includes(key)) {
132
+ this._setInclude(key, value, options);
133
+ return this;
134
+ }
135
+ if (!options.raw) {
136
+ if (!this._isAttribute(key)) {
137
+ if (key.includes(".") && this.constructor._jsonAttributes.has(key.split(".")[0])) {
138
+ const previousNestedValue = Dottie.get(this.dataValues, key);
139
+ if (!import_lodash.default.isEqual(previousNestedValue, value)) {
140
+ this._previousDataValues = import_lodash.default.cloneDeep(this._previousDataValues);
141
+ Dottie.set(this.dataValues, key, value);
142
+ this.changed(key.split(".")[0], true);
143
+ }
144
+ }
145
+ return this;
146
+ }
147
+ if (this.constructor._hasPrimaryKeys && originalValue && this.constructor._isPrimaryKey(key)) {
148
+ return this;
149
+ }
150
+ if (!this.isNewRecord && // @ts-ignore
151
+ this.constructor._hasReadOnlyAttributes && // @ts-ignore
152
+ this.constructor._readOnlyAttributes.has(key)) {
153
+ return this;
154
+ }
155
+ }
156
+ if (!(value instanceof import_sequelize.Utils.SequelizeMethod) && // @ts-ignore
157
+ Object.prototype.hasOwnProperty.call(this.constructor._dataTypeSanitizers, key)) {
158
+ value = this.constructor._dataTypeSanitizers[key].call(this, value, options);
159
+ }
160
+ if (!options.raw && // True when sequelize method
161
+ (value instanceof import_sequelize.Utils.SequelizeMethod || // Check for data type type comparators
162
+ // @ts-ignore
163
+ !(value instanceof import_sequelize.Utils.SequelizeMethod) && // @ts-ignore
164
+ this.constructor._dataTypeChanges[key] && // @ts-ignore
165
+ this.constructor._dataTypeChanges[key].call(this, value, originalValue, options) || // Check default
166
+ // @ts-ignore
167
+ !this.constructor._dataTypeChanges[key] && !import_lodash.default.isEqual(value, originalValue))) {
168
+ this._previousDataValues[key] = originalValue;
169
+ this.changed(key, true);
170
+ }
171
+ this.dataValues[key] = value;
172
+ }
173
+ return this;
174
+ }
175
+ get(key, value) {
176
+ if (typeof key === "string") {
177
+ const [column] = key.split(".");
178
+ if (this.constructor.hasAlias(column)) {
179
+ return super.get(key, value);
180
+ }
181
+ if (this.constructor.rawAttributes[column]) {
182
+ return super.get(key, value);
183
+ }
184
+ const options = super.get(this.magicAttribute, value);
185
+ return import_lodash.default.get(options, key);
186
+ }
187
+ const data = super.get(key, value);
188
+ return {
189
+ ...import_lodash.default.omit(data, this.magicAttribute),
190
+ ...data[this.magicAttribute]
191
+ };
192
+ }
193
+ async update(values, options) {
194
+ this._changed = /* @__PURE__ */ new Set();
195
+ return super.update(values, options);
196
+ }
197
+ };
198
+ __name(_MagicAttributeModel, "MagicAttributeModel");
199
+ let MagicAttributeModel = _MagicAttributeModel;
200
+ // Annotate the CommonJS export names for ESM import in node:
201
+ 0 && (module.exports = {
202
+ MagicAttributeModel
203
+ });
@@ -0,0 +1,35 @@
1
+ import { QueryInterface, Sequelize } from 'sequelize';
2
+ import Database from './database';
3
+ export interface MigrationContext {
4
+ db: Database;
5
+ queryInterface: QueryInterface;
6
+ sequelize: Sequelize;
7
+ }
8
+ export declare class Migration {
9
+ name: string;
10
+ context: {
11
+ db: Database;
12
+ [key: string]: any;
13
+ };
14
+ constructor(context: MigrationContext);
15
+ get db(): Database;
16
+ get sequelize(): Sequelize;
17
+ get queryInterface(): QueryInterface;
18
+ up(): Promise<void>;
19
+ down(): Promise<void>;
20
+ }
21
+ export interface MigrationItem {
22
+ name: string;
23
+ migration?: typeof Migration | string;
24
+ context?: any;
25
+ up?: any;
26
+ down?: any;
27
+ }
28
+ export declare class Migrations {
29
+ items: any[];
30
+ context: any;
31
+ constructor(context: any);
32
+ clear(): void;
33
+ add(item: MigrationItem): void;
34
+ callback(): (ctx: any) => Promise<any[]>;
35
+ }
@@ -0,0 +1,103 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var migration_exports = {};
30
+ __export(migration_exports, {
31
+ Migration: () => Migration,
32
+ Migrations: () => Migrations
33
+ });
34
+ module.exports = __toCommonJS(migration_exports);
35
+ var import_utils = require("@tachybase/utils");
36
+ var import_lodash = __toESM(require("lodash"));
37
+ const _Migration = class _Migration {
38
+ name;
39
+ context;
40
+ constructor(context) {
41
+ this.context = context;
42
+ }
43
+ get db() {
44
+ return this.context.db;
45
+ }
46
+ get sequelize() {
47
+ return this.context.db.sequelize;
48
+ }
49
+ get queryInterface() {
50
+ return this.context.db.sequelize.getQueryInterface();
51
+ }
52
+ async up() {
53
+ }
54
+ async down() {
55
+ }
56
+ };
57
+ __name(_Migration, "Migration");
58
+ let Migration = _Migration;
59
+ const _Migrations = class _Migrations {
60
+ items = [];
61
+ context;
62
+ constructor(context) {
63
+ this.context = context;
64
+ }
65
+ clear() {
66
+ this.items = [];
67
+ }
68
+ add(item) {
69
+ const Migration2 = item.migration;
70
+ if (Migration2 && typeof Migration2 === "function") {
71
+ const migration = new Migration2({ ...this.context, ...item.context });
72
+ migration.name = item.name;
73
+ this.items.push(migration);
74
+ } else {
75
+ this.items.push(item);
76
+ }
77
+ }
78
+ callback() {
79
+ return async (ctx) => {
80
+ return await Promise.all(
81
+ import_lodash.default.sortBy(this.items, (item) => {
82
+ const keys = item.name.split("/");
83
+ return keys.pop() || item.name;
84
+ }).map(async (item) => {
85
+ if (typeof item.migration === "string") {
86
+ const Migration2 = await (0, import_utils.importModule)(item.migration);
87
+ const migration = new Migration2({ ...this.context, ...item.context });
88
+ migration.name = item.name;
89
+ return migration;
90
+ }
91
+ return item;
92
+ })
93
+ );
94
+ };
95
+ }
96
+ };
97
+ __name(_Migrations, "Migrations");
98
+ let Migrations = _Migrations;
99
+ // Annotate the CommonJS export names for ESM import in node:
100
+ 0 && (module.exports = {
101
+ Migration,
102
+ Migrations
103
+ });
@@ -0,0 +1,25 @@
1
+ import { Database, IDatabaseOptions } from './database';
2
+ export declare class MockDatabase extends Database {
3
+ constructor(options: IDatabaseOptions);
4
+ }
5
+ export declare function getConfigByEnv(): {
6
+ username: string;
7
+ password: string;
8
+ database: string;
9
+ host: string;
10
+ port: string;
11
+ dialect: string;
12
+ logging: boolean | typeof customLogger;
13
+ storage: string;
14
+ define: {
15
+ charset: string;
16
+ collate: string;
17
+ };
18
+ timezone: string;
19
+ underscored: boolean;
20
+ schema: string;
21
+ dialectOptions: {};
22
+ };
23
+ declare function customLogger(queryString: any, queryObject: any): void;
24
+ export declare function mockDatabase(options?: IDatabaseOptions): MockDatabase;
25
+ export {};
@@ -0,0 +1,126 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var mock_database_exports = {};
30
+ __export(mock_database_exports, {
31
+ MockDatabase: () => MockDatabase,
32
+ getConfigByEnv: () => getConfigByEnv,
33
+ mockDatabase: () => mockDatabase
34
+ });
35
+ module.exports = __toCommonJS(mock_database_exports);
36
+ var import_path = __toESM(require("path"));
37
+ var import_utils = require("@tachybase/utils");
38
+ var import_nanoid = require("nanoid");
39
+ var import_node_fetch = __toESM(require("node-fetch"));
40
+ var import_database = require("./database");
41
+ const _MockDatabase = class _MockDatabase extends import_database.Database {
42
+ constructor(options) {
43
+ super({
44
+ storage: ":memory:",
45
+ dialect: "sqlite",
46
+ ...options
47
+ });
48
+ }
49
+ };
50
+ __name(_MockDatabase, "MockDatabase");
51
+ let MockDatabase = _MockDatabase;
52
+ function getConfigByEnv() {
53
+ const options = {
54
+ username: process.env.DB_USER,
55
+ password: process.env.DB_PASSWORD,
56
+ database: process.env.DB_DATABASE,
57
+ host: process.env.DB_HOST,
58
+ port: process.env.DB_PORT,
59
+ dialect: process.env.DB_DIALECT || "sqlite",
60
+ logging: process.env.DB_LOGGING === "on" ? customLogger : false,
61
+ storage: process.env.DB_STORAGE,
62
+ define: {
63
+ charset: "utf8mb4",
64
+ collate: "utf8mb4_unicode_ci"
65
+ },
66
+ timezone: process.env.DB_TIMEZONE,
67
+ underscored: process.env.DB_UNDERSCORED === "true",
68
+ schema: process.env.DB_SCHEMA !== "public" ? process.env.DB_SCHEMA : void 0,
69
+ dialectOptions: {}
70
+ };
71
+ if (process.env.DB_DIALECT == "postgres") {
72
+ options.dialectOptions["application_name"] = "tachybase.main";
73
+ }
74
+ return options;
75
+ }
76
+ __name(getConfigByEnv, "getConfigByEnv");
77
+ function customLogger(queryString, queryObject) {
78
+ console.log(queryString);
79
+ if (queryObject.bind) {
80
+ console.log(queryObject.bind);
81
+ }
82
+ }
83
+ __name(customLogger, "customLogger");
84
+ function mockDatabase(options = {}) {
85
+ const dbOptions = (0, import_utils.merge)(getConfigByEnv(), options);
86
+ if (process.env["DB_TEST_PREFIX"]) {
87
+ let configKey = "database";
88
+ if (dbOptions.dialect === "sqlite") {
89
+ configKey = "storage";
90
+ } else {
91
+ configKey = "database";
92
+ }
93
+ const shouldChange = /* @__PURE__ */ __name(() => {
94
+ if (dbOptions.dialect === "sqlite") {
95
+ return !dbOptions[configKey].includes(process.env["DB_TEST_PREFIX"]);
96
+ }
97
+ return !dbOptions[configKey].startsWith(process.env["DB_TEST_PREFIX"]);
98
+ }, "shouldChange");
99
+ if (dbOptions[configKey] && shouldChange()) {
100
+ const nanoid = (0, import_nanoid.customAlphabet)("1234567890abcdefghijklmnopqrstuvwxyz", 10);
101
+ const instanceId = `d_${nanoid()}`;
102
+ const databaseName = `${process.env["DB_TEST_PREFIX"]}_${instanceId}`;
103
+ if (dbOptions.dialect === "sqlite") {
104
+ dbOptions.storage = import_path.default.resolve(import_path.default.dirname(dbOptions.storage), databaseName);
105
+ } else {
106
+ dbOptions.database = databaseName;
107
+ }
108
+ }
109
+ if (process.env["DB_TEST_DISTRIBUTOR_PORT"]) {
110
+ dbOptions.hooks = dbOptions.hooks || {};
111
+ dbOptions.hooks.beforeConnect = async (config) => {
112
+ const url = `http://127.0.0.1:${process.env["DB_TEST_DISTRIBUTOR_PORT"]}/acquire?via=${db.instanceId}&name=${config.database}`;
113
+ await (0, import_node_fetch.default)(url);
114
+ };
115
+ }
116
+ }
117
+ const db = new MockDatabase(dbOptions);
118
+ return db;
119
+ }
120
+ __name(mockDatabase, "mockDatabase");
121
+ // Annotate the CommonJS export names for ESM import in node:
122
+ 0 && (module.exports = {
123
+ MockDatabase,
124
+ getConfigByEnv,
125
+ mockDatabase
126
+ });
@@ -0,0 +1,12 @@
1
+ import { SequelizeHooks } from 'sequelize/types/hooks';
2
+ import Database from './database';
3
+ export declare class ModelHook {
4
+ database: Database;
5
+ boundEvents: Set<string>;
6
+ constructor(database: Database);
7
+ match(event: string | symbol): keyof SequelizeHooks | null;
8
+ findModelName(hookArgs: any): any;
9
+ bindEvent(type: any): void;
10
+ hasBoundEvent(type: any): boolean;
11
+ buildSequelizeHook(type: any): (...args: any[]) => Promise<void>;
12
+ }
@@ -0,0 +1,92 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var model_hook_exports = {};
30
+ __export(model_hook_exports, {
31
+ ModelHook: () => ModelHook
32
+ });
33
+ module.exports = __toCommonJS(model_hook_exports);
34
+ var import_lodash = __toESM(require("lodash"));
35
+ const { hooks } = require("sequelize/lib/hooks");
36
+ const _ModelHook = class _ModelHook {
37
+ database;
38
+ boundEvents = /* @__PURE__ */ new Set();
39
+ constructor(database) {
40
+ this.database = database;
41
+ }
42
+ match(event) {
43
+ if (!import_lodash.default.isString(event)) {
44
+ return null;
45
+ }
46
+ const type = event.split(".").pop();
47
+ return type in hooks ? type : null;
48
+ }
49
+ findModelName(hookArgs) {
50
+ var _a, _b;
51
+ for (const arg of hookArgs) {
52
+ if (arg == null ? void 0 : arg._previousDataValues) {
53
+ return arg.constructor.name;
54
+ }
55
+ if (import_lodash.default.isPlainObject(arg)) {
56
+ if (arg["model"]) {
57
+ return arg["model"].name;
58
+ }
59
+ const plural = (_a = arg == null ? void 0 : arg.name) == null ? void 0 : _a.plural;
60
+ if (this.database.sequelize.isDefined(plural)) {
61
+ return plural;
62
+ }
63
+ const singular = (_b = arg == null ? void 0 : arg.name) == null ? void 0 : _b.singular;
64
+ if (this.database.sequelize.isDefined(singular)) {
65
+ return singular;
66
+ }
67
+ }
68
+ }
69
+ return null;
70
+ }
71
+ bindEvent(type) {
72
+ this.boundEvents.add(type);
73
+ }
74
+ hasBoundEvent(type) {
75
+ return this.boundEvents.has(type);
76
+ }
77
+ buildSequelizeHook(type) {
78
+ return async (...args) => {
79
+ const modelName = this.findModelName(args);
80
+ if (modelName) {
81
+ await this.database.emitAsync(`${modelName}.${type}`, ...args);
82
+ }
83
+ await this.database.emitAsync(type, ...args);
84
+ };
85
+ }
86
+ };
87
+ __name(_ModelHook, "ModelHook");
88
+ let ModelHook = _ModelHook;
89
+ // Annotate the CommonJS export names for ESM import in node:
90
+ 0 && (module.exports = {
91
+ ModelHook
92
+ });
package/lib/model.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ import { Model as SequelizeModel } from 'sequelize';
2
+ import { Collection } from './collection';
3
+ import { Database } from './database';
4
+ interface IModel {
5
+ [key: string]: any;
6
+ }
7
+ export declare class Model<TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes> extends SequelizeModel<TModelAttributes, TCreationAttributes> implements IModel {
8
+ static database: Database;
9
+ static collection: Collection;
10
+ [key: string]: any;
11
+ protected _changedWithAssociations: Set<unknown>;
12
+ protected _previousDataValuesWithAssociations: {};
13
+ get db(): Database;
14
+ static sync(options: any): Promise<any>;
15
+ toChangedWithAssociations(): void;
16
+ changedWithAssociations(key?: string, value?: any): boolean | unknown[] | this;
17
+ clearChangedWithAssociations(): void;
18
+ toJSON<T extends TModelAttributes>(): T;
19
+ private hiddenObjKey;
20
+ private sortAssociations;
21
+ private sortArray;
22
+ }
23
+ export {};