@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,138 @@
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 inherited_collection_exports = {};
30
+ __export(inherited_collection_exports, {
31
+ InheritedCollection: () => InheritedCollection
32
+ });
33
+ module.exports = __toCommonJS(inherited_collection_exports);
34
+ var import_lodash = __toESM(require("lodash"));
35
+ var import_collection = require("./collection");
36
+ const _InheritedCollection = class _InheritedCollection extends import_collection.Collection {
37
+ parents;
38
+ constructor(options, context) {
39
+ if (!options.inherits) {
40
+ throw new Error("InheritedCollection must have inherits option");
41
+ }
42
+ options.inherits = import_lodash.default.castArray(options.inherits);
43
+ super(options, context);
44
+ try {
45
+ this.bindParents();
46
+ } catch (err) {
47
+ if (err instanceof ParentCollectionNotFound) {
48
+ const listener = /* @__PURE__ */ __name((collection) => {
49
+ if (options.inherits.includes(collection.name) && options.inherits.every((name) => this.db.collections.has(name))) {
50
+ this.bindParents();
51
+ this.db.removeListener("afterDefineCollection", listener);
52
+ }
53
+ }, "listener");
54
+ this.db.addListener("afterDefineCollection", listener);
55
+ } else {
56
+ throw err;
57
+ }
58
+ }
59
+ }
60
+ getParents() {
61
+ return this.parents;
62
+ }
63
+ getFlatParents() {
64
+ const parents = [];
65
+ for (const parent of this.parents) {
66
+ if (parent.isInherited()) {
67
+ parents.push(...parent.getFlatParents());
68
+ }
69
+ parents.push(parent);
70
+ }
71
+ return parents;
72
+ }
73
+ parentFields() {
74
+ const fields = /* @__PURE__ */ new Map();
75
+ for (const parent of this.parents) {
76
+ if (parent.isInherited()) {
77
+ for (const [name, field] of parent.parentFields()) {
78
+ fields.set(name, field.options);
79
+ }
80
+ }
81
+ const parentFields = parent.fields;
82
+ for (const [name, field] of parentFields) {
83
+ fields.set(name, field.options);
84
+ }
85
+ }
86
+ return fields;
87
+ }
88
+ parentAttributes() {
89
+ const attributes = {};
90
+ for (const parent of this.parents) {
91
+ if (parent.isInherited()) {
92
+ Object.assign(attributes, parent.parentAttributes());
93
+ }
94
+ const parentAttributes = parent.model.tableAttributes;
95
+ Object.assign(attributes, parentAttributes);
96
+ }
97
+ return attributes;
98
+ }
99
+ isInherited() {
100
+ return true;
101
+ }
102
+ bindParents() {
103
+ this.setParents(this.options.inherits);
104
+ this.setParentFields();
105
+ this.setFields(this.options.fields, false);
106
+ this.db.inheritanceMap.setInheritance(this.name, this.options.inherits);
107
+ }
108
+ setParents(inherits) {
109
+ this.parents = import_lodash.default.castArray(inherits).map((name) => {
110
+ const existCollection = this.db.collections.get(name);
111
+ if (!existCollection) {
112
+ throw new ParentCollectionNotFound(name);
113
+ }
114
+ return existCollection;
115
+ });
116
+ }
117
+ setParentFields() {
118
+ for (const [name, fieldOptions] of this.parentFields()) {
119
+ this.setField(name, {
120
+ ...fieldOptions,
121
+ inherit: true
122
+ });
123
+ }
124
+ }
125
+ };
126
+ __name(_InheritedCollection, "InheritedCollection");
127
+ let InheritedCollection = _InheritedCollection;
128
+ const _ParentCollectionNotFound = class _ParentCollectionNotFound extends Error {
129
+ constructor(name) {
130
+ super(`parent collection ${name} not found`);
131
+ }
132
+ };
133
+ __name(_ParentCollectionNotFound, "ParentCollectionNotFound");
134
+ let ParentCollectionNotFound = _ParentCollectionNotFound;
135
+ // Annotate the CommonJS export names for ESM import in node:
136
+ 0 && (module.exports = {
137
+ InheritedCollection
138
+ });
@@ -0,0 +1,21 @@
1
+ declare class TableNode {
2
+ name: string;
3
+ parents: Set<TableNode>;
4
+ children: Set<TableNode>;
5
+ constructor(name: string);
6
+ }
7
+ export default class InheritanceMap {
8
+ nodes: Map<string, TableNode>;
9
+ removeNode(name: string): void;
10
+ getOrCreateNode(name: string): TableNode;
11
+ getNode(name: string): TableNode;
12
+ setInheritance(name: string, inherits: string | string[]): void;
13
+ isParentNode(name: string): boolean;
14
+ getChildren(name: string, options?: {
15
+ deep: boolean;
16
+ }): Set<string>;
17
+ getParents(name: string, options?: {
18
+ deep: boolean;
19
+ }): Set<string>;
20
+ }
21
+ export {};
@@ -0,0 +1,113 @@
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 inherited_map_exports = {};
30
+ __export(inherited_map_exports, {
31
+ default: () => InheritanceMap
32
+ });
33
+ module.exports = __toCommonJS(inherited_map_exports);
34
+ var import_lodash = __toESM(require("lodash"));
35
+ const _TableNode = class _TableNode {
36
+ name;
37
+ parents;
38
+ children;
39
+ constructor(name) {
40
+ this.name = name;
41
+ this.parents = /* @__PURE__ */ new Set();
42
+ this.children = /* @__PURE__ */ new Set();
43
+ }
44
+ };
45
+ __name(_TableNode, "TableNode");
46
+ let TableNode = _TableNode;
47
+ const _InheritanceMap = class _InheritanceMap {
48
+ nodes = /* @__PURE__ */ new Map();
49
+ removeNode(name) {
50
+ const node = this.nodes.get(name);
51
+ if (!node) return;
52
+ for (const parent of node.parents) {
53
+ parent.children.delete(node);
54
+ }
55
+ for (const child of node.children) {
56
+ child.parents.delete(node);
57
+ }
58
+ this.nodes.delete(name);
59
+ }
60
+ getOrCreateNode(name) {
61
+ if (!this.nodes.has(name)) {
62
+ this.nodes.set(name, new TableNode(name));
63
+ }
64
+ return this.getNode(name);
65
+ }
66
+ getNode(name) {
67
+ return this.nodes.get(name);
68
+ }
69
+ setInheritance(name, inherits) {
70
+ const node = this.getOrCreateNode(name);
71
+ const parents = import_lodash.default.castArray(inherits).map((name2) => this.getOrCreateNode(name2));
72
+ node.parents = new Set(parents);
73
+ for (const parent of parents) {
74
+ parent.children.add(node);
75
+ }
76
+ }
77
+ isParentNode(name) {
78
+ const node = this.getNode(name);
79
+ return node && node.children.size > 0;
80
+ }
81
+ getChildren(name, options = { deep: true }) {
82
+ const results = /* @__PURE__ */ new Set();
83
+ const node = this.getNode(name);
84
+ if (!node) return results;
85
+ for (const child of node.children) {
86
+ results.add(child.name);
87
+ if (!options.deep) {
88
+ continue;
89
+ }
90
+ for (const grandchild of this.getChildren(child.name)) {
91
+ results.add(grandchild);
92
+ }
93
+ }
94
+ return results;
95
+ }
96
+ getParents(name, options = { deep: true }) {
97
+ const results = /* @__PURE__ */ new Set();
98
+ const node = this.getNode(name);
99
+ if (!node) return results;
100
+ for (const parent of node.parents) {
101
+ results.add(parent.name);
102
+ if (!options.deep) {
103
+ continue;
104
+ }
105
+ for (const grandparent of this.getParents(parent.name)) {
106
+ results.add(grandparent);
107
+ }
108
+ }
109
+ return results;
110
+ }
111
+ };
112
+ __name(_InheritanceMap, "InheritanceMap");
113
+ let InheritanceMap = _InheritanceMap;
@@ -0,0 +1,4 @@
1
+ export declare class InheritedSyncRunner {
2
+ static syncInheritModel(model: any, options: any): Promise<void>;
3
+ static createTable(tableName: any, attributes: any, options: any, model: any, parents: any): Promise<any>;
4
+ }
@@ -0,0 +1,175 @@
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 inherited_sync_runner_exports = {};
30
+ __export(inherited_sync_runner_exports, {
31
+ InheritedSyncRunner: () => InheritedSyncRunner
32
+ });
33
+ module.exports = __toCommonJS(inherited_sync_runner_exports);
34
+ var import_lodash = __toESM(require("lodash"));
35
+ const _InheritedSyncRunner = class _InheritedSyncRunner {
36
+ static async syncInheritModel(model, options) {
37
+ const { transaction } = options;
38
+ const inheritedCollection = model.collection;
39
+ const db = inheritedCollection.context.database;
40
+ const dialect = db.sequelize.getDialect();
41
+ const queryInterface = db.sequelize.getQueryInterface();
42
+ if (dialect != "postgres") {
43
+ throw new Error("Inherit model is only supported on postgres");
44
+ }
45
+ const parents = inheritedCollection.parents;
46
+ if (!parents) {
47
+ throw new Error(
48
+ `Inherit model ${inheritedCollection.name} can't be created without parents, parents option is ${import_lodash.default.castArray(inheritedCollection.options.inherits).join(", ")}`
49
+ );
50
+ }
51
+ for (const parent of parents) {
52
+ if (Object.keys(parent.model.rawAttributes).length === 0) {
53
+ throw new Error(
54
+ `can't inherit from collection ${parent.options.name} because it has no attributes, please define at least one attribute in parent collection`
55
+ );
56
+ }
57
+ }
58
+ const tableName = inheritedCollection.getTableNameWithSchema();
59
+ const attributes = model.tableAttributes;
60
+ const childAttributes = import_lodash.default.pickBy(attributes, (value) => {
61
+ return !value.inherit;
62
+ });
63
+ if (!await inheritedCollection.existsInDb({
64
+ transaction
65
+ })) {
66
+ let maxSequenceVal = 0;
67
+ let maxSequenceName;
68
+ if (childAttributes.id && childAttributes.id.autoIncrement) {
69
+ for (const parent of parents) {
70
+ const sequenceNameResult = await queryInterface.sequelize.query(
71
+ `SELECT column_default
72
+ FROM information_schema.columns
73
+ WHERE table_name = '${parent.model.tableName}'
74
+ and table_schema = '${parent.collectionSchema()}'
75
+ and "column_name" = 'id';`,
76
+ {
77
+ transaction
78
+ }
79
+ );
80
+ if (!sequenceNameResult[0].length) {
81
+ continue;
82
+ }
83
+ const columnDefault = sequenceNameResult[0][0]["column_default"];
84
+ if (!columnDefault) {
85
+ throw new Error(`Can't find sequence name of parent collection ${parent.options.name}`);
86
+ }
87
+ const regex = new RegExp(/nextval\('(.*)'::regclass\)/);
88
+ const match = regex.exec(columnDefault);
89
+ const sequenceName = match[1];
90
+ const sequenceCurrentValResult = await queryInterface.sequelize.query(
91
+ `select last_value
92
+ from ${sequenceName}`,
93
+ {
94
+ transaction
95
+ }
96
+ );
97
+ const sequenceCurrentVal = parseInt(sequenceCurrentValResult[0][0]["last_value"]);
98
+ if (sequenceCurrentVal > maxSequenceVal) {
99
+ maxSequenceName = sequenceName;
100
+ maxSequenceVal = sequenceCurrentVal;
101
+ }
102
+ }
103
+ }
104
+ await this.createTable(tableName, childAttributes, options, model, parents);
105
+ if (maxSequenceName) {
106
+ const parentsDeep = Array.from(db.inheritanceMap.getParents(inheritedCollection.name)).map(
107
+ (parent) => db.getCollection(parent).getTableNameWithSchema()
108
+ );
109
+ const sequenceTables = [...parentsDeep, tableName];
110
+ for (const sequenceTable of sequenceTables) {
111
+ const tableName2 = sequenceTable.tableName;
112
+ const schemaName = sequenceTable.schema;
113
+ const idColumnSql = `SELECT column_name
114
+ FROM information_schema.columns
115
+ WHERE table_name = '${tableName2}'
116
+ and column_name = 'id'
117
+ and table_schema = '${schemaName}';
118
+ `;
119
+ const idColumnQuery = await queryInterface.sequelize.query(idColumnSql, {
120
+ transaction
121
+ });
122
+ if (idColumnQuery[0].length == 0) {
123
+ continue;
124
+ }
125
+ await queryInterface.sequelize.query(
126
+ `alter table ${db.utils.quoteTable(sequenceTable)}
127
+ alter column id set default nextval('${maxSequenceName}')`,
128
+ {
129
+ transaction
130
+ }
131
+ );
132
+ }
133
+ }
134
+ }
135
+ if (options.alter) {
136
+ const columns = await queryInterface.describeTable(tableName, options);
137
+ for (const attribute in childAttributes) {
138
+ const columnName = childAttributes[attribute].field;
139
+ if (!columns[columnName]) {
140
+ await queryInterface.addColumn(tableName, columnName, childAttributes[columnName], options);
141
+ }
142
+ }
143
+ }
144
+ }
145
+ static async createTable(tableName, attributes, options, model, parents) {
146
+ let sql = "";
147
+ options = { ...options };
148
+ if (options && options.uniqueKeys) {
149
+ import_lodash.default.forOwn(options.uniqueKeys, (uniqueKey) => {
150
+ if (uniqueKey.customIndex === void 0) {
151
+ uniqueKey.customIndex = true;
152
+ }
153
+ });
154
+ }
155
+ if (model) {
156
+ options.uniqueKeys = options.uniqueKeys || model.uniqueKeys;
157
+ }
158
+ const queryGenerator = model.queryGenerator;
159
+ attributes = import_lodash.default.mapValues(attributes, (attribute) => model.sequelize.normalizeAttribute(attribute));
160
+ attributes = queryGenerator.attributesToSQL(attributes, { table: tableName, context: "createTable" });
161
+ sql = `${queryGenerator.createTableQuery(tableName, attributes, options)}`.replace(
162
+ ";",
163
+ ` INHERITS (${parents.map((t) => {
164
+ return t.getTableNameWithSchema();
165
+ }).join(", ")});`
166
+ );
167
+ return await model.sequelize.query(sql, options);
168
+ }
169
+ };
170
+ __name(_InheritedSyncRunner, "InheritedSyncRunner");
171
+ let InheritedSyncRunner = _InheritedSyncRunner;
172
+ // Annotate the CommonJS export names for ESM import in node:
173
+ 0 && (module.exports = {
174
+ InheritedSyncRunner
175
+ });
@@ -0,0 +1,2 @@
1
+ import { CollectionOptions } from '../collection';
2
+ export declare const beforeDefineAdjacencyListCollection: (options: CollectionOptions) => void;
@@ -0,0 +1,42 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var adjacency_list_exports = {};
20
+ __export(adjacency_list_exports, {
21
+ beforeDefineAdjacencyListCollection: () => beforeDefineAdjacencyListCollection
22
+ });
23
+ module.exports = __toCommonJS(adjacency_list_exports);
24
+ const beforeDefineAdjacencyListCollection = /* @__PURE__ */ __name((options) => {
25
+ if (!options.tree) {
26
+ return;
27
+ }
28
+ (options.fields || []).forEach((field) => {
29
+ if (field.treeParent || field.treeChildren) {
30
+ if (!field.target) {
31
+ field.target = options.name;
32
+ }
33
+ if (!field.foreignKey) {
34
+ field.foreignKey = "parentId";
35
+ }
36
+ }
37
+ });
38
+ }, "beforeDefineAdjacencyListCollection");
39
+ // Annotate the CommonJS export names for ESM import in node:
40
+ 0 && (module.exports = {
41
+ beforeDefineAdjacencyListCollection
42
+ });
@@ -0,0 +1,6 @@
1
+ import Database from '../database';
2
+ export declare const appendChildCollectionNameAfterRepositoryFind: (db: Database) => ({ findOptions, dataCollection, data }: {
3
+ findOptions: any;
4
+ dataCollection: any;
5
+ data: any;
6
+ }) => void;
@@ -0,0 +1,66 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var append_child_collection_name_after_repository_find_exports = {};
20
+ __export(append_child_collection_name_after_repository_find_exports, {
21
+ appendChildCollectionNameAfterRepositoryFind: () => appendChildCollectionNameAfterRepositoryFind
22
+ });
23
+ module.exports = __toCommonJS(append_child_collection_name_after_repository_find_exports);
24
+ const setRowAttribute = /* @__PURE__ */ __name((row, attribute, value, raw) => {
25
+ if (raw) {
26
+ row[attribute] = value;
27
+ } else {
28
+ row.set(attribute, value, { raw: true });
29
+ }
30
+ }, "setRowAttribute");
31
+ const appendChildCollectionNameAfterRepositoryFind = /* @__PURE__ */ __name((db) => {
32
+ return ({ findOptions, dataCollection, data }) => {
33
+ if (findOptions.targetCollection) {
34
+ const collection = db.getCollection(findOptions.targetCollection);
35
+ for (const row of data) {
36
+ setRowAttribute(row, "__collection", collection.name, findOptions.raw);
37
+ setRowAttribute(row, "__schemaName", collection.collectionSchema(), findOptions.raw);
38
+ setRowAttribute(row, "__tableName", collection.model.tableName, findOptions.raw);
39
+ }
40
+ return;
41
+ }
42
+ if (dataCollection.isParent()) {
43
+ for (const row of data) {
44
+ if (row.__collection) {
45
+ continue;
46
+ }
47
+ const fullTableName = findOptions.raw ? `${row["__schemaName"]}.${row["__tableName"]}` : `${row.get("__schemaName")}.${row.get("__tableName")}`;
48
+ const rowCollection = db.tableNameCollectionMap.get(fullTableName);
49
+ if (!rowCollection) {
50
+ db.logger.warn(
51
+ `Can not find collection by table name ${JSON.stringify(row)}, current collections: ${Array.from(
52
+ db.tableNameCollectionMap.keys()
53
+ ).join(", ")}`
54
+ );
55
+ return;
56
+ }
57
+ const rowCollectionName = rowCollection.name;
58
+ setRowAttribute(row, "__collection", rowCollectionName, findOptions.raw);
59
+ }
60
+ }
61
+ };
62
+ }, "appendChildCollectionNameAfterRepositoryFind");
63
+ // Annotate the CommonJS export names for ESM import in node:
64
+ 0 && (module.exports = {
65
+ appendChildCollectionNameAfterRepositoryFind
66
+ });
@@ -0,0 +1,2 @@
1
+ import { Database } from '../database';
2
+ export declare const registerBuiltInListeners: (db: Database) => void;
@@ -0,0 +1,33 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var listeners_exports = {};
20
+ __export(listeners_exports, {
21
+ registerBuiltInListeners: () => registerBuiltInListeners
22
+ });
23
+ module.exports = __toCommonJS(listeners_exports);
24
+ var import_adjacency_list = require("./adjacency-list");
25
+ var import_append_child_collection_name_after_repository_find = require("./append-child-collection-name-after-repository-find");
26
+ const registerBuiltInListeners = /* @__PURE__ */ __name((db) => {
27
+ db.on("beforeDefineCollection", import_adjacency_list.beforeDefineAdjacencyListCollection);
28
+ db.on("afterRepositoryFind", (0, import_append_child_collection_name_after_repository_find.appendChildCollectionNameAfterRepositoryFind)(db));
29
+ }, "registerBuiltInListeners");
30
+ // Annotate the CommonJS export names for ESM import in node:
31
+ 0 && (module.exports = {
32
+ registerBuiltInListeners
33
+ });
@@ -0,0 +1,8 @@
1
+ import { Model } from './model';
2
+ export declare class MagicAttributeModel extends Model {
3
+ get magicAttribute(): string;
4
+ set(key: any, value?: any, options?: any): this;
5
+ setV1(key?: any, value?: any, options?: any): this;
6
+ get(key?: any, value?: any): any;
7
+ update(values?: any, options?: any): Promise<this>;
8
+ }